What is the best way to ensure an image is responsive in CSS and aligns perfectly in height with its neighboring text?

There is an image within the <img> tag and some text inside the <p> tag, both enclosed in a <div>.

Although the image is responsive and resizes proportionally when the browser window changes size, there is an issue where as the text wraps onto multiple lines below, the height of the image decreases in relation to the adjacent text. This affects the design of my site negatively. How can I ensure that the image maintains equal height with the neighboring text when the browser window is resized.

It's important to mention that when the browser window size is less than 768px, both the image and text block expand to 100% width and stack on top of each other.

To better understand the problem, please refer to the screenshots provided below. The first image shows the gap below the image - this is the issue that needs to be resolved. I want the image to stretch all the way down to the baseline of the last line of text while maintaining its aspect ratio.

Current Problem:

Desired Result:

EDIT

Please note that adding HTML width and height attributes to the <img> tag does not impact the image dimensions if CSS rules for image dimensions are specified.

In situations where content is generated from a WYSIWYG editor, it may not be feasible or practical to remove these HTML attributes manually. Especially if I am not the one creating the content.

* {
  -webkit-box-sizing:border-box;
  -moz-box-sizing:border-box;
  box-sizing:border-box;
  margin:0;
}

img {
  max-width:100%;
  height:auto;
}

.container {
  max-width:1200px;
  border:1px solid #eeeeee;
  margin:0 auto;
}

.col {
  width:100%;
  float:left;
  padding:15px;
}

.container {
  content:"";
  display:table;
  clear:both;
}

@media only screen and (min-width: 768px) {
  .left {
    width:25%;
  }

  .right {
    width:75%;
  }
}
<div class="container">
<div class="col left">
    <img src="images/nature.jpg" width="640" height="480" />
  </div>
<div class="col right">
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque nec ligula ut tortor convallis ultricies sed vel leo. Nullam congue ligula sed lectus viverra rhoncus. Nullam tincidunt nibh sed ligula maximus faucibus. Nunc eu mauris purus. Vestibulum et nisl ut massa finibus laoreet in sit amet tortor. Suspendisse non leo quam. Ut consectetur dictum nibh vel vehicula. Vestibulum lobortis purus quam, id mattis purus sollicitudin volutpat. Maecenas ut libero in nisi egestas ornare nec ac lectus. Nullam urna nisi, rhoncus vitae viverra mattis, imperdiet nec velit. Aenean libero elit, luctus id maximus at, congue eget tortor. Nunc ultricies, augue eget facilisis luctus, nisl nibh aliquet libero, id ullamcorper ligula leo auctor ex.</p>
  </div>
</div>

Answer №1

Check out this concept using flexbox, width, and object-fit:

.container {
  max-width: 1200px;
  border: 1px solid #eeeeee;
  margin: 0 auto;
}

.left img {
  max-width: 100%;
}

@media only screen and (min-width: 768px) {
  .container {
    display: flex;
  }
  .left {
    position: relative;
    flex: 0 0 30%;
  }
  .left img {
    position: absolute;    /* take it of the flow, so it doesn't stretch the parent if    col right is shorter */
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
  .right {
    flex: 0 0 75%;
  }
}
<div class="container">
  <div class="col left">
    <img src="https://picsum.photos/id/1041/640/480" width="640" height="480" />
  </div>
  <div class="col right">
    <h1>Please run in fullpage mode, then resized</h1>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque nec ligula ut tortor convallis ultricies sed vel leo. Nullam congue ligula sed lectus viverra rhoncus. Nullam tincidunt nibh sed ligula maximus faucibus. Nunc eu mauris purus. Vestibulum
      et nisl ut massa finibus laoreet in sit amet tortor. Suspendisse non leo quam. Ut consectetur dictum nibh vel vehicula. Vestibulum lobortis purus quam, id mattis purus sollicitudin volutpat. Maecenas ut libero in nisi egestas ornare nec ac lectus.
      Nullam urna nisi, rhoncus vitae viverra mattis, imperdiet nec velit. Aenean libero elit, luctus id maximus at, congue eget tortor. Nunc ultricies, augue eget facilisis luctus, nisl nibh aliquet libero, id ullamcorper ligula leo auctor ex.</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque nec ligula ut tortor convallis ultricies sed vel leo. Nullam congue ligula sed lectus viverra rhoncus. Nullam tincidunt nibh sed ligula maximus faucibus. Nunc eu mauris purus. Vestibulum
      et nisl ut massa finibus laoreet in sit amet tortor. Suspendisse non leo quam. Ut consectetur dictum nibh vel vehicula. Vestibulum lobortis purus quam, id mattis purus sollicitudin volutpat.</p>
  </div>
</div>--

Here's the table version with display:table:

.container {
  max-width: 1200px;
  border: 1px solid #eeeeee;
  margin: 0 auto;
}

.left img {
  max-width: 100%;
}

@media only screen and (min-width: 768px) {
  .container {
    display: table;
    width: 100%;
    table-)layout: fixed;
  }
  .left {
    display: table-cell;
    width: 25%;
    position: relative;
    flex: 0 0 30%;
  }
  .left img {
    position: absolute;
    /* take it of the flow, so it doesn't stretch the parent if    col right is shorter */
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }
  .right {
    padding-left: 1em;
  }
}
<div class="container">
  <div class="col left">
    <img src="https://picsum.photos/id/1041/640/480" width="640" height="480" />
  </div>
  <div class="col right">
    <h1>PLZ, run in fullpage mode , then resized</h1>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque nec ligula ut tortor convallis ultricies sed vel leo. Nullam congue ligula sed lectus viverra rhoncus. Nullam tincidunt nibh sed ligula maximus faucibus. Nunc eu mauris purus. Vestibulum
      et nisl ut massa finibus laoreet in sit amet tortor. Suspendisse non leo quam. Ut consectetur dictum nibh vel vehicula. Vestibulum lobortis purus quam, id mattis purus sollicitudin volutpat. Maecenas ut libero in nisi egestas ornare nec ac lectus.
      Nullam urna nisi, rhoncus vitae viverra mattis, imperdiet nec velit. Aenean libero elit, luctus id maximus at, congue eget tortor. Nunc ultricies, augue eget facilisis luctus, nisl nibh aliquet libero, id ullamcorper ligula leo auctor ex.</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque nec ligula ut tortor convallis ultricies sed vel leo. Nullam congue ligula sed lectus viverra rhoncus. Nullam tincidunt nibh sed ligula maximus faucibus. Nunc eu mauris purus. Vestibulum
      et nisl ut massa finibus laoreet in sit amet tortor. Suspendisse non leo quam. Ut consectetur dictum nibh vel vehicula. Vestibulum lobortis purus quam, id mattis purus sollicitudin volutpat.</p>
  </div>
</div>--

Answer №2

To ensure the code functions properly, delete the height and width attributes from the HTML file.

For the image tag's CSS attribute height, set it to 100% instead of auto.

Replace:

img {
  max-width: 100%;
  height: auto
}

with:

img {
  max-width: 100%;
  height: 100%;
}

This modification should resolve any issues.

Answer №3

After reviewing the issue, I have modified the HTML to address it. Below is a revised example, and if it does not meet your requirements, please inform me:

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  margin: 0;
}

img {
  max-width: 100%;
  height: auto
}

.container {
  max-width: 1200px;
  border: 1px solid #eeeeee;
  margin: 0 auto;
}

.row {
  display: flex;
}

.col {
  width: 100%;
  float: left;
  padding: 15px;
}

.container {
  content: "";
  display: table;
  clear: both;
}

.h-100 {
  height: 100%;
}

@media only screen and (min-width: 768px) {
  .left {
    width: 25%;
  }
  .right {
    width: 75%;
  }
}
<div class="container">
  <div class="row">
    <div class="inner-wrapper col left">
      <div class="h-100 row">
        <img src="https://picsum.photos/1200" />
      </div>
    </div>
    <div class="inner-wrapper col right">
      <div class="row">
        <p class="">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque nec ligula ut tortor convallis ultricies sed vel leo...ex.</p>
      </div>
    </div>
  </div>
</div>

If this solution does not work for you, please reach out for further assistance.

Answer №4

Tip #1 : Set the height to 100%

Tip #2 : Apply display: flex; to the parent container of the left and right columns

For example :

<div class="container" style="display:flex;">
  <div class="left col">
    <img src="myimage" style="height:100%" />
  </div>
  <div class="right col">...my content..</div>
</div>

Remember to set the height of <img> to 100% if it is within another div

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to incorporate text below grid columns in html?

Can you show me how to insert text below a column using the grid system? Here is what I am trying to accomplish Here is the code I have created: .grid{ display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; grid-auto-rows: 240px 240px; row-g ...

Populate User Interface Popover - Placement on Compact Display

I have been grappling with finding a solution to position the material ui popover beneath my anchor consistently, even on smaller screens. Take a look at this sandbox example. The current setup is working alright, but the issue is that the scroll is cont ...

Determine if the HTML Application Cache is void

Our website uses a "manifest.appcache" file to manage the application cache. Some pages are designed to be accessed offline, so they have references in the html tag to the manifest, while others do not. Is there a way to determine if the cache is empty ac ...

Utilizing CSS styling to create page breaks in R Markdown disrupts the flow of table of contents

Encountering difficulties in achieving a visually pleasing document using knitr's export to pdf, I have resorted to converting the html file for my Markdown project to pdf through the wkhtmltopdf command line utility tool. A simplified Markdown docum ...

Alter appearance using various classes

I am seeking assistance in changing the font of text using classes. I have multiple texts with different classes and I want to be able to edit all the texts without adding another dropdown menu. I believe that the change needs to occur in a script. Pleas ...

Enable only a single radio button to be selected in HTML

I am facing an issue with my radio buttons where I only want one to be selected at a time. Despite giving them the same name, all four can still be selected simultaneously. <h1>Portion</h1> <input type="radio" name="portion_n ...

Reordering sections in a dynamic manner

I'm working on a single-page website with four sections arranged like this: <section id=“4”> <section id=“3”> <section id=“2”> <section id=“1”> Now, I want to change the order of these sections when scrolling ...

:host-selector for Angular Material dialog

I am currently working with a dialog component provided by angular-material and I need to customize the appearance of the popup dialog. I am aware that there is some support for styling through the component generation: let dialogRef = dialog.open(MyDi ...

Creating a hover effect for displaying image masks

My website features a profile page displaying the user's photo. I am attempting to create a functionality where when the user hovers over their photo, a transparent mask with text appears allowing them to update their profile picture via a modal. How ...

Guide to smoothly scroll to the top of an element containing collapsible panels using Jquery

I've encountered an issue with a series of divs that are set to toggle the state of a collapsible div upon clicking (similar to an accordion widget). The functionality works as intended, but I'm facing a challenge - I want the page to scroll to t ...

Error with infiniteCarousel in Internet Explorer versions 7 and 8 when using jQuery

Hello everyone! I've run into a little issue with some jQuery code I'm using. It was working fine before, but after making several changes and improvements, I can't seem to figure out what the problem is. I keep getting JS errors in both IE7 ...

Maintain the scrollable feature of the element until the final content is reached

I'm struggling to come up with the right keywords to search for my issue. I've tried using Google, but it seems my search terms are not effective. The problem I'm facing involves two relative div elements with dynamic content. This means th ...

Retrieving InnerHTML of a Rendered DOM Element in AngularJS

Can I retrieve the innerHTML code of a rendered element that contains an ng-repeat loop? Here is an example: <div id="container"> <div ng-repeat="e in ctrl.elements>{{e.name}}</div> </div> ...

Iterate using jQuery through all child div elements

<div id="SelectedSection" class="selected"> <div class="sec_ch" name="7"> <div class="sec_ch" name="8"> <div class="sec_ch" name="9"> <div class="sec_ch" name="11"> <div class="clear"> </div> </di ...

Display the chosen alternative in a Bootstrap dropdown menu

I am currently facing an issue with the dropdown list in my bootstrap menu. <li class="dropdown"> <a aria-expanded="false" aria-haspopup="true" role="button" data-toggle="dropdown" class="dropdown-toggle" href="#">Chose option<span class="c ...

Tips for utilizing $.get information to update specific elements on a web page

I am currently utilizing jQuery 1.4.2 to smoothly navigate between similar web pages within Firefox 3. Upon clicking a link, a JavaScript script should load the new HTML, filter out the correct elements and replace them on the existing page. It appears th ...

Creating a unique bullet style using CSS

I've got a collection of student links that take users to their profiles, and I want to replace the usual circular bullet points with smiley faces. Each picture is 14x14 pixels in size. How can I use CSS to achieve this effect for the bullets? ul { ...

Utilizing jQuery to target a select element's two specific children

I am trying to target the parent element by matching two specific children elements. Here is my code: $('span:contains("11:00am"), span.name:contains("Tom")').parents("a").css("background-color","rgb(255, 255, 255)"); <script src="https://c ...

Align list items in the center horizontally regardless of the width

I have created this container element: export const Container = styled.section<ContainerProps>` display: flex; justify-content: center; `; The current setup is vertically centering three list items within the container. However, I am now looking ...

Strategies for decreasing padding or margin in Bootstrap without removing it

I am currently facing a dilemma with the grid system. Although it is supposed to be easy, I am struggling at the moment. Let's consider that I need to correctly implement the Bootstrap grid for this example: https://i.stack.imgur.com/33vse.png Initi ...