css 如何让已经设置了大小的浮动div左右相贴
时间: 2023-10-21 07:07:12 浏览: 39
可以通过以下两种方式实现已经设置了大小的浮动div左右相贴:
1. 使用margin负值:将左侧的div设置一个负的右侧margin值,将右侧的div设置一个负的左侧margin值,使它们的边框相邻并且没有间隔。
```html
<style>
.left {
float: left;
width: 50%;
margin-right: -1px;
}
.right {
float: right;
width: 50%;
margin-left: -1px;
}
</style>
<div class="left">这是左侧的div</div>
<div class="right">这是右侧的div</div>
```
2. 使用flex布局:将这两个div包裹在一个父容器中,并将父容器设置为flex布局,通过设置`justify-content: space-between`来使两个div分别位于左右两侧并且紧贴着边框。
```html
<style>
.container {
display: flex;
justify-content: space-between;
}
.left, .right {
width: 50%;
}
</style>
<div class="container">
<div class="left">这是左侧的div</div>
<div class="right">这是右侧的div</div>
</div>
```