我最外面一层用banner的大盒子,里面在中心有个w的盒子,然后w盒子有背景,w盒子里面还有两个盒子也就是subnav和couser盒子
为什么我的subnav盒子用左浮动到父盒子w的中间去了
这是我的html部分的代码
```css
/* 版心 */
.w {
width: 1200px;
margin: 0 auto;
}
.banner {
height: 420px;
background-color: #1c036c;
}
.banner .w {
height: 420px;
/* 背景图片写法 no-repeat不重复 */
background: url(images/banner.png) no-repeat top center;
}
.banner .w .subnav {
float: left;
width: 192px;
height: 420px;
/* 用半透明 */
background-color: rgba(0, 0, 0, .3);
}
.banner .w .subnav ul li {
height: 15px;
line-height: 15px;
padding-left: 20px;
padding-top: 27px;
}
.banner .w .subnav ul li a {
font-size: 14px;
color: #fff;
}
.banner .w .subnav ul li a span {
float: right;
padding-right: 20px;
}
.banner .w .subnav ul li a:hover {
color: #00b4ff;
}
.banner .w .course {
float: right;
width: 230px;
height: 300px;
background-color: pink;
}
```,
而我的subnav盒子添加浮动,我的course盒子没有添加浮动,效果图就变下面这种了
求解答!!
.w加一个overflow:hidden
设置浮动会让元素脱离标准流。元素设置左浮动,是朝着左边移动,直到自己的边界紧贴着包块(即自己的父元素)或者其他浮动元素的边界为止。
你这里只有subnav盒子设置浮动,父元素是w盒子,所以subnav盒子就会朝左边移动到贴着w的盒子边界。
.w加一个overflow:hidden或者加个高度
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
padding: 0;
margin: 0;
}
/* 版心 */
.w {
width: 1200px;
margin: 0 auto;
}
.banner {
height: 420px;
background-color: #1c036c;
}
.banner .w {
height: 420px;
/* 背景图片写法 no-repeat不重复 */
/* background: url(images/banner.png) no-repeat top center; */
background: red;
}
.banner .w .subnav {
float: left;
width: 192px;
height: 420px;
/* 用半透明 */
background-color: rgba(0, 0, 0, .3);
}
.banner .w .subnav ul li {
height: 15px;
line-height: 15px;
padding-left: 20px;
padding-top: 27px;
}
.banner .w .subnav ul li a {
font-size: 14px;
color: #fff;
}
.banner .w .subnav ul li a span {
float: right;
padding-right: 20px;
}
.banner .w .subnav ul li a:hover {
color: #00b4ff;
}
.banner .w .course {
float: right;
width: 230px;
height: 300px;
background-color: pink;
}
</style>
</head>
<body>
<div class="banner">
<div class="w">
<div class="subnav">
<ul>
<li>
<a href="">777</a>
</li>
</ul>
</div>
<div class="course"></div>
</div>
</div>
</body>
</html>
我复现了一下 没有出现你说的问题啊
拿你代码试了一下,没有修改你的代码,效果是可以的