What is the method to limit the width/height of an image without exceeding its original size?

When using tailwindcss 3, I found that setting a max width and height for an image required the following code:

.img_preview_wrapper {
    @apply  border-2 m-2 border-gray-400 rounded-lg  md:w-11/12 xl:w-8/12;
    height: auto;
}

This worked well for larger images, but smaller images did not display correctly and appeared broken.

How can this issue be fixed?

UPDATED CODE : Using the following class definition:

.img_preview_wrapper {
    @apply border-2 m-1 p-1 border-gray-400 rounded-lg md:w-11/12 xl:w-8/12;
}

I achieved a similar view to what I desired:

https://i.sstatic.net/ObvWV.png

However, when showing a border around the image, how can I remove the empty space inside the border on the right side of the image?

Answer №1

Have you considered using a max-height property?

.img_preview_wrapper {
  @apply border-2 m-2 border-gray-400 rounded-lg md: w-11/12 xl:w-8/12;
}

.img_preview_wrapper img {
  object-fit: contain;
}
<div class="img_preview_wrapper">
  <img src="your_image_url" class="max-w-full max-h-full" alt="Image Preview">
</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

How do I incorporate a "Fly to marker" feature into an existing button on Leaflet maps?

I'm currently working on implementing a fly function to a marker when a button is clicked. I've already created buttons on the right side of the map and now I want to add this feature to one of them. var latlng = map.getCenter(); // or [47.47805 ...

Create an overlay that completely masks the height of a different element

Having a variety of elements on my screen that require overlays, I find myself facing a challenge. These elements come in different sizes and can be resized while the page is active. Although I managed to position overlays using CSS, the issue of 100% hei ...

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 ...

Issue with X overflow visible at the right edge of the webpage (Vue.js and Tailwind CSS)

RESOLVED There is a strange white overflow on the right side of my responsive website that appears and becomes smaller as I scroll down the page. I can't seem to figure out why this is happening. None of the elements are overflowing into that space. ...

Exploring the Contrasts Between HTML String and Emphasizing the Variances

Challenge Description: The task is to compare two HTML input strings with various styling elements like strong, li, ol, line-through, etc. The goal is to display the original text and the edited text in two separate boxes on the screen. Any words missing i ...

Modifying the CSS attributes within an iframe by referencing the parent window's properties

I am facing a challenge involving running an iframe on two separate pages within the same website, each with distinct CSS properties. The dilemma arises from the limited space available on my index.php page, where I aim to confine text within the iframe to ...

tips on adjusting images to the size of the screen without them being on html files

I understand that sharing links may not be appropriate for this forum, but I'm unsure how else to seek assistance. My apologies for any inconvenience. I am currently working on a website for a doctor, and the image you see is of a patient. I'm ...

Is there a way for me to remove an uploaded image from the system

Here is an example of my HTML code: <input type='file' multiple/> <?php for($i=0;$i<5; $i++) { ?> <div class="img-container" id="box<?php echo $i ?>"> <button style="display: none;" type="submit" cl ...

Distances measured in relation to the present Div

When trying out the jQuery animation effect by editing margins to move, an issue arose. The problem is that while using the animation effect within a div that contains most of the content on the page, the edited margin ends up being relative to the actual ...

The alignment of content in the <a> tag is not centered within a <ul> list item (horizontal menu)

I am new to web development and have recently started creating my first webpage using HTML and CSS. I'm still learning and don't have a strong understanding yet. One issue I'm facing is with centering elements on the page. I have tried usin ...

Can the behavior of "flex-end" be emulated in :before and :after pseudo-elements?

I have come across a piece of code that creates a checkbox using the pseudo-elements :before and :after, along with a hidden input to handle the selection logic. The current setup works fine and behaves as expected. However, I am curious if it is possible ...

Tips for adjusting image size within a Bootstrap card

I am having trouble getting the images to properly fit inside the card. Even when I set the width to 100%, they are not adjusting correctly. Can anyone provide guidance on how to fix this issue? Thank you Below is the HTML code for the card: .card { ...

Center Items in Flexbox, Implementing React Material with React

In my React App, I am utilizing flexbox and React Material to align the searchbar and clear button on the same line with space between them. Here is the link for reference: CLICK HERE <div className={classes.searchBarSection}> <Paper com ...

Webpack Encore: SCSS import order causing unexpected issues

When developing with Symfony + Webpack Encore, I aim to split styles into "layout" and "page-based" categories for convenience. However, despite this organization, I still prefer to compile all styles into a single CSS file. To achieve this, I structure my ...

Display a single button on hover in Angular 2+ framework

I'm looking to have the 'Edit' button only appear when I hover over a selected row. Here's the code: https://stackblitz.com/edit/inline-edit-change-edit2-j8b2jb?file=app%2Fapp.component.html I want the 'Edit' button to show u ...

The expansion animation for the Nextjs/React accordion box did not work as expected when utilizing tailwindcss

I am currently working on creating an animation for a collapsible box (accordion). My goal is to have the child component initially hidden with display:none. When I hover over the parent component, the child should be revealed and the dimensions of the pa ...

Issue with JQuery slide toggle preventing screen from scrolling down

On my website, I have a div classed as .slidingDiv that is hidden until the anchor .show_hide is clicked, causing it to slide down. This feature works perfectly when the .slidingDiv is the only content on the page. However, if there is other content above ...

Adjust the spacing between the title and other elements in an R Shiny application

How do I modify the font and size of a title, as well as add some spacing between the title and other elements? navbar <- navbarPage( Title = "Intervals", tabPanel("Data Import", sidebarLayout(sidebarPanel(fi ...

Unusual perspective of JSON with ng-jsoneditor in AngularJS

Currently, I have integrated ng-jsoneditor into my AngularJS application to display and format JSON data. I found guidance on how to implement this from both here and here. Here is the HTML code snippet: <div ng-jsoneditor="onLoad" ng-model="vm. ...

Overflow: Scroll vertically and Visible horizontally

I'm struggling with a problem and can't seem to find a solution. https://i.sstatic.net/fw8C9.png My goal is to have the container scroll vertically only, while allowing overflow horizontally to display a dropdown menu. Is this even achievable? ...