473,729 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Centering a <div>


To center my <div>, I am currently using something like
<div id="navcontaine r" 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 4633
CMAR wrote:

To center my <div>, I am currently using something like
<div id="navcontaine r" 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.co m> wrote:
To center my <div>, I am currently using something like
<div id="navcontaine r" 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******** ***********@new s.demon.co.uk.. .
CMAR wrote:

To center my <div>, I am currently using something like
<div id="navcontaine r" 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="navcontaine r">
<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.htm l" 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
14467
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 page, there is a huge gap (like 3/8 inch or 25 px) between them. This is driving me bananas. What the hey am I missing? dh ------------------------------------------------ Dan Hansen ------------------------------------------------
4
3486
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 is deprecated. Does anyone have any ideas?
1
7504
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 figure out how to get it to centet vertically. Any ideas? Ron Bott
3
3811
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" onclick="__doPostBack('SitesRadioButtonList_3','')" language="javascript" />
3
4209
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 top level <divthen I can place it with CSS attribute 'left:', but this is a fixed offset. Is it possible to have a <divcentered in the browser window? 2. Verticle centering in <div>
3
7853
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, and the images in them aren't all the same size. So I've sized the boxes to mach the largest of them, and I want to center them, so those that are smaller will be in the middle of the first div. Here's the code on the page: <table border="0"...
8
609
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 to the width of the image and centered, or whether is simply fills the width of the container. I've tried various things to achieve this (e.g setting margins to auto) but I'm not having much luck. The URL for the page is: http://www.eminox.es
4
5284
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 horizonally and vertically centered within a <TD> </TD> area. No matter what I do the two image just present one above the other, albeit centered, NOT on top of each other. I originally did the entire page layout in TABLES until I found out I.E. 7...
8
10047
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 editor. A fellow member of the PCG has helped me by creating a bit of Javascript to emulate the scrolling and using Google I've now gotten it into a state where it almost passes the W3C Markup Validation Service. However, the one error, Error Line 166,...
0
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9281
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.