简体   繁体   English

浮点数和溢出-x:隐藏;

[英]Floats and overflow-x: hidden;

I am trying to hide the rest of a box, if it is beyond the containers width (100% in the actual code) . 我试图隐藏盒子的其余部分,如果它超出了容器的宽度(实际代码中为100%) The elements are floating. 元素是浮动的。

However, it throws them to the next line instead of hiding the remaining part of it. 但是,它将它们扔到下一行,而不是隐藏其余部分。

Here is the example I made for demonstrating the problem. 这是我用来说明问题的示例。

<div class="wrapper">
    <div class="box blue"></div>
    <div class="box red"></div>
    <div class="box blue"></div>
    <div class="box red"></div>

    <!-- Just for showing the pink box -->
    <div style="clear: both;"></div>
</div>

And the CSS 和CSS

.wrapper {
    width: 180px;
    background-color: pink;

    overflow-x: hidden;
}

.wrapper .box {
    width: 50px;
    height: 50px;

    float: left;
}

.blue {
    background-color: blue;
}

.red {
    background-color: red;
}

How can I achieve this? 我该如何实现?

Here's a fiddle demonstrating the problem: http://jsfiddle.net/bLaxyvwb/2/ 这是一个证明问题的小提琴: http : //jsfiddle.net/bLaxyvwb/2/

Here's a JSFiddle with the changes you need to apply. 这是一个JSFiddle,其中包含您需要应用的更改。

Use the following CSS: 使用以下CSS:

.wrapper{
 width: 180px;
 background-color: pink;
 white-space: nowrap;
 overflow-x: hidden;
}

.wrapper .box {
 width: 50px;
 height: 50px;
 display: inline-block;
}

And the HTML should look like this: HTML应该看起来像这样:

<div class="wrapper">
    <div class="box blue"></div>
    <div class="box red"></div>
    <div class="box blue"></div>
    <div class="box red"></div>
</div>

I did the following: 我做了以下工作:

  • Added white-space: nowrap to .wrapper 在.wrapper中添加了空格:nowrap
  • Removed float:left; 删除float:left; from .wrapper .box 来自.wrapper .box
  • Added display: inline-block; 添加显示:inline-block; to .wrapper .box 到.wrapper .box

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

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