[英]How to adjust the height of the parent div after translateY(-50%)
我在使用引導卡時遇到問題。 我使用了transform: translateY(-50%);
為了將元素轉換為 y 軸,我將父 div 的高度設置為自動,但問題是在轉換元素后,我希望高度應該自動調整。
這是 html
<div class="card layer-1-left-sub">
<img class="card-img-top profile-icon" src="images/profile-boy.jpg" alt="Card image cap">
<div class="card-body">
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
</div>
</div>
這是我的 css
.layer-1-left-sub{
padding: 3px;
border: 2px solid #3f51b5;
border-radius: 5px;
margin: 0 auto;
float: none;
height: auto;
}
.profile-icon{
bottom: 0;
right: 0;
left: 0;
width: 150px;
height: 150px;
transform: translateY(-50%);
margin: 0 auto;
float: none;
display: block;
border-radius: 50%;
border: 2px solid #fff;
}
這是我面臨的問題
我在圖像和段落之間有一個額外的空間,我想刪除多余的空間
使用變換屬性更新!
.layer-1-left-sub{ padding: 3px; border: 2px solid #3f51b5; border-radius: 5px; margin: 0 auto; float: none; text-align: center; position: relative; }.profile-icon{ position: absolute; top: 50%; left: 50%; transform: translate(-50%, -100%); width: 150px; height: 150px; display: block; border-radius: 50%; border: 2px solid #fff; }.card-text { margin-top: 100px; }
<div class="card layer-1-left-sub" style="margin-top:100px"> <img class="card-img-top profile-icon" src=" https://pbs.twimg.com/profile_images/758084549821730820/_HYHtD8F.jpg" alt="Card image cap"> <div class="card-body"> <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p> </div> </div>
我解決了你的問題。 我使用了 Display flex 並刪除了一些不必要的 css 屬性。 如果以這種方式使用它,您將擁有更干凈的造型,而無需處理 position absolute
.layer-1-left-sub{ padding: 3px; border: 2px solid #3f51b5; border-radius: 5px; margin: 0 auto; float: none; text-align: center; display: flex; flex-direction: column; align-items: center; }.profile-icon{ margin-top: -80px; width: 150px; height: 150px; display: block; border-radius: 50%; border: 2px solid #fff; }
<div class="card layer-1-left-sub" style="margin-top:100px"> <img class="card-img-top profile-icon" src=" https://pbs.twimg.com/profile_images/758084549821730820/_HYHtD8F.jpg" alt="Card image cap"> <div class="card-body"> <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p> </div> </div>
嘗試將配置文件圖標position
更改為absolute
並為容器添加一些填充,因為您知道圖標大小。
.layer-1-left-sub {
padding: 3px 3px 3px 78px;
border: 2px solid #3f51b5;
border-radius: 5px;
margin: 0 auto;
height: auto;
}
.profile-icon {
position: absolute;
top: 0;
left: 50%;
width: 150px;
height: 150px;
transform: translateY(-50%);
margin: 0 auto;
display: block;
border-radius: 50%;
border: 2px solid #fff;
}
我不知道card-text
是否有上邊距,所以您可能也想添加它(或者,進一步增加容器的底部填充)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.