Forum Moderators: not2easy

Message Too Old, No Replies

Can't get DIVs to sit next to each other

         

Dabrowski

10:13 pm on Apr 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have 2 columns, right column is 150px wide, left is taking remaining space.

Code I have is

#wrapper { width: 100%; height: 100%; overflow: auto; }
.left { height: 100%; }
.right { float: right; width: 150px; height: 100%; }

DIV { border: 1px solid red; } /* Just so I can see them */

<div id='wrapper'>
<div class='right'>Stuff</div>
<div class='left'>Nonsense</div>
</div>

The right hand div is fine, but the left is only as wide as the content, like it's floated left but it's not! Both are full height though.

Setek

2:07 am on Apr 18, 2007 (gmt 0)

10+ Year Member



The right hand div is fine, but the left is only as wide as the content, like it’s floated left but it’s not! Both are full height though.

I tested your code, and using the following doctypes:

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml11.dtd">

The left-hand side was as wide as the space allowed. I suspect though, seeing as you say the height filled what you wanted, that you’re pushing IE into quirks mode, and you never tested it in Firefox (though even when forcing IE into quirks mode myself, I didn’t find any problem with the left-hand side only taking up its necessary space.)

I’m guessing there’s other code messing with it? Did you accidentally have absolute positioning on the left-hand side, or are floating it? These would both account for it only taking up necessary space, rather than all available space.

I think the reason you’re having so many problems (I read the post about tableless layouts) is because your processes are a little muddled. Why don’t you try to:

  • Use a full and correct doctype;
  • Have nothing precede this doctype, such as whitespace or an
    <?xml
    declaration (chances are, you’re serving your page as
    text/html
    anyway so the entire XML-ness of the page is mootpoint);
  • Test in a standards-compliant browser. Take your pick, but I find the one with the least errors is still Firefox, no matter how much Opera wins in the Acid2 Test, it still does some things quirkily;
  • Now, check it in IE 6. Does it look the same? Soon, you’ll be able to pick recurring reasons why certain things trip bugs, and learn to quickly deal with them;
  • Check it in IE 7 too, if this needs a bit of conditional commenting also, do it. Obviously it will need less, since Microsoft did fix some things, but there are others that they still haven’t. By default I have an IE 6 & IE 7 conditional commented stylesheet, which has tweaks that affect both browsers, and a separate IE 6 conditional commented stylesheet, which is just for IE 6; and
  • Throw any other browser you like at it (those called the “standards-compliant” ones) such as Opera, Safari, Mac flavours of anything you want, and give Lynx a go (or any other text-only browser,) as well as JAWS if you have it (or Select All in Opera and hit ‘v’ to trigger text-to-speech.)

Xapti

6:10 am on Apr 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the left is only as wide as it's content, it sounds like it's set to display:inline, or something else other than block. Thing is, inline can't have their height's set, so it's probably more likely a bug from just some browser (maybe the browser in question also allows inline elements to have their height changed too).
Why any browser would display something as simple as block items as inline? no idea.

Dabrowski

10:57 am on Apr 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



because your processes are a little muddled

Yes, I know the theory (or most of it I think) but putting it into practise is a lot harder as I'm sure you know. I am relatively new to CSS layouts as I haven't been doing commercial websites long.

Use a full and correct doctype

Sorry I didn't mention this, I always use strict and will soon start XHTML.

Test in a standards-compliant browser. Take your pick, but I find the one with the least errors is still Firefox

I usually run IE7 and FF next to each other so do both at once.

I'm glad that this should work, that means I have the theory right at least. I think there's maybe some other bit of CSS messing with it causing a float or something, I'll check but thanks for the advice guys.

Fotiman

2:30 pm on Apr 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




I always use strict and will soon start XHTML

Before you do, I suggest you read the thread "Why most of us should NOT use XHTML [webmasterworld.com]"

Fotiman

2:39 pm on Apr 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You should put your code into a minimal test case and post it here. I've taken your code and put it into this:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset:utf-8">
<title></title>
<style type="text/css">
#wrapper { width: 100%; height: 100%; overflow: auto; }
.left { height: 100%; }
.right { float: right; width: 150px; height: 100%; }
DIV { border: 1px solid red; } /* Just so I can see them */
</style>
</head>
<body>
<div id='wrapper'>
<div class='right'>Stuff</div>
<div class='left'>Nonsense</div>
</div>
</body>
</html>

Note, the left column takes up the remaining width, but the height's are not 100%. That's because in order for 100% to be calculated, the parent height has to be known. So below is an updated example in which I've given html and body a height of 100% as well. I've also removed the borders and replaced them with different background colors on the two blocks (since adding a border screws up the height calculation).


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset:utf-8">
<title></title>
<style type="text/css">
html, body { height: 100%; margin: 0; padding: 0; }
#wrapper { width: 100%; height: 100%; overflow: auto; }
.left { height: 100%; background-color: #c00;}
.right { float: right; width: 150px; height: 100%; background-color: #00c;}
</style>
</head>
<body>
<div id='wrapper'>
<div class='right'>Stuff</div>
<div class='left'>Nonsense</div>
</div>
</body>
</html>

Note, the height is correct and the widths do not seem to be shrink wrapping in either IE6 or Firefox. So perhaps you have other styles on your page that are affecting what you're seeing?

Dabrowski

2:44 pm on Apr 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



An interesting thread. I have to admit it's very long and I can't be bothered to read all of it, but my personal oppinion is that I look on loads of dev sites that boast XHTML, with no problems.

Can anyone actually provide an example of a browser that can't handle XHTML with text/html?

As I run my own servers I can soon change the mime-type if I need to, but I've never seen a broken XHTML site.

Anyone else seen one?

Fotiman

3:06 pm on Apr 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




I can't be bothered to read all of it

Ha! That's a good one. If you are considering serving your documents as XHTML, then you definately SHOULD read all of it. It's not a black or white issue. There are several points of interest, including a reference to the Mozilla Web Author FAQ which states:


Serving valid HTML 4.01 as text/html ensures the widest browser and search engine support.

Seriously, spend 30 minutes reading the thread or at least the article posted in the first message of the thread so you are familiar with what the issues are.

Dabrowski

5:33 pm on Apr 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can't be bothered to read all of it

I realise the irony of this given the current ongoing thread! Easier to cope with if you read as you go along though.

Serving valid HTML 4.01 as text/html ensures the widest browser and search engine support

I did note that one. I will read the other article when I decide to have a look in more detail, but still.......can you find an example of some broken XHTML though?

Dabrowski

6:51 pm on Apr 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Fotiman, just read your code example. I did find the problem it was a conflicting CSS rule with 'float: left' on it. Have shuffled my sith as in the previous thread and this fixed it.

I did have html and body set to 100% too, so that was ok.

Thanks for the help.

Fotiman

7:18 pm on Apr 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



My pleasure. Glad you got it sorted. :)