Imitate the actions of images with {width: 100%; height : auto} properties

I am interested in creating a unique layout that consists of a stripe composed of images with varying widths and heights. These images need to be proportional, scaled at the same height, and collectively have a width equal to the parent element.

However, expressing this concept can be quite complex. I am wondering if it is possible for a block to mimic the behavior of an img element where setting a percentage width automatically calculates the height.

To better illustrate my goal, I have created a diagram which you can view here.

The objective is for the combined width of the images to be 100% of the parent's width while maintaining their proportionate scaling at the same height.

Various attempts have been made to achieve this using CSS properties, but so far, none have fully met the requirements. Here is what I have tried:

Attempt #1: Simple Solution

The issue with this approach is that the container height must be predefined.

.container {
        width: 100%;
        border: 1px solid black;
        height: 50px; /* I would like to say here auto */
      }

      .image-wrapper {
        white-space: nowrap;
        height: 100%;
        max-width: 100%;
        border: 1px dashed gray;
      }

      .image {
        height: 100%;
        width: auto;
      }
      

Attempt #2: Display Table Approach

This method presents cropping issues and does not dynamically adjust the container size based on its parent.

.container-wrapper {
        width: 40px;
        height: 50px;
      }

      .container {
        width: 100%;
        border: 1px solid black;
        display: table;
        height: 100%;
      }

      .image-wrapper {
        display: table-row;
        height: 100%;
        border: 1px dashed gray;
      }

      .item {
        display: table-cell;
        border: 1px solid blue;
        width: 100%;
        height: auto;
      }

      .image {
        height: 100%;
        width: auto;
      }
      

My preference is for an HTML/CSS solution without reliance on JavaScript. Any suggestions on how to approach this challenge?

Answer №1

Here's a neat little trick I just thought of: utilizing the auto-scaling feature of an image to scale the filmstrip container div, while keeping it hidden with opacity (in a real scenario, a transparent .png would be used). This method sets the height of the filmstrip in relation to its width. By adjusting the proportions of the .magic image, you can achieve different aspect ratios for your filmstrip.

The inner container is then absolutely positioned to inherit the size of the .magic image.

The individual images within are configured to occupy the full height of the filmstrip, each with varying widths. The image itself is specified using background-image, along with background-size: cover and background-position: center to fill the div.

.filmstrip {
    width: 100%;
    position: relative;
    
    /* Added for visual clarity */
    border: 1px solid red;
}
.magic {
    width: 100%;
    height: auto;
    display: block;
  
    /* Not meant to be visible; solely used for ratio */
    opacity: 0;
}
.contents {
    position: absolute;
    top:  0; bottom: 0;
    left: 0; right:  0;
}
.contents .image {
    height: 100%;
    background-size: cover;
    background-position: center;
    float: left;
    margin-right: 2%;
    
    /* Added for visual clarity */
    border: 1px solid blue;
    box-sizing: border-box;
}
.contents .wide {
    width: 30%;
}
.contents .narrow {
     width: 10%
}
<div class="filmstrip">
    <img class="magic" src="http://placehold.it/400x100" />
    <div class="contents">
        <div class="wide image" style="background-image: url('http://placehold.it/300x100');"></div>
        <div class="narrow image" style="background-image: url('http://placehold.it/300x100');"></div>
        <div class="wide image" style="background-image: url('http://placehold.it/300x100');"></div>
    </div>
</div>

For optimal performance, ensure browser support for Chrome 3+, Firefox 3.6+, IE 9+, Opera 10+, Safari 4.1+, primarily due to the use of background-cover.

Answer №2

Check out my answer on Stack Overflow question 33117027, where I provided tips on designing a unique filmstrip. Feel free to refer to the detailed Codepen example I included for inspiration. You can customize it to fit your needs easily...

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

Unlock the secrets of programming with the must-have guide to mastering variables

As a newcomer to the world of programming, I may be jumping ahead by asking this question too quickly. While reading a book, I came across something that confused me. Why is it necessary to create a variable for the password box element? Wouldn't the ...

How can I adjust two overlapping divs for proper display?

It seems like I have two divs overlapping on the same line. The layout appears fine on a PC but not on an iPad or mobile device. Can someone please review the code and point out any mistakes I might have made? The issue is with the content overlapping the ...

Expanding SVG Button Overlay in ChakraUI

I am trying to design a uniquely shaped button that sits on top of an image, but I am encountering some challenges with the scaling of the button mask. Both the image and masks were created at the same base dimensions for easy alignment at 0,0. Here is a ...

When it comes to choosing between CSS and Angular Animations for supporting animations on both browsers and NativeScript mobile applications, which approach is more effective?

Are angular animations my only option or can I also utilize css animations with native elements on mobile devices? How are css animations from an angular application translated to native mobile, and is this method less effective than angular animations? I ...

Should I use margin-top, padding-top, or top?

I often mix up padding-top, margin-top, and top. My issue arises when I need to create a space of 15px. Using margin-top: 15px seems to work, but I suspect it might be incorrect? .subtitles{ font-size:13px; font-family: "Open Sans","Helvetica Neue", ...

What is the method for turning off Bootstrap for viewport width below a specific threshold?

I'm currently utilizing bootstrap for my website design. However, there's a particular CSS rule causing some frustration on one specific page: @media (min-width: 576px) .col-sm-4 { -ms-flex: 0 0 33.333333%; flex: 0 0 33.333333%; max-w ...

Refrain from using inline JavaScript code within your

Here is the basic structure of my code: <a href="javascript:void(0);" onClick="viewServices(1)">Services</a> <a href="javascript:void(0);" onClick="viewServices(43)">Services</a> <a href="javascript:void(0);" onClick="viewServic ...

Having trouble with implementing ng-hide and ng-show functionality

I have been working on creating my very first Angular website and so far, everything has been going smoothly with the first page. However, I am facing an issue with the second page not appearing as expected when it meets the condition set with ng-show in t ...

Implement JQuery UI Accordion to enhance navigation on mobile devices

I am implementing the JQuery UI Accordion to expand content on a website. Below is the basic markup structure: <div class="accordion"> <h2>Heading</h2> <div>Content</div> <h2>Heading</h2> <div& ...

Limiting the scrolling capability within a flex container by using the nowrap

When using flex with nowrap, it appears that the horizontal scroll bar only allows moving to the right while the items on the left remain hidden. To demonstrate this issue, I have created a sample on CodePen: http://codepen.io/anon/pen/QpgvoZ This behavio ...

Are there any other methods of using HREF in an <asp:Button> besides OnClientClick for invoking an inline div?

I decided to create a concealed <div> on the same page and tried calling it with href="#", which worked perfectly. However, when I attempted to use the same code in ASP.net, I encountered some issues with Javascript or other factors that prevented it ...

The function window.scrollBy seems to be causing a conflict with jjmslideshow, resulting in the page being unable to

I wrote a simple script to create a "smooth scroll" effect when a specific link is clicked: (function() { 'use strict'; // Checking for compatibility if ( 'querySelector' in document && 'addEventListener' in window ...

Adjust the width of the initial column in the table and ensure it remains noticeable

I am a beginner with bootstrap and I find it extremely useful. I have encountered a problem with a table I am working on. I am trying to set the width of the first column and keep it always visible, but I am having trouble achieving this. I have successful ...

Utilize CSS to showcase the full-size version of a clicked thumbnail

I am working on a web page that features three thumbnails displayed on the side. When one of these thumbnails is clicked, the full-size image should appear in the center of the page with accompanying text below it. Additionally, I have already implemented ...

Troubleshooting a Hover Problem in Firefox

Chrome is rendering the following code perfectly fine, but Firefox seems to be having trouble applying the hover effect. HTML <!DOCTYPE html> <html> <head> <link type="text/css" rel="stylesheet" href="stylesheet.css"/> ...

Using Responsive Design with Bootstrap and Media Queries

As a beginner in front-end development, I have recently learned about media queries and their functionality. Having previously studied Bootstrap, I can't help but wonder if media queries are actually necessary. After all, Bootstrap seems to offer simi ...

Troubleshooting: EJ Syncfusion React Scheduler Issues

I have recently implemented a syncfusion calendar for managing appointments in my ReactJs project, following the code examples from this link . After editing the JS and CSS codes (available at ), I encountered an issue with the CSS not displaying correctl ...

The submission of FormData to the PHP server is causing an issue

I am having an issue with sending the formData to my PHP script using AJAX. Despite inspecting the elements, I can't find any errors in the process. Below is the structure of my form: The input values are sent to a JS file onclick event. <form c ...

Updating previous values in reactjs to a new state

I have a div set up to render multiple times based on data retrieved from the database. The background color of the div corresponds to the ID received from the backend. My goal is to change the background color of the currently selected div and remove the ...

What methods can be used to limit URL abbreviations on my website?

I need a simple solution that I have been struggling to figure out. What I want is for anyone attempting to access any webpage directly on my website to be automatically redirected to the home page. For instance, if someone tries to access www.domain-name ...