Tips on resizing images within a container to maintain aspect ratio

I am trying to create a layout with a flex container containing 2 divs - one with an image that floats left, and the other with some paragraphs floating right. How can I make sure that the image scales alongside the paragraphs while maintaining its aspect ratio?

.container_family {
    display: flex;
    justify-content: center;
    flex-direction: row;
}
.container_family .main-block{
    float: right;
    order: 2;
    padding: 0 3%;
    background-color: #001e4c;
    flex-basis: 100%;
}
.container_family .sidebar{
    float: left;
    order: 1;
    height: min-content; 
}
img#Mom-profile-photo{
    resize: both;
    object-fit: cover;
}
        <div class="container_family">
            <div class="main-block">
                <p>
                    Some sample text.
                </p> 
                <p>
                    More sample text.
                </p>
            </div>
            <div class="sidebar">
                <img id ="Mom-profile-photo" src="images/Mom_profile.jpg" alt="Profile picture of Ingrid (mother)">
            </div>
        </div>

Answer №1

There are various methods you can use depending on your specific goal. One approach is to establish a fixed height for the container_family, and then assign the same fixed height to the image.

.container_family {
    display: flex;
    justify-content: center;
    flex-direction: row;
    height: 150px;
    position: relative;
}
.container_family .main-block{
    float: right;
    order: 2;
    padding: 0 3%;
    background-color: #001e4c;
    flex-basis: 100%;
}
.container_family .sidebar{
    float: left;
    order: 1;
    height: min-content; 
}
img#Mom-profile-photo{
    resize: both;
    object-fit: cover;
    height: 150px;
}
<div class="container_family">
    <div class="main-block">
        <p>
            Some text.
        </p> 
        <p>
            Some text.
        </p>
    </div>
    <div class="sidebar">
        <img id ="Mom-profile-photo" src="https://cdn.pixabay.com/photo/2020/05/17/20/21/cat-5183427_960_720.jpg" alt="Profile picture of Ingrid (mother)">
    </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

Converting XML to HTML with the help of XSLT transformation customization

I need assistance in adjusting the XSLT 1.0 transform within the provided xsl file to create an HTML version of the table coded in the attached XML file. The generated HTML output file should display the table from the XML file with alternating row backgro ...

Guide to making a sidebar navigation menu on the left using Bootstrap

How can I create a left navbar using Bootstrap? I managed to do it. <nav class="btn-group-vertical float-left navbar"> <button routerLink="product/home" routerLinkActive="is-active" class="btn btn-outline-dark">Home</button> ...

Incorrect Arrow Direction of Bootstrap Popover on SVG Icon

I set up a bootstrap(v5.1) popover on an svg icon, but the arrow icon direction is incorrect. Check out the code snippet below to see how it's configured; @create text & @modifytext are placeholders for dynamic content. <svg height="20& ...

The background-color is covering up the pseudo element with a z-index of -1

I am working on a problem regarding a .link class with an ::after pseudo element positioned underneath it to create a box-shadow effect upon hover. .bg { background-color: aqua; height: 100vh; } .link { position: relative; font-weight: ...

Error encountered: string literal not closed while attempting to load text using ajax

It appears that many programmers from various languages have encountered this issue. I am using jQuery to fetch AJAX text onto a screen, but firebug is indicating an unterminated string literal with an unhelpful example : $("#content").html("<div cla ...

merge PHP variables together

Currently in the process of constructing a string for the picture information in a lightbox gallery display. Initially, I used this snippet: <td><div style='height:175px;'></div></td><td><a href='images/memw ...

What could possibly be the reason behind jQuery's inability to function properly with HTML content that was dynamically added to the page through an Ajax

The following code snippet is responsible for submitting a form using Ajax and displaying the result from the addNewUser.php script in a popup window. //trigger the signup submit button $('#button_signup_submit').click(function() { document ...

Adjust the transparency of a navigation bar independently from the transparency of the text within the navigation bar

Seeking assistance on adjusting the opacity of this navbar without affecting the text within it. Any tips on changing the text color as well? <nav class="navbar navbar-expand-lg navbar-light bg-light"> <a class="navbar-brand" href="#" ...

Is there a method to individually collapse each div?

Each question in the database has a corresponding button. When one of these buttons is clicked, only the answer to that specific question should be displayed. However, the issue currently is that clicking on any button reveals all the answers simultaneousl ...

Some devices may start the Flutter web page zoomed in by default

Experiencing Zoom Issue on Flutter Web Project: While working on my Flutter web project, I have encountered an issue where the page loads with a zoomed-in view on certain devices. Despite setting the viewport meta tag correctly, the zoom problem persists. ...

What is the method for ensuring text remains within a square while it is being relocated?

Check out this jsfiddle where you can interact with a moving square: http://jsfiddle.net/helpme128/3kwwo53t/2/ <div ng-app="test" ng-controller="testCtrl"> <div id="container"> <div class="shape" ng-draggable='dragOptions& ...

CSS DROP DOWN NAVIGATION MENU - Struggling to maintain hover effect on main menu item while navigating through sub-menu

I am facing a challenge with a CSS-only drop-down menu where the top main menu item loses its highlight when I move the cursor over the sub-menu items. You can view the menu in action here: . For example, if you hover over "Kids" and then move your cursor ...

The $_GET variable disappears once the form is submitted

Here's the issue I'm facing: On my homepage, there are tables with rows generated from the database using while loops. Each row includes a cell where an event can be added to that specific row. To achieve this, I pass the row ID as a $_GET variab ...

Can someone explain to me how I can customize the background of a bootstrap container?

Currently, I am working on an EJS login project and I have created a style.css file with the following code: [theme="bloodRed"] { background-color: #ff2424; color: #e571e1; accent-color: #00ff00; } In my register.ejs ...

What is the process for updating a database using a web browser in Django?

I've completed the ADDING stage successfully, but I'm facing a roadblock in the EDITING stage. The objective is to empower the admin to modify the specifics of a specific member via the browser. Any suggestions? While I've made progress with ...

What is the best way to incorporate animation using Tailwind CSS within my Next.js project?

I have a challenge with placing three images in a circular path one after the other using Tailwind CSS for styling. I've identified the circular path and keyframe style for the images. How do I configure this style in my Tailwind config file and imple ...

The proper way to link a style sheet within an MVC framework

Currently, I am working on transitioning a website that I created in the Atom text editor to an ASP.NET environment. To adhere to best practices, I have decided to implement the MODEL VIEW CONTROLLER design pattern in this project. While attempting to ad ...

Is there a way to ensure that text is always displayed at the bottom of an image, perfectly aligned with the left edge of the image, regardless of the parent alignment?

I'm working on creating a layout in HTML/CSS/Bootstrap with an image and text below it. The challenge is to ensure that the text always starts at the left border of the image, regardless of its length. Here's what I need the layout to look like: ...

Exclude a particular row from a table when there is no identifier

My PHP code generates a basic table. <table border="1" id="control> <tbody> <tr>...</tr> <tr>...</tr> <tr>...</tr> //Row #3 <tr>...</tr> <tr>... ...

Switching between filters using AngularJS toggle buttons

Currently, we are in the process of converting a jQuery application into an Angular application using JavaScript. The existing jQuery app functions, but we aim to transform it into a full-fledged app utilizing the Angular framework. The main concept behin ...