I have a table
in a div
inside another div
. The direct container has a width of 40%
, and I want the table to be as wide as this div, by setting width: 100%
.
This works most of the time, however depending on the width of the browser window, the width of the table is sometimes off by a single pixel:
As you can see to the right, the border is a pixel to the left of that of the sibling div .info
. These borders should align.
#userListContainer{width: 40%; float: left; }
.info{display:block;line-height:22px; height:22px; padding-left:10px; }
#userListContainer .info {border-right: 1px solid #999999;}
.userList {
width: 100%;
border-right: 1px solid #999999;
word-break: break-all; border-spacing: 0;
border-collapse: separate;
}
Seems like a rendering bug to me. It occurs in Chrome 34.0.1847.131, not in IE10. I can't reproduce it in IE10 or the current version of FireFox.
On the other hand, if you specify width:100% , the element's total width will be 100% of its containing block plus any horizontal margin, padding and border (unless you've used box-sizing:border-box , in which case only margins are added to the 100% to change how its total width is calculated).
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.
Yes, as per the CSS 2.1 Specification, all non-negative values are valid for width, that includes percentage values above 100%.
To set the table width in HTML, use the style attribute. The style attribute specifies an inline style for an element. The attribute is used with the HTML <table> tag, with the CSS property width.
I had the same problem and managed to solve it by setting the width slightly higher:
width: 100.12%
I made an attempt to make the extra percentage small enough to solve most cases, but not create an overflow of 1 px.
It worked for me. This is somewhat of a dirty fix though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With