views:

503

answers:

2

UPDATE: Turns out that Bart's answer is the correct one. 4*64+8*1 = 264px wide div to contain the other divs and their 1px borders gives exactly the effect I wanted. I modified my sample code to contain his answer. Thanks Bart.

I'm struggling, again, with CSS. This sample shows 1,2,3,4 at the size I want them using a table. When I try to do the same with CSS the div collapses to the size of the content rather than then height/width specified. Target browser: IE8

<!doctype html>
<html>
<head>
<style type="text/css" media="screen"> 
#one, #two, #three, #four, #five, #six, #seven, #eight 
{
    height: 64px; width: 64px;
    border: 1px solid black;
    background-color: lightgreen;
}
#five, #six, #seven, #eight { float:left; }
#cont {width:264px;}
</style>
</head>
<body>
<center>

<table><tr>
<td><div id="one">1</div></td>
<td><div id="two">2</div></td>
<td><div id="three">3</div></td>
<td><div id="four">4</div></td>
</tr></table>

<div id="cont">
<div id="five">5</div>
<div id="six">6</div>
<div id="seven">7</div>
<div id="eight">8</div>
</div>

</center>
</body>
</html>
A: 

Add padding and margin, otherwise the browser ignores the height and width.

Good job trying to not use tables for layout!

Elizabeth Buckwalter
What values of padding do I need to add to achieve height/width of 64px independent of the text in the div?
Andrew
I start with a couple pixels and play with the numbers in firebug. Generally the final width will have to be a tad smaller, ymmv. Let me know.
Elizabeth Buckwalter
I tried it but what I end up with is an amount of padding around the contents of the DIV. So as the DIV content varies in size so does the total size. But I need precisely 64 height/width.
Andrew
This is one of those things that web developers forget. The content is what you're developing for. If the content change length then so will the layout. Truncate the content and add a read more if you really want to keep a 64px size. Remember, design for the content. That won't change weather or not you use tables
Elizabeth Buckwalter
A: 

Setting them to display: inline turns them into inline elements, which don't play nicely with width, height, and other box model properties that are meant for block elements, which the div is by default.

If you want to set their widths and heights, and you want them next to each other, try this:

#five, #six, #seven, #eight { 
  float: left;
  height: 64px; 
  width: 64px;
  border: 1px solid black;
  background-color: lightgreen;
}
No Surprises
That works but pushes the divs to the far left side of the screen. How can I get them to retain their size, be next to each other but still be centered on the page?I'm not married to divs. Could switch to spans if they would work better.
Andrew
To center those divs on the page, you could add the following CSS to the DIV surrounding the five, six, seven, and eight divs: {width:256px; margin:0 auto;}
Bart