TUTORIAL: Mise à flot deux divs la même hauteur avec CSS
- righteous_trespasser
- Scuffle


- Inscription: Mar 12, 2007
- Messages: 6228
- Loc: South-Africa
- Status: Offline
Introduction
Il ya certaines situations où vous construisez un site avec un menu de gauche et le menu et les parties du corps sont à la fois flottait à gauche pour montrer à côté de l'autre. Toutefois, le contenu du menu et le contenu sont à la fois dynamiques et donc, vous ne pouvez pas spécifier manuellement la hauteur pour l'une d'elles. Vous devez donc vous assurer qu'ils sont tous les deux la même hauteur, ce que je te montre là ne rend pas effectivement les deux colonnes à la même hauteur, mais il donne l'impression qu'elles sont la même hauteur.
Flottants, les deux colonnes
Cela ne devrait pas être un problème pour vous, car vous arrivez à ce tutoriel avec deux divs qui sont déjà flottait à côté de l'autre, mais permet simplement d'avoir un rapide coup d'oeil à ce que ce code va ressembler, et ce que la sortie sera.
Le code:
Code: [ Select ]
<!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" xml:lang="en" lang="en">
<head>
<title>R_T Tutorial</title>
<style type='text/css'>
body{
background-color:#dddddd;
}
div#container{
width:980px;
margin:0px auto 0px auto;
}
div#header{
width:980px;
height:225px;
background-image:url('images/header.gif');
}
div#body_container{
width:980px;
}
div#menu_left{
width:230px;
background-color:#004163;
float:left;
}
div#body{
background-color:#ffffff;
width:750px;
float:left;
}
</style>
</head>
<body>
<div id='container'>
<div id='header'></div>
<div id='body_container'>
<div id='menu_left'>
<a href='#'>menu link</a>
</div>
<div id='body'>
<h1>Body Heading</h1>
<p>Some body text here. this is just some sample text to take up a bit of space and show you what is going on.</p>
</div>
</div>
</div>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>R_T Tutorial</title>
<style type='text/css'>
body{
background-color:#dddddd;
}
div#container{
width:980px;
margin:0px auto 0px auto;
}
div#header{
width:980px;
height:225px;
background-image:url('images/header.gif');
}
div#body_container{
width:980px;
}
div#menu_left{
width:230px;
background-color:#004163;
float:left;
}
div#body{
background-color:#ffffff;
width:750px;
float:left;
}
</style>
</head>
<body>
<div id='container'>
<div id='header'></div>
<div id='body_container'>
<div id='menu_left'>
<a href='#'>menu link</a>
</div>
<div id='body'>
<h1>Body Heading</h1>
<p>Some body text here. this is just some sample text to take up a bit of space and show you what is going on.</p>
</div>
</div>
</div>
</body>
</html>
- <!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" xml:lang="en" lang="en">
- <head>
- <title>R_T Tutorial</title>
- <style type='text/css'>
- body{
- background-color:#dddddd;
- }
- div#container{
- width:980px;
- margin:0px auto 0px auto;
- }
- div#header{
- width:980px;
- height:225px;
- background-image:url('images/header.gif');
- }
- div#body_container{
- width:980px;
- }
- div#menu_left{
- width:230px;
- background-color:#004163;
- float:left;
- }
- div#body{
- background-color:#ffffff;
- width:750px;
- float:left;
- }
- </style>
- </head>
- <body>
- <div id='container'>
- <div id='header'></div>
- <div id='body_container'>
- <div id='menu_left'>
- <a href='#'>menu link</a>
- </div>
- <div id='body'>
- <h1>Body Heading</h1>
- <p>Some body text here. this is just some sample text to take up a bit of space and show you what is going on.</p>
- </div>
- </div>
- </div>
- </body>
- </html>
votre sortie devrait maintenant ressembler à ceci:
Attachments:
The first time you view it, it should look like this.
Faisant d'eux les mêmes hauteur
De faire maintenant les deux colonnes à la même hauteur, il ya trois choses que nous devons faire, à savoir:
- Donnez la body_container "" div une image de fond
- Donnez la body_container "" Div A background-color
- Ajouter une div avec clear: both à l'intérieur du body_container "" div
Code: [ Select ]div#body_container{
background-image:url('images/body_background_cheat.gif');
background-position:top left;
background-repeat:repeat-y;
}
- div#body_container{
- background-image:url('images/body_background_cheat.gif');
- background-position:top left;
- background-repeat:repeat-y;
- }
Appliquer le background-color:
nous allons rendre le background-color la même couleur que le "corps" div, ce sera alors faire croire que si le "corps" div est tout aussi élevé que le menu_left "" div même si elle n'est pas.
Code: [ Select ]div#body_container{
background-image:url('images/body_background_cheat.gif');
background-position:top left;
background-repeat:repeat-y;
background-color:#ffffff;
}
- div#body_container{
- background-image:url('images/body_background_cheat.gif');
- background-position:top left;
- background-repeat:repeat-y;
- background-color:#ffffff;
- }
Ajout du "clear: both part"
Et d'ajouter un div sous les deux divs flottaient avec la propriété "clear: both" faire de l'body_container "" div prolonger tout le chemin vers le bas des deux divs flottait, et voici le code pour que celui-ci:
Code: [ Select ]div.clear{
width:100%;
height:0px;
clear:both;
}
- div.clear{
- width:100%;
- height:0px;
- clear:both;
- }
maintenant s'il vous plaît la note, la hauteur ": 0px;" partie ne fonctionnera pas avec IE6, il sera 5px de hauteur, IE6 ne permet pas une div qui est de moins de 5px de hauteur pour le quelque raison obscure.
En tout cas, maintenant vous allez ajouter ce div sous les deux divs flottait et vous serez alors fin prêt.Le résultat final
Une fois que le youve a ajouté tout ce code votre devrait ressembler à ceci:
Code: [ Select ]<!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" xml:lang="en" lang="en">
<head>
<title>R_T Tutorial</title>
<style type='text/css'>
body{
background-color:#dddddd;
}
div#container{
width:980px;
margin:0px auto 0px auto;
}
div#header{
width:980px;
height:225px;
background-image:url('images/header.gif');
}
div#body_container{
width:980px;
}
div#menu_left{
width:230px;
background-color:#004163;
float:left;
}
div#body{
background-color:#ffffff;
width:750px;
float:left;
}
div#body_container{
background-image:url('images/body_background_cheat.gif');
background-position:top left;
background-repeat:repeat-y;
background-color:#ffffff;
}
div.clear{
width:100%;
height:0px;
clear:both;
}
</style>
</head>
<body>
<div id='container'>
<div id='header'></div>
<div id='body_container'>
<div id='menu_left'>
<a href='#'>menu link</a>
</div>
<div id='body'>
<h1>Body Heading</h1>
<p>Some body text here. this is just some sample text to take up a bit of space and show you what is going on.</p>
</div>
<div class='clear'></div>
</div>
</div>
</body>
</html>
- <!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" xml:lang="en" lang="en">
- <head>
- <title>R_T Tutorial</title>
- <style type='text/css'>
- body{
- background-color:#dddddd;
- }
- div#container{
- width:980px;
- margin:0px auto 0px auto;
- }
- div#header{
- width:980px;
- height:225px;
- background-image:url('images/header.gif');
- }
- div#body_container{
- width:980px;
- }
- div#menu_left{
- width:230px;
- background-color:#004163;
- float:left;
- }
- div#body{
- background-color:#ffffff;
- width:750px;
- float:left;
- }
- div#body_container{
- background-image:url('images/body_background_cheat.gif');
- background-position:top left;
- background-repeat:repeat-y;
- background-color:#ffffff;
- }
- div.clear{
- width:100%;
- height:0px;
- clear:both;
- }
- </style>
- </head>
- <body>
- <div id='container'>
- <div id='header'></div>
- <div id='body_container'>
- <div id='menu_left'>
- <a href='#'>menu link</a>
- </div>
- <div id='body'>
- <h1>Body Heading</h1>
- <p>Some body text here. this is just some sample text to take up a bit of space and show you what is going on.</p>
- </div>
- <div class='clear'></div>
- </div>
- </div>
- </body>
- </html>
et il devrait ressembler à ceci:Attachments:This would be the final result.
Conclusion
Faire ce travail est très simple, il suffit de se rappeler que nous avons ajouté un body_container autour des deux divs flottait, et a donné cette div "body_container" un background-image (1px x menus de largeur) et un fond de couleur (de la même couleur comme le "corps" divs arrière-plan)...Puis nous avons ajouté un div avec "clear: both" pour s'assurer que le body_container "" div s'étend tout le chemin jusqu'à la fin de la divs flotté.
Espérons que cela aide un peu
Let's leave all our *plum* where it is and go live in the jungle ...
- Anonymous
- Bot


- Inscription: 25 Feb 2008
- Messages: ?
- Loc: Ozzuland
- Status: Online
Septembre 11th, 2008, 6:38 am
- Bogey
- Bogey


- Inscription: Juil 14, 2005
- Messages: 8212
- Loc: USA
- Status: Offline
Digital media a plus de cette technique dans son tutoriel, mais merci pour le rappel 
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
Page 1 sur 1
Pour répondre à ce sujet, vous devez vous connecter ou vous enregistrer. Il est gratuit.
Afficher de l'information
- Total des messages de ce sujet: 2 messages
- Modérateur: Tutorial Writers
- Utilisateurs parcourant ce forum: Aucun utilisateur enregistré et 2 invités
- Vous ne pouvez pas poster de nouveaux sujets
- Vous ne pouvez pas répondre aux sujets
- Vous ne pouvez pas éditer vos messages
- Vous ne pouvez pas supprimer vos messages
- Vous ne pouvez pas joindre des fichiers
