Having difficulty resizing the image to fit the container correctly

I am struggling to adjust the size of an image within a container. In my setup, there are two columns - the left column is fixed at 280px and the right column expands to fill the remaining space. The image is placed in the right column, but its dimensions are unknown beforehand (in this case, it is 947px).

Everything looks fine when the window is large enough. However, as the window size decreases, the image's dimensions cause the right column to drop below the left column. Eventually, the image will scale down to fit the body once the window becomes small enough.

<!DOCTYPE html>
<head>
  <style type='text/css'>
    @media only screen and ( min-width: 641px ) { /* 641 or greater */
      img { max-width: 100%; height: auto; display: block; }
      .leftcol  { width: 280px; float: left; outline: 5px dotted green; } 
      .rightcol {               float: left; outline: 5px dotted red;   }
    }
  </style>
</head>

<body>
  <div class="leftcol">
    Home
  </div>
  <div class="rightcol">
    <img src="https://glsmyth.files.wordpress.com/2014/07/a-space-in-time.jpg" alt="Homepage image" />
  </div>
</body>
</html>

Answer №1

If you're looking to achieve a particular layout, Flexbox could be the perfect solution. By making some adjustments as outlined below, you can ensure that it functions exactly as intended.

<!DOCTYPE html>
<html>
<head>
    <style type='text/css'>
        @media only screen and ( min-width: 641px) {
            body {
                display: flex;
                align-items: flex-start;
            }
            img {
                max-width: 100%;
            }
            .leftcol {
                width: 280px;
                min-width: 280px;
                outline: 5px dotted green;
            }
            .rightcol {
                outline: 5px dotted red;
            }
        }
    </style>
</head>

<body>
    <div class="leftcol">
        Home
    </div>
    <div class="rightcol">
        <img src="https://glsmyth.files.wordpress.com/2014/07/a-space-in-time.jpg" alt="Homepage image" />
    </div>
</body>
</html>

Avoid using float, ensure that min-width is specified for the .leftcol element, and restrict the image to have only a max-width.

Answer №2

Here are a couple of key points to consider:

  1. The Float property may not calculate remaining space accurately, so using calc(100% - 280px) for your right column ensures it always takes up the correct width relative to the other column.

  2. Make sure to set width: 100% on your image as well since max-width is based on the image's actual size.

@media only screen and ( min-width: 400px) {
  /* 641 or greater */
  img {
    max-width: 100%;
    height: auto;
    display: block;
    width: 100%;
  }
  .leftcol {
    width: 280px;
    float: left;
    outline: 5px dotted green;
  }
  .rightcol {
    float: left;
    outline: 5px dotted red;
    width: calc(100% - 280px)
  }
}
<div class="leftcol">
  Home
</div>
<div class="rightcol">
  <img src="https://glsmyth.files.wordpress.com/2014/07/a-space-in-time.jpg" alt="Homepage image" />
</div>

Switching to use flex can be a more optimal solution in this case.

@media only screen and ( min-width: 400px) {
  /* 641 or greater */
  .flex {
    display: flex;
    align-items: flex-start;
  }
  img {
    max-width: 100%;
    height: auto;
    display: block;
    width: 100%;
  }
  .leftcol {
    width: 280px;
    outline: 5px dotted green;
  }
  .rightcol {
    outline: 5px dotted red;
    flex: 1 1 100%;
  }
}
<div class="flex">
  <div class="leftcol">
    Home
  </div>
  <div class="rightcol">
    <img src="https://glsmyth.files.wordpress.com/2014/07/a-space-in-time.jpg" alt="Homepage image" />
  </div>
</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

Parallax Effect Slows Down When Scrolling In Web Page

Currently in the process of creating a website with a scrolling parallax effect using Stellar.js on the header and three other sections. However, I'm experiencing lag when scrolling, especially at the top of the page. I've attempted to reduce la ...

Embed full content in iframe using bootstrap 4

Embedding an iframe of an appointment scheduling frontend on my page has been a challenge. While the width is correct, the height of the frame is too small. Despite attempting various CSS modifications, I have not been able to achieve the desired result. I ...

The React MUI v5 Card's background features a subtle white hue with a 5% opacity, leaving me curious about its origin

I've noticed a strange styling issue in my code. It seems to be related to a class called .css-18bsee0-MuiPaper-root-MuiCard-root, possibly from MUI's Card or MUI's Paper components. However, I can't find it in my own code. While tryin ...

Display a unique and unconventional image as a dynamic full-screen background, ensuring the entire image is visible using React/RedwoodJS

Looking for a somewhat unusual solution here - I want to use an image as a background with all edges of the picture visible at all times. Typically, I would use the "cover" setting for this, but that doesn't achieve what I need (image included). After ...

Mastering the utilization of React props within a Tailwind component

Looking to implement a straightforward button component in a Next App using Tailwind CSS, where I can pass values such as background color, text color, etc. through props and use them to render different types of buttons within my application. Take a look ...

Using the columns property in CSS3 along with background-color

My ul-element has multiple lis with a background color assigned to it. I am using the CSS3 columns property and everything is working well, but the background color of my ul is only visible in the first column. What could be causing this issue? Thank you ...

Ensuring that the contents hidden by overflow:hidden remain truly concealed

When applying overflow: hidden; to a div, I have encountered situations where keyboard actions can still cause the div to scroll, even without visible scrollbars. This makes it difficult to return the div to its original state. Are there any additional ste ...

What causes a fixed position div to extend beyond the boundaries of the main container?

Below is the CSS for the fixed div: .top-container { position: fixed; width: 100%; z-index: 999; } Upon zooming out the screen, the div causes the main container to break on the right side while the left side remains unchanged. Refer to the screenshot be ...

Attempting to increase the size of the menu text upon hover action

It seems like a basic mistake on my part, but I just can't seem to make it work. What I'm trying to achieve is that when you hover over a specific item in the menu, the text within that item should increase in size. However, currently it only en ...

"Hovering over one div does not trigger the display of another div

I seem to have forgotten my CSS skills, but I am facing an issue. Here is the code snippet: <div class="footer_container"> <div class="website_logo_to_footerexpand"></div> <div class="info_cont"> <div class ...

Utilizing CSS Grid to create a modern layout design featuring a fullwidth header and footer,

I have a traditional website layout with a header, main content area with a sidebar, and a footer. I want the header and footer to span the entire width on wider screens, while the main content and sidebar are fixed width, surrounded by whitespace. When th ...

What is the best way to position divs on opposite ends around another div?

I have experimented with this scenario so far, http://jsfiddle.net/BXSJe/4/ My goal is to position two divs on the left and right sides within another div, using float:left and float:right resulted in them appearing on separate lines. What I envision is ...

What is the best way to center a SVG and a span vertically within an inline unordered list?

My <ul> element contains 2 <li> items with styling using display: inline-block. Unfortunately, the SVG and text inside each <li> are not vertically aligned. I attempted to use vertically-align: middle since they're both inline-block ...

Can you provide guidance on configuring the image class within a DOM element using echo?

What is the method to set the class attribute of an img element using echo function? <div class="allnis" style="padding: 15px; text-transform: uparcase;"> <?php foreach($tv_id->networks as $prod){ echo "<img val ...

Contrasting text-transform: uppercase; and the use of all capital letters

Could you explain the distinction between <span style="text-transform: uppercase;">some text</span> and <span>SOME TEXT</span> They appear identical visually, but I've come across a suggestion that it's preferable to k ...

Ways to modify the color of a chosen item in a Xul listbox?

Is there a way to modify the color of the selected item in a Xul listbox? I attempted this code snippet: listitem:focus { background-color: red; color: red; } However, it does not seem to be working. I've searched through CSS and xul docume ...

What is the purpose of the tabindex in the MUI Modal component?

Struggling with integrating a modal into my project - it's refusing to close and taking up the entire screen height. On inspection, I found this troublesome code: [tabindex]: outline: none; height: 100% How can I remove height: 100% from the ...

I can't get Toggleclass to switch classes properly, am I missing something?

I have been experimenting with creating a button that toggles a class and reveals another div below it. Feel free to check out the example on jsFiddle that I created for you. While the first function of showing and hiding the div is working perfectly, I ...

"(PHP)WordPress produces a dynamic menu that adapts to different screen sizes

I recently created a menu on my WordPress site [ and I'm facing an issue with making it display vertically in a responsive way. The code in header.php is as follows: <!-- header --> <header> <div class="overNavPanel"> ...

The animation of the background color over an image appears glitchy when viewed on Safari

I've been attempting to create an animation that changes the background color over an image. Everything seems to be working fine on all browsers and operating systems except for Safari on macOS. I've searched extensively on SO and Google but have ...