In this tutorial I will show you how you can force two or more div's to be on the same line without any form invalid XHTML/CSS appied. This tutorial is a must if you have features like top bars on your website!
Step 1: Creating the two Div Classes
When ever you place two div's next to each other you automatically get a line break that forces the following div's a line below, this can get annoying say if you want a row of divs on the same line!
Example:
<div id="something">Content</div> <div id="random">More Content</div>
If you were to place this within a website you will notice that a automatic 1 line gap is present after the first div. But did you know you can stop from happening? Yes you can, but we'll need to create 2 Div classes first.
If you are using a global stylesheet for your site and want to apply this to it, I'd suggest you go ahead and modifiy your main style sheet. If your doing this to test it then I'd suggest simply adding it to the head tags of your XHTML document (No need to create a whole CSS document for such a small hack)
Our first Div Class:
.alignleft { float: left; }
This will display content to the left of our screens
Our Second Div class:
.alignright { float: right; }
This will display content to the right of our screens.
How does this work? Simply by using the float attribute the forced line break is stopped.
Step 2: Applying it in XHTML:
For this hack to work you must always have one of the two classess included in your code. Without these classes your hack will fail. Here's how you would go about including it in your div's:
<div id="something" class="alignleft">Content</div> <div id="random" class="alignleft">More Content</div> <div id="something2" class="alignright">Content</div> <div id="random2" class="alignright">More Content</div>
The first two div's will be on the left and two other divs will be on the right but all on the same line! This sneaky little hack is great for using on Top-bars like the one at the top of your screen now! I gave you basic code for hack, you can also added in such attributes such as margins and padding to make your div's look presentable and not all squashed up.
Taking note:
You do not have to use this within in a div. It can be also used in the anchor tag etc, as long as you add in the class="alignleft"> part into your non div tag. However it is recommended that if you are using it within a div, you define a width.











Avangelist
August 29th
Of course this will not work if you have not set sizes for the div's?
Do you not also need to set position?
James
August 29th
This code shown is the only code you need for this to work. Nothing else is needed.
Eduardo (Brazil)
August 30th
The recommended approach is to set the width of the DIV when you float it.
James
August 30th
@ Eduardo (Brazil) Says:
Yes that woulld be a error on my part. You should specify width's for divs, the only reason why I didn't say anything about width's as this can also be used in non div tags. But thanks for your comment. I've gone and added in a note about widths at the end of the tutorial, thanks!
Mediakursy
August 30th
Many thanks for the detailed explanatory.
Bailey
September 27th
Works great in firefox, although Im having quite a few problems in IE 7
Solomon Larsen
January 10th
hi
76c3i7ce7cpwnq54
good luck
Fire G
January 10th
@Bailey: Yes, they code that James provided will work, but only if certain pre-requisites are met:
*.) a Width or Padding is declared
*.) BOTH 'alignLeft' and 'alignRight' must be declared
*.) there is a set area in which the items are displayed in
Without these, many "errors" will occur with the output.
So technically James is correct in his code. He just didn't provide or detail all the additional code needed to achieve consistent results.