14

please, can someone explain to me, why the red div isn't expanding to the right? It stops where the screen ends. What do I have to do, to make it expand?

One thing that works is to "display: table-cell" the red div but I was wondering if there's another way and why this happens...?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head />
<body>
  <div style="background-color: #f00;">
    <div style="width: 2000px; height: 100px; " />
  </div>
</body>
</html>
Prasanth K C
  • 7,073
  • 5
  • 39
  • 62

9 Answers9

3

That is indeed bizarre. Try floating the outer:

<div style="background-color: #f00; float:left;">
    <div style="width: 2000px; height: 100px;" />
</div>
Crescent Fresh
  • 115,249
  • 25
  • 154
  • 140
  • I think floating should only be used to... well actually to float something. Overusing float can become a big mess. Setting width to auto should work. – Brian Kim Jan 30 '09 at 01:09
  • While I personally don't think floating is the only solution here, this works best across all browsers and table-cell doesn't. Down-vote removed. =) – Brian Kim Jan 30 '09 at 01:26
2

The div that it's contained in is smaller than 2000px. You need to do something like this:

  <div style="background-color: #f00; width: 2000px;">
    <div style="width: 2000px; height: 100px; " />
  </div>

Of course, now the inner div's width doesn't need to be specified unless it is different than the outer div's.

Paige Ruten
  • 172,675
  • 36
  • 177
  • 197
2

Put overflow: auto; in the parent <div>'s style.

Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99
Bari
  • 21
  • 1
1

Check with this one. it will expand the container as the text goes on...

    <div style="background-color: #f00; height: 100px; display: inline-block;">
      <div style="background-color: blue; height: 50px; white-space:nowrap; display: inline-block;">
        here is some content
      </div>
    </div>
Prasanth K C
  • 7,073
  • 5
  • 39
  • 62
1
  <div style="background-color: #f00;">
    <div style="width: 2000px; height: 100px; " />
  </div>

Closing a DIV tag like that (i.e. a self-closing tag) will cause trouble.

It may be legal (See this SO Thread) but in my experience it often causes strange behavior.

However, that is not the problem. The following code works in Chrome and IE9.

  <div style="background-color: #f00; display: inline-block; height: 100px;">
    <div style="width: 3000px; background-color: blue; height: 50px;">
        here is some content
      </div>
  </div>

Display: inline-block; tells the element to grow as much (or as little) as necessary.

Fiddle: http://jsfiddle.net/sArvr/

This does seem like counter-intuitive behavior.

Community
  • 1
  • 1
Tim M.
  • 53,671
  • 14
  • 120
  • 163
0

Depending on what you're using this for, I'm assuming the div element is going to be the main focus of the page; if this is the case, you can take that width: 2000px; line and just apply it to the body instead of directly to the block-level element you want to stretch:

<style>
body
 {
width: 2000px;
 }
</style>

If this method doesn't work for you for whatever reason, creating a transparent pixel image and giving it the width you require should work just as well.

Hexagon Theory
  • 43,627
  • 5
  • 26
  • 30
0

The width of the inner div can change, I need to grow the outer one so I can't assign a width to it.

0

It seems like max width of parent div is limited by browser screen width even though child div's width grows beyond browser screen width. This is an odd discovery...

I suggest using float on parent div. (ref: crescentfresh) I know for sure that table-cell does not work for IE6. And a quick test on IE7 on my machine doesn't work either although it should...

This should work. (EDIT: This doesn't work...)

<div style="background-color: #f00; width: auto;">
  <div style="width: 2000px; height: 100px; "></div>
</div>
Community
  • 1
  • 1
Brian Kim
  • 24,916
  • 6
  • 38
  • 26
0

put <div style="min-height: 100px; " />

when you grow the content more, our div should grow its size automatically.

Selvin
  • 12,333
  • 17
  • 59
  • 80