/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Help-a-n00b

Hi everyone. I’m trying to find my bearings with css layout and positioning and I keep running into a quirk. I’m following a tutorial from the Art & Science of CSS p.93. It works fine as described, but when I try to apply it to my layout, my list items (sidebar=”nav” do not center like the tut. If anyone can help me out of my ineptitude, It would be appreciated.

CSS:

[CODE]
html {
font:small/1.4 “Lucida Grande”, Tahoma, sans-serif;
background:beige;
margin:0;
padding:0;
}
body {
font-size:92%
}

#wrap {
width:600px;
height:550px;
margin: 0 auto;
border: 1px solid black;
}
#head {
display:block;
margin: 20px auto;
width:550px;
height:100px;
border: 1px solid black;
background-attachment: relative;
background-image: url(bald-monkey.gif);
background-repeat: no-repeat;
background-position: center;
}
#nav {
display:block;
float:left;
margin-left:25px;
width:150px;
height:330px;
border: 1px solid black;
background-color: goldenrod;
}
#nav li {
position:relative;
margin: 0 auto;
list-style-type:none;
}

#nav a {
text-align:center;
display:block;
color:#fff;
text-decoration:none;
padding: 0 15px;
line-height: 2.5;
border-bottom:1px solid #fff;
}

#nav li:hover {
background: white;
}

#main {
display:block;
float:right;
margin-right:25px;
width:375px;
height:330px;
border: 1px solid black;
}

#foot {
display:block;
border: 1px solid black;
float:left;
width:550px;
height:35px;
margin-top:20px;
margin-left:25px;
}
h3 {
margin:0;
display:block;
text-align:center;
background-color:beige;
padding:30px 0;
font-family:verdana,sans-serif;
border-bottom:4px dotted black;
}
[/CODE]

XHTML:

[CODE]<body>
<div id=”wrap”>

<div id=”head”>

</div>

<div id=”nav”>
<h3>SIDEBAR</h3>
<ul>
<li><a href=”/”>Item One</a></li>
<li><a href=”/”>Item Two</a></li>
<li><a href=”/”>Item Three</a></li>
<li><a href=”/”>Item Four</a></li>
</ul>

</div>

<div id=”main”>

</div>

<div id=”foot”>

</div>
</div>
</body>[/CODE]

to post a comment
CSS

6 Comments(s)

Copy linkTweet thisAlerts:
@WebJoelMar 06.2008 — The first things that springs into mind is, -do you have a valid !doctype? Without one, you're not going to get anything to 'center' if you're using IE..

I'm fond of this !doctype. Place this first on your document.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
Copy linkTweet thisAlerts:
@CascadingIdiotauthorMar 06.2008 — Thanks.

This is my full css/wdoctype:

[CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">


<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
<style type="text/css">

html {
font:small/1.4 "Lucida Grande", Tahoma, sans-serif;
background:beige;
margin:0;
padding:0;
}
body {
font-size:92%
}

#wrap {
width:600px;
height:550px;
margin: 0 auto;
border: 1px solid black;
}
#head {
display:block;
margin: 20px auto;
width:550px;
height:100px;
border: 1px solid black;
background-attachment: relative;
background-image: url(bald-monkey.gif);
background-repeat: no-repeat;
background-position: center;
}
#nav {
display:block;
float:left;
margin-left:25px;
width:150px;
height:330px;
border: 1px solid black;
background-color: goldenrod;
}
#nav li {
position:relative;
margin: 0 auto;
list-style-type:none;
}

#nav a {
text-align:center;
display:block;
color:#fff;
text-decoration:none;
padding: 0 15px;
line-height: 2.5;
border-bottom:1px solid #fff;
}

#nav li:hover {
background: white;
}

#main {
display:block;
float:right;
margin-right:25px;
width:375px;
height:330px;
border: 1px solid black;
}

#foot {
display:block;
border: 1px solid black;
float:left;
width:550px;
height:35px;
margin-top:20px;
margin-left:25px;
}
h3 {
margin:0;
display:block;
text-align:center;
background-color:beige;
padding:30px 0;
font-family:verdana,sans-serif;
border-bottom:4px dotted black;
}


</style>
</head>[/CODE]


And it works fine in IE7.
Copy linkTweet thisAlerts:
@WebJoelMar 06.2008 — ... but when I try to apply it to my layout, my list items (sidebar="nav" do not center like the tut. If anyone can help me out of my ineptitude, It would be appreciated.[/quote]
#nav {

display:block;

[B]float:left;[/B]

margin-left:25px;

width:150px;

height:330px;

border: 1px solid black;

background-color: goldenrod;


}[/QUOTE]
If this is the side="#nav", the "float:left;" is not going to "center" it...

Since it has a width, use "[B]margin:0 auto;[/B]" and that will center this container... and :

#nav li {

position:relative;

list-style-type:none;
[B]text-align:center;[/B]"

}[/QUOTE]
will center the [I]contents of the list-item[/I] in the "<li></li>"s
Copy linkTweet thisAlerts:
@CascadingIdiotauthorMar 07.2008 — My fault. The #nav needs to stay float:left, I put [I]text-align:center[/I], back into [I]#nav li[/I], and it does center the text between the padding, but for I need the whole [I]#nav li[/I] to center within [I]#nav[/I]. I followed the tutorial word for word, and their example, w/no padding, the [I]#nav li[/I] centered within [I]#nav [/I]and without specifying padding, the border-bottom extended from edge to edge of [I]#nav[/I].

[upl-file uuid=94298802-59e4-435f-8656-153a1ac385f5 size=9kB]nav.JPG[/upl-file]
Copy linkTweet thisAlerts:
@CentauriMar 07.2008 — Looks like you are getting caught with browser default margins/paddings here. List items are normally indented, and IE uses left margin on the <ul> whereas FF uses left padding. You need to control both margins and padding of lists to get cross-browser conformity. Either zero the values on the <ul> [CODE]#nav ul {
margin: 0;
padding: 0;
}[/CODE]
or as I prefer, start the css with the universal selctor to zero all defaults first - then you don't have to worry about them [CODE]* {
margin: 0;
padding: 0;
}[/CODE]
Copy linkTweet thisAlerts:
@CascadingIdiotauthorMar 07.2008 — Thanks Centauri, the universal selector to zero all defaults was the answer... I'll remember that tip!

Cheers
×

Success!

Help @CascadingIdiot spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...