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 or responsive.
Web designers can create gradients in many different ways while they designing the website. But web designers mostly 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 or vertical 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.
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.
Design HTML layout
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;
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.
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;
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;.
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.
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.
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.
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.
How to make gradiant box responsive or 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;
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.
Full CSS code
Full HTML code
- PSD to HTML Conversions
PSD to HTML coding service provider you can rely on for converting all your designs into fully browser compatible websites in no time.
- Gradient Rounded Corner Box
Here is the working sample of fluid Gradient Rounded corner box.
This article is accurate and true to the best of the author’s knowledge. Content is for informational or entertainment purposes only and does not substitute for personal counsel or professional advice in business, financial, legal, or technical matters.
© 2012 wecode
wecode (author) from Australia on January 13, 2013:
Yes the site ref looks good thanks it shows many cs3 stuff
Stanley Soman from New York on January 13, 2013:
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/