Different width, same height - images arranged side by side in a responsive layout

The problem lies in the images: https://i.sstatic.net/rydNO.png

Here is an example of desktop resolution: https://i.sstatic.net/uv6KT.png

I need to use default size pictures (800x450px) on the server. This means I have to resize and crop them with CSS to fit the container div's width while keeping the height consistent for all images in a row. Additionally, when the window is resized, the images should maintain their aspect ratio.

Below is an example of HTML code:

<div class="wrap">
<div class="container col-7">
    <img src="http://lorempixel.com/800/450/sports/1/" alt="">
    <h2>Title1</h2>
</div>
<div class="container col-5">
    <img src="http://lorempixel.com/800/450/sports/2/" alt="">
    <h2>Title2</h2>
</div>
<div class="container col-5">
    <img src="http://lorempixel.com/800/450/sports/3/" alt="">
    <h2>Title3</h2>
</div>
<div class="container col-4">
    <img src="http://lorempixel.com/800/450/sports/4/" alt="">
    <h2>Title4</h2>
</div>
<div class="container col-3">
    <img src="http://lorempixel.com/800/450/sports/5/" alt="">
    <h2>Title5</h2>
</div>

And here is an example of CSS code:

.wrap { 
    display: flex; 
    flex-direction: row; 
    flex-wrap: wrap;
}
.container { 
    display: flex; 
    flex-direction: column;
}
.container img { 
    width: 100%; 
    display: block; 
}

Answer №1

If you're looking for a simple way to achieve this, consider displaying images as div backgrounds. Utilize CSS3 background properties like:

CSS:

div {
      background-image:url('image-url-here');
      background-repeat:no-repeat;
      background-size:cover;
      background-position: center; 
  }

For instance, your image divs could be structured as follows:

HTML:

<div class="container col-7">
    <div id="id1" class="myImage"></div>
    <h2>Title1</h2>
</div>

CSS:

   .myImage {
              background-repeat:no-repeat;
              background-size:cover;
              background-position: center; 
              width: 100%;
              height: 450px;
            }

   #id1     {
              background-image:url('http://lorempixel.com/800/450/sports/1/');
            }

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

Tips for modifying a state array in Vuex

I have the ability to add and remove entries, but I'm struggling with editing. How can I achieve this using VUEJS, VUEX, and JavaScript? For adding entries, I know I should use PUSH, and for removing entries, SPLICE works fine. But what about editing ...

What are some ways to incorporate inline TypeScript Annotations into standard JavaScript code?

If you're using VSCode, there's a new feature that allows you to implement type checking for traditional JavaScript files. There are instances where I wish to specify the type of a variable or parameters in a method or function to enhance auto-co ...

Find the current location of the scroll bar on the view port

Currently, I am utilizing the Addazle React grid for my project. However, I need to incorporate endless scrolling functionality which is not already included in this grid system. Thus, I am tasked with devising my own solution for this issue. To successful ...

Ways to display a vertical scrollbar within a select box

I'm currently working with an Angular select box and I'm looking to display a scroll bar if there are more than 5 data entries. <select class="form-control" data-ng-model='projectListData.appVersion' ng-selected ng-options="v.versio ...

Discovering inner arrays within an outer array using the Plus function

Apologies if these questions seem basic in terms of JavaScript. Challenge 1. I am attempting to write code that can identify an inner array within an outer array, and the structure of the array looks something like this; var array = ['Bob', [ ...

When using Next.js, an error may occur when trying to use DOMPurify.sanitize(), displaying a TypeError message saying that dompurify__WEBPACK_IMPORTED_MODULE_6___default

Utilizing DOMPurify.sanitize() within dangerouslySetInnerHTML={{}} to render the innerHtml retrieved from the database. Initially, I'm employing getServersideProps() alongside next-redux-wrapper for this specific page. Installed dompurify using: npm ...

Using ASP .NET controls in conjunction with Bootstrap

Is there a way to easily apply bootstrap classes to all asp .net controls in my application at once? Here is an example of how I formatted my menu control using bootstrap. I am looking for a solution where I can apply the bootstrap theme commonly to all m ...

Prompting a 401 error immediately after attempting to login through the Express REST API

Recently, I've encountered a strange issue with the controller for my login route. It was working perfectly fine yesterday without any hiccups, but suddenly it stopped functioning properly. Despite not making any changes to the code, it now consistent ...

Detecting race conditions in React functional components promises

There's an INPUT NUMBER box that triggers a promise. The result of the promise is displayed in a nearby DIV. Users can rapidly click to increase or decrease the number, potentially causing race conditions with promises resolving at different times. T ...

Incorporating Bootstrap Content: A Guide to Including a PHP File within Another PHP File

I am encountering an issue when trying to import a PHP file containing my navigation bar into another PHP file. Here's the code for the navigation file: <?php echo " <html> <head> <nav class = "navbar navbar-default navbar-fixed-top ...

What is the best way to connect an external CSS file to an HTML file in Pycharm?

Within the project directory, I've placed both the HTML and CSS files in the 'venv' library root folder. The HTML file is named index.html The CSS file is named styles.css This is the code snippet I used to link the CSS: <link rel=" ...

What's the best way to place my image inside a Bootstrap Accordion so that it is housed within the accordion-item?

I've been facing an issue with a Bootstrap Accordion. Everything works fine, except when I try to insert an image <img src="https://placehold.co/600x400" class="img-fluid" /> into an accordion-item <div class="accord ...

AngularJS: The `$locationChangeStart` event

I am attempting to check the next token in the next, but I am encountering an error: TypeError: next.localStorage is not a function APP.JS .run(function ($rootScope, $location,$log,User) { $rootScope.$on("$locationChangeStart", function (event,nex ...

What is the best way to ensure a consistent time interval between invoking two functions in JavaScript?

Is there a way to create a code that will call one function, let's say toStart(), and then immediately call another function, toStop(), exactly two seconds after the first function is initiated? I want this process to continue until a specific button, ...

The JQuery function is not functioning properly on a SharePoint display form, although it works perfectly on the add form

I have implemented a custom script on the Content Editor Web Part in SharePoint to modify forms. Although the script is successful on the default new form, it fails to run on both the default display and edit forms. Interestingly, I discovered that certai ...

``"Selecting a location upon clicking a marker in the array

I have limited experience with javascript but I am determined to improve my skills. Currently, I am facing a major roadblock in a project that I am working on and urgently require assistance. The project involves creating a map with marked locations from ...

Refresh the main view in MVC from a partial view

I am currently working on an application following the MVC architecture. Within my project, I have a Parent View that incorporates a Partial View. The Partial View contains a submit function that triggers a Controller Method. In case of any errors, I nee ...

How to place an absolutely positioned div inside a scrollable div with overflow-y: scroll

Here is the HTML code: <div class="container"> <div class="scrollable-block"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, whe ...

When using TypeScript with custom components as children in React, the `type` returned by React.Children is a string representing the function

It might sound a bit odd, or maybe I'm completely off track here. While going through some articles and React documentation on getting children and identifying specific child components using React.Component.map(), I ran into an issue with my custom c ...

`Vue.js child component not reflecting updated prop value`

Created a functionality within the App.vue parent component to communicate with all child components, instructing them to close any open modals. Additionally, implemented a feature for a child component to notify the parent to hide the overlay when the mai ...