Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a float top with CSS?

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)?

like image 661
mck89 Avatar asked Dec 04 '09 14:12

mck89


People also ask

Is there a float top CSS?

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 make a float in CSS?

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.

How do I make a div float on top of another CSS?

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.


4 Answers

Simply use vertical-align:

.className {
    display: inline-block;
    vertical-align: top;
}
like image 196
Abhishek Goel Avatar answered Oct 21 '22 15:10

Abhishek Goel


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.

like image 33
Guillaume Flandre Avatar answered Oct 21 '22 15:10

Guillaume Flandre


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:
enter image description here
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.

  • Here is a live demo: http://codepen.io/HugoGiraudel/pen/DoAIB
  • You can read more on this in Hugo's blog post
like image 16
Arash Milani Avatar answered Oct 21 '22 15:10

Arash Milani


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>
like image 8
James Goodwin Avatar answered Oct 21 '22 16:10

James Goodwin