[英]How can I vertical align a div within another div?
我在父div中將兩個不同的內容放入div中,並且我想將第一個div與第二個div垂直對齊
對不起,語言不好
.title-pin { position: relative; } .pin-icon { position: absolute; width: 50px; height: 50px; -webkit-border-radius: 25px; -moz-border-radius: 25px; border-radius: 25px; text-align: center; background-color: #333; color: #fff; } .star { font-size: 30px; line-height: 50px; } .title-container { padding-left: 60px; width: 400px; font-size: 24px; }
<div class="title-pin"> <div class="pin-icon"> <span class="star">★</span> </div> <div class="title-container"> <h3>This title is unstable. This title is unstable. This title is unstable.`enter code here`</h3> </div> </div>
將此添加到您的CSS
.pin-icon {top: 50%; transform: translateY(-50%);}
.title-pin { position: relative; } .pin-icon { display: inline-block; width: 50px; height: 50px; -webkit-border-radius: 25px; -moz-border-radius: 25px; border-radius: 25px; text-align: center; background-color: #333; color: #fff; vertical-align: middle; } .star { font-size: 30px; line-height: 50px; } .title-container { padding-left: 30px; width: 400px; display: inline-block; font-size: 24px; vertical-align: middle; }
<div class="title-pin"> <div class="pin-icon"> <span class="star">★</span> </div> <div class="title-container"> <h3>This title is unstable. This title is unstable. This title is unstable.`enter code here`</h3> </div> </div>
您可以通過多種方式獲得此信息。 簡而言之,我只在您的CSS類中添加一行。pin-icon 頂部:25%
.title-pin { position:relative; } .pin-icon { position: absolute; width: 50px; height: 50px; -webkit-border-radius: 25px; -moz-border-radius: 25px; border-radius: 25px; text-align: center; background-color:#333; color:#fff; top:25%; } .star { font-size: 30px; line-height: 50px; } .title-container { padding-left: 60px; width:400px; font-size:24px; }
<div class="title-pin"> <div class="pin-icon"> <span class="star">★</span> </div> <div class="title-container"> <h3>This title is unstable. This title is unstable. This title is unstable.`enter code here`</h3> </div> </div>
您只需使用Flexbox即可實現。
只需添加display: flex
和align-items: center
到包裝div的align-items: center
,在這種情況下為.title-pin
。
使用display: flex
一些優點display: flex
屬性
.title-container
文本將.title-container
多長時間不再.title-container
。 .pin-icon
始終具有相同的高度和寬度,因此將二者都設置為50px很好。 .title-pin { position: relative; display: flex; align-items: center; } .pin-icon { width: 50px; height: 50px; border-radius: 50%; text-align: center; color: #fff; background: #000; margin: 0 20px; } .star { font-size: 30px; line-height: 50px; } .title-container { width: 400px; font-size: 24px; }
<div class="title-pin"> <div class="pin-icon"> <span class="star">★</span> </div> <div class="title-container"> <h3>This title is unstable. This title is unstable. This title is unstable.`enter code here`</h3> </div> </div>
.title-pin { position: relative; display: block; margin: auto; } .pin-icon { <!--remove relateive: absolute; --> width: 50px; <!-- add this --> margin-left: 55px; <!-- add this --> height: 50px; -webkit-border-radius: 25px; -moz-border-radius: 25px; border-radius: 25px; text-align: center; background-color: #333; color: #fff; } .star { font-size: 30px; line-height: 50px; } .title-container { padding-left: 60px; width: 400px; font-size: 24px; }
<div class="title-pin"> <div class="pin-icon"> <span class="star">★</span> </div> <div class="title-container"> <h3>This title is unstable. This title is unstable. This title is unstable.`enter code here`</h3> </div> </div>
將以下內容添加到您的.pin-icon CSS中。 希望這會幫助你。
.pin-icon {
top:50%;
margin-top:-25px;
}
只要該元素具有height
,這將使該元素垂直居中。
position: absolute;
top: 0;
bottom: 0;
margin: auto;
.title-pin { position: relative; } .pin-icon { position: absolute; width: 50px; height: 50px; -webkit-border-radius: 25px; -moz-border-radius: 25px; border-radius: 25px; text-align: center; background-color: #333; color: #fff; top: 0; bottom: 0; margin: auto; } .star { font-size: 30px; line-height: 50px; } .title-container { padding-left: 60px; width: 400px; font-size: 24px; }
<div class="title-pin"> <div class="pin-icon"> <span class="star">★</span> </div> <div class="title-container"> <h3>This title is unstable. This title is unstable. This title is unstable.`enter code here`</h3> </div> </div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.