怎么让父极元素的宽度自动设为所有子集的宽度之和呢?或者怎么保证子集不换行?不考虑用js!

如题,因为子集的个数不确定,但是又必须保证父极的宽度足够包含子集,不让其换行?这里不能用js动态设置,请问有没有什么方法?

https://img2.mukewang.com/5c6e501f0001bd3508000330.jpg

红糖糍粑
浏览 869回答 1
1回答

慕仙森

要让父元素的宽度是所有子元素的宽度之和,那就是说跟据内容自适应,设置父元素的display: inline-block;;要让子元素不换行,在父元素上设置white-space: nowrap<!DOCTYPE html><html><head>&nbsp; <meta charset="UTF-8">&nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; <meta http-equiv="X-UA-Compatible" content="ie=edge">&nbsp; <title>Document</title>&nbsp; <style>&nbsp; &nbsp; div {&nbsp; &nbsp; &nbsp; white-space: nowrap;&nbsp; &nbsp; &nbsp; display: inline-block;&nbsp; &nbsp; &nbsp; border: 1px solid #f00;&nbsp; &nbsp; }&nbsp; &nbsp; p {&nbsp; &nbsp; &nbsp; width: 100px;&nbsp; &nbsp; &nbsp; height: 100px;&nbsp; &nbsp; &nbsp; display: inline-block;&nbsp; &nbsp; &nbsp; border: 1px solid #00f;&nbsp; &nbsp; }&nbsp; </style></head><body>&nbsp; <div>&nbsp; &nbsp; <p>1</p>&nbsp; &nbsp; <p>1</p>&nbsp; &nbsp; <p>1</p>&nbsp; &nbsp; <p>1</p>&nbsp; &nbsp; <p>1</p>&nbsp; &nbsp; <p>1</p>&nbsp; &nbsp; <p>1</p>&nbsp; &nbsp; <p>1</p>&nbsp; &nbsp; <p>1</p>&nbsp; &nbsp; <p>1</p>&nbsp; &nbsp; <p>1</p>&nbsp; </div></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript