我创建了以下 div,但因为我使用负左边距来偏移图标,所以无法将整个 div 居中。我们可以在桌面上摆脱它,因为你不能很容易地看出整个 div 向左移动了一点,但在移动设备上,圆形图标的左侧被切断。如何使整个 div 居中?
.icon-text-box { border: 1px solid red; } .icon-box { background: #fff; border-radius: 33px; margin: 6rem auto; padding: 4rem 4rem 4rem 7rem; box-shadow: 0 3px 6px -2px rgb(0 0 0 / 20%), 0 6px 12px rgb(0 0 0 / 10%); position: relative; display: flex; align-self: center; max-width: 900px; } .icon-box p { font-size: 22px; margin-bottom: 0; } .icon-box-icon { position: absolute; left: -90px; overflow: hidden; display: flex; align-self: center; height: 177px; } .icon-box-icon img { border-radius: 40px; }
<div class="container-fluid mw-972 icon-text-box"> <div class="icon-box"> <div class="icon-box-icon"> <img src="https://www.freepnglogos.com/uploads/spotify-logo-png/spotify-simple-green-logo-icon-24.png" /> </div> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p> </div> </div>
P粉1079910302024-01-30 00:23:25
代替 auto
边距,您可以使用不同的想法来居中,同时在小屏幕上添加一些边距
margin: 6rem max(90px,(100% - 900px)/2);
完整代码:
.icon-text-box { border: 1px solid red; } .icon-box { background: #fff; border-radius: 33px; margin: 6rem max(90px,(100% - 900px)/2); padding: 4rem 4rem 4rem 7rem; box-shadow: 0 3px 6px -2px rgb(0 0 0 / 20%), 0 6px 12px rgb(0 0 0 / 10%); position: relative; display: flex; } .icon-box p { font-size: 22px; margin-bottom: 0; } .icon-box-icon { position: absolute; left: -90px; top: 50%; transform: translateY(-50%); overflow: hidden; display: flex; height: 177px; } .icon-box-icon img { border-radius: 40px; }
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
如果您想要一侧有边距,也如下所示:
.icon-text-box { border: 1px solid red; } .icon-box { background: #fff; border-radius: 33px; margin: 6rem max(0px,(100% - 900px)/2) 6rem max(90px,(100% - 900px)/2); padding: 4rem 4rem 4rem 7rem; box-shadow: 0 3px 6px -2px rgb(0 0 0 / 20%), 0 6px 12px rgb(0 0 0 / 10%); position: relative; display: flex; } .icon-box p { font-size: 22px; margin-bottom: 0; } .icon-box-icon { position: absolute; left: -90px; top: 50%; transform: translateY(-50%); overflow: hidden; display: flex; height: 177px; } .icon-box-icon img { border-radius: 40px; }
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.