There are many different website layouts that are designed differently, many website layouts choose to have one or more sidebar, but a problem that you many encounter is how can you get the sidebar to be same length as your content? This is a good question, because if your sidebar ends before your content then your website layout will look strange. But there are methods of being able to achieve a sidebar that is the same height as your content. In this Quick CSS segment, im going to show you how you can achieve this effect, it’s pretty simple and basic, but it’s good to know!
First off, pretend your a freelance website designer who has just got a job from a client who wants a two column website layout with the column on the right. This is a two column website layout:
The website layout is two columns because the content counts as one column and of course the sidebar on the right makes two. So we now know what the imaginary client wants, we know need to find a method of doing this with XHTML and CSS. We could use the height attribute in CSS, but this wouldn’t work because in most cases website content lengths always change depending on the content length. For example, the height of the frontpage of James’ Blog will vary depending on how long the summary of each post is. So we can’t predict the height, so using the height attribute is no good.
Infact I’ll not string you along, there is no one special attribute to achieve what we want. The way that im going to show you if by creating a few divs and organisng them in such a way with some CSS code to help get the layout we want.
The XHTML (The Div’s)
We will first need to form the layout, so lets start off by creating our divs. Here’s is how im going to form the layout:1 2 3 4 5 6 | < div id = "container" > < div class = "sidebar" >
|
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | < div id = "container" > < div class = "sidebar" > < h5 >Sidebar content 1
|
The CSS:
1 2 3 4 5 6 7 | body { background-color : #333333 ; } #container { margin : 0px auto ; width : 900px ; background-color : #DEDEDe ; } .content { width : 70% ; background : #FFFFFF ; padding : 20px ; } .sidebar { margin-top : 20px ; width : 210px ; float : right ; } |
But what does it mean? Remember when I said that the container div was important, here’s why. Notice, that the container has a background color but the actual sidebar div doesn’t. If you did set a background color on the sidebar div then it would not be extend to the same length as the content. This is why the container div is important, if you notice the content div has a background color set so it is different to the sidebar. You can still modify various elements of the sidebar, such as padding and margin etc. Just the background color will need to be changed on the container div.
So there we have a simple way to create a two column webiste layout, where the sidebar is the same height as the content.
No comments:
Post a Comment