html - 为什么 div 垂直而不是水平显示?
问题描述
这是我的html。样式部分有点不同,因为我在 React 中编写,但否则我猜我的问题是由于 css 和 html 而发生的。为什么内部 div 出现在下排。我希望它水平显示在外部 div 内,并在 x 轴上滚动。我已经给出了诸如 white-space:nowrap 之类的属性。
<div style={{position:'absolute', width:'1200px', height:'80px',background:'#cc5',top:'270px',left:'100px',whiteSpace: 'nowrap',display:'inline-block',overflowX:'auto'}}>
<div style={{width:'100px',height:'60px',background:'white',display:'inline-block',margin:'10px',float:'left'}}></div>
<div style={{width:'100px',height:'60px',background:'white',display:'inline-block',margin:'10px',float:'left'}}></div>
<div style={{width:'100px',height:'60px',background:'white',display:'inline-block',margin:'10px',float:'left'}}></div>
<div style={{width:'100px',height:'60px',background:'white',display:'inline-block',margin:'10px',float:'left'}}></div>
<div style={{width:'100px',height:'60px',background:'white',display:'inline-block',margin:'10px',float:'left'}}></div>
<div style={{width:'100px',height:'60px',background:'white',display:'inline-block',margin:'10px',float:'left'}}></div>
<div style={{width:'100px',height:'60px',background:'white',display:'inline-block',margin:'10px',float:'left'}}></div>
<div style={{width:'100px',height:'60px',background:'white',display:'inline-block',margin:'10px',float:'left'}}></div>
<div style={{width:'100px',height:'60px',background:'white',display:'inline-block',margin:'10px',float:'left'}}></div>
<div style={{width:'100px',height:'60px',background:'white',display:'inline-block',margin:'10px',float:'left'}}></div>
<div style={{width:'100px',height:'60px',background:'white',display:'inline-block',margin:'10px',float:'left'}}></div>
<div style={{width:'100px',height:'60px',background:'white',display:'inline-block',margin:'10px',float:'left'}}></div>
<div style={{width:'100px',height:'60px',background:'white',display:'inline-block',margin:'10px',float:'left'}}></div>
<div style={{width:'100px',height:'60px',background:'white',display:'inline-block',margin:'10px',float:'left'}}></div>
</div>
解决方案
您的代码需要进行一些修改,如下所示:
<div style="position: absolute; width: 1200px; height: 80px;background: #cc5;top: 270px;left: 100px;white-space: nowrap;display: inline-block;overflow-x: auto;">
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
<div style="width: 100px;height: 60px;background: white;display: inline-block;margin: 10px;float: left"></div>
</div>
由于您具有内联样式,因此您可以将它们移动到 css 文件中。另外,overflowX
需要改成overflow-x
推荐阅读
- javascript - "ng-circle-progress" percent attribute only works when the renderOnClick attribute is set to true
- android-tv - How to figure out what is wrong when NEP on Presenter.onCreateViewHolder in leanback was thrown
- scala - 在列表中搜索以下以特定方式关联的项目
- openssl - 适用于 CentOS 7 的 OpenSSL 1.1.1 RPM
- java - 使用数据库 jpa 中现有和不存在的数据保留新数据时出错
- python - 状态对象没有属性“正在加载”
- mysql - 将 10GB 的 mysql 表发送到远程服务器
- cmake - CMake 错误地识别有效的 C++ 编译器选项?
- python - 我的程序不会未读不需要的消息
- python - 在 Python 上打印变量的语法错误