定位的宽度问题

198 阅读1分钟

.box { height: 40px; /width: 100%;/ background: #c82519; position: absolute; top: 0px; } .box div:nth-child(1){ width: 40px; height: 40px; background: #fff2ca; position: absolute; left: 0; top: 0; } .box div:nth-child(2){ /width: 40px;/ height: 40px; background: #666666; } .box div:nth-child(3){ width: 40px; height: 40px; background: pink; position: absolute; left: 40px; top: 0; }

一个父盒子里面装着三个盒子 第一个和第三个盒子采用定位absolute来做,第二个用标准流来做 发现父盒子用相对定位可以,用固定定位和绝对定位会有宽度的问题?

原因: 块级元素添加绝对或者固定定位,如果不给宽度或者高度,默认大小是内容的大小。