CSS Tricks episode 1: 100% width in a variable width container

Posted on 29 June 2008 (01:25 PM)

I'm extremely busy these days. Busy working my dayjob, where lots of clients ask for my attention, and busy planning to move to another appartment in the coming weeks.

Because of that, Whatstyle hasn't seen any big articles nor updates for a long time now. Because of that, I've decided to start a series of small articles with quick solutions in the field of CSS. Every time I solve some problem that might be unusual, difficult, or interesting, I'll write up an example and explain it here. Something you can then add to your toolbox, in the back of your head, so you can easily implement it and move on the next issue.

Today the article's about giving an absolutely positioned element a 100% width when it's variable width parent has padding on the sides.

Source order control

Lately I'm working real hard on creating a logical source order in my HTML files. Main content always comes first, followed by some optional secondary and/or tertiary content. Navigation elements always come last in my documents. It is then the job of CSS to make sure the navigation ends up in the right place.

Most of the time I achieve this using absolute positioning, pulling the content that's near to the bottom up to the top of the screen. There are, however, some problems with that technique, because the absolutely positioned content loses context and isn't aware anymore of its surrounding elements, and vice versa.

100% width and padding

Take a look at the following CSS:

  1. #container {
  2. position: relative;
  3. width: 600px;
  4. padding: 0 10px;
  5. }
  6. #child {
  7. position: absolute;
  8. top: 0;
  9. left: 10px;
  10. width: 600px;
  11. }
  12. Download this code: /code/css-tricks-1-100-percent-width-in-a-variable-width-container1.txt

This works, see for yourself. But what if we change some values to variable units? Something like this:

  1. #container {
  2. position: relative;
  3. width: 80%;
  4. padding: 0 3em;
  5. }
  6. #child {
  7. position: absolute;
  8. top: 0;
  9. left: 3em;
  10. width: 100%;
  11. }
  12. Download this code: /code/css-tricks-1-100-percent-width-in-a-variable-width-container2.txt

As you probably understand, I cannot set an exact width in pixels on the #child, because I can never know the exact width of the parent. The result of the above attempt however, is the child spilling out of the container.

What to do now?

Use borders for padding

If your container has a solid background, you can use borders instead of padding to achieve the exact same layout.

Change your CSS like this:

  1. #container {
  2. position: relative;
  3. width: 80%;
  4. padding: 0;
  5. border: 3em solid #fff;
  6. border-width: 0 3em;
  7. }
  8. #top {
  9. position: absolute;
  10. top: 0;
  11. left: 0;
  12. width: 100%;
  13. }
  14. Download this code: /code/css-tricks-1-100-percent-width-in-a-variable-width-container3.txt

What I've done here is remove the padding on the parent and replace it with borders of the same width. Presto, the child is contained within its parent again.

This works because borders always render outside an element, as opposed to padding, which happens on the inside. Using this technique, I don't have to think of the width of the child element as 75% - 3em = ?. The width isn't altered at all in the last example, because I used borders instead of padding, while the final layout remains the same.

Remember this one, it's so stunningly easy to implement, with so little side-effects, that I think this trick deserves a place in the back of your head.

Back to top

Filed under HTML and CSS

Comments:

No comments have been added yet, you could be the first!

Leave a comment

RE: CSS Tricks episode 1: 100% width in a variable width container

Note to spammers: rel="nofollow" will be added to links. If I consider your comment spam, your IP-address might get blocked.

HTML not allowed. URLS will be auto-linked. Maximum length is 1250 characters.

I understand this is inconvenient, but the spam I'm receiving on this website is driving me nuts. Please forgive me and cope with it until I find a better solution.

Mandatory fields are marked by an asterisk (*).

Increase textarea-size Decrease textarea-size

Back to top

Preferences

These settings will be saved for your convenience.