htmlcssresponsive-designcss-floatclearfix

Responsive Media Card Image on left, text on right


Here's the my current code in jsfiddle:

<div class="card-container">
  <div class="float-layout">
    <div class="card-image">
      <img src="https://images.pexels.com/photos/534151/pexels-photo-534151.jpeg?auto=compress&cs=tinysrgb&dpr=3&h=750&w=1260">
      <div class="card">
        <div class="card-title">Title</div>
        <div class="card-desc">
          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce ullamcorper
          mollis tempus. Mauris eu maximus lectus, eu auctor justo. Aenean porta purus
          vel commodo consequat.
        </div>
      </div>
    </div>
  </div>
</div>
.float-layout {
  padding: 5px 5px;
  float: left;
  width: 100%;
  height: auto;
  box-sizing: border-box;
  margin: 0;
}

.card-container {
  overflow: hidden;
}

.card {
  background-color: dodgerblue;
  color: black;
  height: 100%;
  width: 50%;
  float: right;
}

.card-title {
  font-size: 30px;
  text-align: center;
  font-weight: bold;
  padding-top: 20px;
}

.card-desc {
  padding: 10px;
  text-align: left;
  font-size: 18px;
}

div.card-image img {
  width: 50%;
  height: auto;
}

/* Phone Devices Query */
@media only screen and (max-width: 37.5em) {
  div.card-image img {
    width: 100%;
    height: auto;
  }

  .card {
    width: 100%;
    margin-top: -4px;
  }
}

I am not sure if I need the clearfix hack to solve this, but when I adjust the browser window, the right side of my media card (contains title and description) does not properly scale with the left side where my image is set. I would like for both of them to responsively scale at equal height and size.

To get a better idea, my current code looks like this: https://i.sstatic.net/xebaE.png

I want it to scale at equal height, responsively like this as I adjust the browser window to any size: https://i.sstatic.net/IkXok.png


Solution

  • Was it necessary?

    .float-layout {
      padding: 5px 5px;
      float: left;
      width: 100%;
      height: auto;
      box-sizing: border-box;
      margin: 0;
    }
    
    .card-container {
      overflow: hidden;
    }
    
    .card {
      background-color: dodgerblue;
      color: black;
      min-height: 100%; /*replace this it in width: 100%*/
      width: 50%;
      float: right;
    }
    
    .card-title {
      font-size: 30px;
      text-align: center;
      font-weight: bold;
      padding-top: 20px;
    }
    
    .card-desc {
      padding: 10px;
      text-align: left;
      font-size: 18px;
    }
    
    /*add this it*/
    .card-image {
      display: flex;
    }
    /*-------------*/
    
    div.card-image img {
      width: 50%;
      height: auto;
    }
    
    /* Phone Devices Query */
    @media only screen and (max-width: 37.5em) {
      div.card-image img {
        width: 100%;
        height: auto;
      }
      
      /*add this it*/
      .card-image {
         flex-direction: column;
      }
      /*----------------------*/
    
      .card {
        width: 100%;
        margin-top: -4px;
      }
    }
    <div class="card-container">
      <div class="float-layout">
        <div class="card-image">
          <img src="https://images.pexels.com/photos/534151/pexels-photo-534151.jpeg?auto=compress&cs=tinysrgb&dpr=3&h=750&w=1260">
          <div class="card">
            <div class="card-title">Title</div>
            <div class="card-desc">
              Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce ullamcorper
              mollis tempus. Mauris eu maximus lectus, eu auctor justo. Aenean porta purus
              vel commodo consequat.
            </div>
          </div>
        </div>
      </div>
    </div>