Stop images from stretching in a horizontal flex container

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

I am facing a challenge with a horizontal flexbox containing 3 columns. The left column is black, the center one is white filled with text, and the rightmost column is blue also with text.

The issue arises when the text columns automatically adjust their height to match the black column, causing an imbalance in the layout due to the image's aspect ratio preservation. This results in the image stretching the text columns beyond their necessary height.

My objective is to have the flexbox match the height of the tallest text column without allowing the image column to dictate its overall height. In Tailwind, I attempted using object-cover and object-center on the image to fill the gray area within the black box and maintain proper dimensions through object-fit.

Various attempts such as setting the image height to max-content and experimenting with different height values have not yielded a satisfactory solution so far. Is there anyone who can provide assistance?

<div class="flex items-center h-full">
    <div class="w-1/3 black">
        <img class="max-h-max object-cover object-center" />
    </div>
    <div class="w-1/3 white">
        <p>Short Text</p>
    </div>
    <div class="w-1/3 blue">
        <p>Short Text</p>
    </div>
</div>

Answer №1

By removing the items-center class, the flex items now have equal height. To maintain the left column's height without being affected by the image, the position of the img should be set to absolute. In order to position the img, its parent tag must have a relative class. The classes translate-x-1/2 and right-1/2 are used to center the img.

<script src="https://cdn.tailwindcss.com/"></script>
<div class="flex">
    <div class="w-1/3 bg-stone-200 relative">
        <img class="absolute translate-x-1/2 right-1/2 h-full" src="https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon.png" />
    </div>
    <div class="w-1/3 bg-stone-50">
        <p>Short Text</p>
    </div>
    <div class="w-1/3 bg-blue-900">
        <p>Short Text</p>
    </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

Currently, VS2019 is in the process of compiling some commented out code within the ASP.NET framework

Currently, I am in the process of tidying up a website that is quite messy both at the back and front ends. I have chosen not to delete any of the old code, but when I try to comment out the old code using "<!-- -->", the code inside still gets compi ...

Utilizing jQuery for asynchronous image uploading

Just started learning jQuery and I'm having trouble uploading a jpg image file using the ajax method. It seems like it's not working properly. Can anyone guide me through this process? HTML <form action="" method="POST" enctype="multipart/fo ...

Automatic padding within Bootstrap5

I'm struggling to understand why I am experiencing a consistent padding at the bottom of my container in Bootstrap5. I attempted to use align-items-center within the row to center the content, but it had no effect. <!DOCTYPE html> <html la ...

Discover the basics of incorporating libraries using npm

As a beginner in JavaScript, I am looking to incorporate moment.js or another library into my project. However, I am unsure of how to properly set up my project so that I can import from the library. Here is how I have structured my HTML: <!DOCTYPE html ...

Toggle the font weight in a text area by clicking a button

I need help with creating a button that will change the font-weight to bold when clicked and then revert back to normal when un-clicked. However, I only want the specific part of the text area where the user clicks the button to be bolded, not the entire t ...

JavaScript failing to adjust span width as intended

In an attempt to adjust the width of a span dynamically based on its content, I am facing a challenge. My application consists of multiple spans in rows and users can modify the content of these spans. To ensure uniformity in span width after content chang ...

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

align the bootstrap when the screen resolution is less than 900 pixels

As a newbie in web development and Bootstrap, I'm working on creating a responsive website. Everything was going smoothly until I encountered an issue when transitioning from desktop to mobile view. I have three buttons aligned in a row on desktop, bu ...

No JavaScript needed: Create custom overflow/scroll indicators using CSS only

Is there a way to align the content of a box to the left when the box doesn't overflow? I have a scroll indicator created with CSS only, which works well but has this alignment issue. Any suggestions or improvements on the code are welcome :) html ...

Preserve the form's values after an onclick event to maintain the user's input in session storage

My website consists of a single page with an interface designed to showcase prediction results in {{ prediction_text }} from a flask backend. Users input values into a form that utilizes the app.py script to generate prediction outcomes. The issue I am fac ...

How can the scrollbar functionality and grey border box/outline be removed from the canvas?

I am currently developing a Django project where I have two canvas elements stacked on top of each other. The top canvas has functionality that allows me to mouseover it and reveal an image loaded into the canvas below. While this functionality works well, ...

Is there a way I can appropriately display an image in a specific size box?

Check out the code snippet I wrote below: import React from 'react' function OurCourse() { return ( <div className='w-full '> <div className='w-full h-[390px]' style={{ backgroundImage:&apos ...

Utilizing jquery-ui themes for customizing buttons with dual-image requirements

I have a unique designer button with a 3D-like effect that cannot be created using just background and border radius. The "radius" of the button also needs to be an image. While tutorials like this one show how to make similar buttons, I find that jquery- ...

I am experiencing compatibility issues with my code in Chrome, although it works fine in Firefox and Edge. What could be the

My code is working fine in Firefox and Edge, but it's not displaying correctly in Chrome. Can anyone help me figure out what I'm doing wrong? I am trying to create three boxes that should be displayed side by side across the full width of the di ...

The entity referred to as [object Object] lacks the capability to execute the 'addEventListener' method

When I add the most recent version of jQuery from the official site, like this: <script src="http://code.jquery.com/jquery-latest.js"></script> The issue arises on line 3 of the code below (pscript.js): function init() { $("textbox").foc ...

What is the best way to make a div span the entire height of the body?

I have a menu that I want to wrap inside a yellow div. The yellow div should take up the entire height of the green square, regardless of the content size in the green square. For example, if the orange square expands, the yellow div should also expand. h ...

Tips for aligning an element in the center in relation to its sibling above

Help Needed with Centering Form Button https://i.sstatic.net/QhWqi.png I am struggling to center the "Send" button below the textarea in the form shown in the image. Despite trying various methods like using position absolute, relative, and custom margin ...

Determine the height using jQuery before adding the class

I've been grappling with jQuery to accurately calculate the height of an element and then apply a specific CSS class to adjust its height. The issue I'm facing is that jQuery seems to be executing these calculations out of order, which is causing ...

Reacting to multiple checkboxes in a check handling system

In my latest project, I have designed a component that utilizes multiple checkboxes generated within a .map function. While testing the functionality, it came to my attention that users could select more than one checkbox at a time. This is not ideal as th ...

Trouble aligning text next to boxes in Bootstrap Modal

Having trouble aligning the input text boxes in a modal form created with Bootstrap? The fields are currently showing under the labels instead of beside them. Looking for suggestions on how to fix this alignment issue. <button type="button" class= ...