当父项太小时,防止div换行

时间:2018-06-25 22:34:55

标签: javascript html css

如何阻止父div内的div换行而不是被切断?

代码:

var slider = document.getElementById("slider"),
    cont1 = document.getElementById("container1"),
    cont2 = document.getElementById("container2");

slider.addEventListener("input", function() {
    cont1.style.width = slider.value + "px";
    cont2.style.width = slider.value + "px";
}, false); 
#item {
  width: 100px; 
  height: 100px;  
  float: left;
}

.container {
   height: 100px; 
   background-color: #ccc; 
}
<p>use slider to change width of the container</p>
<input id="slider" type="range" max="500">
<p>overflow: hidden</p>
<div class="container" id="container1" style="width: 300px; overflow: hidden;">
  <div id="item" style="background-color: red;"></div>
  <div id="item" style="background-color: green;"></div>
</div>

<p>overflow: visible</p>
<div class="container" id="container2" style="width: 300px; overflow: visible;">
  <div id="item" style="background-color: red;"></div>
  <div id="item" style="background-color: green;"></div>
</div>

jsfiddle

如您所见,当我更改父div的大小时,将自动换行。搜索之后,我找到了两种解决方案。其中一个要求使用绝对定位的div来设置容器的宽度。另一个使用white-space: none,但没有运气。

2 个答案:

答案 0 :(得分:4)

考虑使用inline-block代替浮动,可以使用with-space技巧:

var slider = document.getElementById("slider"),
  cont1 = document.getElementById("container1"),
  cont2 = document.getElementById("container2");

slider.addEventListener("input", function() {
  cont1.style.width = slider.value + "px";
  cont2.style.width = slider.value + "px";
}, false);
#item {
  width: 100px;
  height: 100px;
  display: inline-block;
}

.container {
  height: 100px;
  background-color: #ccc;
  white-space: nowrap;
  font-size: 0; /*to avoid white-space between inline-block*/
}
<p>use slider to change width of the container</p>
<input id="slider" type="range" max="500">
<p>overflow: hidden</p>
<div class="container" id="container1" style="width: 300px; overflow: hidden;">
  <div id="item" style="background-color: red;"></div>
  <div id="item" style="background-color: green;"></div>
</div>

<p>overflow: visible</p>
<div class="container" id="container2" style="width: 300px; overflow: visible;">
  <div id="item" style="background-color: red;"></div>
  <div id="item" style="background-color: green;"></div>
</div>

答案 1 :(得分:-2)

如果我正确理解了您的意图,则希望灰色滑块“淡入”绿色方块,而将绿色方块留在原处。

为此,您需要将overflow: hiddenposition: absolute结合使用:

var slider = document.getElementById("slider");
var cont1 = document.getElementById("container1");

slider.addEventListener("input", function() {
  cont1.style.width = slider.value + "px";
}, false);
#green {
  position: absolute;
  left: calc(100px + 8px); /* Red offset + body margin */
}

.item {
  width: 100px;
  height: 100px;
  float: left;
}

.container {
  height: 100px;
  background-color: #ccc;
}
<p>use slider to change width of the container</p>
<input id="slider" type="range" max="500">
<div class="container" id="container1" style="width: 300px; overflow: hidden;">
  <div class="item" style="background-color: red;"></div>
  <div id="green" class="item" style="background-color: green;"></div>
</div>

请注意,您提供的代码也具有重复的#item ID,这是无效的标记。在上面的示例中,我将它们更改为类。