Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not using widths & padding/margins on the same element?

Tags:

html

css

I've seen numerous people mentions that you shouldn't use widths and padding or margins on the same element with CSS. Why is that?

like image 394
davethegr8 Avatar asked Sep 16 '08 19:09

davethegr8


People also ask

Should you use 100% width?

In many cases, applying width: 100% to a block level element is either unnecessary or will bring undesirable results. If you're using padding on the inner element and you use box-sizing: border-box , then you'll be safe.

Should I use Width or Max Width?

This is a simple way to put it: if the element would render wider than the max-width says it should be, then the max-width property wins over the width property. But if it would render less than the max-width says, then the width property wins.

Why do we need to set a width with auto?

Width: auto When an element has auto as a value for width, it can have margin, padding, and border without becoming bigger than its parent element. The width of its content box will be the content itself with the subtraction of margin, padding, and border.

Why is th width not working?

The width attribute of <th> is not supported in HTML5. Use CSS instead. In our CSS tutorial you can find more details about the width property.


2 Answers

Because some browsers treat the width as the total width of the element including the padding and the margins, and others treat the width as the base width to which the padding and margins are added. As a result your design will look different in different browsers.

For more information, see the W3C page on Box Model and quirksmode's take.

like image 101
Stephen Deken Avatar answered Oct 15 '22 15:10

Stephen Deken


A lot of people still cling to notions about faulty box-models in IE and reckon that if you start messing around with element widths, padding and margins, you're going to get into trouble.

That's pretty outdated advice - assuming you're using a correct doctype, all fairly modern browsers (including IE6+) will work to the same box model, and you shouldn't really have too many issues related to it.

This being CSS, you will obviously have a million other cross-browser issues, but the infamous IE box-model is becoming a thing of the past.

like image 45
David Heggie Avatar answered Oct 15 '22 13:10

David Heggie