What happens to the remaining 1px when a div with an odd width is split 50%/50%?

前端 未结 2 1816
一生所求
一生所求 2021-01-12 00:50

Let\'s say I wanted to make a background for div#wrapper so that half is blue and half is red using two divs with width:50%, like so:

HTML:

相关标签:
2条回答
  • 2021-01-12 01:12

    See http://jsfiddle.net/dq323/.

    In IE and Firefox, the right side takes up the extra pixel. In Chrome, there's actually a gap between the two.

    Setting the background of the container seems like the best way to address this.

    0 讨论(0)
  • 2021-01-12 01:17

    One possible solution is not set a width to the second DIV (#rightSide) and set float: left; only on the left DIV. Since these are block elements, they will always expand to the full available width if none was set.

    In the example, the wrapper width is 3px, the left container has a width of 1-2px (depending on the browser) and the right container will require the leftover horizontal space inside the wrapper:

    http://jsfiddle.net/dq323/1/

    0 讨论(0)
提交回复
热议问题