Home arrow Style Sheets arrow Page 2 - Styling HTML Lists with CSS: Resetting Padding and Margins
STYLE SHEETS

Styling HTML Lists with CSS: Resetting Padding and Margins


In this first part of the series on styling HTML lists with CSS, I explain how to reset their padding and margins. The entire styling process is very easy to follow.

Author Info:
By: Alejandro Gervasio
Rating: 4 stars4 stars4 stars4 stars4 stars / 3
August 30, 2010
TABLE OF CONTENTS:
  1. · Styling HTML Lists with CSS: Resetting Padding and Margins
  2. · Giving a consistent look: resetting padding and margins

print this article
SEARCH DEVARTICLES

Styling HTML Lists with CSS: Resetting Padding and Margins - Giving a consistent look: resetting padding and margins
(Page 2 of 2 )

In reality, resetting the padding and margins of an unordered list is very similar to performing the same task with any other HTML element. The process is reduced to assigning a value of 0 to the list’s “padding” and “margin” properties.

Logically, this can be done separately, in the following way:

ul#sample_list {
    padding: 0;
}

ul#sample_list {
    margin: 0;
}

which admittedly is a real waste of time and code, or in a more compact fashion, like this:

ul#sample_list {
    padding: 0;
    margin: 0;
}

Done. Even though at first glance the earlier CSS styles seem to be pretty basic, they’re the starting point for building HTML lists that can be rendered consistently across different browsers. For obvious reasons, to see the effect that the styles in question produce in the target list, they must be included in the previous web page.

That’s exactly what I plan to do in the following section. So hurry up and read the lines to come.

Putting all of the pieces together: including the previous CSS styles in a web page

As I promised in the segment that you just read, here’s the full version of the previous web page, which this time includes the sample HTML list with its “padding” and “margin” CSS properties set to 0. Check it out:

<!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>Sample list with padding and margins set to 0</title>
<style type="text/css">
body {
    padding: 0;
    margin: 0;
    background: #fff;
    font: 0.9em Arial, Helvetica, sans-serif;
    color: #000;
}
p {
    margin: 0 0 10px 0;
}
#wrapper {
    width: 960px;
    margin: 0 auto;
}
#header, #content, #footer {
    padding: 20px;
}
ul#sample_list {
    padding: 0;
    margin: 0;
}
</style>
</head>
<body>
<div id="wrapper">
    <div id="header">
        <h1>Sample list with padding and margins set to 0</h1>
        <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. Quisque rhoncus sodales sapien ac blandit. Nam lacus urna, commodo eget tincidunt vitae, ullamcorper at nulla. Vivamus ac iaculis justo. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aliquam erat volutpat. Sed quis elit erat, et ultricies diam. Phasellus non turpis malesuada erat ultrices tincidunt sed vitae magna. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Duis purus risus, lacinia at faucibus id, luctus nec diam. In nulla neque, consequat ac hendrerit ac, pulvinar eu dui. Aenean in arcu felis, non hendrerit est.</p>
    </div>
    <div id="content">
        <h2>Main content section</h2>
        <ul id="sample_list">
            <li><a href="#">List Item 1</a></li>
            <li><a href="#">List Item 2</a></li>
            <li><a href="#">List Item 3</a></li>
            <li><a href="#">List Item 4</a></li>
            <li><a href="#">List Item 5</a></li>
            <li><a href="#">List Item 6</a></li>
            <li><a href="#">List Item 7</a></li>
            <li><a href="#">List Item 8</a></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. Quisque rhoncus sodales sapien ac blandit. Nam lacus urna, commodo eget tincidunt vitae, ullamcorper at nulla. Vivamus ac iaculis justo. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aliquam erat volutpat. Sed quis elit erat, et ultricies diam. Phasellus non turpis malesuada erat ultrices tincidunt sed vitae magna. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Duis purus risus, lacinia at faucibus id, luctus nec diam. In nulla neque, consequat ac hendrerit ac, pulvinar eu dui. Aenean in arcu felis, non hendrerit est.</p>
    </div>
</div>
</body>
</html>

Mission accomplished, at least for the moment. At this point, if you test the previous web page on your browser, the target HTML list should look similar to the one shown in the following picture:

Well, that’s a small breakthrough, but definitely a solid one! Thanks to the use of a couple of CSS properties, the sample HTML list now has a consistent appearance, even when it is displayed on different browsers. Of course, resetting the padding and margins of an unordered list is pretty basic, at least to the eyes of an experienced web designer. Keep in mind, however, that this is a work in progress, and there’s still a lot to cover when it comes to giving HTML lists a cohesive and polished look. 

Final thoughts

In this introductory part of the series on styling HTML lists with CSS, I explained how to reset their padding and margins. As you just saw, the whole styling process is so easy to follow that it doesn’t bear any further analysis.

In the next tutorial, things will be a bit more interesting. I’m going to discuss how to assign specific values in pixels to the “padding” and “margin” properties of a given HTML list, which will allow you to have more control in the way that it’s displayed on screen.

Don’t miss the next part!


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
STYLE SHEETS ARTICLES

- CSS Padding Overview
- CSS: Margins Overview
- Top CSS3 Button Tutorials
- More Top CSS3 Tutorials for Animation and Im...
- CSS Mega Menus: Adding Extra Sections
- CSS Mega Menus
- CSS3 Accordian Menu: Horizontally Display Hy...
- CSS Combinators: Working with Child Combinat...
- CSS Combinators: Using General Siblings
- Intro to CSS Combinators
- CSS Semicircles and Web Page Headers
- Drawing Circular Shapes with CSS3 and Border...
- More CSS Pagination Link Templates
- CSS Pagination Links
- Animated CSS3 Image Gallery: Advanced Transi...

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