简体   繁体   English

防止div缠绕在相邻的div周围

[英]Prevent the div from wrapping around adjacent div

I have an outer div and 2 inner divs - one left-alignd and another is right next to it. 我有一个外部div和2个内部div-一个左对齐,另一个在其旁边。 The issue I am having is that the left div is shorter then the right and then right wraps around the left. 我遇到的问题是,左div短于右,然后右环绕左。

Below is my html and CSS: 以下是我的html和CSS:

<div id='green'>
  <div id="orange">test</div>
  <div id="red">
     Effects<br/>

        Add Class<br/>
     Color Animation<br/>
   Easing<br/>
  Effect<br/>
   Hide<br/>
        Remove Class  
       Show
     Switch Class
    Toggle
   Toggle Class
    </div>
</div>

and here is CSS: 这是CSS:

#green {
  padding-top: 0.75em;
  padding-bottom: 0.25em;
  padding-right: 1em;
  overflow: hidden;
  border:20px solid green;
}

#orange {
  width:185px;
  border:10px solid orange;
  float:left;
}

#red {
  border:5px solid red;
  width: 100%;
  display: block;
}

My question is how can I prevent the right div from wrapping around the left? 我的问题是如何防止右侧div缠绕在左侧? Preferable without setting a margin on the right div. 最好在右侧div上不设置边距。

I also want the red div to always be on the right of the orange div, never going under it or wrapping around it, even if the page is resized or if the page is viewed on a mobile browser 我还希望红色div始终位于橙色div的右侧,即使在调整页面大小或在移动浏览器中查看过该页面的情况下,也不要一直在其下方或环绕它

You can use flexbox for this. 您可以为此使用flexbox。 Using the following changes to your CSS above: 对上面的CSS使用以下更改:

#green{
  display: flex;
  align-content: top;
  padding-top: 0.75em;
  padding-bottom: 0.25em;
  padding-right: 1em;
  overflow: hidden;
  border:20px solid green;
}


#orange{
  align-self:flex-start;
  width: 185px;
  border:10px solid orange;
}

#red{
  width: 100%;
  border:5px solid red;
}

If you want #orange to be the same height as #red, remove align-self: flex-start 如果您希望#orange与#red高度相同,请删除align-self: flex-start

Demo: http://codepen.io/anon/pen/YwOjyP 演示: http//codepen.io/anon/pen/YwOjyP

I got it working by adding display: inline-flex; 我通过添加display: inline-flex;使其工作display: inline-flex; to #green . #green

Look: https://jsfiddle.net/4k1ohc10/ 外观: https//jsfiddle.net/4k1ohc10/

By the way, you didn't ask for a specific browser, so you can check this page: http://caniuse.com/#feat=flexbox 顺便说一句,您并不需要特定的浏览器,因此可以查看以下页面: http : //caniuse.com/#feat=flexbox

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

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