我在父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>
&#13;
答案 0 :(得分:2)
将此添加到您的css
.pin-icon {top: 50%; transform: translateY(-50%);}
答案 1 :(得分:1)
.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>
答案 2 :(得分:1)
Many ways you can get this. In short I just add one line on your css class .pin-icon top: 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>
答案 3 :(得分:1)
您只需使用 Flexbox 即可实现此目的。
只需将display: flex
和align-items: center
添加到您的包装div中,在您的情况下为.title-pin
。
使用display: flex
属性
.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;
}
&#13;
<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>
&#13;
答案 4 :(得分:1)
.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>
答案 5 :(得分:1)
将以下内容添加到.pin-icon CSS中。希望这会对你有所帮助。
.pin-icon {
top:50%;
margin-top:-25px;
}
答案 6 :(得分:0)
as long as the element has a height
, this will center the element vertically.
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>