Error with column arrangement: absolute-positioned elements are displaying in the incorrect column

I'm currently trying to implement masonry layout using the CSS property column-count. While I am aware that there are simpler solutions involving creating multiple columns for the masonry structure, the complexity of my project built on PHP Laravel limits me to using only column-count in CSS for displaying images.

The specific issue I am encountering is depicted in the following screenshot: https://i.sstatic.net/dPeSA.png

Due to column-count: 3, the position: absolute element (represented by the cross button) appears partially in the previous column. I have attempted to resolve this issue with the following CSS properties:

-webkit-column-break-inside: avoid;
            page-break-inside: avoid;
                 break-inside: avoid;

However, these adjustments do not seem to rectify the problem.

Here is a snippet of the relevant CSS and HTML code:

<div class="card-columns">
                @if ($gallery_images->isNotEmpty())
                    @foreach ($gallery_images as $gallery)
                        <div class="card">
                            <img class="card-img-top" src="{{ $gallery->url }}">
                            <div class="card-img-overlay">
                                <a class="preview-remove-btn" href="{{ route('delete_gallery_image',$gallery->id) }}">
                                    @if(Auth::user()->user_type_id != 3)
                                    <i class="mdi mdi-close-circle preview-remove-btn-icon"></i>
                                    @endif
                                </a>
                            </div>
                        </div>
                    @endforeach
                @endif
            </div>

.card-img-overlay {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    padding: 1.25rem;
}

@media (min-width: 576px)
.card-columns {
    -moz-column-count: 3;
    column-count: 3;
    -moz-column-gap: 1.25rem;
    column-gap: 1.25rem;
    orphans: 1;
    widows: 1;
}

@media (min-width: 576px)
.card-columns .card {
    display: inline-block;
    width: 100%;
}

.card-columns .card {
    margin-bottom: 24px;
}

.card-img, .card-img-top, .card-img-bottom {
    flex-shrink: 0;
    width: 100%;
}

.preview-remove-btn {
    position: absolute;
    width: 10px;
    height: 10px;
    top: -16px;
    right: -2px;
    font-size: 18px;
}

Answer №1

To avoid encountering this issue, include a margin-top in your card styling:

/** CSS Added **/
.card{
  margin-top: 20px;
}

Preview with reference to Bootstrap 4.5:

.card-img-overlay {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    padding: 1.25rem;
}

@media (min-width: 576px)
.card-columns {
    -moz-column-count: 3;
    column-count: 3;
    -moz-column-gap: 1.25rem;
    column-gap: 1.25rem;
    orphans: 1;
    widows: 1;
}

@media (min-width: 576px)
.card-columns .card {
    display: inline-block;
    width: 100%;
}

.card-columns .card {
    margin-bottom: 24px;
}

.card-img, .card-img-top, .card-img-bottom {
    flex-shrink: 0;
    width: 100%;
}

.preview-remove-btn {
    position: absolute;
    width: 10px;
    height: 10px;
    top: -16px;
    right: -2px;
    font-size: 18px;
}

/** CSS Added **/
.card{
  margin-top: 20px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d0a0bfa0a0b5a2febaa390e1fee1e6fee1">[email protected]</a>/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA==" crossorigin="anonymous" />

<div class="card-columns">
<!-- Card examples go here -->
</div>

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

Button featuring a search icon that transforms into a loading animation

I'm struggling to add a loading animation inside my Search button. Any ideas on how I can achieve this? Would appreciate your suggestions. <button class="btn btn-primary tap" type="button" id="searchForContent"≷Searc ...

I am having trouble getting my media query to function correctly

I am currently troubleshooting an issue with my media query in a project. The media query is set for a screen width of 768px, but it appears to not be working as expected. Interestingly enough, when I resize my screen to 360px, the media query does work. ...

Looking for a way to transfer the value of a variable to a PHP variable using any script or code in PHP prior to submitting a form?

Within this form, the script dynamically updates the module dropdown list based on the selected project from the dropdown box. The value of the module list is captured in a text field with id='mm', and an alert box displays the value after each s ...

Using a 90% zoom level in the browser does not trigger any media queries

When addressing the width of a specific class, I utilized CSS media queries as shown below: @media (max-width:1920px) { .slides { width: 860px; } } @media (max-width:1500px) { .slides { width: 852px; } } @media (max-width:1 ...

Tips for changing the <title> in an AngularJS one-page application

I am working on a single-page AngularJS application. The index.html file is structured as follows: <html ng-app="myApp" ng-controller="MyCtrl as myctrl"> <head> <link rel="stylesheet" href="my-style-sheet.css"> <title>{{ ...

"Windows Phone Users Experiencing Issue with Website Scroll Functionality

My latest project involved creating a responsive website with nodejs integration, and it performs well on various devices. However, a problem arises when viewing the website on the Windows Phone IE browser - the webpage refuses to scroll down. The cause o ...

Choose different components that possess the X designation

Can X number of elements (h1, p, span...) be modified only if they have a specific class? I'm searching for a solution like this: (elem1, elem2, elem3, elem4).class { /* add customization here */ } I previously attempted using parentheses, curly b ...

"Prevent triggering the onClickRow function in a bootstrap table when clicking on a

Whenever I click a button within a column, the onClickRow function in the Bootstrap table is being triggered. I am looking for a way to prevent this from happening when a button is clicked within a column. ...

Locate specific elements on a webpage without scanning through the entire content

Is there a way to optimize the performance of getElementsByTagName("td") so it doesn't search the entire document? I'm experiencing long wait times for results. Here is an excerpt from my code: $ie.Document.getElementsByTagName("td") | ? {($_.c ...

Is there a way for me to acquire the Amazon Fire TV Series?

I'm currently working on developing a web app for Amazon Fire TV, and I require individual authorization for each app. My plan is to utilize the Amazon Fire TV serial number for this purpose. However, I am unsure how to retrieve this serial using Jav ...

Having trouble with customizing the active style of the React Bootstrap nav links

Is there a way to create an active tab menu that changes style when hovered over? I've tried using activeClassName="selected" but it doesn't seem to be working. Can anyone provide some guidance on how to address this issue? Here is the CSS I am ...

Empty gaps separating two divs nested within another div within a div (unable to function)

I'm diving into the world of HTML, CSS, and JS as I embark on creating a weather application. My vision is to include the temperature, followed by the city and its corresponding weather (accompanied by an icon), and lastly additional details like humi ...

Wait for the canvas to fully load before locating the base64 data in HTML5

Wait until the full canvas is loaded before finding the base64 of that canvas, rather than relying on a fixed time interval. function make_base(bg_img, width, height) { return new Promise(function(resolve, reject) { base_image = new Image(); base_imag ...

Creating a CSS layout with tabs that include a visually appealing right space

My webpage is set up for tabbed browsing, with the tabs displayed as <li>s in block form. The parent div containing the tabs is floated to the right. However, I am encountering an issue where the last tab is not fully aligning with the right side of ...

Troubleshooting Problem with CSS and Javascript Dropdown Menu

Greetings, fellow web designers! I am relatively new to the world of web design and currently, I am immersed in the process of creating a website. My current project involves implementing a dropdown menu using CSS and Javascript. While I have made signific ...

Take charge of Bootstrap 4 dropdown transformation class while creating a Megamenu

I am currently working on creating a Megamenu using Bootstrap 4's dropdown Component. The issue I'm facing is related to Javascript adding CSS styles with transform, such as transform: translate3d(9px, 5px, 0px); This is causing disruption in m ...

"The issue of the search form CSS class loading twice and causing placement issues following the completion of the AJAX

I implemented a search form that fetches data from the server and displays it in a div, which is working smoothly. However, when I added an ajaxComplete function to add a class for animating the results, it resulted in unexpected behavior. Upon entering t ...

The jquery datepicker is malfunctioning after switching to another component

My current setup includes the following versions: jQuery: 3.3.1 jQuery UI: 1.12.1 AngularJS: 6 Here's a snippet of my code: <input id="test" type="text" class="form-control" value=""> In my component (component.t ...

Warning from Google Chrome: Ensure password forms include optional hidden username fields for improved accessibility

Upon visiting the "reset password" route of my single-page application and checking the Chrome browser console, I am presented with a warning message: [DOM] Password forms should contain (optionally hidden) username fields for accessibility: (More info: g ...

Is it possible to create a query selector that is able to find an element based on its attributes while also excluding elements with a specific class name?

Imagine you have elements that look like this: <div class="a b" data-one="1" data-two="2"></div> <div class="c" data-one="1" data-two="2"></div> <div class="b" data-one="1" data-two="2"></div> Is there a way to selec ...