58

I have an item on the DOM that I'd simply like to have fill its parent's width, regardless of what that is:

<div width="800">
    <div class="filler"></div>
</div>

How can I specify in CSS that the filler class match the width of its parent?

.filler {
    ?
}
Luke
  • 4,825
  • 2
  • 30
  • 37
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
  • 1
    By default a block level element (eg `div`) will fill it's parent... you can specify `width:100%;` too if you want, although that's the default, and will cause issues if you have padding/margin/borders in some browsers ;) – Rudu Mar 16 '11 at 19:49
  • Does this answer your question? [How to make CSS width to fill parent?](https://stackoverflow.com/questions/2232440/how-to-make-css-width-to-fill-parent) – Abdollah Apr 12 '20 at 15:34

6 Answers6

62

Have you tried: width: 100%; ?

BinaryTox1n
  • 3,486
  • 1
  • 36
  • 44
50

Depending on what you inner item is, there are various approaches.

If it's a block-level element (a paragraph, a div, etc.), it will automatically adjust itself to fill 100% of the container's width.

If it's an inline element, too bad for you, it won't accept width:100% until you convert it to a block-level element: display:block.

Floated elements are a special case: they will only span to the width of their inner content, even if they're block level elements. They require width:100%.

Absolutely positioned elements are even tougher: they need width:100%, but the container also needs a positioning context, eg. position:relative.

Examples of all four cases: http://jsfiddle.net/dD7E4/

mingos
  • 23,778
  • 12
  • 70
  • 107
  • it's possible to assign `width:100%` to inline elements such as span or inline-block. – Magne Sep 28 '13 at 14:07
  • @Magne, I beg to differ. Inline-block will happily accept `width:100%` due to its block-like nature, but a barebones, unstyled `span` will not. – mingos Sep 30 '13 at 07:58
  • you might be right about the `span`. I haven't tested that. But both inline-block and span are treated as inline elements outside, so I assumed that the behaviour would be the same.. – Magne Sep 30 '13 at 14:15
  • 2
    It is not. An unstyled `span` has `display:inline` and its width cannot be set. If it's styled with `dipsplay:inline-block`, it starts sharing the properties of both displays: it appears in the document flow similarly to an inline element, but its dimensions can be forced just as if it was a block element. – mingos Oct 03 '13 at 11:14
6

If the inner element is not a div and has padding or margin, flexbox might be the best solution:

<div class="container">
    <div class="filler"></div>
</div>
.container {
    display: flex;
}
.filler {
    flex-grow: 1;
}

See also this answer about how to fill remaining vertical space.

xeruf
  • 2,602
  • 1
  • 25
  • 48
2

Unless there's something stopping them, block-level elements such as div and p will always fill the entire width of their container. If you have an inline element such as a span or an a, you could style it as display: block to turn it into a block-level element, but this will also put a line break before and after it.

Brant Bobby
  • 14,956
  • 14
  • 78
  • 115
1

div is a block element and by default fill his parent.
if it doesn't you probably use float:left or float:right or display:inline or your parent is not 800px.
(maybe you should try with style="width:800px" or width="800px" instead of width="800")
I usually put a color border to see how it works.

Simon Watiau
  • 637
  • 5
  • 17
0

By default it will fill its parent element's width as div is an block element.

Sandeep K Goyal
  • 280
  • 2
  • 9