/    Sign up×
Community /Pin to ProfileBookmark

Problem with "wrapper" div.

Hello web developers! ?

I am new to html and css and I have a problem that have been boggling my mind. I hope you guys can help me with this
and perhaps point me at the right direction.

I have been taught to place all my header, footer, divs, images, text etc. in a div called “wrapper”.
Example:

[code=html]
<div id=”wrapper”>
<header>
HEADER
</header>
<div id=”box1″>
BOX 1
</div>
<div id=”box2″>
BOX 2
</div>
<div id=”box3″>
BOX 3
</div>
<footer>
FOOTER
</footer>
</div>

[/code]

In order to layout my web page, I need to float box1, box2 and box3. The problem is, when I float all the 3 boxes, the footer will immediately touch my header.

I have been taught to use “overflow:hidden;” on the css for the “wrapper” div, but the footer is still touching.

The ONLY way I can fix this, is to separate my “header” and “footer” divs outside my “wrapper”… but is that considered best practice? I have seen alot of websites having all their divs in a “wrapper” div successfully.

Can anybody explain to me this problem? :/ Below is the CSS I used.

[code=html]
#wrapper{
width:900px;
background-color:red;
margin:auto;
overflow:hidden;
}

header{
width:900px;
height:300px;
background-color:green;
}

#box1{
width:200px;
height:200px;
background-color:yellow;
float:left;
}

#box2{
width:200px;
height:200px;
background-color:white;
float:left;
}

#box3{
width:200px;
height:200px;
background-color:pink;
float:left;
}

footer{
width:900px;
height:100px;
background-color:black;
}
[/code]

Another thing, is it okay to have an embedded AND an external css stylesheet when buidling web pages? I prefer to do that. I like to keep the layout
style for each page embedded in the “head” of my web page since the layout are different from each page. And the rest of the styles which affect all the webpages in an external stylesheet. Is that considered best practice or should I dump everything in an external stylesheet?

Thanks in advance for any input you guys. ?

to post a comment
HTML

5 Comments(s)

Copy linkTweet thisAlerts:
@auntniniNov 11.2012 — You could try the following. Remember, new HTML5 elements (namely your <header> and <footer>) need to be declared display: block; and browsers may not yet recognize them so it would be better to create #ID divs at this point.

<i>
</i>&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="UTF-8"&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;style type="text/css"&gt;
html, body { margin: 0; padding: 0; background: #3FF;}
/* remember to set HTML 5 elements display:block: */
#wrapper{
width:900px;
background-color:red;
margin:auto;
overflow:hidden;
}
.container { width:90%; margin:2px auto;
background-color:white;
}
header{ display: block;
margin: 2px auto;
width:90%;
height:300px;
background-color:green;
}
hr { clear:both;}
#box1{
width:30%;
height:200px;
background-color:yellow;
float:left;
margin:10px;
padding:2px;
}

#box2{
width:30%;
height:200px;
background-color:purple;
float:left;
margin:10px;
padding:2px;
}

#box3{
width:30%;
height:200px;
background-color:pink;
float:left;
margin:10px;
padding:2px;
}

footer{ display: block;
clear:both; <br/>
margin: 2px auto;
width:90%;
height:100px;
background-color:black;
}
&lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div id="wrapper"&gt;
&lt;header&gt;
HEADER
&lt;/header&gt;
&lt;div class="container"&gt;
&lt;div id="box1"&gt;
BOX 1
&lt;/div&gt;
&lt;div id="box2"&gt;
BOX 2
&lt;/div&gt;
&lt;div id="box3"&gt;
BOX 3
&lt;/div&gt;
&lt;/div&gt;&lt;!--closeCONTAUNER--&gt;
&lt;hr&gt; <br/>
&lt;footer&gt;
FOOTER
&lt;/footer&gt;
&lt;/div&gt;&lt;!--closeWRAPPER--&gt;
&lt;/body&gt;
&lt;/html&gt;


As you know, CSS = Cascading Style Sheets so <style> embedded in page overrides external/linked stylesheet. The goal would be to keep the HTML page as lightweight as possible so it downloads quickly. The external stylesheet should also be as lightweight as possible, but you can declare all the elements in the external stylesheet and only use a certain selection of them in any given HTML page.
Copy linkTweet thisAlerts:
@happeepillauthorNov 11.2012 — Thanks for the detailed reply auntnini!

I learnt alot especially the css rule for the footer; "clear:both;" .

However, I noticed another problem. :/

I tried to move "box1" down 50px by using...

[code=html]
#box1{
position:relative;
top:50px;
}
[/code]


... and "box1" goes ON TOP of the footer! ? I want the footer to be pushed down no matter where I place box1, box2 and box3. If I push the footer 50px down to accommodate "box1", my other web pages may have a different layout and the footer will have a 50px gap on the rest of the pages. I hope you get what I mean.

Below is the entire code:

[code=html]
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<style type="text/css">
html, body { margin: 0; padding: 0; background: #3FF;}
/* remember to set HTML 5 elements display:block: */
#wrapper{
width:900px;
background-color:red;
margin:auto;
overflow:hidden;
}
.container { width:90%; margin:2px auto;
background-color:white;
}
header{ display: block;
margin: 2px auto;
width:90%;
height:300px;
background-color:green;
}

hr{
clear:both;
}

#box1{
width:30%;
height:200px;
background-color:yellow;
float:left;
margin:10px;
padding:2px;
position:relative;
top:50px;
}

#box2{
width:30%;
height:200px;
background-color:purple;
float:left;
margin:10px;
padding:2px;
}

#box3{
width:30%;
height:200px;
background-color:pink;
float:left;
margin:10px;
padding:2px;
}

footer{
display: block;
clear:both;

margin: 2px auto;
width:90%;
height:100px;
background-color:black;
}
</style>
</head>

<body>
<div id="wrapper">
<header>
HEADER
</header>
<div class="container">
<div id="box1">
BOX 1
</div>
<div id="box2">
BOX 2
</div>
<div id="box3">
BOX 3
</div>
</div><!--closeCONTAUNER-->
<hr>

<footer>
FOOTER
</footer>
</div><!--closeWRAPPER-->
</body>
</html>
[/code]


Thanks! ?
Copy linkTweet thisAlerts:
@happeepillauthorNov 12.2012 — I realized that I need to use margins and padding for layout rather than just relying on position.

Thanks anyway. ?
Copy linkTweet thisAlerts:
@auntniniNov 13.2012 — See for instance http://webdesign.about.com/od/styleproperties/p/blspposition.htm, http://www.w3schools.com/cssref/pr_class_position.asp. etc.

position: relative; offsets element but original space is still reserved for it. Think what you want is [B]margin-top[/B]: 50px; (shorthand: top right bottom left = margin: 50px 10px 10px 10px;}. Listing one value -- as in margin: 10px; -- is shorthand for all 4 sides; two values would be top/bottom right/left.

I use position;relative manly to establish a "positioned parent" element for any nested elements.
Copy linkTweet thisAlerts:
@auntniniNov 15.2012 — Think the basic answer to your original question was that the new HTML 5 elements have default [B]display:inline[/B] (because most browsers have not yet set their styles to make them display:block) so you have to style them display:block.

Then you have to consider that some browsers (especially IE) do not support HTML5 elements.

This got me to thinking ... maybe you should simplify your style sheet by just using classes for [B].container[/B] and [B].box[/B] (the following has 30% .boxThird and 45% .boxHalf) and classes for different background colors. You can apply multiple space-separated classes to same element, Hence the following (for fun).

<i>
</i>&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="UTF-8"&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;style type="text/css"&gt;
html, body { margin: 0; padding: 0; background: #3FF;}
/* remember to set HTML 5 elements display:block: */
#wrapper{
width: 95%; max-width:900px; min-width: 400px;
margin:auto;
overflow:hidden;
}
.container { width:90%; margin:2px auto; padding: 2px; border: thin #FF0 solid; overflow:auto;
}
hr { clear:both;}

.boxThird{
width:30.5%;
height:200px;
float:left;
margin: 20px .95%;
padding:2px;
border: thin #000 solid;
}

.boxHalf {
width:47%;
height:200px;
float:left;
margin: 20px .97%;
padding:2px;
border: thin #fff solid;
}
.bgRed { background-color: #F00;}
.bgGreen { background-color: #0F0;}
.bgBlue { background-color: #00F;}
.bgBlack { background-color: #000;}
.bgWhite { background-color: #FFF;}
.bgYellow { background-color: #FF0;}
.bgPink { background-color: #F0F;}
.bgPurple { background-color: #90F;}
.bgGray { background-color: #CCC;}
.bgAlpha { background: rgba(255,255,255, 0.50); }

&lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div id="wrapper" class="bgAlpha"&gt;
&lt;div class="container bgRed"&gt;
&lt;h1&gt;HEADER&lt;/h1&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;

<i> </i>&lt;/div&gt;&lt;!--closeCONTAUNER for header--&gt;
&lt;hr&gt;
&lt;div class="container bgWhite"&gt;
&lt;div class="boxThird bgBlue"&gt;
&lt;h2&gt;BOX 1&lt;/h2&gt;
&lt;/div&gt;
&lt;div class="boxThird bgGreen"&gt;
&lt;h2&gt;BOX 2&lt;/h2&gt;
&lt;/div&gt;
&lt;div class="boxThird bgRed"&gt;
&lt;h2&gt;BOX 3&lt;/h2&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;!--closeCONTAUNER for 3 boxes--&gt;
&lt;hr&gt; <br/>
&lt;div class="container bgYellow"&gt;
&lt;div class="boxHalf bgPurple"&gt;
&lt;h2&gt;BOX half&lt;/h2&gt;
&lt;/div&gt;
&lt;div class="boxHalf bgGreen"&gt;
&lt;h2&gt;BOX half&lt;/h2&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;!--closeCONTAUNER for 3 boxes--&gt;
&lt;hr&gt;
&lt;div class="container bgGray"&gt;
&lt;h3&gt;FOOTER&lt;/h3&gt;
&lt;/div&gt;&lt;!--closeCONTAUNER for footer--&gt;
&lt;/div&gt;&lt;!--closeWRAPPER--&gt;
&lt;/body&gt;
&lt;/html&gt;
×

Success!

Help @happeepill 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.21,
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,
)...