473,386 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Centering a <div>


To center my <div>, I am currently using something like
<div id="navcontainer" align="center">
This centers fine, but is deprecated code. I have seen these two solutions
recommended.

1)

body {
text-align: center;
min-width: 500px;
}
#wrapper {
position: relative;
text-align: left;
width: 500px;
margin-left: auto;
margin-right: auto;
}

This will undoubtedly work, but unfortunately centers text in the entire
page. And you can't use "text-align: center" in a <div>
2)
Centering a block with a width of 60% and a height of 30%:

..centerblock {
position: absolute;
width: 60%;
height: 30%;
left: 20%;
top: 35%;
}

I tried this one but it does not work for me. It does not center the <div>,
so I am stuck with deprecated coded, unless someone has a solution.

Thanks, CMA
Jul 20 '05 #1
3 4611
CMAR wrote:

To center my <div>, I am currently using something like
<div id="navcontainer" align="center">
This centers fine, but is deprecated code.


http://dorward.me.uk/www/centre/#block (noting the bugs it describes in IE,
and the various work arounds)

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Jul 20 '05 #2
"CMAR" <cm***@yahoo.com> wrote:
To center my <div>, I am currently using something like
<div id="navcontainer" align="center">
This centers fine, but is deprecated code. I have seen these two solutions
recommended.

1)

body {
text-align: center;
Only needed by IE5- (and IE6 if you trigger quirks mode)
min-width: 500px;
Not needed.
} #wrapper {
position: relative;
Not needed.
text-align: left;
If you have this, then you shouldn't have the problem described below.
width: 500px;
margin-left: auto;
margin-right: auto;
This is the correct way of centering a block element and works in
modern browsers.
}

This will undoubtedly work, but unfortunately centers text in the entire
page.
So set text-align:left; on the child elements, just like you did
above.
And you can't use "text-align: center" in a <div>
Yes you can. It centers the content of the div, exactly as it's
supposed to.
2)
Centering a block with a width of 60% and a height of 30%:

.centerblock {
position: absolute;
width: 60%;
height: 30%;
left: 20%;
top: 35%;
}

I tried this one but it does not work for me. It does not center the <div>,
so I am stuck with deprecated coded, unless someone has a solution.


That will center it horizontally. (But width: 60%; margin-left: 20%;
margin-right: 20%; is simpler.)

Vertically it might work depending on the height of the parent
element.

The two bits of code you've given are very different, what are you
actually trying to do?

Steve

--
"My theories appal you, my heresies outrage you,
I never answer letters and you don't like my tie." - The Doctor

Steve Pugh <st***@pugh.net> <http://steve.pugh.net/>
Jul 20 '05 #3

"David Dorward" <do*****@yahoo.com> wrote in message
news:c8*******************@news.demon.co.uk...
CMAR wrote:

To center my <div>, I am currently using something like
<div id="navcontainer" align="center">
This centers fine, but is deprecated code.
http://dorward.me.uk/www/centre/#block (noting the bugs it describes in

IE, and the various work arounds)

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>


Thanks to David and Steve for the very useful posts.
David, I was able to use your code with some modification and tweaking to
get the centering I want. It looks good in IE6. I assume that it will also
be centered in IE5 and IE5.5, since that was the whole purpose of the
exercise.

<div style="text-align:center;">
<div id="navcontainer">
<ul id="navlist">
<li><a href="mailto:xy*@efg.com" title="Send email">Email</a></li>
<li><a href="order.htm" title="orders">To Order</a></li>
<li><a href="index.html" title="Homepage">Home</a></li>
</ul>
</div>
</div>

The relevant style is
#navcontainer {background-color: #FF8040; margin-left: auto; margin-right:
auto;
padding-top: .45em; width: 100%;}
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Daniel Hansen | last post by:
I know this must seem totally basic and stupid, but I cannot find any reference that describes how to control the spacing between <p>...</p> and <div>...</div> blocks. When I implement these on a...
4
by: Hal Canary | last post by:
<div align="center"> does not seem to be identical to <div style="text-align:center">, as this example http://home.earthlink.net/~canaryh3/example.html points out. Yet the 'align' attribute...
1
by: Ron Bott | last post by:
I have a fixed size <div> I would like to display in the center of a browser window. To get the horizontal alignment is no problem using 'margin-left: auto' and 'margin-right: auto' but I cannot...
3
by: Josef K. | last post by:
Asp.net generates the following html when producing RadioButton lists: <td><input id="RadioButtonList_3" type="radio" name="MyRadioButtonList" value="644"...
3
by: John Pote | last post by:
1. Horizontal centering a <divin browser window. The current trend seems to be to place page content in a fixed width area in the middle of the browser window. How is this achieved? If I use a...
3
by: rsteph | last post by:
I'm using two divs to create a shadowed-box type effect. Within the top div I want to put an image. I can get the image to center right to left, but not top to bottom. I'm making a series of boxes,...
8
by: CJM | last post by:
I've inserted a new banner in an existing page and I want the banner image to be centered (horizontally) within its container. I'm not bothered whether the accompanying line of text is restricted...
4
by: harryusa | last post by:
I am trying to center 2 images concentrically which are z-indexed to lay on top of each other making an image with a border from another image that has a transparent center. I need the images to be...
8
prino
by: prino | last post by:
Hi all, I've written code (in REXX) that takes files in legacy languages (PL/I, COBOL, z/OS assembler, etc) and converts them into HTML in a format similar to what's displayed in the z/OS ISPF...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.