html溢出x失败
html overflow x failed
我遇到了一个奇怪的问题,我设置了一个样式为的div
overflow-x:滚动
但结果是它没有溢出,只是从一行开始,我的源代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=1">
<style>
.fl {
float: left;
}
.cp-header {
height: 40px;
padding: 7px 0px;
border-bottom: 2px solid #cdcdcd;
box-sizing: border-box;
}
.cp-header span {
display: inline-block;
border-right: 2px solid #cdcdcd;
box-sizing: border-box;
text-align: center;
height: 24px;
line-height: 24px;
font-size: 14px;
}
</style>
</head>
<body>
<div style="width: 30%; box-sizing: border-box" class="fl">
<div class="cp-header" style="width:100%;">
<span style="width:100%;">name</span>
</div>
</div>
<div id="tabGrp" style="width: 70%; box-sizing: border-box;" class="fl">
<div class="cp-header" style="display: inline-block; overflow-x: scroll; white-space: nowrap;">
<span class="fl" style="width: 100px;">
tab
</span>
<span class="fl" style="width: 100px;">
tab
</span>
<span class="fl" style="width: 100px;">
tab
</span>
<span class="fl" style="width: 100px;">
tab
</span>
<span class="fl" style="width: 100px;">
tab
</span>
<span class="fl" style="width: 100px;">
tab
</span>
<span class="fl" style="width: 100px;">
tab
</span>
<span class="fl" style="width: 100px;">
tab
</span>
<span class="fl" style="width: 100px;">
tab
</span>
<span class="fl" style="width: 100px;">
tab
</span>
<span class="fl" style="width: 100px;">
tab
</span>
</div>
</div>
</body>
</html>
正如你从上面看到的,我希望"#tabGrp"中的内容可以水平滚动,因为我已经设置了overflow-x:scroll属性,如果它的子元素的宽度超过了父元素的宽度,则父元素应该滚动,但它不起作用,我有什么问题吗?
2件事,你的span
不能有/不需要float,cp-header
需要一个宽度
注意,你不想混合内联和外部风格,所以我把它改为外部(推荐)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=1">
<style>
.tabCap {
width: 30%;
box-sizing: border-box;
}
.tabGrp {
width: 70%;
box-sizing: border-box;
}
.fl {
float: left;
}
.cp-header {
display: inline-block;
white-space: nowrap;
width: 100%;
height: 60px;
padding: 7px 0px;
border-bottom: 2px solid #cdcdcd;
box-sizing: border-box;
}
.cp-header.scroll {
overflow-x: scroll;
}
.cp-header span {
display: inline-block;
width: 100%;
border-right: 2px solid #cdcdcd;
box-sizing: border-box;
text-align: center;
height: 24px;
line-height: 24px;
font-size: 14px;
}
.cp-header.scroll span {
width: 100px;
}
</style>
</head>
<body>
<div class="tabCap fl">
<div class="cp-header">
<span>name</span>
</div>
</div>
<div class="tabGrp fl">
<div class="cp-header scroll">
<span>tab</span>
<span>tab</span>
<span>tab</span>
<span>tab</span>
<span>tab</span>
<span>tab</span>
<span>tab</span>
<span>tab</span>
<span>tab</span>
<span>tab</span>
</div>
</div>
</body>
</html>
根据评论进行更新。
span
之间的空白是由标记中的换行引起的空白。这种情况发生在所有内联元素上。
这里有一种方法可以解决这个问题,将开始和结束标签放在同一行上
更新的plnkr
<span>
tab
</span><span>
tab
</span>
它对我来说很好,检查这个
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=1">
<style>
.fl2 {
float:left;
}
.cp-header {
height: 40px;
padding: 7px 0px;
border-bottom: 2px solid #cdcdcd;
box-sizing: border-box;
}
.cp-header span {
display: inline-block;
border-right: 2px solid #cdcdcd;
box-sizing: border-box;
text-align: center;
height: 24px;
line-height: 24px;
font-size: 14px;
}
</style>
</head>
<body>
<div style="width: 30%; box-sizing: border-box" class="fl2">
<div class="cp-header" style="width:100%;">
<span style="width:100%;">name</span>
</div>
</div>
<div id="tabGrp" style="width: 70%; box-sizing: border-box;" class="fl2">
<div class="cp-header" style="width: 100%; display: inline-block; overflow-x: scroll; white-space: nowrap;">
<span class="fl" style="width: 100px;">
tab
</span>
<span class="fl" style="width: 100px;">
tab
</span>
<span class="fl" style="width: 100px;">
tab
</span>
<span class="fl" style="width: 100px;">
tab
</span>
<span class="fl" style="width: 100px;">
tab
</span>
<span class="fl" style="width: 100px;">
tab
</span>
<span class="fl" style="width: 100px;">
tab
</span>
<span class="fl" style="width: 100px;">
tab
</span>
<span class="fl" style="width: 100px;">
tab
</span>
<span class="fl" style="width: 100px;">
tab
</span>
<span class="fl" style="width: 100px;">
tab
</span>
</div>
</div>
</body>
</html>
相关文章:
- 如何检测是否有溢出
- 当json解析空响应时,Whatwg-Fetch失败,我该如何防止它
- 如何通过溢出来判断元素被切断了多少像素:隐藏在父级上
- JsFiddle在分叉后描述失败
- 如何在AngularJS应用程序的主体上动态设置溢出
- 为什么不'当单元测试出现解析错误时,我的因果报应测试会失败
- 当一些承诺失败时,如何继续使用$q.all()
- 失败:等待Protractor与页面同步时出错:“”;在窗口上找不到角度”;
- 在Jquery中单击传播失败
- 网页上失败的javascript会导致所有其他脚本失败
- 使用滚动溢出-x进行无限循环
- 带有对象解析的响应javascript ajax失败
- 一台特定计算机的Ajax请求数据未定义/失败
- Nodejs服务器:加载资源失败:服务器的响应状态为404(未找到)
- Android在HTML容器中滚动,该容器具有CSS溢出属性
- jQuery.getJSON失败,语法错误
- ng disabled在放入多个表达式时失败
- Jasmine单元测试在监视服务方法时失败
- 在滚动时,在隐藏滚动条和隐藏溢出的同时触发事件
- html溢出x失败