Home arrow HTML arrow 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
(Page 1 of 2 )

Most of the time the “display” CSS property is used only for hiding/displaying HTML elements, via the complementary “none” and “block” values, or turning the elements into inline ones (and vice versa). The property offers an additional ability, however, that so far has remained underused -- quite possibly because many web designers simply are unaware of its existence. As you may have already guessed from the article’s title, if the “inline-block” value is attached to one or multiple block-level containers via the aforementioned property, it will cause the elements to be rendered as inline ones; however, their “block” nature will be preserved, making it possible to assign to them widths, heights and so forth.

Naturally, if you’re wondering what’s so exciting about this approach, the answer is simple: you can use the “inline-block” value to create web page layouts, without having to deal with CSS floats. Needless to say, this statement must be backed up with some functional examples. In the introductory chapter of this two-part tutorial I created one that showed how to use the functionality of this value to build a simple layout comprised of a few divs.

The most relevant facet of the entire development process was that these elements were rendered next to each other without having to code a single “float” property. Not too bad, considering that this was nothing but an example!

Of course, it’s possible to use this approach with any other block-level element, apart from the aforementioned divs. In this last installment  I’ll be demonstrating how to utilize the “inline-block” value  for styling the items of an unordered list.

Has my proposal caught your attention already? Them start reading!

Taking the first step: creating a basic web page

As I expressed in the introduction, my plan here consists of demonstrating how to use the “inline-block” value with the items of an unordered list. This way, you can see how easy it is to turn them into inline elements that preserve their block-level properties.

To achieve this goal, the first thing we need to do is define a web page that contains the target list. So, below I created a basic (X)HTML document which includes the pertinent list. Take a peek 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>
</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>

Effectively, the structure of the previous web page is pretty easy to follow. Aside from defining the typical “header,” “main” and “footer” areas, it contains the target list, in this case identified as “sections.” What’s more, if at this point you test the page with no styles attached to it, each list item will be rendered from top to bottom in a separate line, which is the expected behavior of any block-level element.

Admittedly, this is pretty boring stuff that you’ve surely seen many before, so it’s time to make it interesting by using the “inline-block” value. This approach will allow each item of the list to be displayed next to each other in the same line.

The fine details of this styling process will be discussed in depth in the next section. Thus, click on the link below and keep reading.

Next: Using >>

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-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials