Tips on adjusting an image to fill the remaining height and setting its width

Exploring this particular question along with its chosen solution.

I have taken the code from the selected answer and made some adjustments in the code snippet below, replacing #div1 with .header and #div2 with .contents, then placing both divs within a container.

My requirement is simple: I want to include an image in the .contents div that occupies the same height as the .contents div itself (thus utilizing the remaining height in the container). The width of this image should be automatic, maintaining its aspect ratio.

The main issue at hand: how can I ensure that all divs have the exact same width as that of the computed image?

The following code accomplishes everything mentioned above, except for the fact that the divs do not match the image's width.

    .container {
      position: absolute;
      height: 60%; /* mandatory condition; height must be a percentage */
      width: 40%;  /* this line needs to be removed, as the width should match the image */
      border: 5px solid black;  
    }
    .header {
  width: 100%; /* the width should correspond to the image's width */
      height: 100px; 
      background: red;
    }
    .contents {
      width: 100%; /* the width should align with the image's width */
      position: absolute;
      top: 100px;
      bottom: 0;
      background: blue;
    }
    .contents img {
      height: 100%;
      width: auto; /* the width will be calculated based on the height while maintaining the aspect ratio */
    }
 <div class="container">
      <div class="header"></div>
      <div class="contents">
        <img src="http://via.placeholder.com/200x200" width="200" height="200" alt="" title="" />
      </div>
    </div>

If feasible, I would prefer a resolution devoid of javascript.

Answer №1

        
 html, body, .shell {
      height: 100%; // ensure all parent elements have a specified height
}

    .container {
  display: inline-block;
      height: 60%; /* must maintain this percentage */
      //width: 40%;  /* remove this line to match image width */
      border: 5px solid black;  
    }
    .header {
  width: 100%; /* set width to image width */
      height: 100px; 
      background: red;
    }
    .contents {
      height: calc(100% - 100px);
      top: 100px;
      bottom: 0;
      background: blue;
    }
    .contents img {
      height: 100%;
      width: auto; /* adjust width for aspect ratio */
    }
 <div class="shell">
   <div class="container">
        <div class="header"></div>
        <div class="contents">
        <img src="http://via.placeholder.com/200x200" width="200" height="200" alt="" title="" />
      </div>
   </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

Issue with JQuery .fadeToggle() function: Unexpected behavior causing automatic fade-out

$(document).on('click', '.tree label', function(e) { $(this).next('ul').fadeToggle(); e.stopPropagation(); }); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul cl ...

Horizontal JQuery Slider for CategoriesDiscover the smooth, sleek category navigation with

Looking to create a sliding horizontal menu with various categories using a clever div layer setup. The goal is to have one fixed-width div containing another wider div, with only a portion of the second one visible at a time. Encountering two challenges ...

Fluid and static containers in Bootstrap offer flexible and fixed-width layout

I am experimenting with combining fluid containers and fixed containers within a single page layout using Bootstrap. For example, I want to have a large image as a hero unit that spans 100% of the width and height of the viewport, or three column images e ...

Issues regarding innerHTML

I have a collapsed table with a link that collapses its content. My goal is to change the link (such as from "+" to "-" and vice versa) using JavaScript. I was able to achieve this, but now my table no longer collapses. Here is the code snippet: <div c ...

Tips for adjusting large resolution images to fit all screen sizes without exceeding the boundaries of the screen

I am in the process of creating a set of HTML pages specifically tailored for iPad Air and iPad Mini. These pages will feature several large images, with dimensions such as 1600x300. However, upon testing in Windows browsers, I encountered an issue where t ...

Navigating to an offline HTML webpage using JavaScript in a PhoneGap application

I am currently developing a phonegap application. I am attempting to create a login feature where upon clicking the submit button on the Login.html page, I should be directed to a local HTML file. Login.html <tr> <td>&nbsp;</td> ...

Interactive HTML Table in PHP Email

I am curious to know if it is possible to include a user-defined function in PHP Mail. $to = "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ceabb6afa3bea2ab8eabb6afa3bea2abe0ada1a3">[email protected]</a>"; $mess ...

The styling of the Material UI autocomplete listbox does not affect its appearance

I am working on an autocomplete feature that groups items by titles. I am trying to adjust the height of the first group to be different from the rest, but I am facing some challenges since there is no unique identifier for the ul component. To work around ...

Issues with html5 dropdown menu not displaying properly when hovered over

As a beginner in programming, I have been tasked with creating a website using HTML5 and CSS. The main issue I am encountering is with my horizontal menu bar that has vertical sub-menus appearing on hover. The menu consists of 3 main tabs: Home, Health, a ...

The CSS property :read-only is used on elements that do not have the readonly attribute

Could someone please clarify why the CSS pseudo class :read-only is being added to elements that are not actually readonly? To see an example, check out this link I have tested this in recent versions of Edge, Chrome, and Firefox. All of them apply the i ...

CSS Dialect Debate: Container or Wrapper - Which is the Best Term to

Can you explain the distinction between a container and a wrapper, as well as their respective meanings? ...

Utilizing AJAX to call an HTML file that incorporates JavaScript and CSS components

Currently, I am incorporating AJAX to fetch an HTML file that contains jQuery and JavaScript. Despite already including these scripts on the page, the JavaScript (and CSS) are not running as expected. Any suggestions on how to make my JavaScript function ...

Python struggles to find HTML elements when gathering information from a website through

I attempted to extract data from a website by utilizing the following code snippet: import requests requests.get("myurl.com").content Unfortunately, certain crucial components of the website were not retrieved. Is there a way for me to access th ...

Stop Chrome from highlighting the canvas section

One interesting feature of our whiteboard application is that Chrome users have the ability to highlight the entire canvas by dragging their mouse in a specific manner: Do you have any suggestions on how we can prevent this behavior? Thank you! ...

Which is quicker, generating a gradient using CSS or directly loading an image?

I am curious about the potential benefits of the new CSS3 styling techniques. However, I am unsure if the effort is worth it! (Question: Can jQuery be used to apply a vignetting effect to a webpage?) Appreciate any insights! ...

Enhance user experience with AngularJS Bootstrap autocomplete feature by automatically filling input box when hovering over dropdown options

I am currently implementing the AngularJs Bootstrap typeahead functionality. Typically, when we enter text that matches an option, the dropdown list appears. https://i.stack.imgur.com/395Ec.png What I aim for is to automatically fill the textbox with the ...

What could be causing my JSON product list to not load properly?

My list is not loading and I can't figure out why. I've included my json, jquery, and HTML below. The console isn't showing any errors, but the list is still blank. Any help would be greatly appreciated as I am new to working with json. Than ...

What is the best method for combining several variables into a single one using a parameter?

On the webpage, there is a table containing 11 checkboxes. I am looking to create a keyword that allows testers to input just 1, 2, or 3 to select a specific checkbox without needing multiple lines of element variables. Here are the elements: xpath=(//in ...

What is the process for setting up a "Highlight tab" feature in Next.Js?

I am currently working on implementing a selected tab feature in Next.Js. Users will have the ability to search for either Users or Posts by clicking on corresponding buttons. https://i.sstatic.net/mRBgQ.png Once the user clicks on a button, it should ch ...

What is the best way to centralize the storage of my website's header and footer?

Constantly updating headers and footers requires me to manually transfer the code to each web page every time, which is not the most efficient method. I'm considering using a "header" and "footer" div, and using jQuery's $(document).ready functio ...