Issue with the parent element size when containing an image smaller than 100% width

This is the default appearance with all images set to 100% width:

image description here

These images must maintain their aspect ratio, so I can only use percentages for sizing.

When the image width is set to 60%, it looks like this:

image description here

The images have 60% width but the parent element still occupies 100% width.

How can I make the parent element match the width of the image?

<div class="casing">
    <div class="casing__item">
        <div class="item__icon"><img src="PK.png"></div>
        <div class="item__label"><span>PK</span></div>
    </div>  
    <div class="casing__item">
        <div class="item__icon"><img src="PK-PW2.png"></div>
        <div class="item__label"><span>PK+PW2</span></div>
    </div>
    <div class="casing__item">
        <div class="item__icon"><img src="PK2W-PW2.png"></div>
        <div class="item__label"><span>PK2W+PW2</span></div>
    </div>
</div>

I have tried the following:

.casing__item {
    display: flex;
    flex-wrap: wrap;
    gap: 0;
}
.casing__item > div {
    width: auto;
}
.casing__item img {
    width: 60%;
}
.casing__item {
    display: table;
}
.casing__item > div {
    display: table-cell;
    vertical-align: middle;
}
.casing__item img {
    width: 60%;
}

Answer №1

It appears that you are seeking to adjust the size of a row of images while maintaining their proportions and consistent height. To achieve this, set the height and allow the width to adapt automatically.

If I have misunderstood your request, please provide further clarification for better assistance.

.wrapper {
  padding: 1em;
  background: #f0f0f0;
  border-radius: 0.3em;
  margin-bottom: 1.5em;
  display: flex;
  gap: 0.5em;
}

.row1 img {
  height: 120px;
}

.row2 img {
  height: 60px;
}

.row3 img {
  height: 30vw;
}
<p>no specified image height ... images displayed at natural size</p>
<div class="wrapper row1">
    <div class="row__item">
        <div class="item__pic"><img src="https://example.com/100/200"></div>
        <div class="item__label"><span>Item A</span></div>
    </div>  
    <div class="row__item">
        <div class="item__pic"><img src="https://example.com/150/150"></div>
        <div class="item__label"><span>Item B</span></div>
    </div>
    <div class="row__item">
        <div class="item__pic"><img src="https://example.com/200/100"></div>
        <div class="item__label"><span>Item C</span></div>
    </div>
</div>

<p>image height specified as 120px</p>
<div class="wrapper row2">
    <div class="row__item">
        <div class="item__pic"><img src="https://example.com/100/200"></div>
        <div class="item__label"><span>Item A</span></div>
    </div>  
    <div class="row__item">
        <div class="item__pic"><img src="https://example.com/150/150"></div>
        <div class="item__label"><span>Item B</span></div>
    </div>
    <div class="row__item">
        <div class="item__pic"><img src="https://example.com/200/100"></div>
        <div class="item__label"><span>Item C</span></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

The website was designed to only allow scrolling on desktop devices, not on mobile devices

I noticed that the website is not scrolling properly on mobile devices, specifically when viewed on a Samsung Galaxy S4 Mini. Does anyone have suggestions on how to resolve this issue? ...

A guide on incorporating an unflattened SVG file into a React component

I am having trouble embedding an SVG file in a React component because it seems to appear flattened. Is there a way to display it as pure SVG? Please take a look at where it is posted: Also, this is how it's supposed to be viewed: This is the metho ...

Arranging the list items in a perfectly straight vertical line

Concern: I'm struggling with alignment issues on my website. I have country flags displayed using display:inline;. The issue arises when the number next to the flag exceeds single digits (e.g., 10 or more) as it affects the alignment due to the consi ...

Extracting information from secured HTML

I am trying to extract data using a vb.net form from but the retrieved code contains some unnecessary white space, hiding the table I'm interested in. <th width="93">Факт. время прибытия</th> ...

Blend Bootstrap 4 columns with alternating rows of content

My website, using bootstrap, has a unique layout consisting of two columns side by side on desktop. In each column, the content is statically stacked with div elements of varying heights: A | B A | D C | D E | F E Currently, I have set up this layout by ...

Mastering the Art of Modifying HTML with Node.js

Is it possible to manipulate HTML using Node.js? 1. First, I need to retrieve data from a database. 2. Then, I need to modify or add HTML code within Node. In essence, the goal is to fetch data and integrate it into an HTML file. The JavaScript snippet ...

Explore various SVG paths by hovering to reveal or conceal different divs

Hi there, I'll do my best to explain clearly. If this question has already been asked, I apologize as I couldn't find an answer when searching. I've used SVG to create a map on my HTML webpage and it's looking great. I also have hidden ...

how to dynamically update a button's text using Vue.js

I have a challenge where I need to dynamically change the text of a button based on whether a value is true or false. Below is the code snippet that I have been working on: <button class="btn btn-primary table-button" type="button&quo ...

What is the procedure for adding a JavaScript function to an HTML element that was exclusively created in JavaScript?

I am trying to dynamically change the background color of a row in a table when selecting an object from a dropdown list, but only if the selected number is even. The challenge I am facing is that the objects are created using JavaScript and not directly i ...

Impact on adjacent div

Within my code, I have two sibling divs that contain additional nested divs as shown below: <div class="btn_lists"> <div class="btn green_btn"> <img src="<?= asset_url() ?>images/escolar_07__1.png" /> </div> & ...

Troubles encountered when loading pages into a div using ajax navigation

As someone new to the world of programming, I am embarking on the journey of creating a simple website for myself. Currently, my website consists of a header, navigation links, an image below the nav (to be updated later), and 3 content divs beneath that. ...

A JavaScript code snippet that stores the total number of bytes read from a CSV file in a variable

I currently have a CSV file located on a web server that is regularly updated with numeric and string data of varying lengths. I am seeking a solution to calculate the total byte values of each row when reading the file, as the byte count is crucial due ...

css align center issue

<div id="headermain"> <div id="logo"> <a href="#">net</a> </div> Having trouble centering the #logo div within #headermain. Even with margin:auto; applied, the #logo div remains off-center. #headermain i ...

What is the process of turning an SVG element into a clickable link?

Is there a way to transform each element within an SVG image embedded in an HTML file into an active link? I want to create an SVG image with clickable elements. Here is a snippet of the SVG image: <svg version="1.1" id="Layer_1" xmlns="http://www.w3 ...

Issue encountered when attempting to use an HTML document as a blueprint for generating additional HTML documents with the

I am currently working on an exercise that involves creating four different files: index.html, home.html, about.html, and contact.html. The objective is to utilize jQuery's .load function to load the other three HTML files within index.html while stil ...

What causes line-height to be overlooked in a span element but effective in a div element?

Take a look at this example: CSS: How to reduce line spacing of text? I've experimented with it, and I noticed that the CSS property line height is not very effective when applied to a span element. Why is this the case? Why does it only work properl ...

Adding and removing attributes with Jquery upon clicking a button

How can I make my listed items add an ID when clicked, or what am I doing incorrectly? $('.ex-menuLi #tt').attr('id', 'test'); $('.ex-menuLi').on('click', function(){ $(this).attr('id', &apos ...

Tips for creating sliding header content alongside the header

Is it possible for the content in the header to scroll up, displaying only the navigation list and hiding the logo when a user scrolls on the website? Currently, while the header background slides up, the content within the header remains in place. I feel ...

The expression '<xsl:value-of select="document(content)//title"/>' results in a null node response

Can someone assist me in retrieving the title of a basic HTML document in order to create a sitemap? I have been receiving an empty value every time I try. Upon inspecting the issue, it appears that the document(content) is returning document nodes. It s ...

Creating a carousel of cards using JavaScript, CSS, and HTML

Here is a visual reference of what I'm attempting to achieve: https://i.stack.imgur.com/EoQYV.png I've been working on creating a carousel with cards, but I'm struggling to synchronize the button indicators with card advancement when clicke ...