Create fluid gradient rounded corner box
By wecode
First step you need to do is slicing a gradient rounded corner box. You have to plan a bit since it is not an easy task in psd to html, especially if you want to make the gradient box fluid. You can create gradients in many different ways. Let’s consider how to convert the design on left to html/css, 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 way of expanding html boxes. Let’s look at how we could do the slicing of this psd to html shown below;
Can you notice, there are two different colors we have used in the gradient box. Step two is needed to plan in optimizing the slices of the gradient box. This is one of the most important things that psd to html coders should consider. HTML Coder needs to think about making use of css Sprites? What is the minimum use of slices? 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 for the gradient box. First slice will create the top two rounded corners, second 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 sprite image. In that case we can implement the gradient box using only two slices.
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;.
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.
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.
Time to make the gradient box liquid now. How? first you need to add a background color. if you carfully 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 cant 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.
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>
- 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. - Fluid Gradient Rounded Corner Box
Here is the working sample of fluid Gradient Rounded corner box.
Comments
No comments yet.