What is the best way to ensure consistent image sizes using CSS?

I am struggling to make my 3 images all the same size. Setting the width and height styles is not working for me because the images maintain their aspect ratios, resulting in a messy layout.

Here is what I currently have: https://i.sstatic.net/6AWPz.png

Here is what I am aiming for: https://i.sstatic.net/XK3L3.png

.social {
  display: flex;
}

.items_soc {
  height: auto;
  width: 50px;
  object-fit: cover;
  /* filter: brightness(0%) invert(100%); */
}
<div class="social">
  <img class="items_soc" src="https://via.placeholder.com/100x150" />
  <img class="items_soc" src="https://via.placeholder.com/150x150/aaa" />
  <img class="items_soc" src="https://via.placeholder.com/150x100" />
</div>

Answer №1

For optimal results, object fit should be used with a consistent aspect ratio. Revise the code to

.social {
  display: flex;
}

.items_soc {
  height: 50px;  /* Specify the desired height */
  width: 50px;  /* Specify the desired width */
  object-fit: cover;
  /* filter: brightness(0%) invert(100%); */
}
<div class="social">
  <img class="items_soc" src="https://via.placeholder.com/100x150" />
  <img class="items_soc" src="https://via.placeholder.com/150x150/aaa" />
  <img class="items_soc" src="https://via.placeholder.com/150x100" />
</div>

Answer №2

If you're looking to make all images the same height and width, it will depend on whether you want them in a vertical or horizontal order using the method I will explain.

To achieve this, each image must be enclosed within a <div> element. You can then use CSS to specify the height and width by adding the style attribute within the div opening tag. For example,

style = "width : 100px; height: 100px;"
. The images should maintain their aspect ratio and fit within the div.

Next, you can insert your images with their sources. Here is an example code snippet:

<!DOCTYPE html>
<html>
<body>
<div style = "width: 1000px; height: 50px;">
<img class="items_soc" src="https://via.placeholder.com/100x150" style = "height: 50px; width: auto;"/>
<img class="items_soc" src="https://via.placeholder.com/150x150/aaa" style = "height: 50px; width: auto;"/>
<img class="items_soc" src="https://via.placeholder.com/150x100" style = "height: 50px; width: auto;"/>
</div>
</body>
</html>

This technique is commonly used for lists and rosters, but you can also set a specific height value and have the width set as auto.

<!DOCTYPE html>
<html>
<body>
<img class="items_soc" src="https://via.placeholder.com/100x150" style = "height: 50px; width: auto;"/>
<img class="items_soc" src="https://via.placeholder.com/150x150/aaa" style = "height: 50px; width: auto;"/>
<img class="items_soc" src="https://via.placeholder.com/150x100" style = "height: 50px; width: auto;"/>
</body>
</html>

Same outcome, different approach.

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

Need to update various form fields at once with jquery?

There is a form with fields for firstname, lastname, email, country, along with edit icon and submit/cancel buttons. When the user clicks on the edit icon in the top right corner, all values will be displayed in textboxes and the country will be shown in ...

CSS3 flip effect on hover and click causing issues

I have successfully implemented a card flip effect using CSS3. The card is flipped using jQuery and CSS CSS .flipped { -webkit-transform: rotateY( 180deg ); -moz-transform: rotateY( 180deg ); -o-transform: rotateY( 180deg ); transfor ...

Enhancing the aesthetic appeal of a form

I have created a form in HTML that utilizes JavaScript to pull data from a database. I am looking to style the form, but I'm unsure of how to proceed. Below is the form along with some CSS code. How can I integrate the two together? <form id=" ...

Allow the words to seamlessly transition back and forth between columns in a continuous cycle

Currently, I am attempting to showcase a large text in a format similar to a book. Each "page" has designated width and height, with content displayed over two columns: left and right. In this layout, page 1 is on the left, page 2 on the right, page 3 on t ...

What's the best way to continuously increase my counter using the data received from my AJAX calls?

On the final step of my first ajax project, I have implemented a thumbs-up icon that increments a column in the database when clicked. The following code achieves this: Viewable Page HTML and jQuery: <div id="comment_id">+1</div> <div id=" ...

Disable or set input text to read-only in Internet Explorer 9 and earlier versions using JavaScript or jQuery

Can anyone offer any suggestions on how to accomplish this task? I attempted using JavaScript with the code below, but it did not yield the desired results. element.readOnly="true"; element.readonly="readonly"; element.disabled="disabled"; I also tried ...

Learn how to save user input from form fields and text areas into JSON format using Angular

Is there a way to add comments using Angular when a user clicks on a button? Currently, whenever text is entered in the input field or textarea, it disappears and an empty block without any name, country, and comments is displayed. The entered text should ...

The copying of array values is not supported by Chromium

While utilizing Webworker to process an array of pixels from Canvas and then assign it back, I encountered a strange behavior. Firefox works flawlessly in this scenario, however, Chromium seems to insert an empty pixel array into the Canvas. Upon further ...

The alignment of my Navbar Brand is off in my Navigation Bar and won't align to

<body> <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <div class="container-fluid"> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs ...

Ul list centered alignment appears misaligned to the left in Safari browser exclusively

I'm experiencing an issue with my menu alignment. It appears perfectly centered in FF, Chrome, and IE, but shifts to the left in the Safari browser. I'm not sure if there is a specific CSS property I need to use to correct this alignment. <d ...

Using data-image as the source for Bootstrap Modal

I am currently working on an image gallery that utilizes the Paver jQuery plugin. The gallery is functional, but I am facing an issue where it displays the same image in the modal instead of showing the respective data-image for each image. My goal is to ...

Challenges with iFrame Resizing on Internet Explorer

After following a method highlighted in a forum post to set up an iframe on my webpage, I encountered some issues. To display just a section of the page within the iframe, I utilized the zoom/scale feature available in different browsers. To target a spec ...

JQuery table sorter is unable to effectively sort tables with date range strings

I am facing an issue with sorting a column in my table that contains text with varying dates. The text format is as follows: Requested Statement 7/1/2014 - 9/16/2014 When using tablesorter, the sorting does not work properly for this column. You can see ...

The parameter necessary for [Route: admin.request.update] is missing. The required URI is admin/request/{request}, and the missing parameter is 'request'

When attempting to access detail.blade.php, I encountered an error stating "Missing required parameter for [Route: admin.request.update] [URI: admin/request/{request}] [Missing parameter: request]." Despite following the steps and codes exactly as in my pr ...

Is it possible to design and implement Materialize CSS inputs without relying on Materialize CSS?'

Is there a method to implement Materialize CSS input elements that include placeholders and icons without directly using the Materialize CSS framework? I prefer not to mix frameworks in order to avoid potential conflicts, as I am already utilizing anothe ...

Is there a way to align the text input and text area next to each other?

I need help with styling a contact form that consists of 4 text input fields and 1 text area. Here is the current code for the contact form: <form class="contact_form"> <input type="text" placeholder="Name"> <input type="text" plac ...

Removing Shadow, Border, and Outline from Bootstrap v5.0.2 Navbar

I have been struggling to get rid of the shadow/outline/border on my navigation bar. Despite trying various CSS tweaks, I have not been successful. Below is the specific HTML code: <nav class="navbar navbar-expand-sm" id="site-navigation ...

The issue with displaying inline block is that the divs are not appearing side by side on the

Two of my div elements, namely form-panel and data-panel, are currently not aligned on the same line. How can I use display:inline-block to align them in a single line? Please review the code provided below. I have already used display:inline-block on both ...

Generate an HTML table dynamically from a PostgreSQL query

I am facing a challenge that may be simple for experienced individuals but is proving to be difficult for me as a newbie. The task at hand involves retrieving JSON data from a database query and displaying it in an HTML table using Node.js, Express, and Po ...

Using jQuery to store the name of a Div in a variable and subsequently invoking it in a function

Recently, I've been grappling with a task involving storing a div name in a variable for easier editing and incorporating it into standard actions like show/hide. My code functions perfectly without the variables, but introducing them causes the div ...