【问题标题】:How to put divs on opposite sides of parent div, with parent width relative to them on scaling?如何将 div 放在父 div 的相对两侧,并在缩放时相对于它们的父宽度?
【发布时间】:2017-11-06 06:42:26
【问题描述】:

我有一个容器 div (不能浮动) 有两个子元素。我希望子元素位于相反的两侧 - 第一个在左边,第二个在右边。在 100% 浏览器宽度上,儿童摘要宽度小于容器,但在更大的比例上则不然,因此容器也应该更大。幸灾乐祸的孩子长大后如何设置容器成长?

UPD: 类似this 我需要所有元素在任何比例下都保持在一条线上。

 <div id="page">
     <div id="container">
         <div id="left">
           <div>first</div>
           <div>second</div>
           <div>third</div>
         </div>
         <div id="right">
            right
         </div>
      </div>
    </div>
    <style>
    #page {
      background-color: grey;
      width: 100%;
      height: 300px;
    }
    #container {
      /*this styles are needed to other parts*/
      position: relative; 
      clear: both;
      /*=====================================*/
      background-color:red;
      height: 50px;
      width: 90%;
      margin: 0 5%;
    }
    #left {
        float: left;
        background-color: blue;
    }
    #left div {
      width: 50px;
      display: inline-block;
    }
    #right{
     float: right; 
     background-color: green;
     display: block;
     max-width: 200px;
    }
    </style>

【问题讨论】:

  • 什么是绿色 div 宽度??
  • 请发布您尝试过的 HTML 和 CSS
  • @j08691 页面代码太复杂了。
  • @TEXHIK 所以简化它然后发布它。只需去掉我们不需要查看的所有内容,并为我们提供您迄今为止创建它的基本方式。

标签: html css


【解决方案1】:
.container {
  display: flex;
  justify-content: space-between;
}

它应该这样做。 谷歌上 FlexBox Introduction 以获得很好的解释。

【讨论】:

  • 使用右 div,但左 div 的子元素现在转到下一行。
【解决方案2】:

类似的东西?

我使用display:flex 让两个 div 很好地对齐,浮动只需要内框

https://jsfiddle.net/070rk2e1/1/

【讨论】:

  • 宽度百分比没有问题,但宽度不能固定。左边部分包含不同条件下或多或少的元素,并且该元素在px中具有绝对值。
  • 我已经更新了我的 jsfiddle,在左侧部分添加了更多 div,以向您展示它是如何工作的。
  • 左容器中的 Divs 下降,但我需要它们来增加父级。他们必须在所有尺度上保持一致
  • 你可以给左边的容器一个min-width。如果你想避免这种情况,我建议使用媒体查询来控制不同屏幕尺寸的盒子/容器的宽度
猜你喜欢
  • 2014-02-21
  • 2013-02-17
  • 1970-01-01
  • 1970-01-01
  • 2018-05-19
  • 2011-08-17
  • 1970-01-01
  • 1970-01-01
  • 2022-12-15
相关资源
最近更新 更多