HighDots Forums  

centering two floated divs?

Cascading Style Sheets Layout/presentation on the WWW (comp.infosystems.www.authoring.stylesheets)


Discuss centering two floated divs? in the Cascading Style Sheets forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
yawnmoth
 
Posts: n/a

Default centering two floated divs? - 11-16-2007 , 08:33 PM






I'd like to center two floated divs but am unsure of how to do so.
Here's my code:

http://www.frostjedi.com/terra/scrip...at-center.html

It seems like setting the width of the outer div to 200px might work
but I don't want to do that because you're not always going to know
the width in advance...

Reply With Quote
  #2  
Old   
eholz1
 
Posts: n/a

Default Re: centering two floated divs? - 11-16-2007 , 10:20 PM






On Nov 16, 6:33 pm, yawnmoth <terra1... (AT) yahoo (DOT) com> wrote:
Quote:
I'd like to center two floated divs but am unsure of how to do so.
Here's my code:

http://www.frostjedi.com/terra/scrip...at-center.html

It seems like setting the width of the outer div to 200px might work
but I don't want to do that because you're not always going to know
the width in advance...
Here is one way - not too elegant - perhaps others will have better
suggestions -

<style type="text/css">

body {
width: 800px;
}


#container {
padding-left: 200px; /* LC width */
padding-right: 200px; /* RC width */
margin-right:-55px;
float:left;
position: relative;
border: solid 2px blue;
}

#reddiv {

}
#greendiv {

}

</style>

<body>

<div id="container" style="margin: 0 auto; text-align: center">
<div id="reddiv" style="float: left; background: red; width: 100px;
height: 100px"></div>
<div id="greendiv" style="float: left; background: green; width:
100px; height: 100px"></div>
</div>

</body>

</html>

good luck,

eholz1


Reply With Quote
  #3  
Old   
Jonathan N. Little
 
Posts: n/a

Default Re: centering two floated divs? - 11-16-2007 , 10:55 PM



yawnmoth wrote:
Quote:
I'd like to center two floated divs but am unsure of how to do so.
Here's my code:

http://www.frostjedi.com/terra/scrip...at-center.html

It seems like setting the width of the outer div to 200px might work
but I don't want to do that because you're not always going to know
the width in advance...
text-align: center; centers text not blocks, it's the margin on blocks.
Anyway your floats defeat your "centering" and unless your give the
containing block a width it will expand to the full with and margins
left & right set to auto will not mean anything.

Set a width on the container DIV and set "position: relative" Then set
inside DIVs "position: absolute" which will be relative to container.


<div style="margin: 0 auto; width: 200px; position: relative;">

<div style="position: absolute; left: 0; background: red; width:
100px; height: 100px"></div>

<div style="position: absolute; left: 100px; background: green;
width: 100px; height: 100px"></div>

</div>


--
Take care,

Jonathan
-------------------
LITTLE WORKS STUDIO
http://www.LittleWorksStudio.com


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.