css - 如果它不适合,防止 div 跳转到下一行
问题描述
有什么办法可以防止 div3 被换行到下一行,而不是垂直滚动做水平?
<table>
<tr>
<td>td1</td>
<td>
<div class="container">
<div class="inner">div1</div>
<div class="inner">div2</div>
<div class="inner">div3</div>
</div>
</td>
</tr>
</table>
table { width: 400px; height: 100px; }
td { width: 50%; border: 1px solid gray; }
div.container { height: 100px; width: 100%; background: red; overflow-x: scroll; }
div.inner { height: 100px; width: 80px; display: block; float: left; background: blue; color: #fff; }
解决方案
使用浮动,您不能在水平轴上滚动,因为如果浮动元素无法容纳在剩余空间中,它将落到下一行。
所以我们必须将 div 视为内联级别块并设置white-space:nowrap
为防止 div 中断到下一行。
table {
width: 400px;
height: 100px;
table-layout: fixed;
}
td {
width: 50%;
border: 1px solid gray;
}
div.container {
height: 100px;
width: 100%;
background: red;
overflow-x: scroll;
/* added */
white-space: nowrap;
/* to remove space between divs */
font-size: 0;
}
div.inner {
height: 100px;
width: 80px;
background: blue;
color: #fff;
/*display: block; removed */
/*float: left; removed */
/* added */
display: inline-block;
/* because font size in inherited it will be set to 0
but we want text to apear
*/
font-size: 16px;
}
<table>
<tr>
<td>td1</td>
<td>
<div class="container">
<div class="inner">div1</div>
<div class="inner">div2</div>
<div class="inner">div3</div>
</div>
</td>
</tr>
</table>
推荐阅读
- php - xpdo - 将多维数组插入 db-table - 一次最多 5 行
- api - 无法初始化类 jmeter HTTPHC4Impl
- java - arrayList for 循环到 hashMap 大小显示是正确的,但元素少了一个。没有使用重复的密钥
- wordpress - WordPress 多站点 2019-2020 中的单点登录
- android - 使用 Ionic Android 应用更新本机 Android 应用
- javascript - 错误:EXDEV:不允许跨设备链接,重命名
- node.js - npm install 突然失败并出现各种错误,包括 Ubuntu 上的权限被拒绝(带和不带 sudo)
- sequelize.js - Sequelize 子查询产品和评级
- html - CSS:z-index 不起作用,当我签入智能手机浏览器时
- android - 如何在 Firebase Android 中检索列表数据