简体   繁体   English

HTML CSS 浮动和内联块问题

[英]HTML CSS floats and inline-block issues

There are three blocks, first and last one are floated left and the middle one is displayed inline-block and cleared both.共有三个块,第一个和最后一个向左浮动,中间的一个显示为 inline-block 并清除两者。 Why my middle block is coming at the end?为什么我的中间块最后来了? Here is my code.这是我的代码。

 .box { width: 200px; height: 200px; background: red; } .block { height: 200px; width: 200px; background: blue; display: inline-block; clear: both; } .box1 { float: left; } .box2 { float: left; background: green; }
 <div class="box box1">1st Block</div> <div class="block">Middle Block</div> <div class="box box2">Third block</div>

https://codepen.io/suraj_122/pen/EdZpag https://codepen.io/suraj_122/pen/EdZpag

All float elements placed from left first one after the other and then other unfloated elements are placed所有浮动元素从左开始依次放置,然后放置其他未浮动元素

if you want the block element to be in middle如果您希望块元素位于中间

then make this .box1{ float:left;} .box2{ float:right;}然后使这个.box1{ float:left;} .box2{ float:right;}

and then automatically the inline block element will come to center.然后内联块元素将自动居中。

i suggest you to make all the elements inline block itself as they are all same width and height.it is best way for responsive design also.我建议您将所有元素内联块本身,因为它们的宽度和高度都相同。这也是响应式设计的最佳方式。

clear property applies to only block level elements, so adding it to the inline-block won't have any effect and will not clear float as you expect. clear属性仅适用于块级元素,因此将其添加到inline-block不会产生任何效果,并且不会像您预期的那样清除浮动。

在此处输入图片说明

https://developer.mozilla.org/en-US/docs/Web/CSS/clear https://developer.mozilla.org/en-US/docs/Web/CSS/clear


The float CSS property specifies that an element should be placed along the left or right side of its container, allowing text and inline elements to wrap around it . float CSS 属性指定元素应沿其容器的左侧或右侧放置,允许文本和内联元素环绕它 The element is removed from the normal flow of the web page, though still ref该元素从网页的正常流程中移除,但仍然是ref


inline-block内联块

This value causes an element to generate an inline-level block container ref该值导致元素生成内联级块容器引用

Remove css in middle box "display: inline-block;clear: both;"删除中间框中的 css“display: inline-block;clear: both;” and add " float:left"并添加“浮动:左”

 .box { width: 200px; height: 200px; background: red; } .block { height: 200px; width: 200px; background: blue; /* display: inline-block; clear: both; */ float:left; } .box1 { float: left; } .box2 { float: left; background: green; }
 <div class="box box1">1st Block</div> <div class="block">Middle Block</div> <div class="box box2">Third block</div>

You can remove the inline-block from the middle block (.block) and rap it all inside new div that row's it all.您可以从中间块 (.block) 中删除 inline-block 并将其全部放入新 div 中,该行就是全部。 Just like this:像这样:

CSS: CSS:

.rowed {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
}

HTML: HTML:

<div class="rowed">
  <div class="box box1">1st Block</div>
  <div class="block">Middle Block</div>
  <div class="box box2">Third block</div>
</div>

** Just like Bootstrap are doing ** 就像 Bootstrap 所做的那样

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

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