473,487 Members | 2,680 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to center nested divs

I'm trying to style a kind of minimalist welcome page where each of X
number of (gray?) boxes will be links to a part of the site.

I want to implement the navbar (ie, set of boxes linking to other
pages) as an unordered list, with the <ulitem being linked to an ID
that sets layout, etc., and each <lilinked to an ID that renders its
box a different height.

So far, body looks as follows:
<body>
<div id="container">

<h1 id="header"><a href="#">site name</a></h1>

<ul id="navmenu">
<li id="blog"><a href="#">blog</a></li>
<li id="writing"><a href="#">writing</a></li>
<li id="photos"><a href="#">photos</a></li>
</ul>

</div>
</body>

The container ID is meant to center the entire menu on the page. It
has worked to a degree--the container is, itself, centered, but the
elements inside it are not.

Now, for the CSS:
@import "style.css";

#header {
text-align: center;
}

#container {
position: relative;
margin-left: auto;
margin-right: auto;
width: 600px;
}

ul#navmenu {
padding: 0;
list-style: none;
}

li {
padding: .6em 0 0 .8em;
float: left;
margin-left: 1em;
width: 5em;
height: 18em;
}

li, li a:link {
display: block;
font-family: "Lucida Grande", sans-serif;
font-size: 10pt/1.25;
color: #BBBBBB;
text-decoration: none;
background-color: #BBBBBB;
}

li a:hover {
color: #FFF;
}

li#blog {
height: 12em;
}

li#writing {
height: 15em
}

li#photos {
height: 10em;
}

What is in the imported file isn't relevant, as far as I can tell, as
the <uland <lielements are always 'namespaced' under specific IDs
in that stylesheet.

So: how do I center the blocks within the container? It's easy enough
to center a single element within another one... but how do you center
those 3 blocks with a 1em spacing in between each one?

As a quick aside, how can I make it so hovering over any portion of
the box triggers a:hover?

Thanks,
David
Jun 27 '08 #1
2 7181
On 2008-05-05, David <db******@gmail.comwrote:
I'm trying to style a kind of minimalist welcome page where each of X
number of (gray?) boxes will be links to a part of the site.

I want to implement the navbar (ie, set of boxes linking to other
pages) as an unordered list, with the <ulitem being linked to an ID
that sets layout, etc., and each <lilinked to an ID that renders its
box a different height.

So far, body looks as follows:
<body>
<div id="container">

<h1 id="header"><a href="#">site name</a></h1>

<ul id="navmenu">
<li id="blog"><a href="#">blog</a></li>
<li id="writing"><a href="#">writing</a></li>
<li id="photos"><a href="#">photos</a></li>
</ul>

</div>
</body>
[...]
#container {
position: relative;
margin-left: auto;
margin-right: auto;
width: 600px;
}
[...]
li {
padding: .6em 0 0 .8em;
float: left;
margin-left: 1em;
width: 5em;
height: 18em;
}
[...]
So: how do I center the blocks within the container? It's easy enough
to center a single element within another one... but how do you center
those 3 blocks with a 1em spacing in between each one?
You can't centre floats-- they float to one side or the other.

You could make them display: inline-block and vertical-align: top
instead of float: left and then put text-align: center on the container.

But that won't work in (at least) Firefox 2.

Alternatively, you could shrink-to-fit and center the container by
putting display: table and margin: 0 auto on ul#navmenu.

That won't work in IE, but see also

http://netweaver.com.au/alt/shrinkTo...rinkToFit.html

for a possible workaround.

But you can get away without centred shrink-to-fit here since you are
setting the widths of the LIs to 5em anyway. You want 1em between each,
so that's 17em altogether (3*5+2). Set the UL to width: 17em and margin:
0 auto and that should work.

Make sure you also set padding and margin to 0 on the ul-- one or other
of those will usually be 40px on the left from the default stylesheet.
As a quick aside, how can I make it so hovering over any portion of
the box triggers a:hover?
Do something like li:hover a { color: #fff }. That matches an A inside a
hovered-over LI.
Jun 27 '08 #2
On May 5, 4:19*pm, Ben C <spams...@spam.eggswrote:
On 2008-05-05, David <dbjac...@gmail.comwrote:
I'm trying to style a kind of minimalist welcome page where each of X
number of (gray?) boxes will be links to a part of the site.
I want to implement the navbar (ie, set of boxes linking to other
pages) as an unordered list, with the <ulitem being linked to an ID
that sets layout, etc., and each <lilinked to an ID that renders its
box a different height.
So far, body looks as follows:
<body>
<div id="container">
<h1 id="header"><a href="#">site name</a></h1>
<ul id="navmenu">
* *<li id="blog"><a href="#">blog</a></li>
* *<li id="writing"><a href="#">writing</a></li>
* *<li id="photos"><a href="#">photos</a></li>
</ul>
</div>
</body>
[...]
#container {
* *position: relative;
* *margin-left: auto;
* *margin-right: auto;
* *width: 600px;
}
[...]
li {
* *padding: .6em 0 0 .8em;
* *float: left;
* *margin-left: 1em;
* *width: 5em;
* *height: 18em;
}
[...]
So: how do I center the blocks within the container? It's easy enough
to center a single element within another one... but how do you center
those 3 blocks with a 1em spacing in between each one?

You can't centre floats-- they float to one side or the other.

You could make them display: inline-block and vertical-align: top
instead of float: left and then put text-align: center on the container.

But that won't work in (at least) Firefox 2.

Alternatively, you could shrink-to-fit and center the container by
putting display: table and margin: 0 auto on ul#navmenu.

That won't work in IE, but see also

* *http://netweaver.com.au/alt/shrinkTo...ingShrinkToFit....

for a possible workaround.

But you can get away without centred shrink-to-fit here since you are
setting the widths of the LIs to 5em anyway. You want 1em between each,
so that's 17em altogether (3*5+2). Set the UL to width: 17em and margin:
0 auto and that should work.

Make sure you also set padding and margin to 0 on the ul-- one or other
of those will usually be 40px on the left from the default stylesheet.
As a quick aside, how can I make it so hovering over any portion of
the box triggers a:hover?

Do something like li:hover a { color: #fff }. That matches an A inside a
hovered-over LI.
Thanks so much for the info. This has irked me for a while.

David
Jun 27 '08 #3

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

Similar topics

9
4056
by: Dustin | last post by:
Here's what I am trying to do: (Names represent CSS classes.) I want to create a photo gallery. I want the entire gallery to be surrounded by a box named PhotoGallery. I want a fluid placement...
3
4995
by: Philip | last post by:
I am trying to make a bunched of left-floated divs that will be contained in a larger div. the floated divs all contain a left-floated img and text of varying sizes. If I don't set a height (or...
2
2066
by: BenOne© | last post by:
Hi all, I'm trying to create an image gallery with CSS but I can't get the thumbnails to center horizontally as a group. The page, including css defined in the <HEAD> is located at:...
3
23873
by: Jennifer | last post by:
I have a page with three divs (top, left and center). I want to place hyperlinks in the left column (div), where each link calls a separate page. I want each separate page to open in the center...
2
1940
by: Guadala Harry | last post by:
Please follow my thinking here and tell me if I'm nuts or if this is a reasonable thing to do given the objective: I'd like to present 3 data values on an aspx page - each styled uniquely, and...
40
4421
by: Mark | last post by:
Hello i like to make the following lay out: - 3 columns - center column = fixed width - left and right columns are not fixed and must take all the available space there is left. - left...
19
6174
roula
by: roula | last post by:
I HATE DIVS AND DIFFERENT BROWSERS!!! i spent the whole day and night searching for a way to center align stupid divs, the problem is that the code is so sensitive, i lost my mind trying to make them...
1
3467
by: judacris | last post by:
I've seen the threads here about molding 2 divs in a centered fashion. but I can't seem to solve this thing. my blogger blog is functioning well on my site for now, but the blog feed (left) and...
2
2157
by: Ben C. | last post by:
Hi If I have a container div and inside the container div I float two divs (one left and one right), the containting div will collapse as the floated divs will no longer prop it up, meaning that...
0
7106
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
7137
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,...
0
7181
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...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7349
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...
0
5442
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,...
1
4874
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4565
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...
0
1381
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 ...

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.