Ways to adjust image height to fit within a container while preserving its original aspect ratio

Does anyone know how to make an image in a two-column section fill its container without becoming distorted? I've tried using object fit and adjusting max widths, but nothing seems to work. Any advice would be greatly appreciated. Thank you.

 #section-3{
      display: flex;
     }

    @media only screen and (max-width: 1244px) {
    #section-3{
    flex-direction:column;
    }
    }

    .glasses-wrap{
    width:50%;
    }
    @media only screen and (max-width: 1244px) {
    .glasses-wrap{
    width:100%;
    }
    }



    .glasses-wrap img{
    width: 100%;
    max-width:100%;
    display: block;
    height:auto;
    }


    .text-wrap{
    background-color:#191919;
    width:50%;
    color:white;
    padding-left:100px;
    padding-right:100px;
    padding-top:100px;
    padding-bottom: 100px;
    display:flex;
    flex-direction: column;
    }

    @media only screen and (max-width: 1244px) {
    .text-wrap{
    width:100%;
    }
    }

    .text-wrap h1{
    font-size:2.5em;
    margin-bottom:45px;
    font-family: 'Source Serif Pro', serif;
    font-weight:500;
    }


    .text-wrap p{
    column-count: 2;
    color:#7E7E7E;
    font-family:'Montserrat', sans-serif;
    column-gap: 40px;
    }

    .text-wrap p:nth-child(1){
    margin-bottom: 20px;
    font-weight:600;
    font-size: 0.8em;
    }

    .text-wrap p:nth-child(3){
    font-weight:500;
    font-size:14px;
    line-height: 2;
    }


    @media only screen and (max-width: 1342px) {
    .text-wrap p {
    column-count: 1;
    }
    }
        <section id="section-3">

         <div class="glasses-wrap">
          <img src="public/images/glasses.jpg">
         </div>



         <div class="text-wrap">
          <p> GOURMET FOOD</p>
          <h1>A Dining Experience</h1>
          <p>Praesent fermentum ligula in dui imperdiet, vel tempus nulla ultricies. Phasellus at 
          commodo 
          ligula. Nullam molestie volutpat sapien, a dignissim tortor laoreet quis. Class aptent taciti 
          sociosqu ad litora torquent per conubia nostra. Phasellus at commodo ligula. Nullam molestie 
          volutpat 
          sapien, a dignissim tortor per inceptos himenaeos laoreet quis. Class aptent taciti soci osqu 
          ad 
          litora torquent per conubia nostra, per inceptos himenaeos.</p>
          </div>

        </section>

Answer №1

Have you ever considered using the image as a background image in this way?

.sunglasses-holder{
  width:50%;
  background-image: url("https://images.unsplash.com/photo-1498440844592-7528421f9781");
  background-size: cover;
  background-position: center;
}

https://codepen.io/johndoe/pen/AbCdEfG

Answer №2

Don't forget to utilize the code snippet (object-fit: contain;)

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <title>Demo</title>
  <style type="text/css">
    body {
      margin: 0;
    }
    
    #section-3 {
      display: flex;
    }
    
    @media only screen and (max-width: 1244px) {
      #section-3 {
        flex-direction: column;
      }
    }
    
    .glasses-wrap {
      width: 50%;
    }
    
    @media only screen and (max-width: 1244px) {
      .glasses-wrap {
        width: 100%;
      }
    }
    
    .glasses-wrap img {
      width: 100%;
      max-width: 100%;
      display: block;
      height: auto;
      background-position: top center;
      background-size: cover;
      object-fit: contain;
    }
    
    .text-wrap {
      background-color: #191919;
      width: 50%;
      color: white;
      padding-left: 100px;
      padding-right: 100px;
      padding-top: 100px;
      padding-bottom: 100px;
      display: flex;
      flex-direction: column;
    }
    
    @media only screen and (max-width: 1244px) {
      .text-wrap {
        width: 100%;
      }
    }
    
    .text-wrap h1 {
      font-size: 2.5em;
      margin-bottom: 45px;
      font-family: 'Source Serif Pro', serif;
      font-weight: 500;
    }
    
    .text-wrap p {
      column-count: 2;
      color: #7E7E7E;
      font-family: 'Montserrat', sans-serif;
      column-gap: 40px;
    }
    
    .text-wrap p:nth-child(1) {
      margin-bottom: 20px;
      font-weight: 600;
      font-size: 0.8em;
    }
    
    .text-wrap p:nth-child(3) {
      font-weight: 500;
      font-size: 14px;
      line-height: 2;
    }
    
    @media only screen and (max-width: 1342px) {
      .text-wrap p {
        column-count: 1;
      }
    }
  </style>
</head>

<body>
  <section id="section-3">
    <div class="glasses-wrap">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSAtvFcFI8b-wn7jGSsslqDJfUmZjCv4RaZxnFBrsaPWndmlCSd&s">
    </div>
    <div class="text-wrap">
      <p> GOURMET FOOD</p>
      <h1>A Dining Experience</h1>
      <p>Praesent fermentum ligula in dui imperdiet, vel tempus nulla ultricies. Phasellus at commodo ligula. Nullam molestie volutpat sapien, a dignissim tortor laoreet quis. Class aptent taciti sociosqu ad litora torquent per conubia nostra. Phasellus
        at commodo ligula. Nullam molestie vol
    
    </div>
  </section>
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>

</html>

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

The hovering effect on the image link is causing the surrounding div to stretch too far

Whenever I hover over specific points below an image link wrapped in a div, it triggers the hovering effect for the CSS-created image link. Despite trying various solutions like overflow:hidden and display:inline-block, nothing seems to resolve this issue. ...

Troubleshooting a problem with scrolling functionality while keeping the header in place and ensuring the correct tab is highlighted

I've successfully created a page with a fixed menu that appears when scrolling, and the tabs activate based on their corresponding section IDs. However, I'm encountering some issues: The scrolling behavior is not precise; when scrolling to sec ...

Nested div elements maintain background-image continuity

Is it possible to maintain the background image of the body within a child div that has a red background color with opacity? * { box-sizing: border-box; } body { background-image: url('https://d6scj24zvfbbo.cloudfront.net/306f4bc782c04bbe4939f8c ...

Tips for keeping my wordpress menu at the forefront

This piece of code is responsible for controlling the main menu on my website. Currently, it's set up so that when I scroll down, the menu disappears, and when scrolling up, it reappears. However, I would like the menu to always remain displayed at t ...

Customizing Ngx-bootstrap Carousel Indicator, Previous, and Next Button Styles

<carousel > <a href=""> <slide *ngFor="let slide of slides"> <img src="{{slide.imgUrl}}" alt="" style="display: block; width: 100%;"> </slide> 1. Is there a way to substitute the indicators with images ...

Searching for a column fails to yield any results after altering the value in

Here is a snippet of code that I am working with: <!DOCTYPE HTML> <html lang="en"> <head> <title> Exact matches overview </title> <script type="text/javascript" src="/static/s ...

What are the steps to installing and utilizing the Chart.js package on your local machine?

I thought installing chart.js on my Raspberry Pi would be a simple task, but I seem to be struggling with it. Due to the nature of my project, I need to have it installed locally rather than relying on an online version. Following the usual steps, I navig ...

Emphasize Expandable Sections based on Search Term

I have been working on developing an HTML page for my company that will showcase a list of contacts in an "experts list". Currently, the list is structured using collapsible DIVs nested within each other. Additionally, the HTML page features a search func ...

How can one gain access to a specifically generated element?

In my task of dynamically generating multiple elements, I am facing an issue related to accessing specific ones. To illustrate this problem as simply as possible, I have provided the following code: $(document).ready(function() { var num1 = 0; v ...

Design unique bar graphs to display on your website

I'm working on developing a webpage that will display dynamic data in a visually appealing bar graph. The design and aesthetics of the graph are crucial in this project. You can view the desired design of the bar graph by clicking on this link to the ...

Despite using "border-box" for the box-sizing property, the table cells are

Padding needs to be added to the cells of a table, but this is causing overflow issues with text and inputs inside the table. Despite attempting to set box-sizing: border-box, the problem persists. An example below demonstrates the issue: * { box-siz ...

Difficulty with navigation buttons on multiple Bootstrap carousels implemented using ngFor in a single page

Currently, I'm engaged in developing a practice eCommerce platform where multiple product cards are showcased using data from a sample JSON file. Additionally, several Bootstrap carousels are integrated into the website to display images of each item. ...

Unable to choose an option from the panel

I am currently working on a menu and panel setup (you can view the demo here: http://jsfiddle.net/vals/j2LrQ/1/). The visibility of the panel depends on the selected item from the menu. However, I am facing an issue where the panel disappears when I move ...

Creating an HTML table with collapsed borders and hidden rows/columns using the `border-collapse

I am facing a situation where I have a table with multiple lines as shown below: <table> <tr id="line1"><td>Line</td><td>1</td></tr> <tr id="line2"><td>Line</td><td>2</td></t ...

The CSS does not display properly on iPad or mobile devices

Recently, I had the opportunity to design a website for a local music school. This project was a great learning experience that has shown me the importance of continuous improvement and practice. Check out the site here Initially, I wanted to make the w ...

Troubleshooting problem with RDCOMClient related to UTF-8 encoding

Creating emails with R using the RDCOMClient package has been a challenge for me. The emails contain HTML text encoded in UTF-8 along with images. However, I've encountered an issue where special characters do not display correctly after saving the em ...

images not loading correctly in unique WordPress theme design

I am currently working on developing a unique custom WordPress theme. Within my Theme folder, I have the following files and folders: header.php index.php footer.php style.css /images picture-1.jpg One issue I am encountering is that I am unable to prop ...

Tips for verifying the status of an input element following the dynamic addition of a required element

I have implemented a required field for all input elements in a specific state using the following code: for (var i = 0; i < data.length; i++) { // activate required function v = data[i].Required; if (v) document.ge ...

Unable to get Bootstrap 4 overflow hidden to function properly within card layouts during animated transitions

In my project, I have an image inside a card layout and I am working on implementing a zoom effect on mouseover. However, I am facing an issue where during the animation, the image overflows from its parent wrapper. After the animation is completed, the ov ...

Use jQuery to generate and add several elements to the DOM

Hey there! Currently, I'm working on a real-time chat application using socket io. My goal is to display the user's username and message in a unique way by encapsulating the username in a strong tag. I've made some attempts: $('<div ...