Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to float div in CSS?

Tags:

html

css

I have this list in HTML and I would like order it by columns. I tried using floats it gives me this:

EDIT : Height of these <li> is undefined, but I would like C below B, E below E and G below F without change the structure and change the order. I don't want to use position absolute. I'm wondering if there are other solutions.

HTML :

<ul>
   <li class="item">A</li>
   <li class="item">B</li>
   <li class="item">C</li>
   <li class="item">D</li>
   <li class="item">E</li>
   <li class="item">F</li>
   <li class="item">G</li>
<ul>

OUTPUT (using float: left; width: 240px; border-left: 1px solid #EEE) :

enter image description here

What I want is more like this below, without changing the HTML because I already use this structure to make responsive.

enter image description here

Is it possible ?

like image 264
Steffi Avatar asked Jul 03 '14 11:07

Steffi


People also ask

How to float to the right end of Div in CSS?

And then, we can use this class name inside DIV class attribute, to float to the right end. CSS contains clear property, to cancel the applied float property to prevent its effect on the DIV containers to be displayed to the browser with new line. This property is mainly used when we are required to display content in grid view using DIV tags.

How do I use float in HTML?

Inherits this property from its parent element. Read about inherit Let the first letter of a paragraph float to the left and style the letter: Use float to create a homepage with a header, footer, left content and main content: Do not allow floating elements on the left or the right side of a specified <p> element:

What is the default value of float in CSS?

The float property can have one of the following values: left - The element floats to the left of its container. right - The element floats to the right of its container. none - The element does not float (will be displayed just where it occurs in the text). This is default.

How do you solve the float problem in CSS?

A way to solve the problem is forcing some in-flow element to be placed below all floats. Then, the height of the parent will grow to wrap that element (and thus the floats too). This property indicates which sides of an element's box (es) may not be adjacent to an earlier floating box.


1 Answers

You could use column CSS and a margin-bottom on the first LI tag, like this.

ul {
     column-count:4;
     padding:0;
     column-rule: solid lightgray 1px;
}

li {
     display:inline-block;
     width:100%;
}

li:before {/* demo purpose to set an height to lis */
     content:'';
     float:left;
     padding-top:50%;
}

li:first-of-type {
     margin-bottom:50%;
}

You may need a JavaScript prefixer or add vendor-prefix manually.

like image 60
G-Cyrillus Avatar answered Oct 05 '22 23:10

G-Cyrillus