ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

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

    • wecode profile imageAUTHOR

      wecode 

      5 years ago from Australia

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

    • expertscolumn profile image

      Stanley Soman 

      5 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/

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)