[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Center a div using CSS?

Posted on 2003-11-04
9
Medium Priority
?
47,670 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
This article discusses four methods for overlaying images in a container on a web page
In this Micro Tutorial users will learn how to embed custom fonts into websites using @font-face in CSS Select a font: Ensure the EULA allows you to use @font-face: Download the font: Get the browser-compatible files you need: Edit your CSS       - Name …
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
Suggested Courses

656 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