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

Viewed 1140

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

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

Try to change profile icon position to absolute and add some padding for the container, since you know the icon size.

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

I don't know if card-text has top margin, so you might want to add that too (or, alternatively, further increase bottom padding for the container).

Related