| | |
Centering DIVs inside CSS
Please support our HTML and CSS advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2009
Posts: 38
Reputation:
Solved Threads: 0
I'm trying to use DIVs contained within a DIV to show content which is centered horizontally on the page, as per this example.
+=========== DIV 1 ===========+
+------+=====+=====+=====+-------+
+------+ DIV 2 + DIV 2 + DIV 2 +--------+
+------+=====+=====+=====+-------+
+==========================+
Here's the CSS and HTML code I'm using:
CSS
HTML
I've learnt that by setting margin-left and margin-right to 'auto' in DIV1 the content inside is centered, but only if it contains one DIV2 inside it.
When I add more DIV2s then the content inside DIV1 now shows on the left and ignores any centering...
+=========== DIV 1 ===========+
+=====+=====+=====+---------------+
+ DIV 2 + DIV 2 + DIV 2 +----------------+
+=====+=====+=====+---------------+
+==========================+
Does anyone please know why this is happening, and how I can fix it?
Thanks
+=========== DIV 1 ===========+
+------+=====+=====+=====+-------+
+------+ DIV 2 + DIV 2 + DIV 2 +--------+
+------+=====+=====+=====+-------+
+==========================+
Here's the CSS and HTML code I'm using:
CSS
HTML and CSS Syntax (Toggle Plain Text)
.DIV1 { width: 100%; margin-left: auto; margin-right: auto; height: 70px; } .DIV2 { float: left; text-align: center; width: 80px; }
HTML
HTML and CSS Syntax (Toggle Plain Text)
<div class='DIV1'> <div class='DIV2'>Content 1</div> <div class='DIV2'>Content 2</div> <div class='DIV2'>Content 3</div> </div>
I've learnt that by setting margin-left and margin-right to 'auto' in DIV1 the content inside is centered, but only if it contains one DIV2 inside it.
When I add more DIV2s then the content inside DIV1 now shows on the left and ignores any centering...
+=========== DIV 1 ===========+
+=====+=====+=====+---------------+
+ DIV 2 + DIV 2 + DIV 2 +----------------+
+=====+=====+=====+---------------+
+==========================+
Does anyone please know why this is happening, and how I can fix it?
Thanks
that's because you are doing it the wrong way, -it is the div2 who should have its margins set to auto and there should be no float.
-Here, try this:
-Here, try this:
.DIV1 {
width: 100%;
height: 70px;
}
.DIV2 {
margin: auto ;
display: inline;
text-align: center;
width: 80px;
} Last edited by Troy III; Aug 13th, 2009 at 8:57 pm.
sorry that will not do, either!
This proves to be more stubborn than expected...
You should use spans, instead of divs if you want them lined in the ceter of the container div and apply display property of inlilne-block
as in this example, since IE will not override DIVs native block property with css command but will add it to the span element.
Here's a working example:
This proves to be more stubborn than expected...
You should use spans, instead of divs if you want them lined in the ceter of the container div and apply display property of inlilne-block
as in this example, since IE will not override DIVs native block property with css command but will add it to the span element.
Here's a working example:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<head>
<style type="text/css">
#centered{
margin: auto;
width: 80%;
padding: 5px;
background: #eee;
text-align: center;
border: solid 1px #777;
}
.center{
width:160px;
background: silver;
display: inline-block;
padding: 0px 10px 0px 10px;
text-align: left;
border: solid 1px #777;
}
</style>
</head>
<body>
<div id="centered">
<span class='center'>1 This is centered</span>
<span class='center'>2 This is centered</span>
<span class='center'>3 This is centered</span>
</div>
</body>
</html> I recall all sorts of issues with inline divs. Can't remember the detail.
The only CSS I know that works cross browser, as already posted on Daniweb several times, is:
Just make sure that margin-left is neg 0.5 of the width.
Airshow
The only CSS I know that works cross browser, as already posted on Daniweb several times, is:
css Syntax (Toggle Plain Text)
.DIV1 { width: 100%;/* or whatever */ height: 70px;/* or whatever */ } .DIV2 { position : relative; left : 50%; width: 80px; margin-left : -40px; }
Airshow
Last edited by peter_budo; Aug 14th, 2009 at 12:49 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
50% of the solution lies in accurately describing the problem!
•
•
Join Date: Jun 2009
Posts: 38
Reputation:
Solved Threads: 0
Thanks Troy III... your solution works well!!
It's funny, but it's seems a bit strange to me why DIVs are so problematic in these situations. In moments like this I'm tempted to resort back to HTML tables, and that's saying something... the very thing DIVs are meant to replace.
But thanks for replying and providing a solution... I've tried it in my code, and it does the job perfectly!
By the way, are you aware of any issues with inline divs that Airshow refers to in his post?
It's funny, but it's seems a bit strange to me why DIVs are so problematic in these situations. In moments like this I'm tempted to resort back to HTML tables, and that's saying something... the very thing DIVs are meant to replace.
But thanks for replying and providing a solution... I've tried it in my code, and it does the job perfectly!
By the way, are you aware of any issues with inline divs that Airshow refers to in his post?
Last edited by nigelburrell; Aug 13th, 2009 at 11:18 pm. Reason: To add more content that I forgot to include
•
•
Join Date: Jun 2009
Posts: 38
Reputation:
Solved Threads: 0
Thanks Airshow for your post and solution.
I tried your code and it works to a certain extend, but instead of centering the content perfectly, it appears with a slight right offset beyond 50% across the page, but that's minor as I only had to adjust the 100% width in DIV1 to 80% instead. And another thing is that each DIV2 block re-positions itself as the page loads which looks a little strange and rather cumbersome, especially if I have a large numer of DIV2s appearing on the page.
In the end, I went with Troy III's solution simply because the DIV2 content appears instantly (without re-positioning during page loads), but I am curious about your comment that you know of some issues with inline divs...? Is this a cross-browser thing?
Thanks again for your code, I really appreciate your help.
I tried your code and it works to a certain extend, but instead of centering the content perfectly, it appears with a slight right offset beyond 50% across the page, but that's minor as I only had to adjust the 100% width in DIV1 to 80% instead. And another thing is that each DIV2 block re-positions itself as the page loads which looks a little strange and rather cumbersome, especially if I have a large numer of DIV2s appearing on the page.
In the end, I went with Troy III's solution simply because the DIV2 content appears instantly (without re-positioning during page loads), but I am curious about your comment that you know of some issues with inline divs...? Is this a cross-browser thing?
Thanks again for your code, I really appreciate your help.
•
•
•
•
Thanks Troy III... your solution works well!!
It's funny, but it's seems a bit strange to me why DIVs are so problematic in these situations. In moments like this I'm tempted to resort back to HTML tables, and that's saying something... the very thing DIVs are meant to replace.
But thanks for replying and providing a solution... I've tried it in my code, and it does the job perfectly!
By the way, are you aware of any issues with inline divs that Airshow refers to in his post?
Divs are block-level, do-nothing containers, and since this is the main purpose of their existence;
If you remove that property its no longer a DIV, it would become a simple inline element and will break the HTML semantics and/or shouldn't be capable of holding other block-level elements anymore.
So instead of making e block-level element behave like inline element, it is better to use a true inline element instead. Other coders are using ready inline-block elements like <li> but that will again break the HTML semantcs and should not be missused also.
So, again the best solution that remains for this type of positioning is using an inline element and add to it, an inline-block display property.
Yes, the soultion I suggested would have to be cross-browser, and since I allready mentioned, css instruction inline-block will not override the native line-break property of the div element in explorer.
Regards.
When you say:
and before you do that, - I envite you to see this:
http://i25.tinypic.com/23hwphk.png [sorry I dont see how I could link to an image here!] (That's how it will look on IE 6)
After all - not bad at all!
•
•
•
•
"I'm tempted to resort back to HTML tables"
http://i25.tinypic.com/23hwphk.png [sorry I dont see how I could link to an image here!] (That's how it will look on IE 6)
HTML and CSS Syntax (Toggle Plain Text)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>center elements</title> <style type="text/css"> #centered{ margin: auto; width: 90%; padding: 3px; background: gray; text-align: center; vertical-align: middle; } div span { display: inline-block; background: #fff; margin: 1px; padding: 2px; font: normal 10pt tahoma; padding-left: 10px; } span b { position: relative; display: inline-block; width: 2em; background:gray; color:white } span span { width:160px; background: silver; display: inline-block; padding: 0 10px 0 10px; text-align: left; margin: 2px; } </style> </head> <body> <div id="centered"> <span> <b>A</b> <span>1 This is centered</span> <span>2 This is centered</span> <span>3 This is centered</span> </span> <br> <span> <b>B</b> <span>1 This is centered</span> <span>2 This is centered</span> <span>3 This is centered</span> </span> <br> <span> <b>C</b> <span>1 This is centered</span> <span>2 This is centered</span> <span>3 This is centered</span> </span> <br> <span> <b>D</b> <span>1 This is centered</span> <span>2 This is centered</span> <span>3 This is centered</span> </span> <br> <span> <b>E</b> <span>1 This is centered</span> <span>2 This is centered</span> <span>3 This is centered</span> </span> <br> <span> <b>F</b> <span>1 This is centered</span> <span>2 This is centered</span> <span>3 This is centered</span> </span> <br> <span> <b>G</b> <span>1 This is centered</span> <span>2 This is centered</span> <span>3 This is centered</span> </span> <br> <span> <b>H</b> <span>1 This is centered</span> <span>2 This is centered</span> <span>3 This is centered</span> </span> <br> <span> <b>I</b> <span>1 This is centered</span> <span>2 This is centered</span> <span>3 This is centered</span> </span> <br> <span> <b>J</b> <span>1 This is centered</span> <span>2 This is centered</span> <span>3 This is centered</span> </span> </div> </body> </html>
After all - not bad at all!
•
•
•
•
I tried your code and it works to a certain extend, but instead of centering the content perfectly, it appears with a slight right offset beyond 50% across the page, but that's minor as I only had to adjust the 100% width in DIV1 to 80% instead.
•
•
•
•
And another thing is that each DIV2 block re-positions itself as the page loads which looks a little strange and rather cumbersome, especially if I have a large numer of DIV2s appearing on the page.
•
•
•
•
In the end, I went with Troy III's solution simply because the DIV2 content appears instantly (without re-positioning during page loads), but I am curious about your comment that you know of some issues with inline divs...? Is this a cross-browser thing?
Airshow
50% of the solution lies in accurately describing the problem!
![]() |
Similar Threads
- apply css to nested divs (HTML and CSS)
- Adding CSS content into an PHP include(); (PHP)
- tables vs divs & workflow advice? And a list for CSS workarounds for browsers? (Site Layout and Usability)
- CSS based design vs. Table based design (HTML and CSS)
- css looks good in IE terrible in FF ... help (HTML and CSS)
- IE6 large spaced between divs (HTML and CSS)
- javascript inside css (HTML and CSS)
- css not working in IE (HTML and CSS)
- Align floated images in center of page with CSS (HTML and CSS)
Other Threads in the HTML and CSS Forum
- Previous Thread: Images
- Next Thread: ie6 and png transparancy (sorry lol)
| Thread Tools | Search this Thread |
appointments asp background backgroundcolor beta browser bug calendar cart cgi code codeinjection corporateidentity css design development displayimageinsteadofflash dreamweaver emailmarketing epilepsy explorer firefox flash form format google griefers hackers hitcounter hover html ide ie7 ie8 iframe image images internet internetexplorer intranet iphone javascript jpeg layout macbook maps marketshare microsoft mozilla multimedia navigationbars news offshoreoutsourcingcompany opacity opera optimization perl pnginie6 positioning problem scroll seo shopping studio swf swf. textcolor timecolor titletags url urlseparatedwords visual visualization web webdevelopment webform website windows7 xml xsl





