浮动元素怎么撑开父元素

   html:  

    <div class="two">

        <div class="img1"></div>

        <div class="img2"></div>

        <div class="img3"></div>

    </div>

    css:

    

.img1{


width: 500px;

height: 500px;

background: url(a)no-repeat;

background-size: 500px 500px;

}

.img2{


width: 500px;

height: 500px;

background: url(../csstexting/b.JPEG)no-repeat;

background-size: 500px 500px;

}


.img3{


width: 500px;

height: 500px;

background: url(../csstexting/c.JPEG)no-repeat;

background-size: 500px 500px;

}

请问一下父元素内部的3个DIV元素我设置了左浮动。怎么在不设置父元素宽度的情况下让内部的DIV横向排列啊。我要做轮播图。默认的是纵向排列的 也就是我设置了左浮动3个DIV盒子会乡下排列1500px


幕布斯7119047
浏览 1418回答 2
2回答

30秒到达战场

我个人不喜欢浮动,先推荐一下inline-block<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; <title>hello world</title></head><style>&nbsp; &nbsp; .two {&nbsp; &nbsp; &nbsp; &nbsp; /* 禁止内联元素换行 */&nbsp; &nbsp; &nbsp; &nbsp; white-space: nowrap;&nbsp; &nbsp; &nbsp; &nbsp; /* 隐藏超出部分 */&nbsp; &nbsp; &nbsp; &nbsp; overflow: hidden;&nbsp; &nbsp; }&nbsp; &nbsp; .img {&nbsp; &nbsp; &nbsp; &nbsp; /* 设置为内联块 */&nbsp; &nbsp; &nbsp; &nbsp; display: inline-block;&nbsp; &nbsp; &nbsp; &nbsp; width: 500px;&nbsp; &nbsp; &nbsp; &nbsp; height: 500px;&nbsp; &nbsp; &nbsp; &nbsp; /* 垂直对齐为顶部对齐 */&nbsp; &nbsp; &nbsp; &nbsp; vertical-align: top;&nbsp; &nbsp; &nbsp; &nbsp; background-size: 500px 500px;&nbsp; &nbsp; }&nbsp; &nbsp; .img1 {&nbsp; &nbsp; &nbsp; &nbsp; background: red url(a)no-repeat;&nbsp; &nbsp; }&nbsp; &nbsp; .img2 {&nbsp; &nbsp; &nbsp; &nbsp; background: green url(../csstexting/b.JPEG) no-repeat;&nbsp; &nbsp; }&nbsp; &nbsp; .img3 {&nbsp; &nbsp; &nbsp; &nbsp; background: blue url(../csstexting/c.JPEG) no-repeat;&nbsp; &nbsp; }</style><body>&nbsp; &nbsp; <div class="two">&nbsp; &nbsp; &nbsp; &nbsp; <div class="img img1"></div><!--&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 内联元素之间的空格不会被忽略&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 所以采用比较特殊的换行方法&nbsp; &nbsp; &nbsp; &nbsp; --><div class="img img2"></div&nbsp; &nbsp; &nbsp; &nbsp; ><div class="img img3"></div>&nbsp; &nbsp; </div></body></html>然后是浮动的,首先要明白,元素浮动之后就脱离了文档流,不能再影响父级,所以不但不能撑开宽度,高度也不能撑开,需要用overflow: hidden;, clear: both之类的方式才能防止浮动塌陷,所以只能靠直接设置父级来决定父级宽度。&nbsp;如果没有设置父级宽度,浮动的元素不但不会撑开父级,还会因为父级宽度不足向下跑,就出现你题目里向下排列的情况了。<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="utf-8">&nbsp; &nbsp; <title>hello world</title></head><style>&nbsp; &nbsp; .two {&nbsp; &nbsp; &nbsp; &nbsp; /* 限制显示宽度 */&nbsp; &nbsp; &nbsp; &nbsp; width: 100%;&nbsp; &nbsp; &nbsp; &nbsp; /* 隐藏超出部分 */&nbsp; &nbsp; &nbsp; &nbsp; overflow: hidden;&nbsp; &nbsp; }&nbsp; &nbsp; .wrap {&nbsp; &nbsp; &nbsp; &nbsp; /* 实际内容宽度 */&nbsp; &nbsp; &nbsp; &nbsp; width: 1500px;&nbsp; &nbsp; }&nbsp; &nbsp; .img {&nbsp; &nbsp; &nbsp; &nbsp; float: left;&nbsp; &nbsp; &nbsp; &nbsp; width: 500px;&nbsp; &nbsp; &nbsp; &nbsp; height: 500px;&nbsp; &nbsp; &nbsp; &nbsp; background-size: 500px 500px;&nbsp; &nbsp; }&nbsp; &nbsp; .img1 {&nbsp; &nbsp; &nbsp; &nbsp; background: red url(a)no-repeat;&nbsp; &nbsp; }&nbsp; &nbsp; .img2 {&nbsp; &nbsp; &nbsp; &nbsp; background: green url(../csstexting/b.JPEG) no-repeat;&nbsp; &nbsp; }&nbsp; &nbsp; .img3 {&nbsp; &nbsp; &nbsp; &nbsp; background: blue url(../csstexting/c.JPEG) no-repeat;&nbsp; &nbsp; }</style><body>&nbsp; &nbsp; <div class="two">&nbsp; &nbsp; &nbsp; &nbsp; <div class="wrap">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="img img1"></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="img img2"></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="img img3"></div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript