简体   繁体   English

在中心div的左侧和右侧对齐div

[英]Align divs left and right of center div

I have a total of 7 divs, where 1 one is designated for the middle. 我总共有7个div,其中1个被指定为中间。

It's supposed to look something like this: 应该看起来像这样: 表示

I get the top two pictures and the knife correctly aligned, but the other does not appear how I want them. 我得到了最上面的两张图片,并且刀正确对齐,但是另一张却没有出现我想要的样子。

Here's the CSS: 这是CSS:

.vegetablewrapper {
    width: 100%;
    overflow: hidden;
    background-color: white;
}
.vegetableleft {
    float: left;
    width: 350px;
    padding: 5px;
    margin-left: 5px
}
.vegetableright {
    float: right;
    width: 345px;
    padding: 5px;
    margin-right: 8px;
}
.vegetablemiddle {
    float: left;
    width: 200px;
    padding: 5px;
}

And HTML: 和HTML:

    <div class="vegetableleft">
        <img src="bilder/leek.jpg" alt="leek"/>
    </div>

    <div class="vegetablemiddle">
        <img src="bilder/knife.jpg" alt="knife"/>
    </div>  

    <div class="vegetableright">
        <img src="bilder/leekcut.jpg" alt="leek cut"/>
    </div>

    <div class="vegetableleft">
        <img src="bilder/onion.jpg" alt="onion"/>
    </div>

    <div class="vegetableright">
        <img src="bilder/onioncut.jpg" alt="onion cut"/>
    </div>

    <div class="vegetableleft">
        <img src="bilder/carrot.jpg" alt="carrot"/>
    </div>

    <div class="vegetableright">
        <img src="bilder/carrotcut.jpg" alt="carrot cut"/>
    </div>

</div>

With the given code my site looks like this: 使用给定的代码,我的网站如下所示: 在此处输入图片说明

How could this be done correctly? 如何正确地做到这一点?

You have wrong structure. 您的结构错误。

From you code you have 7 divs: 从您的代码中,您有7个div:

  • left side - 3 左侧-3
  • middle - 1 中-1
  • right - 3 右-3

But you need to have only 3: 但是您只需要3:

  • left - 1 (float left) 左-1(向左浮动)
  • middle - 1 (float left) 中-1(向左浮动)
  • right - 1 (float left or right) 右-1(向左或向右浮动)

This 3 parts can contain any amount of images without "float" that will lead to something like this for your example: 这3个部分可以包含任意数量的不带“浮动”的图像,对于您的示例,这将导致如下所示:

<div class="vegetableleft">
    <img src="bilder/leek.jpg" alt="leek"/>
    <img src="bilder/onion.jpg" alt="onion"/>
    <img src="bilder/carrot.jpg" alt="carrot"/>
</div>  

<div class="vegetablemiddle">
    <img src="bilder/knife.jpg" alt="knife"/>
</div>  

<div class="vegetableright">
    <img src="bilder/leekcut.jpg" alt="leek cut"/>
    <img src="bilder/onioncut.jpg" alt="onion cut"/>
    <img src="bilder/carrotcut.jpg" alt="carrot cut"/>
</div>

What's your problem ? 你怎么了 ? Float left means that the div will try to stack on the left of the previous one, if it fits horizontally. 向左浮动表示div将在水平放置的情况下尝试堆叠在前一个的左侧。 Keep in mind that the elements will stack in the same order as they appear in the HTML. 请记住,元素的堆叠顺序与HTML中出现的顺序相同。

With your code: 使用您的代码:

  • leek.jpg is placed at the upper left corner leek.jpg放置在左上角
  • knife.jpg stacks at the left of the leek knife.jpg堆栈在韭菜的左侧
  • leekcut.jpg stacks at the left of the knife. leekcut.jpg堆叠在刀的左侧。
  • onion.jpg stacks at the left of the knife (impossible, so it tries the next available position which is right under leekcut.) From there start to see the problem. onion.jpg堆叠在刀的左侧(不可能,因此它将尝试在韭葱切割下的下一个可用位置。)从那里开始看问题。
  • And so on 等等

Possible solution: 可能的解决方案:

Html HTML

<div class="conainer">
  <div class="left">
      <div class="photo1"></div>
      <div class="photo1"></div>
      <div class="photo1"></div>
  </div>

  <div class="middle">
      <div class="photo2"></div>
  </div>

  <div class="right">
      <div class="photo1"></div>
      <div class="photo1"></div>
      <div class="photo1"></div>
  </div>
</div>​

CSS 的CSS

.left, .right {
    width: 200px;
    float: left;
}
.middle {
    width: 100px;
    float: left;
    padding: 0px 5px 5px 5px;
}

.photo1 {
    width: 200px;
    height: 100px;
    background-color: red;
    margin: 5px;
}

.photo2 {
    width: 100px;
    height: 300px;
    background-color: yellow;
    margin: 5px;
}
​

Fiddle here: http://jsfiddle.net/BfEu5/1/ 在这里提琴: http : //jsfiddle.net/BfEu5/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM