Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table width 100% is off by a single pixel (depending on actual width)

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%.

Relevant Markup

Markup

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:

Pixel errror

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.

Relevant CSS

#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.

like image 713
MarioDS Avatar asked May 09 '14 11:05

MarioDS


People also ask

What is the meaning of width 100%?

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).

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.

Can we give width more than 100%?

Yes, as per the CSS 2.1 Specification, all non-negative values are valid for width, that includes percentage values above 100%.

How do you define table width?

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.


1 Answers

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.

like image 105
Thijs Riezebeek Avatar answered Sep 29 '22 16:09

Thijs Riezebeek