How to fit images of different sizes into div containers

We are creating circular player images within circles, like so:

div.intable-player-image {
    border: 1px solid #666;
    border-radius: 50%;
    margin: 1px auto;
    max-height: 25px;
    overflow: hidden;
    width: 25px;
}
<div style="display: flex; width: 150px; ">
  <div class="intable-player-image" style="pointer-events: auto;">
    <a class="logo" href="/">
      <img src="https://storage.googleapis.com/cbb-image-files/PlayerHeadshots/105304-965025.png" alt="player-face" style="width: 100%;/* height: 100%; */">
      </a>
  </div>
  <div class="intable-player-image" style="pointer-events: auto;">
    <a class="logo" href="/">
      <img src="https://storage.googleapis.com/cbb-image-files/PlayerHeadshots/105676-963793.png" alt="player-face" style="width: 100%;">
    </a>
  </div>
  <div class="intable-player-image" style="pointer-events: auto;">
    <a class="logo" href="/">
      <img src="https://storage.googleapis.com/cbb-image-files/PlayerHeadshots/104923-1321451.png" alt="player-face" style="width: 100%;">
    </a>
  </div>
</div>

The player portraits come in different proportions. Some are wider than they are tall (first image), while others are taller than wide (2nd and 3rd image). Our aim is to display them uniformly and attractively with a one-size-fits-all approach.

In the provided example, the challenge lies in fitting the first image properly without any white space visible. Any suggestions on adjusting the styles to enhance the presentation of the first image without affecting the appearance of the 2nd and 3rd images? Additionally, enhancing the visibility of the player's face in the 2nd image would be a welcomed improvement to our current solution.

Answer №1

object-fit and object-position are essential for controlling how elements are positioned and scaled within their containers.

Using the object-fit property determines how an image should be resized to fit its container while keeping its aspect ratio intact.

The object-position property, on the other hand, dictates where the image should be placed within its container.

div.intable-player-image {
    border: 1px solid #666;
    border-radius: 50%;
    margin: 1px auto;
    max-height: 150px;
    overflow: hidden;
    width: 150px;
    display: block;
}

div.intable-player-image img {
  object-fit: cover;
  object-position: center 10%;
  width: 100%;
  height: 100%;
}
<div style="display: flex; width: 500px; ">
  <div class="intable-player-image" style="pointer-events: auto;">

      <img src="https://storage.googleapis.com/cbb-image-files/PlayerHeadshots/105304-965025.png" alt="player-face" style="width: 100%;/* height: 100%; */">

  </div>
  <div class="intable-player-image" style="pointer-events: auto;">
    <a class="logo" href="/">
      <img src="https://storage.googleapis.com/cbb-image-files/PlayerHeadshots/105676-963793.png" alt="player-face" style="width: 100%;">
    </a>
  </div>
  <div class="intable-player-image" style="pointer-events: auto;">
    <a class="logo" href="/">
      <img src="https://storage.googleapis.com/cbb-image-files/PlayerHeadshots/104923-1321451.png" alt="player-face" style="width: 100%;">
    </a>
  </div>
</div>

By using object-fit: cover;, the image is set to completely cover the container while maintaining its aspect ratio, cropping any excess from either the width or height to fit perfectly inside the container div.

With object-position: center 10%;, the image is centered horizontally and positioned 10% from the top of the container, resulting in a horizontal centering and cropping of the top 10% of the image.

These properties work together to ensure that images are scaled to fit their container divs while preserving their aspect ratios and desired positioning accurately.

Answer №2

To ensure an image remains within the boundaries of a containing div element, you can simply set the max-width of the image to 100%. This way, it will not exceed the size of the div.

<div>
  <img src="your-image-source" alt="remember-to-add-meaningful-alt-text">
</div>
div {
  width: 120px;
}

img {
  max-width: 100%;
}

In this scenario, the maximum width of the image will be determined by the enclosing div and will not surpass 120px.

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

Is there a way to keep an image centered while using bootstrap 3.0?

Can anyone help me figure out why the image at the center keeps moving when resizing the window in Bootstrap 3.0? Here is my current HTML code: <div class="text-vertical-center"> <img id="output" class="img-responsive img-center" src= ...

Animate sliding bar to move from the left using jQuery

I am currently experiencing an issue with a sliding animation on mouseover in the navigation. The animation works fine, but the problem arises when I hover over any menu item - the sliding bar starts from the very left instead of starting where the navigat ...

Retrieving the value of a formControl within a formArray is made possible through a reactive form in

How can I retrieve the value of ItemName in my HTML code? When I attempt to use {{invoiceForm.controls[i].items.controls.itemName.value | json}}, it returns as undefined. <form [formGroup]="invoiceForm"> <div formArrayName="items" *ngFor="let ...

Ways to avoid margin overflow in CSS

I am facing an issue with the code below where the last row overflows the parent container. I have tried using overflow: auto; to fix it, but that just adds scrollbars. Any suggestions on how to prevent the overflow? <link rel="stylesheet" href="http ...

Guide to redirecting on click when utilizing an iframe

I needed to prevent the page from reloading before the action was complete by using an iframe. However, I encountered an issue where I needed the page to refresh after the action took place. I attempted two methods to achieve this - refreshing the page and ...

How do you position items in the center and lower right corner of a flexbox that takes up the entire

One feature on my website is a flex box that takes up the entire window with a width of 100% and a height of 100vh. Within this flex box, I've centered text and a button, but now I'd like to add a footer to the page. If I simply place the footer ...

"Enhance Your CSS Styling with an Additional Dot for Dotted Under

Is it possible to make the dots at the end and sometimes beginning of a word thicker and more round? https://i.stack.imgur.com/W9WPi.png ' CODE <span style="border-bottom: 3px dotted #111;">Personal Project Magazine Ads Personal Project Magaz ...

Organize data in a Vue.js table

Currently facing an issue with sorting my table in vue.js. Looking to organize the table so that campaigns with the highest spend are displayed at the top in descending order. Here is the code I'm working with: <template> <div class=" ...

What could be causing my jQuery to not function on a separate domain?

Two of my websites have the same code, but one is functioning correctly while the other seems to be having trouble with the jQuery functions <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html;char ...

Align all elements to the center and arrange them in a vertical column

I have implemented a form using Bootstrap 4 and here is the code: <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dcbeb3b3a8afa8aebdac9ce8f2eaf2ec">[email protected]</a&g ...

How can I use PHP to choose an option from a dropdown menu and show its corresponding information?

What is the process for selecting a supplement and displaying its associated data, such as description and cost? Any assistance would be greatly appreciated. PHP: require('database.php'); $supplementID = filter_input(INPUT_POST, 'su ...

Why isn't my HTML file acknowledging AngularJS variables?

<!DOCTYPE html> <html ng-app=""> <head> <title>hello</title> <script type="text/javascript" src="lib/angular.js"></script> </head> <body> <input type="text" ng-model="name"> <p>{{nam ...

Creating a responsive layout with two nested div elements inside a parent div, all styled

I'm currently facing an issue with using padding in my CSS to make the two divs inside a parent div responsive at 50% each. However, when combined, they exceed 100%. I suspect this is due to the padding, but I'm unsure how to resolve it. .column ...

Preventing a user with the same name as the one in the table from being added by utilizing a jQuery contains check proved

Check out my fiddle here: http://jsfiddle.net/jd5pgdz2/4/ I encountered an issue when attempting to add a user with the same name as one already in my table (the displayed users are static in the HTML). Despite using 'contains' to check if the u ...

Passing a variable from a jQuery function to a submitted form: the ultimate guide

I stumbled upon this code snippet online and it's exactly what I needed to learn from as a beginner using jQuery. However, I have a question about transferring or passing the value of counter that was used in the jQuery script within the HTML head tag ...

unnecessary shading effect on SVG text

My SVG text suddenly has a strange drop shadow that I can't seem to get rid of. Despite checking the reference guide I purchased on Amazon, I still haven't found any solutions. There are no filters applied and the markup seems to align with examp ...

React Material Design Cards for Responsive Layouts

Hi there, I am currently navigating my way through Material Design and attempting to ensure that the cards section is responsive by utilizing media queries and flex. However, I have encountered an issue where only a portion of the image is displayed when t ...

Updating an element on the page without having to reload the entire page

I have implemented a functionality on my Laravel page where multiple locations can be saved. Now, I am looking to enhance this feature by allowing users to edit and update location data without refreshing the page. Below is the HTML form structure: <for ...

How can I extract the "href" attribute from an anchor tag using XPath?

I am working with HTML code that looks like this: <a href="/images/big_1.jpg" class="class-a"> <img class="class-img" src="/images/small_1.jpg"/> <span class="class-span"> <img src="/images/img_1.png"> </ ...

Positioning a Form with Sliding Out Effects Using CSS

Hello, I recently came across a tutorial online that helped me create a form slide effect on my website. However, the issue I'm facing is that the form is positioned to the left and I would like it to be placed on the right instead. For reference, h ...