Could images be positioned in this manner without using the float property?

Looking to arrange images in a left-to-right flow while keeping them centered on the screen:

https://i.stack.imgur.com/WHBv9.png However, I'm struggling to achieve this as the last image always ends up centered horizontally as shown below:

https://i.stack.imgur.com/X2QAq.png

Is there a way to align them in that order and still maintain the center positioning?

In my CSS, I'm using display: inline-block within #gallery > div

CODE: https://jsfiddle.net/m9xuj8aa/

HTML:

<div class="container">
    <header>
        <div id="logo">
            <img src="" alt="">
        </div>
        <nav class="cl-effect-21">
            <ul>
                <li>
                    <a href="index.html" class="scroll">
                        <span data-hover="Home">Home</span>
                    </a>
                </li>
                // More list items
            </ul>
        </nav>
    </header>

    <div class="portfolio clearfix">
        <div id="gallery">
            <div id="img-wrapper">
                <a href="image.jpg">
                    <img src="image.jpg" alt="">
                    <div id="overlay">
                        <h1 class="fa fa-search"></h1>
                    </div>
                </a>
            </div>
            // More image wrappers
        </div>
    </div>
  <div class="push"></div>
</div>

// Footer section

CSS:

/** External Stylesheet **/

.container {
    min-height: 100%;
    margin: 0 auto -60px;
}

footer, .push { 
    height: 20px;
    padding: 20px 0px;
}

.cl-effect-21 a {
    /* Styling for link effects */
}

/* Additional styles for header, gallery, etc. */

.portfolio {
    width: 100%;
    margin:0 auto;
}

#gallery {
    width: 100%;
    margin:0 auto;
    display: table;
    text-align: center;
}

#gallery img {
    width: 100%;
    height: auto;
}

#gallery > div {
    /* Image block styling */
}

#gallery a div {
    /* Overlay styling */
}

.clearfix {
    clear: both;
}

Answer №1

To effectively showcase the set of 4 images, consider placing them in a designated container where you can adjust the spacing between each photo to achieve the desired arrangement.

Answer №2

If you're using flexbox, here's an update for your JSFiddle. I've made some changes: removed the display table on #gallery, added display flex with row as default, and included a wrap property.

#gallery {
            display: flex;
            flex-wrap: wrap;
            width: 100%;
            margin:0 auto;
            padding: 0px 10px;
            text-align: center;
        }

UPDATE#1

I have updated the fiddle link with a temporary fix that may give you some ideas. Keep in mind that for responsive design, you will need to handle window resize events. This solution may not be ideal for all cases.

Answer №3

After some investigation, I managed to solve the issue. It appears that the images had varying sizes which was causing them to not align properly when floated from left to right.

The problem was resolved by specifying a specific height for each image.

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

Ways to eliminate gaps between div elements with CSS

I am currently working on a webpage that consists of one main div housing two inner divs (left_menu and content), along with an additional footer div outside the main container. When viewing the page, there seems to be a noticeable gap between the footer ...

Leverage the URL parameter with variables in HTML using AngularJS

I'm facing the same issue as yesterday: trying to extract and utilize parameters from the URL within my HTML page using AngularJS. What am I doing wrong? The parameter I need to work with is: https://url.com/index.html?user I aim to retrieve the "u ...

Discovering all invalid elements in an Angular 8 Form using Typescript by revealing required fields post button click

Once the button is clicked, I want to retrieve all invalid elements in the Form and showcase those fields that are either incomplete or required. ...

Load images in advance using jQuery to ensure they are cached and retrieve their original sizes before other activities can proceed

When a user clicks on a thumbnail, I aim to display the full-size image in a div. To achieve this, I want to determine the original size of the image based on its source URL before it loads, allowing me to set the appropriate dimensions for the container. ...

Using TypeScript with Angular, you can easily include parameters to HTML tags

I have an HTML element that looks like this: eventRender(info){ console.log(info.el); } This is the output I'm seeing: https://i.stack.imgur.com/G0hmw.png Now, I want to include these attributes: tooltip="Vivamus sagittis lacus vel augue ...

I am encountering issues with my JavaScript code not functioning properly

I am currently working on a website project in HTML where I need to retrieve JSON-formatted data using JavaScript and then display it on the site. However, I am facing an issue as I cannot seem to identify any errors in my code. I followed this sample code ...

What is the best way to ensure that TwentyTwenty.js images are always positioned in the

Looking to utilize the TwentyTwenty.js code for image comparison, but facing an issue where the images are aligned to the left side. Is there a way to keep them centered without explicitly setting the width on the container? <div id="beforeafter" class ...

Regular expression for matching a CSS definition that includes a specific name and value

Looking to match all CSS definitions with the name "test" and color set to gray? Here are a few examples: a.test { color: grey; } or .test { color: grey; }, as well as a.test:after, a.test { font-size: 10px; color: gray; } It's important to consider ...

The navbar in mobile view is not affected by the z-index property

Having created a navbar with scroll effect, I am encountering an issue with the mobile responsive view. Can someone please assist me in solving this problem? I would like to position the bottom nav-list below the navbar in the mobile view when clicking on ...

Utilizing the HTML button element within an ASP file combined with JavaScript can lead to the reloading of the

I am attempting to create a JavaScript function for an HTML button element. However, I have noticed that it causes the page to reload. Strangely, when I use an input with a button type, everything works perfectly. What could be causing this issue and why a ...

Enhancing the visual appeal of a javascript canvas animation

I am facing a small issue and I want to incorporate an animation from: http://codepen.io/chuckeles/pen/mJeaNJ My main query is how can I modify it so that instead of dots, there would be small images? Which part of the code should I edit for this purpose? ...

List on the Left Side with Scroll Capability

In my ASP.NET web application that targets .NET 3.5 and utilizes an AJAX asp:UpdatePanel, I have structured the page layout using multiple div elements to divide the vertical space into sections - header, first content, second content, third content, and f ...

Resizing tables dynamically using HTML and JavaScript

I am attempting to reproduce the functionality demonstrated in this example When dealing with a large table, I want it to resize to display 10 entries and paginate them accordingly. The only thing I require is the pagination feature without the search bar ...

Why isn't the HTML template opening in Outlook 7?

Does anyone know how to fix the code issue on Outlook 7? I've only included the header section of my code here, but please let me know if you need more information. I would appreciate any help with this problem. Thanks! ...

Alignment of content layout across two wrapper elements

My goal is to achieve a specific layout where each pair of cards in a two-column layout is aligned based on the card with the largest content. Both cards share the same content layout and CSS. If you have any ideas or implementations that could help me ac ...

Controlling Javascript events

Currently, I am working on implementing an in-place editor using jQuery. The functionality is such that when you click on the text you wish to edit, it will replace the content with an input field, specifically a select tag. Everything seems to be functio ...

jQuery Show/Hide Not Working Properly

I'm attempting to showcase one Tweet at a time and have it rotate through different tweets. I'm facing an issue where it works correctly on one type of page, but not on the other. Could this be due to a CSS precedence rule overriding the function ...

Limit 4 items per row in a flexbox layout with automatic width

I am currently using tables to display my items, but I want to switch to flexbox. However, when I tried using flexbox, the item widths were not dependent on their content. Any suggestions on how I can achieve this? Thank you in advance. Here is a snippet ...

Images cannot be shown until they have been uploaded

My issue involves a menu that loads an external file (form) on the same page, and for the menu, I am utilizing a specific function for menu styling. Custom Menu Function function getXmlHttp() { try { return new ActiveX ...