css div boxes

He has: 688 posts

Joined: Feb 2001

If I make a code like this

<div class="whatever">whatever</div>
<div class="whatever">whatever</div>
<div class="whatever">whatever</div>
<div class="whatever">whatever</div>
'
Then (if display: block) you'll end up with 4 boxes verically stacked without even having tags. Okay fine, but how can I make my div boxes side by side horizontally? What I'm doing is reverting to using a table with class tags for each but how can you do this without the table?

Thanks

AyntRyte's picture

He has: 145 posts

Joined: Jun 2004

Try this?

<div style="float:left; border: black solid 1px; padding:5px">whatever</div>
<div style="float:left; border: black solid 1px; padding:5px">whatever</div>
<div style="float:left; border: black solid 1px; padding:5px">whatever</div>
<div style="float:left; border: black solid 1px; padding:5px">whatever</div>
'

\\// Robert

The grass is always greener on the other side -- but that's because they use more manure.

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Or, you can give it a width and make it display:inline;

He has: 688 posts

Joined: Feb 2001

That was easy. Thanks!

He has: 688 posts

Joined: Feb 2001

DIFFERENT QUESTION:

I don't want to clog up the forums with lots of threads so I'm going to ask another (yet different) css question here. I used the code above here to make the vertical boxes which worked great. All of that code is contained in an overall content container div, appropriately named "container". Now I want to center my container div but I just discovered that XHTML does not support . Sad So seperately I tried the css code of text-align:center and the html code of align="center". Both solutions (either together as they are now or seperately) work fine in Internet Explorer but not in Firefox. :eek: (Isn't it usually that things work in FF but not IE) Anyway, since I can't use and the two workarounds don't work in FF, what else can I do to center my container div and still validate, or any content that I may need to center in the future?

Thanks yet again.

He has: 1,758 posts

Joined: Jul 2002

To center things in IE you have to use "text-align: center" applied to the surrounding div or if it's the outer most div, on the body tag. This is another one of those "ie not working properly things". To center in standards compliant browsers you set the left and right margin's to "auto".

Using inline styles (yuk!) here's an example that should work across the board:

<body style="text-align: center;">
  <div style="margin: 0 auto 0 auto;"><p>Content</p></div>
</body>
'

Please note, if you set the outer element to text-align: center the effect with cascade and effect the inner element as well, so you need to add text-align: left to the inner div to correct that.

Andy

He has: 688 posts

Joined: Feb 2001

andy206uk wrote: To center things in IE you have to use "text-align: center" applied to the surrounding div or if it's the outer most div, on the body tag. This is another one of those "ie not working properly things". To center in standards compliant browsers you set the left and right margin's to "auto".

Using inline styles (yuk!) here's an example that should work across the board:

<body style="text-align: center;">
  <div style="margin: 0 auto 0 auto;"><p>Content</p></div>
</body>
'

Please note, if you set the outer element to text-align: center the effect with cascade and effect the inner element as well, so you need to add text-align: left to the inner div to correct that.

Sorry to bring back an old thread but I'm still having this centering issue, but with a different section. Your solution seemed to work on my previous issue but now I'm just tying to center a plain simple image.

On this page I'm just trying to center the drawing image of homes at the bottom. At first I did it inline but then I tried making the css external so that I can use it over and over again. I've tried just one, then just the other, now both and nothing works in either IE or FF. Sad

.center {text-align: center; margin: 0 auto 0 auto;}'
<img src="/development/graphics/drawing.jpg" width="600" height="125" alt="" class="center" />'
Confused

AyntRyte's picture

He has: 145 posts

Joined: Jun 2004

I must admit to a boo-boo in my code above. Class must be used instead of ID, as ID is unique and can only be used once. :blush:

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

add display:block; to .center

He has: 688 posts

Joined: Feb 2001

that did it. awesome awesome awesome! Thanks yet again. Smiling

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.