Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I align sizes when using flex and line-height+vh?

Tags:

html

css

I got pretty close to what I want however I think the 32vh is what's ruining my size.

When I use img {height: 100%;} they become too big so I settled down for 32vh; However the left and right div are a few pixels off. It's especially noticeable if you zoom in a little (press ctrl+'+'). It happens on both firefox and chrome. I tried using 50% and 100% height on the images and putting position relative on testB but it didn't seem to help. How do I make both size equal height? I don't mind if there's extra pixels between the right two divs but they must be same height as left aka div a

body * {
  box-sizing: border-box;
  color: white;
}

.test {
  margin: 0 auto;
  position: relative;
  height: 65vh;
  width: 80vw;
  display: flex;
  text-align: center;
}

.testA {
  background-color: black;
  width: 61.5%;
  display: inline-block;
  padding-right: 18px;
  line-height: 65vh;
}

.testB {
  background-color: green;
  flex-grow: 1;
  line-height: 32vh;
  position: relative;
}

.testC {
  background-color: royalblue;
  vertical-align: top;
  margin-bottom: 10px;
}

.testD {
  flex-grow: 1;
  background-color: purple;
  vertical-align: bottom;
  margin-top: 10px;
}

.test img {
  vertical-align: middle;
}

.testA img {
  height: 100%;
}

.testC img {
  vertical-align: top;
  height: 32vh;
}

.testD img {
  vertical-align: bottom;
  height: 32vh;
}
<div class="test">
  <div class="testA">
    a
  </div>
  <div class="testB">
    <div class="testC">
      c
    </div>
    <div class="testD">
      d
    </div>
  </div>
</div>
<hr>
<div class="test">
  <div class="testA">
    <img src="https://placehold.it/300x500">
  </div>
  <div class="testB">
    <div class="testC">
      <img src="https://placehold.it/300x500">
    </div>
    <div class="testD">
      <img src="https://placehold.it/300x500">
    </div>
  </div>
</div>
<hr>
<div class="test">
  <div class="testA">
    <img src="https://placehold.it/500x300">
  </div>
  <div class="testB">
    <div class="testC">
      <img src="https://placehold.it/500x300">
    </div>
    <div class="testD">
      <img src="https://placehold.it/500x300">
    </div>
  </div>
</div>
like image 636
UnixUser Avatar asked Jan 22 '20 08:01

UnixUser


2 Answers

I hope this is what u r expecting:

<style>
  body * {
    box-sizing: border-box;
    color: white;
  }
  
  .test {
    margin: 0 auto;
    position: relative;
    height: 65vh;
    width: 80vw;
    display: flex;
  }
  
  .testA {
    background-color: black;
    width: 61.5%;
    display: inline-block;
    padding-right: 18px;
    line-height: 65vh;
    text-align: center;
  }
  
  .testB {
    background-color: green;
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    line-height: 32vh;
    text-align: right;
    position: relative;
  }
  
  .testC {
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: royalblue;
    vertical-align: top;
  }
  
  .testD {
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: purple;
    flex-grow: 1;
    vertical-align: bottom;
  }
  
  .testA img {
    height: 100%;
  }
  
  .testC img {
    vertical-align: top;
    height: 32.5vh;
  }
  
  .testD img {
    vertical-align: bottom;
    height: 32.5vh;
  }
</style>

<div class="test">
  <div class="testA">
    a
  </div>
  <div class="testB">
    <div class="testC">
      c
    </div>
    <div class="testD">
      d
    </div>
  </div>
</div>
<hr>
<div class="test">
  <div class="testA">
    <img src="https://placehold.it/300x500">
  </div>
  <div class="testB">
    <div class="testC">
      <img src="https://placehold.it/300x500">
    </div>
    <div class="testD">
      <img src="https://placehold.it/300x500">
    </div>
  </div>
</div>
<hr>
<div class="test">
  <div class="testA">
    <img src="https://placehold.it/500x300">
  </div>
  <div class="testB">
    <div class="testC">
      <img src="https://placehold.it/500x300">
    </div>
    <div class="testD">
      <img src="https://placehold.it/500x300">
    </div>
  </div>
</div>
like image 164
Amaresh S M Avatar answered Oct 21 '22 17:10

Amaresh S M


not quit sure if I understand your question, but here is my approach. Use "background-image" instead and then background-size: cover;

<style>
    body * {
        box-sizing: border-box;
        color:white;
    }

    .test {
        margin: 0 auto;
        position: relative;
        height: 65vh;
        width: 80vw;
        display: flex;
    }
    .testA {
    background-image: url(https://placehold.it/500x300);
    width: 61.5%;
  }

    .testB {
        background-color: green;
        flex-grow:1;
        line-height: 32vh;
        text-align: right;
        position: relative;
    }

    .testC {
            background-image: url(https://placehold.it/500x300);
margin-bottom: 6  px;
background-size: cover;
    }

    .testD {
            background-image: url(https://placehold.it/500x300);
margin-bottom: 10px;
background-size: cover;
    }

    .test img {
        vertical-align: middle;
    }

    .testA img {
        height: 100%;
    }

    .testC img {
        vertical-align: top;
        height: 32vh;
    }

    .testD img {
        vertical-align: bottom;
        height: 32vh;
    }

</style>

<div class="test">
    <div class="testA">
        a
    </div>
    <div class="testB">
        <div class="testC">
            c
        </div>
        <div class="testD">
            d
        </div>
    </div>
</div>
like image 41
MattOpen Avatar answered Oct 21 '22 15:10

MattOpen