Home arrow HTML arrow Page 2 - Creating Inline-Block HTML Elements with CSS
HTML

Creating Inline-Block HTML Elements with CSS


If you enjoyed the first part of this two-part tutorial on using the "inline-block" value to create web page layouts without using CSS floats, you'll love this part. You'll learn how to use this value to style the items in an unordered list.

Author Info:
By: Alejandro Gervasio
Rating: 5 stars5 stars5 stars5 stars5 stars / 10
October 18, 2011
TABLE OF CONTENTS:
  1. · Creating Inline-Block HTML Elements with CSS
  2. · Using

print this article
SEARCH DEVARTICLES

Creating Inline-Block HTML Elements with CSS - Using
(Page 2 of 2 )

To be frank, turning the list items of the previous list into inline elements that maintain their block nature is simply a matter of assigning to them the “inline-block” value, and nothing else. The entire styling process is that simple.

Of course, the best way to demonstrate this is with some functional, tangible code. Thus, below I coded a basic CSS snippet that performs this task in a snap. Check it out:

/* section list */
ul#sections {
    list-style: none;
    padding: 0;
    margin: 0;
}
/* inline-block list items */
ul#sections li {
    display: inline-block;
    width: 200px;
    padding: 20px;
    background: #ffffc0;
    /* Hack for IE 7 */
    zoom: 1;
    *display: inline;
}

If you've created horizontal navigation bars using the structure provided by HTML lists, the above code sample should be pretty familiar to you. As you can see, once the wrapping list has been properly styled, the “inline-block” value has been assigned to the corresponding items, which places them next to each other in the same line. On the other hand, though, the items have been given a fixed width along with a padding value, in this manner demonstrating that the elements actually preserve their block model.

Finally, the “zoom: 1” and “display: inline” declarations are nothing but a hack for Internet Explorer 7, which was discussed in detail in the preceding tutorial; so keep it untouched if you want your “inline-block” elements to be rendered correctly by Microsoft’s browser (yes, some facts of life simply can’t be changed).

So far, so good. At this moment, I’m sure that you've already grasped how the earlier CSS styles do their business. In consequence, the next logical step is to include them in the web page previously coded.

This will be done in the following segment. So, jump ahead and read the lines to come. 

Adding the earlier CSS styles to a web page

As I anticipated in the preceding section, here’s the finished version of the earlier web page, which now includes the CSS styles just defined. Take a close look at it:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Using the 'inline-block' value with an unordered list</title>
<style type="text/css">
body {
    padding: 0;
    margin: 0;
    background-color: #fff;
    font: normal 0.8em Arial, Helvetica, sans-serif;
    color: #000;
}
h2 {
    margin: 0 0 10px 0;
    font-size: 2em;
    color: #666; 
}
p {
    margin: 0 0 15px 0;
    line-height: 1.3em;
}
/* main wrapper */
#wrapper {
    width: 780px;
    margin: 0 auto;
    background-color: #fff;
}
/* header, main and footer elements */
#header, #main, #footer {
    padding: 20px;
}
/* section list */
ul#sections {
    list-style: none;
    padding: 0;
    margin: 0;
}
/* inline-block list items */
ul#sections li {
    display: inline-block;
    width: 200px;
    padding: 20px;
    background: #ffffc0;
    /* Hack for IE 7 */
    zoom: 1;
    *display: inline;
}
</style>
</head>
<body>
<div id="wrapper">
    <div id="header">
        <h2>Header section</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet.</p>
    </div>
    <div id="main">
        <ul id="sections">
            <li>
                <h2>Section A</h2>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet.</p>
            </li>
            <li>
                <h2>Section B</h2>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet.</p>
            </li>
            <li>
                <h2>Section C</h2>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet.</p>
            </li>
        </ul>
    </div>
    <div id="footer">
        <h2>Footer section</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse auctor commodo risus, et ultrices sapien vestibulum non. Maecenas scelerisque quam a nulla mattis tincidunt. Etiam massa libero, pharetra vel laoreet et, ultrices non leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed posuere ullamcorper lacus et sollicitudin. Morbi ultrices condimentum lacus, sit amet venenatis purus bibendum sit amet.</p>
    </div>
</div>
</body>
</html>

Definitely, there’s no much that can be said about the above (X)HTML document, as its source code speaks for itself. However, this shouldn’t stop you from testing the document and seeing the output that it generates. So, launch your browser and do so. If all goes as expected, the items of the target list should be rendered in the following way:

As the above screen capture shows, the “inline-block” value yields quite impressive results, regardless of whether you use it with divs, list items or any other block-level HTML element. In summary, does this mean that you should start to see CSS floats as an old-fashioned approach for building web page layouts? Well, perhaps not that quickly.

Nevertheless, the web is a creature in constant evolution, and as such, there’s no better time that now to start looking into newer methodologies that let you create better and more cohesive designs. So, what are you waiting for? Go ahead and give the “inline-block” approach a try! Who knows? It might be your ticket for entering the world of float-free CSS layouts.  

Final thoughts

In this two-part tutorial, I provided you with a humble introduction to using the “inline-block” value supported by the “display” CSS property. This process permits you to create modern web page layouts without having to deal with the complexities of CSS floats. As you saw in the couple of examples previously developed, the value is in general well supported by most browsers -- even though IE 7 requires you to code some ugly hacks. Therefore, if you can happily live with this minor issue, feel free to work with this value when building your own designs. As I said before, it won’t revolutionize the way that you create your web pages, but it might be a good start.

See you in the next web design tutorial!


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
HTML ARTICLES

- Does HTML5 Need a Main Element?
- Revisiting the HTML5 vs. Native Debate
- HTML5: Not for Phone Apps?
- HTML5 or Native?
- Job Hunting? Freelancer.com Lists This Quart...
- HTML5 in the News
- Report: HTML5 Mobile Performance Lags
- The Top HTML5 Audio Players
- Top HTML5 Video Tutorials
- HTML5: Reasons to Learn and Use It
- More of the Top Tutorials for HTML5 Forms
- MobileAppWizard Releases HTML5 App Builder
- HTML5 Boilerplate: Working with jQuery and M...
- HTML5 Boilerplate Introduction
- New API Platform for HTML5

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials