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

Is it possible to combine the :last-child pseudo-class with the universal selector (*) in CSS or Sass

Here is the code I am currently using: .loken5 * padding-right: 5px .loken5:last-child padding: 0 I am wondering if there is a way in SASS to achieve the same effect with just one '.loken5' tag for saving space. Thank you :) ...

CSS Dialect Debate: Container or Wrapper - Which is the Best Term to

Can you explain the distinction between a container and a wrapper, as well as their respective meanings? ...

Steps to display the datatable footer on the printed page

I am having trouble understanding the solutions provided for my table query. The current table setup is as follows: <table class="table table-bordered make_datatable"> <thead> <tr> <th>SL No</th> ...

Resolving the issue of the 'Failed to load resource: net::ERR_FILE_NOT_FOUND' error

I'm encountering issues with loading my CSS in different browsers when using the "Copy Path" feature in VSCode. While everything displays correctly in preview mode within VS, none of the styles appear when pasting the path into Chrome or Edge. Below ...

Obtaining the source id using HTML5 Drag & Drop functionality

Is there a way to retrieve the id of the div I am dragging from, similar to how it is demonstrated here? Your insights are greatly appreciated. Thank you. ...

Troubleshooting my HTML5 local storage issues for optimal functionality

I've been working on using HTML5's localstorage to save two variables and load them upon page refresh, but I seem to be encountering some issues when trying to load the saved items: Variables in question: var cookies = 0; var cursors = 0; Savi ...

Exploring the Canvas with Full Element Panning, Minimap Included

Currently, I am working on incorporating a mini map onto my canvas that mirrors what is displayed on the main canvas. The main canvas includes zoom and pan functions. I have created a rectangular shape for the minimap to display the content of the canvas. ...

Mobile video created with Adobe Animate

Currently, I am designing an HTML5 banner in Adobe Animate that includes a video element. However, due to restrictions on iPhone and iPad devices, the video cannot autoplay. As a workaround, I would like to display a static image instead. Can anyone provid ...

Is there a way to enable text selection on a jQuery draggable div?

I utilized jQuery to make a specific div draggable, but I've encountered an issue. Inside this draggable box, there is some important text that I need to be able to copy and paste. However, whenever I click on the box, it triggers the dragging action ...

Maintain the selected image

I am working on a program that allows users to choose images and I have given them the pseudo-class. .my_image_class:hover{ border:3px solid blue; -webkit-box-sizing: border-box; } This makes the images bordered in blue when hovered over, giving a "sele ...

Tips for displaying a digital keyboard when a webpage loads on iOS Safari

While developing a website, I noticed that the virtual keyboard fails to appear when an input field gains focus during the page load process. As per Apple's policy restrictions, this functionality is not allowed. However, I am interested in finding a ...

Changing HTML tags programmatically in Angular while inheriting a template

In my project, I have a Component called DataGrid that represents a table with expandable rows. Each row can be expanded to show a child DataGrid table, which is similar to the parent DataGrid component. To simplify this setup, I created a base class DataG ...

Tips for including a close button in a slidedown panel

Having this as a functional slidedown effect: ::-webkit-scrollbar { width: 0px; /* remove scrollbar space */ background: transparent; } /* optional: show position indicator in red */ ::-webkit-scrollbar ...

What is the best way to set the width of an element to 100% while accounting for padding

My dilemma involves an HTML input field. The input currently has padding: 5px 10px;, but I need it to occupy 100% of its parent div's width (which is fluid). If I try using width: 100%;, the input ends up being wider than intended due to the padding ...

Tips for customizing the event target appearance in Angular 2?

After following the steps outlined in this particular blog post (section 3, event binding), I successfully added an event listener to my component class. I can confirm that it responds when the mouse enters and exits. <p class="title" (mouseenter)="unf ...

Setting up button alignment in Bootstrap

In my current template, I am utilizing Bootstrap and need to position three buttons accordingly. The first button should be all the way to the left, the second in the middle, and the third (final) one on the right within a container element. While attempt ...

What is the simplest method for transferring data to and from a JavaScript server?

I am looking for the most efficient way to exchange small amounts of data (specifically just 1 variable) between an HTML client page and a JavaScript server. Can you suggest a script that I can integrate into my client to facilitate sending and receiving d ...

how can a select dropdown be dynamically displayed based on the previous selection?

If the first dropdown is set to "Professor" I want to display a second dropdown, but if it is set to "Student" then I do not want to display the second dropdown. function checkPrivilege() { var privilege = document.getElementById("permisija5").value; ...

What is the best way to implement autoplay sound on a JavaScript webpage?

I'm currently working on a web system aimed at monitoring values from a database, and I have the requirement to trigger a sound alert when a specific range of values is received. Despite trying various examples found online, none of them have been suc ...

Utilizing Angular for Webcam Integration

After trying out this code snippet: <video autoplay playsinline style="width: 100vw; height: 100vh;"></video> <script> navigator.mediaDevices.getUserMedia({ video: { facingMode: 'user' } }) .then(stream =&g ...