由於子 div 的左邊距為負值,父 div 無法居中
P粉677573079
P粉677573079 2024-01-29 15:04:54
0
1
506

我建立了以下 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粉677573079
P粉677573079

全部回覆(1)
P粉107991030

取代 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.

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板