[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Center a div using CSS?

Posted on 2003-11-04
9
Medium Priority
?
47,673 Views
Last Modified: 2011-08-18
Is there a way to center a div using CSS? NOT text-align -- that centers the TEXT inside it, but not the entire div. I do NOT want to center the text.

If I use align=center it works perfectly. However, that doesn't validate.
0
Comment
Question by:webwoman
9 Comments
 
LVL 4

Expert Comment

by:Fendrin
ID: 9680892
set margin-left and margin-right to auto

margin: 0 auto 0 auto;
0
 
LVL 4

Assisted Solution

by:Fendrin
Fendrin earned 500 total points
ID: 9680942
or of course, what ever top and bottom margin you want..i just put those zeroes there as placeholders...

also, to warn you, there are some old browsers (IE5/Mac?) that don't center like this correctly. However, as a work around, you can use 'text-align: center' on the parent div and then 'text-align:left' on the div iteslf...

(X)HTML
<div class="wrapperbox">
   <div class="centeredbox">
      non-centered text
   </div>
</div>

CSS
.wrapperbox { text align: center;}
.centeredbox {margin: auto; text-align:left;}
0
 
LVL 4

Expert Comment

by:Fendrin
ID: 9680957
oh, keep in mind that you need to use both parts for total compatability.. text-align *shouldn't* move block elements.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 19

Author Comment

by:webwoman
ID: 9681479
Text-align does NOT work. It aligns the text INSIDE the div. That is NOT what I want.
Margins don't work either, I tried that already. It sets the margin to what I want, but that's NOT what I'm looking for. That is NOT centered. It puts the div whatever margin I set, that is NOT centered.

Try it yourself. It doesn't work.
0
 
LVL 4

Expert Comment

by:Fendrin
ID: 9682208
I have used this technique many times... rest assured it does work. take a look at this: www.lebor.net/~fendrin/centertest.htm go ahead and look at the soucre code. the only thing different i did was to explicitly set the width of the centered block

one of two things is happening here...
1) you have not implemented what I intended correctly (whether it be a mistake on your part, a mistake on my part, a simple misunderstanding.. what ever)
2) what you are looking to do is more complicated that what you have said... perhaps if you post your code, or give more context, I might be able to come up with a solution.
0
 
LVL 31

Assisted Solution

by:seanpowell
seanpowell earned 500 total points
ID: 9682210
If you know the width of the div - you can use the margin property:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Page title</title>
<style type="text/css">
<!--
.centered
    {
    position:absolute;
    width:200px;
    left:50%;
    margin-left:-100px;
    background-color:#CCCCCC;
    height:100%;
    top:0px;
    }
//-->
</style>
</head>
<body>
<div class="centered">I'm in the middle</div>
</body>
</html>
0
 
LVL 17

Accepted Solution

by:
dorward earned 800 total points
ID: 9684969
0
 
LVL 8

Assisted Solution

by:tncbbthositg
tncbbthositg earned 200 total points
ID: 9701569
Webwoman, the only way I have found to produce this effect is similar to the method USED (as opposed to suggested) by Fendrin.  What Fendrin did was create a container that centers the item.  If you embed a div in a div and center the daughter div in the parent div, it works fine in IE, NS, Moz, and Firebird (even most older versions).

Another option is to stick with tables.  Last I checked (a while ago) tables were not only implemented in the XHTML Strict DTD, but there are no plans to deprecate the use of tables.  They are a very effective means of achieving desired layout.

. . . for what it's worth.


Good luck Webwoman,
TNC
0
 
LVL 4

Expert Comment

by:Fendrin
ID: 9704132
Glad to have helped. Thanks for the points!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question