I know that CSS only supports left and right values for the float property, but is there a technique to implement a floating top? I will try to explain. I have the following code:
<div style="float:left">
<div style="float:left">
<div style="float:left">
....
With this code every div is floated to left until the right limit of the page is reached. I want to do the same thing but vertically, so that every div is placed at the bottom of the previous one and then, when the bottom limit of the page is reached, a new column is created. Is there a way to do this using only CSS (and maybe editing the HTML code)?
The float CSS property places an element on the left or right side of its container, allowing text and inline elements to wrap around it. The element is removed from the normal flow of the page, though still remaining a part of the flow (in contrast to absolute positioning).
How do you float other HTML elements in CSS? As mentioned earlier, any HTML element can be floated in CSS, not just images. Let's say you want a button to float to the left of the text in a container. You can use a class selector to target the button class and define it with the rule float: left or float: right.
Answer: Use the CSS z-index Property You can use the CSS position property in combination with the z-index property to overlay an individual div over another div element.
Simply use vertical-align
:
.className {
display: inline-block;
vertical-align: top;
}
The only way to do this with CSS only is by using CSS 3 which is not going to work on every browser (only the latest generation like FF 3.5, Opera, Safari, Chrome).
Indeed with CSS 3 there's this awesome property : column-count that you can use like so :
#myContent{
column-count: 2;
column-gap: 20px;
height: 350px;
}
If #myContent is wrapping your other divs.
More info here : http://www.quirksmode.org/css/multicolumn.html
As you can read there, there are serious limitations in using this. In the example above, it will only add up to one another column if the content overflows. in mozilla you can use the column-width property that allows you to divide the content in as many columns as needed.
Otherwise you'll have to distribute the content between different divs in Javascript or in the backend.
Hugogi Raudel has came up with an interesting way to to achieve this by CSS
. suppose here is our HTML
markup:
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
</ul>
You can achieve a 3-row column using this CSS
code:
li:nth-child(3n+2){
margin: 120px 0 0 -110px;
}
li:nth-child(3n+3) {
margin: 230px 0 0 -110px;
}
And here is the end result:
What we are doing here is adding a appropriate margin for each item in the row. This approach limitation is that you have to determine the column row count. it's not going to be dynamic. I'm sure it has use cases so I included this solution here.
There is no float:top
, only float:left
and float:right
If you wish to display div underneath each other you would have to do:
<div style="float:left;clear:both"></div>
<div style="float:left;clear:both"></div>
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