How to position images in the center horizontally using CSS without using percentage (%) measurements

There seems to be some excess space on the right side of my gallery...

The container for my images:

.my-gallery figure {
  display: block;
  float: left;
  width: 150px;
}

Is it possible to always horizontally center images on different screen sizes without using a percentage value? Any brilliant ideas to make the excess space less noticeable?

Or is using a percentage value the only solution?

In screen A:

https://i.sstatic.net/L4hiZ.jpg

In screen B:

https://i.sstatic.net/1XoCi.png

.my-gallery {
  width: 100%;
  float: left;
}
.my-gallery img {
  width: 100%;
  height: 112px;
}
.my-gallery figure {
  display: block;
  float: left;
  margin: 0 5px 5px 0;
  width: 150px;
}
.my-gallery figcaption {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  min-height: 26px;
}
.my-gallery img {
  max-width: 100%;
}
<div class="my-gallery">
  <figure>
    <a href="big1.jpg">
      <img src="http://placehold.it/112x150" alt="1" />
    </a>
    <figcaption>111111111111111111111111</figcaption>
  </figure>
  <figure>
    <a href="big2.jpg">
      <img src="http://placehold.it/112x150" alt="2" />
    </a>
    <figcaption>222222222222222222222222</figcaption>
  </figure>
  <figure>
    <a href="big3.jpg">
      <img src="http://placehold.it/112x150" alt="3" />
    </a>
    <figcaption>3333333333333333333333333333333</figcaption>
  </figure>
  <figure>
    <a href="big4.jpg">
      <img src="http://placehold.it/112x150" alt="4" />
    </a>
    <figcaption>444444444444444444444444</figcaption>
  </figure>
  ...
</div>

Answer №1

If you're having trouble with using the % symbol, one alternative solution is to utilize CSS flexbox.

Check out this JSFiddle example for reference

To achieve the desired layout, adjust the CSS properties of .my-gallery and eliminate the float property in figure:

.my-gallery {
    width: 100%;
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
}
.my-gallery figure {
  display: block;
  margin: 0 5px 5px 0;
  width: 150px;
}

Answer №2

The best solution is to utilize @media queries

I have also updated the .my-gallery rule to

.my-gallery {
  margin: 0 auto
}

Example code snippet

.my-gallery {
  margin: 0 auto
}
.my-gallery img {
  width: 100%;
  height: 112px;
}
.my-gallery figure {
  display: block;
  float: left;
  margin: 0 5px 5px 0;
  width: 150px;
}
.my-gallery figcaption {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  min-height: 26px;
}
.my-gallery img {
  max-width: 100%;
}
@media screen and (min-width: 310px) {
  .my-gallery {
    width: 310px;
  }
}
@media screen and (min-width: 465px) {
  .my-gallery {
    width: 465px;
  }
}
@media screen and (min-width: 620px) {
  .my-gallery {
    width: 620px;
  }
}
<div class="my-gallery">
  <figure>
    <a href="big1.jpg">
      <img src="http://placehold.it/112x150" alt="1" />
    </a>
    <figcaption>111111111111111111111111</figcaption>
  </figure>
  <figure>
    <a href="big2.jpg">
      <img src="http://placehold.it/112x150" alt="2" />
    </a>
    <figcaption>222222222222222222222222</figcaption>
  </figure>
  <figure>
    <a href="big3.jpg">
      <img src="http://placehold.it/112x150" alt="3" />
    </a>
    <figcaption>3333333333333333333333333333333</figcaption>
  </figure>
  <figure>
    <a href="big4.jpg">
      <img src="http://placehold.it/112x150" alt="4" />
    </a>
    <figcaption>444444444444444444444444</figcaption>
  </figure>
</div>

Answer №3

Instead of using %, another option is to utilize a table structure with the property table-layout: fixed; to organize your items.

HTML

<table>
    <tr>
        <td>
            1
        </td>
        <td>
            2
        </td>
        <td>
            3
        </td>
    </tr>
</table>

CSS

table{
  table-layout: fixed;
}

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

Expand the HTML Template and Resize Children to Fit the Window

My software creates HTML email templates that typically range from 600px to 650px in width, but can sometimes be as wide as 900px. The templates have nested table elements for email clients, with all dimensions specified in fixed pixels rather than relativ ...

Can heuristic methods be utilized to identify encoding in XML/HTML documents?

Imagine having an XML file stored on a remote computer with an unknown encoding. The goal is to read and manipulate this file, but the challenge lies in identifying the encoding used within the document. Considering the <?xml version="1.0" encoding="xx ...

Tips for eliminating the margin in a bootstrap navigation bar when hovering?

Here is the Navigation Bar that I customized, along with the CSS code I used to override the Bootstrap styling. Below are images showing the current output and the expected output. <div class="row fixed-top"> <div class="col-md-1"></div> ...

The offspring surpasses the parent's limits and extends beyond its

I am designing a webpage layout that includes 2 navigation bars (top), a top-graphic (middle) flexbox, and a content flexbox (bottom). Within the top-graphic flexbox, I want to add a black box for text. However, I am facing issues with the box overflowing ...

The alignment of a background image in CSS is different from using margin:0 auto, especially when dealing with scrollbars

Check out the following link to view the issue: http://jsfiddle.net/7zb6P/1/ In a scrolling div, both the yellow box and the background image are centered, however their centers appear slightly different. The discrepancy seems to be because the background ...

Verify if the entire block contains any children with text inside

When working with Selenium WebDriver, I encountered an issue trying to locate a block that contains children elements with specific inner text. <div class="chat_dialog"> <div class="chat_message"> <span class="chat_timestamp" style="disp ...

Responsive navigation menu featuring dynamic dropdown menus using HTML and CSS

Looking to design a responsive HTML and CSS navigation menu with multiple dropdown menus? You're in the right place. I've scoured countless YouTube videos, but haven't quite found the perfect template for a two-part navigation menu that tic ...

Using jQuery to Retrieve Check Box Labels and Populate Them into Textboxes

I would like to display the name of the selected checkbox label in a textbox. If multiple checkboxes are selected, I want their labels to be separated by commas and displayed in the textbox. Please excuse my poor English. $(document).ready(function() { ...

Tips for enhancing the clarity of an `html2canvas` screenshot

Here is the code I've been using with html2canvas to capture and download screenshots: Create HTML button <button class="btn btn-default btn-sm" style="margin:0px 0px -10px 970px; padding:2px 4px 1px 4px" onclick="genScreenshot()"><span cla ...

Gather numerical values and transform them into dates using AngularJS

My Angularjs project involves 3 scopes for year, month, and day which are retrieved from 3 input boxes. I need to combine them into a date, but the current code inserts the date with an additional 22:00:00 like 2019-06-01 22:00:00.000. How can I insert the ...

Ways to determine if a date matches today's date within a component template

I am currently displaying a list of news articles on the webpage and I want to show the word "Today" if the news article's date is equal to today's date. Otherwise, I want to display the full date on the page. Is there a way to compare the news.D ...

Using the linear-gradient method in CSS for border images

I managed to successfully create a design using the CSS properties border-image and linear-gradient. However, I am struggling to understand the syntax involved. While I grasp the concept that the series of colors defines the gradient transition, as well as ...

Implementing checkboxes for each API data in JavaScript

I am fairly new to this and I am currently working on a to-do list. I want to include a checkbox next to each item from the API to indicate whether it has been completed or not. <script type="text/javascript"> fetch('http://localhos ...

How to extract selected value from a dropdown menu in a React component

Is there a way for me to retrieve the chosen value from the dropdown menu? The select dropdown contains 12 options. My goal is to capture the selected value and then utilize it in handlecolumnchange to manipulate the number of columns added or removed. Des ...

Using Vue to alter data through mutations

Greetings! I am currently in the process of developing a website for storing recipes, but as this is my first project, I am facing a challenge with modifying user input data. My goal is to create a system where each new recipe added by a user generates a u ...

What is the proper functionality of the data-bs-toggle attribute in Bootstrap 5?

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="424147474c4a4a4c5f7e79777a">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq ...

Slideshow development with code

This is the code I currently have: HTML: <head> <title>JQuery demo</title> <script type="text/javascript" src="scripts/jQuery.js"></script> <script type="text/javascript" src="scripts/slider.js"></script ...

Change CSS style sheets depending on the size of the viewport

I've tried to implement the method from CSS-Tricks that allows for changing stylesheets based on the viewport width using multiple .css files and jQuery. However, my code isn't functioning as expected. I've spent the past hour trying to figu ...

Obtain a Compilation of Video Sources in an HTML5 Format

Hey there, I am using an HTML5 video. This is just an example <video width="320" id="video" height="240" controls> <source src="video.mp4" type="video/mp4"> <source src="video1.mp4" type="video/mp4"> <source src="movie.ogg" typ ...

Footer positioned correctly with relative DIV

I find myself in a state of complete confusion. Coding is not exactly my forte, so I suspect I have made a significant error somewhere that is causing the following issue: My goal is to create a sticky footer. The footer does indeed stick to the bottom of ...