How to setup a website layout

UndeadDragon

Super Moderator
Reaction score
447
How to setup a website layout

I will be splitting this tutorial up in to different sections about how to add different elements to a webpage. These sections are: Centered web page, Floats, Two Column Layout and Three Column Layout.

Centered web page
A lot of websites are based on a centered layout. This means that they have equal space on either side of a container which holds all other parts of the website.

This can be achieved very easily. The method is to set the left and right margins to auto. This will automatically center the webpage. You must also set the div’s width for this to work. Example code:

Code:
<html>
<head>
<title>CSS Test</title>
<style type="text/css">
body
{
margin: 0;
padding: 0;
}
#container
{
margin: 0 auto; /*Shorthand way of writing margin: 0 auto 0 auto;*/
width: 900px;
}
</style>
</head>
<body>
<div id="container">
   <p>This is some text in a centered div</p>
</div>
</body>
</html>

Preview here.

Floats
Floats can be used for various reasons. You can use them to nest a picture in some text, you can align your divs with them and you can create columned layouts.

Page elements can float to the left or right. The CSS for a float is simply: float:left or float:right.

IMPORTANT: When using float for a page layout and the containing div does not have a fixed height, it will not stretch down as you fill the floated divs with content. After using float in this case you should always add the line: <div style="clear:both"></div> This will not add any content to the page, but the containing div will recognise the content inside the floated div.

In this first example I will show you how to float a div to the right of the container (centered container). Example:

Code:
<html>
<head>
<title>CSS Test</title>
<style type="text/css">
body
{
margin: 0;
padding: 0;
}
#container
{
margin: 0 auto; /*Shorthand way of writing margin: 0 auto 0 auto;*/
width: 900px;
}
.container p
{
float: right;
}
</style>
</head>
<body>
<div class="container">
   <p>This text is floated to the right</p>
</div>
</body>
</html>

Preview here.

Simply by setting all p elements in #container it will make the text float to the right of the div. You can also add a margin-right to make it float away from the edge of the div.

You can achieve an image gallery effect by floating each picture container to the left, whilst setting the width to something that will allow multiple images on each line. For example, if you have a width of 900px you could have 3 images on each row if you set each ones width to 300px (excluding margins). When the webpage cannot fit any more onto the row it should start a new row for the next ones. Example:

Code:
<html>
<head>
<title>CSS Test</title>
<style type="text/css">
body
{
margin: 0;
padding: 0;
}
#container
{
margin: 0 auto; /*Shorthand way of writing margin: 0 auto 0 auto;*/
width: 900px;
}
.gallery_container
{
float: left;
width: 250px;
margin: 10px; /*Spacing out the containers*/
padding:  10px; /*Adding spacing inside the container*/
border:  1px solid #666;
background: #ddd;
}
.gallery_container img
{
width:  230px;
height: 230px;
}
.gallery_container span
{
color: #000;
}
</style>
</head>
<body>
<div id="container">
   <div class="gallery_container"><img src="image1.jpg" /><span>An Image 1</span></div>
   <div class="gallery_container"><img src="image2.jpg" /><span>An Image 2</span></div>
   <div class="gallery_container"><img src="image3.jpg" /><span>An Image 3</span></div>
   <div class="gallery_container"><img src="image4.jpg" /><span>An Image 4</span></div>
   <div style="clear:both"></div>
</div>
</body>
</html>

Preview here.

Two Columned Web Page
Web pages with two columns are very popular. They usually consist of a main, larger, content area and a smaller sidebar, often used for navigation. You can make one by floating one div to the left and another to the right. Example:

Code:
<html>
<head>
<title>CSS Test</title>
<style type="text/css">
body
{
margin: 0;
padding: 0;
}
#container
{
margin: 0 auto;
width: 900px;
}
#header
{
width: 900px;
height: 200px;
background: #ddd;
}
#left
{
float: left;
width: 580px;
padding: 10px;
}
#right
{
float: right;
width: 280px;
padding: 10px;
}
#footer
{
width: 880px;
padding: 10px;
background: #eee;
}
</style>
</head>
<body>
<div id="container">
               <div id="header">
	 </div>
               <div id="left">
	    <span>This is in the left column</span>
	 </div>
	 <div id="right">
	    <span>This is in the right column</span>
	 </div>
	 <div style="clear: both"></div>
	 <div id="footer">
	    <span>This text is in the footer</span>
	 </div>
</div>
</body>
</html>

Preview here.

I also added a header and footer in there to show that you can combine floated divs with normal divs.

Note: You can add the attribute: clear:both; to the footer, rather than having it as a seperate div if you want. That will only work if the footer is in the container. I just use <div style="clear:both"></div> because I have got used to using it.

Three Columned Web Page
Another common webpage layout is the three columned layout. This follows the same method as the two column layout, but the content div (center) is not floated.
Code:
<html>
<head>
<title>CSS Test</title>
<style type="text/css">
body
{
margin: 0;
padding: 0;
}
#container
{
margin: 0 auto; /*Shorthand way of writing margin: 0 auto 0 auto;*/
width: 900px;
}
#header
{
width: 900px;
height: 200px;
background: #ddd;
}
#left
{
float: left;
width: 180px;
padding: 10px;
margin: 0;
}
#right
{
float: right;
width: 180px;
padding: 10px;
margin: 0;
}
#content
{
width: 480px;
padding: 10px;
margin: 0;
}
#footer
{
width: 880px;
padding: 10px;
background: #eee;
}
</style>
</head>
<body>
<div id="container">
   <div id="header">
	 </div>
   <div id="left">
	 <span>This is in the left column</span>
	 </div>
	 <div id="right">
	 <span>This is in the right column</span>
	 </div>
	 <div id="center">
	 <span>This is in the center column</span>
	 </div>
	 <div style="clear: both"></div>
	 <div id="footer">
	 <span>This text is in the footer</span>
	 </div>
</div>
</body>
</html>

Preview here.

Note: In the body section, the content div should come last, otherwise it will make the side columns float strangely (one will be lower and one will be higher), but this is easily solved if you just float everything before putting in the unfloated divs (apart from if the unfloated divs are supposed to be above the floated divs :p)

Thanks for reading, any requests for more stuff?
 

UndeadDragon

Super Moderator
Reaction score
447
Yes... This is quite similar :p

I guess I will add some more things so it is more unique.
 

UndeadDragon

Super Moderator
Reaction score
447
Previews added.
 

DDRtists

ɹoʇɐɹǝpoɯ ɹǝdns
Reaction score
415
Shouldn't the CSS be class type (.class) and not ID type(#class)? If you have multiple ID's of the same name and you try to use JavaScript, you will have problems...
 

UndeadDragon

Super Moderator
Reaction score
447
Shouldn't the CSS be class type (.class) and not ID type(#class)? If you have multiple ID's of the same name and you try to use JavaScript, you will have problems...

Most of the layout divs are fine with an ID type, because they will only be used once. The exceptions are the gallery boxes and the first text class.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top