html - 左右浮动两个div

标签 html css

我的代码结构是这样的

<div style="height: 100px;
            Width: 200px;"> <!-- Container -->
  <div style="float: left;
              height: 50px;
              Width: 100px;
              background-color: red;">
  </div>
  <div style="float: left;
              height: 50px;
              Width: 100px;
              background-color: blue;">
  </div>
  <div style="float: right;
              height: 50px;
              Width: 100px;
              background-color: green;">
  </div>
</div>

但是元素的正确位置应该是这样的:

┌──────┬──────┐
│ red  │green │
├──────┼──────┘
│ blue │
└──────┘

我不能更改或添加任何额外的代码,唯一的方法是使用 CSS。 我应该如何按照我上面提到的正确顺序 float div?

编辑:我的代码没有也不能包含带有 clear 的 div。

最佳答案

你不需要 float 。使用 !important 禁用所有 float 以覆盖内联样式,然后使用 :nth-of-type() 选择绿色 div 并使用 right 和 top 绝对定位等于 0;

div {
  position: relative;
}
div > div{
  float: none !important;
}
div > div:nth-of-type(3) {
  position: absolute;
  right: 0;
  top:0;
}
<div style="height: 100px; Width: 200px;">
  <!-- Container -->
  <div style="float:left; height: 50px; Width:100px; background-color:red;">
  </div>
  <div style="float:left; height: 50px; Width:100px; background-color:blue;">
  </div>
  <div style="float:right; height: 50px; Width:100px; background-color:green;">
  </div>
</div>

关于html - 左右浮动两个div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29830205/

相关文章:

javascript - 如果显示自定义字段,则显示/隐藏按钮

html 元素与 float 对齐 :left and float:right

html - <pre> 标签和 Paper 之间的空间

html - 相对于元素宽度或高度的单位

twitter-bootstrap - 如何使阴影位于响应式边框上

html - 'display: block; float: left' 与 'display: inline-block; float: left' 之间的区别?

PHP 回显 block 行为

html - Angular 动画 : div enter behind another div

html - Flexbox 中的 Flexbox 不适用于省略号文本溢出

javascript - 如何阻止固定的 100% 宽度元素与浏览器的滚动条重叠?