2

So, what I know is that in the W3C box model, padding adds to the width of a box, because the width represents the width of the box's content. I believe CSS3's calc() function, which allows you to do arithmetic operations such as subtracting a pixel width from a percentage width, was made to counter problems caused my this (imo terrible) box model.

However, both Firefox and chrome give me the same strange result when using calc() like this:

.feedpost_form {
    position: relative;
    height: 80px;
    border-bottom: 1px solid #999999;
    margin: 0;
    padding: 8px;
    width: calc(100% - 8px*2);
    background-color: rgba(0,0,0,0.1);
}
.feedpost_form textarea {
    width: calc(100%);
}

The element width of .feedpost_form is 100%, as expected. The padding seems to be 8px, as expected, because I notice the textarea is 8px from the left. This means the css width, and therefore width of the content, should be the element width minus net horizontal padding. For example, if the .feedpost_form element is 116px wide, I believe its css width (the content width) to be 100px.

I believe the textarea should then span the width of its parent's content width, because it has a width of 100%. For example, if .feedpost_form's element width is 116px and css width is 100px, I believe the textarea's width should be 100px and stay within the padding.

The actual result I get is the textarea's width being larger than expected. The most confusing part is that it doesn't overflow it's container, but only cuts into the right padding. I'm supposing this means the resulting width of the textarea is the .feedpost_form's css width + 1/2 net padding, and setting the textarea's width to calc(100% - 8px) does solve the problem.

So here's my question: Is this the intended result, or are both browsers making the same mistake? If it is intented, what am I missing? Doesn't it contradict the box model?

4
  • 8px * 2 doesn't make any sense seeing as it can't ever be changed dynamically. Commented Jul 25, 2013 at 19:05
  • @Connor: It's constant, but it makes perfect sense for readability. His padding is 8px -- he wants it to match up. Commented Jul 25, 2013 at 19:12
  • I suppose but this is just as good calc( 100% - 16px ); /* double of padding */ Commented Jul 25, 2013 at 19:13
  • Factoring it like that does make it a lot easier if you have a text editor like Sublime though. I can select the padding, press Ctrl+D, and then I only have to type the new padding once. Small detail though; makes it more fun to change the padding than anything else. Commented Jul 25, 2013 at 20:14

1 Answer 1

1

I do think you are understanding calc correctly, and the result you get actually seems correct to me. You forgot that the border is also added to the width in the default css boxmodel, and a textarea has a 1px border by default. That is why you get the textarea running into its parent's padding.

So if you take those 2px into account, or 1px * 2 as you like to put it, it should work just fine:

.feedpost_form textarea {
    width: calc(100% - 1px * 2);
} 

I have put them side by side so you can see for yourself: http://jsfiddle.net/rmHU3/

Btw, if you are so opposed to the default box model, why not use the box-sizing: border-box, which would be a lot easier then calculating all the time (especially if you start playing around with the css) http://css-tricks.com/box-sizing/

Sign up to request clarification or add additional context in comments.

2 Comments

It seems the default border is much thicker than that, but I'd imagine that's system dependant. Setting the border to something custom causes it to work exactly as you described though. :D Thanks!
Note that in my fiddle i used the normalized css option, which resets all browser default margins, paddings, borders,... for cross browser consistency. I would advise you to use some sort of reset / normalize CSS at all times!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.