hadivoice
hadivoice

Reputation: 311

How can I vertical align a div within another div?

i put to div with two different content in a parent div and i want set vertical align first div with second div

sorry for bad language

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

Upvotes: 0

Views: 106

Answers (7)

SvenL
SvenL

Reputation: 1954

You can simply achieve this with Flexbox.

Just add display: flex and align-items: center to your wrapper div, in your case the .title-pin.

Some advantages of using the display: flex property

  • It doesn't play a role anymore how long your text in .title-container is gonna be.
  • In principle, you also don't even need to set a specific height to the children elements, but I think in your case you want that the .pin-icon has always the same height and width so it's just fine to set both to 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>

Upvotes: 1

Praveen Murali
Praveen Murali

Reputation: 741

Add the following to your .pin-icon CSS. Hope this will help you.

.pin-icon {
  top:50%;
  margin-top:-25px;
}

Upvotes: 1

Daniela Donna
Daniela Donna

Reputation: 270

Add this to your css

.pin-icon {top: 50%; transform: translateY(-50%);}    

Upvotes: 2

Jamiu Oloyede
Jamiu Oloyede

Reputation: 27

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

Upvotes: 1

Baezid Mostafa
Baezid Mostafa

Reputation: 2728

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>

Upvotes: 1

Ron.Basco
Ron.Basco

Reputation: 2438

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>

Upvotes: 0

kapil
kapil

Reputation: 351

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

Upvotes: 1

Related Questions