Float left and right

2020-02-24 07:32发布

this problem has been bothering me for some time. So I have created some visual descriptions of my problem

First here is my HTML structure I have 6 divs.. the first 3 float left and the last 3 float right. The last image shows the result I want but can't seem to get. Can someone out there help me here

EDIT// Sorry heres my HTML and CSS

<style>
    .left { float:left; }
    .right { float:right; }
</style>
<div id="container">
   <div class="left"></div>
   <div class="left"></div>
   <div class="left"></div>
   <div class="right"></div>
   <div class="right"></div>
   <div class="right"></div>
</div>

NOTE: I Cant do a left right left right left right option because Im getting my data from PHP via a Query to my database.. first query goes left second query goes right.... thanks a bunch

/EDIT

This is a mocukup of my HTML structure

My floats result in this

this is my current result

This is what I want

I want this

标签: css css-float
8条回答
看我几分像从前
2楼-- · 2020-02-24 07:47

Suppose you have another div in the middle of them. Then use this chronological order:

<div class="left"></div>
<div class="right"></div>
<div class="content"></div>

Or if you don't, just add another div that provide a style clear:both to it.

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-02-24 07:47

Try this:

.leftcolums {
  float: left;
}

.rightcolums {
  float: right;
}

.clear {
  clear: both;
}
<div class="leftcolums">
  <div class="left">1</div>
  <div class="left">2</div>
  <div class="left">3</div>
</div>
<div class="rightcolums">
  <div class="right">4</div>
  <div class="right">5</div>
  <div class="right">6</div>
</div>
<div class="clear">7</div>

查看更多
甜甜的少女心
4楼-- · 2020-02-24 07:50

You can use CSS3 column-count property for this. Write like this:

.parent{
    -moz-column-count: 2;
    -moz-column-gap: 50%;
    -webkit-column-count: 2;
    -webkit-column-gap: 50%;
    column-count: 2;
    column-gap: 50%;
}
.parent div{
    width:50px;
    height:50px;  
    margin:10px;
}
.left{
    background:red;
}
.right{
    background:green;
}

Check this http://jsfiddle.net/UaFFP/6/

查看更多
孤傲高冷的网名
5楼-- · 2020-02-24 07:54

Using the :nth-child selector and clearing after 2 divs:

​div {
    width: 50px;
    height: 50px;
    background-color: red;
    float: left;
}
div:nth-child(2n) {
    background-color: green;
    float: right;
}

Live example

Otherwise use this fairly hacky method, which requires no additional markup:

div {
    width: 50px;
    height: 50px;
    background-color: red;
    float: left;
}
div:nth-child(n) {
    clear: both;
}

div:nth-child(2n) {
    background-color: green;
    float: right;
    margin-top: -50px; //match this to the div height
}

Live example

查看更多
Melony?
6楼-- · 2020-02-24 07:55

column-count is already widely supported - http://caniuse.com/#feat=multicolumn So if old browsers doesn't bother you consider using it.

查看更多
兄弟一词,经得起流年.
7楼-- · 2020-02-24 08:02

Add the first left div, then the first right div and after them add <br style="clear:both"> and repeat the procedure.

Edit: Here's an updated answer:

<div style="border:1px solid blue;float:right;height:100px;width:100px;clear:right;"></div>
<div style="border:1px solid red;float:left;height:100px;width:100px;clear:left;"></div>
<div style="border:1px solid blue;float:right;height:100px;width:100px;clear:right;"></div>
<div style="border:1px solid red;float:left;height:100px;width:100px;clear:left;"></div>
<div style="border:1px solid blue;float:right;height:100px;width:100px;clear:right;"></div>
<div style="border:1px solid red;float:left;height:100px;width:100px;clear:left;"></div>
查看更多
登录 后发表回答