Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS

Is it bad practice - Using HTML element to style page?

Hello,

I was wondering if it was considered bad practice to style a website in this way:

Let's say I wanted to adjust the width of my webpage to 78% and set the margin to 0 and auto (to center align everything within the now "78% squished" page).

From what I understand using the HTML element could be an easy way to make these changes, but I'd rather use a container div around the body, it feels like less of a universal change this way, even though the result is the same.

What's the preferred method?

1 Answer

andren
andren
28,558 Points

The preferred method is generally using a container. Though it should be a container inside of the body, not around the body. Something like this:

<body>
    <div class="container">
        <!-- Page elements go here  -->
    </div>
</body>

The main advantage to using a container over styling html or body directly is that it allows you to have some elements that are not restricted. For example headers and footers are often made to stretch across the entire width of the page, as cutting them off can often look weird if they have a colored background.