ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Internet & the Web

Slice Gradient rounded corners box using html/css

Updated on May 1, 2016

The gradient round corner box

Source


In this article I will demonstrate how to code a gradient rounded corner box using css/html. First step you need to slicing the Gradient rounded corner box. You need to plan a bit before you cut up the slices. Since it is not strait forward task in psd to html, especially because you want to make the gradient box fluid. Web designers can create gradients in many different ways while they designing the website. But web designers use gradients either vertical or horizontal. Let’s consider how to convert the design image display on the left using html/css This is a gradient box with top to bottom spreading gradient. In this scenario the box can expand from top to bottom and it is the general pattern of behavior expanding html boxes. Let’s look at how we could cut the slices of this design shown below;


As you can notice, there are two different colors used in the design by the designer in the gradient box design. Step two we need to plan how to optimize the slices of the gradient box. This is the most important part in psd to html coding. HTML Coder needs to think about making use of css Sprites. What are css sprites? According to W3C definition it is "An image sprite is a collection of images put into a single image." HTML coders must try to use the minimum number of slices in the html code. For this example I've come up with the below-mentioned plan of slicing to implementing the gradient box.

How to slice the image

possible slices
possible slices

Plan html slices


This is the slicing plan I've come up with the gradient box design. As you can notice the first slice will create the top two rounded corners of the box, second slice will create the body of the gradient box and the third will create the bottom two corners. Let's see the possibilities of using CSS Sprites. Yes we can merge top and bottom slices and create a css single sprite image. In general using the css sprites we can reduce the number of images we are going to use in the html code. This is a one good example of how we can implement the gradient box using only two slices.

CSS Sprite image

After combine top and bottom slices to make a CSS Sprite image
After combine top and bottom slices to make a CSS Sprite image

Now the Images needed to create the gradient box is ready. Let’s create the table-less html div structure in order to create the rounded corner box with html/css. When you are creating table-less html layouts most html coders prefer using divs to build the layout of html coding. I’m going to use the below-mentioned div structure;

   <div class="gradient-box">
 <div class="gradient-box-top">
 </div>
 <div class="gradient-box-middle">
 </div>
 <div class="gradient-box-bottom">
 </div>
 </div>


I've decided to use four divs to build the box. Outer div will act as the container or the place holder and other three divs will create top, middle and bottom respectively. Let's look at how to do CSS Code to style above divs using above sliced images; Let’s pull out our css to html knowledge in html styling.Starting with the first div of the above div structure let's make this div as the container using css. It is a very easy css code as follows.

.gradient-box{float:left;width:150px;}


I will be using floated divs throughout the implementation of gradient box. Floated divs give the css/html coders more control in Browser Compatibility issues.

Let's look at how we can code top part using css coding. In this case you need to remember the height of the top slice image; following css code will do the trick;

.gradient-box-top{height:12px;width:150px;background-image:url(css-sprite.png);}


Here we are using the css sprite image to build the top, even though the css sprite image height is 28px, we only need the top part of it. So we use the height as 12px. Let's check it with the browser, you will see the top part appears over there as follows;.

Here is the top of the rounded corner box

Coding middle part of the gradient box is the most important. Here you need to be very careful. Why? This is the div that will play the liquid role or the expanding role. In this case the div can only grow vertically from the bottom. Let’s look at how we could code this div using css.

.gradient-box-middle{height:130px;width:150px; background-image:url(gradient-box-m.png);} 

Note that still the div box is not liquid because of the fixed height. Let's view your code on the browser. You can notice that top and middle parts of the gradient box are present.

Half completed gradient box


Now comes the interesting part of coding the bottom div. Here we are going to use the real value of the css sprite image. Let’s look at how we code css sprite image using css/html.

.gradient-box-bottom{height:12px;width:150px;
background-image:url(css-sprite.png);background-position:left -16px;}  

Let’s dig into css code in bit more detail. Only one extra css property you can notice in the code. That is background position. Most important property when it comes to css sprites. By positioning your images at the right place, we manage to get the exact bottom. You can notice one thing, we are still using the same image for top and bottom. This will save one request from the server. If you reuse this gradient box in many places in your web site, each time you use it will save the requested bandwidth and increase your page performance.

Once everything is done refresh the web page, you will notice your complete gradient box.

Rounded corner box not yet liquid


Time to make the gradient box liquid now. How? first you need to add a background color. if you carefully look at the middle sliced image, it is only 130px in height. So when the box expands it needs to keep the same gradient look. In this case the background color should be the color at the bottom of the middle slice. Here the color is(#c7fc9c). By selecting the middle sliced bottom color as the background color, it will appear as the filling gap between bottom slice and the middle slice. Another point is the bottom color of the middle slice is the top color of the bottom slice, so you can't see a difference at all. One thing you need to control is the background repeat property. Here we will only allow background image to repeat along the x axis. To allow box to be liquid we need to remove the height from middle div tag which will expand vertically. Let's check revised css code;

.gradient-box-middle{width:150px;background-image:url(gradient-box-m.png)
;background-repeat:repeat-x;background-position:top;background-color:#c7fc9c;}
 

To show how this div behaves I've added a paragraph in the middle to show div expansion. You can use images or text or any other valid html inside the middle div. The following image shows the final result.

Liquid gradient rounded corners box

fully liquid gradient box
fully liquid gradient box

Full CSS code

p{margin:0;padding:0;}
.gradient-box{float:left;width:150px;}
.gradient-box-top{height:12px;width:150px;background-image:url(css-sprite.png);}
.gradient-box-middle{width:150px;background-image:url(gradient-box-m.png);background-repeat:repeat-x;background-position:top;background-color:#c7fc9c;}
.gradient-box-bottom{height:12px;width:150px;background-image:url(css-sprite.png);background-position:left -16px;}

Full HTML code

    <div class="gradient-box">
        <div class="gradient-box-top">
        </div>
        <div class="gradient-box-middle">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    	Fusce ut libero ligula. Proin viverra convallis lorem, at pharetra   
        ligula vehicula id. Nunc convallis accumsan commodo. Ut sed elit vel 
        dui feugiat tincidunt quis id lorem. Fusce dignissim odio nec erat 
        interdum dictum. Phasellus est nulla, feugiat sit amet ultricies
        non,  egestas iaculis justo. </p>
        </div>
        <div class="gradient-box-bottom">
        </div>
    </div>

Comments

    0 of 8192 characters used
    Post Comment

    • expertscolumn profile image

      Stanley Soman 4 years ago from New York

      thanks for this, additionally you can create rounded corners for your divs without using a background image, but pure html/css, this site should help: http://border-radius.com/

    • wecode profile image
      Author

      wecode 4 years ago from Australia

      Yes the site ref looks good thanks it shows many cs3 stuff

    Click to Rate This Article