Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS width 100% including overflow

Tags:

For various reasons, I have a nested ol inside of a div, where the contents of the list exceeds the size of the container.

Because the container has a fixed width, the list element's background does not exceed the viewable area of the container, yet the contents scroll properly.

I have created a jsFiddle showing a simplified example of what I'm trying to explain.

I would like the width of the contained element to match that of the overflowed content. In the jsFiddle, that would mean the red background doesn't get cut off midway.

Thanks.

div {     border: 1px solid black;     margin: 33% auto;     overflow: scroll;     white-space: nowrap;     width: 100px; }  div > ol {    background: red;    width: 100%; }​ 
like image 705
Jeff Jenkins Avatar asked Oct 04 '12 03:10

Jeff Jenkins


People also ask

How do I change the width to 100% in CSS?

It seems like this should be one of the easiest things to understand in CSS. If you want a block-level element to fill any remaining space inside of its parent, then it's simple — just add width: 100% in your CSS declaration for that element, and your problem is solved.

Does width 100% include padding?

No, element width does not include padding, margin, or border.

Can we give width more than 100% in CSS?

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


1 Answers

Just use display: inline-block. You can read more in the W3C specs.

Replace width:100% with display:inline-block in those two element styles.

like image 103
ShouravBR Avatar answered Sep 29 '22 11:09

ShouravBR