I can't figure out how to achieve the following layout with CSS (probably because I don't actually know CSS).
I have a bunch of divs like this:
<div class="right"> <p>1</p> </div>
<div class="left"> <p>2</p> </div>
<div class="left"> <p>3</p> </div>
<div class="left"> <p>4</p> </div>
<div class="right"> <p>5</p> </div>
<div class="right"> <p>6</p> </div>
(not the real contents)
Now I want the layout to look like two equal columns of divs, with the "right" ones on the right, and the "left" ones on the left, thus:
2 1
3 5
4 6
[Edit: In a previous version of this question I had textareas inside the divs, and the divs all had different names like "one" and "xyz".] I tried something like
div.right { width:50%; float:right; clear:right; }
div.left { width:50%; float:left; clear:left;}
but it doesn't quite work: It produces something like:
2 1
3
4 5
6
(without the "clear"s, it blithely produces
2 1
3 4
6 5
which is not what is wanted).
It is apparent that it can be made to work if the divs are ordered differently, but I'd like not to do that (because these divs are generated dynamically if the browser has Javascript, and I don't want to change the actual order that is displayed in the absence of Javascript, for semantic reasons). Is it still possible to achieve the layout I want?
[For what it's worth, I'm willing to have it not work on IE or older versions of other browsers, so if there is a solution that works only on standards-compliant browsers, that's okay :-)]
The following works for me in Firefox 3:
div {
width: 198px;
border: 1px solid black;
}
div.onediv, div.tendiv, div.xyzdiv { float: right; }
div.twodiv, div.abcdiv, div.pqrdiv { float: left; }
<div style="width: 400px;">
<div class="onediv"><p>One name</p> <textarea id="one"></textarea></div>
<div class="twodiv"><p>Two name</p> <textarea id="two"></textarea></div>
<div class="tendiv"><p>Ten name</p> <textarea id="ten"></textarea></div>
<div class="abcdiv"><p>Abc name</p> <textarea id="abc"></textarea></div>
<div class="xyzdiv"><p>Xyz name</p> <textarea id="xyz"></textarea></div>
<div class="pqrdiv"><p>Pqr name</p> <textarea id="pqr"></textarea></div>
</div>
Just a quick note: putting div inside your class name (i.e. abcdiv) is a little redundant and weird. Without going into a huge diatribe about proper class usage, I would leave it out.
You can actually simplify your code if you just say "col1" and "col2". That way you can even swap the columns later if you want:
div {
width: 198px;
border: 1px solid black;
}
div.col1 { float: right; }
div.col2 { float: left; }
<div style="width: 400px;">
<div class="col1"><p>One name</p> <textarea id="one"></textarea></div>
<div class="col2"><p>Two name</p> <textarea id="two"></textarea></div>
<div class="col1"><p>Ten name</p> <textarea id="ten"></textarea></div>
<div class="col2"><p>Abc name</p> <textarea id="abc"></textarea></div>
<div class="col1"><p>Xyz name</p> <textarea id="xyz"></textarea></div>
<div class="col2"><p>Pqr name</p> <textarea id="pqr"></textarea></div>
</div>
You try to separate your input stream into two independent streams, and I don't think CSS allows you to do it. Using left and right floats is a clever idea, but it will not always work. CSS spec says in 9.5.1 rule 5:
The outer top of a floating box may not be higher than the outer top of any block or floated box generated by an element earlier in the source document.
So, your floats can jump left, right and down, but not up.
You can achive everything with absolute positioning, but there is nothing automatic about it.
EDIT: I said in comments that I don't expect CSS3 to support a feature like that because it violates separation of content and layout. Later, I remembered that CSS3 plans to have footnotes. So, if you mark some of your divs with "float: note", the selected divs will flow into a separate, "footnote" area of the page. I guess you can place them on the right of the rest of the layout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With