Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to adjust the height of the parent div after translateY(-50%)

Tags:

html

css

I have a problem while using bootstrap card. I have used the transform: translateY(-50%); for translate the element to the y-axis , I have set the height of the parent div to auto, but the problem is that after translate the element, I want the height should be adjust automatically.

this is the 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>

this is my 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;
}

this is the problem I am facing

enter image description here

I have a extra space between image and paragraph , I want to remove the extra space

like image 582
Tayyab Vohra Avatar asked Oct 18 '25 20:10

Tayyab Vohra


1 Answers

Updated with using transform property!

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

I solved your problem. I used Display flex and removed some unnecessary css properties. If you use it this way, you will have a much cleaner styling without having to deal with 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>
like image 198
CanUver Avatar answered Oct 21 '25 10:10

CanUver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!