The CSS elements are positioned beneath the top border when applying a scale transformation within columns

I have a layout with 4 columns of images that I want to scale on mouse hover using the 'column-count' property.

The first column works correctly, but the next three end up positioned under the upper border during the transformation.

I've experimented with different margins and padding values, as well as tried using 'z-index', but nothing seems to work.

Check out the live example on JSFiddle

Here is the code snippet:

.content{
            margin-top: 20px;
            background-color: black;
        }

        .photos img {
            margin-bottom: 10px;
            vertical-align: middle;
            height: auto;
            width: 100%;
        }

        .photos {
            -webkit-column-count: 4;
            -moz-column-count: 4;
            column-count: 4;
            -webkit-column-gap: 10px;
            -moz-column-gap: 10px;
            column-gap: 10px;
        }

        li img {
            -webkit-transition: 0.2s;
        }


        li img:hover {
            -webkit-transition: .5s;
            transform: scale(1.3);
        }
<div class="content">
            <ul class="photos">
                <li>
                    <img src="https://syndlab.com/files/view/5db9b150252346nbDR1gKP3OYNuwBhXsHJerdToc5I0SMLfk7qlv951730.jpeg">
                </li>

                <li>
                    <img src="https://lh3.googleusercontent.com/proxy/t2UjB23Xy8xLCPcwavD_5pqDWQH8wx1tOggm85KZq22oyODukZGZyMDIfGHwIKyZj1U4JeAzn3t5bkgPXcV8pJ60udJ1eQ">
                </li>

                <li>
                    <img src="https://images.all-free-download.com/images/graphicthumb/bavarian_landscape_515460.jpg">
                </li>

                <li>
                    <img src="https://cdn-ep19.pressidium.com/wp-content/uploads/2019/08/16-9-original-1.jpg">
                </li>

            </ul>
        </div>

Answer №1

If you want to achieve a similar behavior, consider transitioning to flexbox.

Simply adjust your CSS code like this:

.photos {
    display: flex;
}

li {
  width: 25%;
}

For a live demonstration, check out this Codepen link: https://codepen.io/alezuc/pen/jObWjLm

Update:

To accomplish the layout described in the comment below, I recommend utilizing a pure CSS Masonry approach. Here's the suggested code snippet:

<div class="content">
   <div class="item">
      <img src="image-url-here.jpg">
   </div>
   (additional item divs with image sources)
   <span class="item break"></span>
   (additional span elements for breaking columns)
</div>

Check out the CSS code below:

.content {
  background-color: black;
  display: flex;
  (additional CSS properties)
}

.item {
  width: 24%;
  (additional CSS properties for items)
}

.break {
  flex-basis: 100%;
  (additional CSS properties for column breaks)
}

img {
  max-width: 100%;
  height: auto;
  transition: 0.2s;
}

img:hover {
    transition: .5s;
    transform: scale(1.12);
}

View the functional Codepen here: https://codepen.io/alezuc/pen/MWayKaq

For further insights on this technique, refer to this resource.

Answer №2

It seems like the issue with the columns is that the browser may be getting confused and placing the non-first images lower down on the page.

I have come up with two potential solutions to fix this problem, with my favorite being: display: flex;

.content{
    margin-top: 50px;
    background-color: black;
}

.photos img {
    margin-bottom: 10px;
    vertical-align: middle;
    height: auto;
    width: 100%;
}

.photos {
    padding: 0;
    list-style: none;
    display: flex;
}

.photos li{
  flex-basis: 25%;
  padding: 0 5px;
  margin: 0;
}

li img {
    -webkit-transition: 0.2s;
}


li img:hover {
    -webkit-transition: .5s;
    transform: scale(1.12);
}
<div class="content">
    <ul class="photos">
        <li>
            <img src="https://syndlab.com/files/view/5db9b150252346nbDR1gKP3OYNuwBhXsHJerdToc5I0SMLfk7qlv951730.jpeg">
        </li>

        <li>
            <img src="https://lh3.googleusercontent.com/proxy/t2UjB23Xy8xLCPcwavD_5pqDWQH8wx1tOggm85KZq22oyODukZGZyMDIfGHwIKyZj1U4JeAzn3t5bkgPXcV8pJ60udJ1eQ">
        </li>

        <li>
            <img src="https://images.all-free-download.com/images/graphicthumb/bavarian_landscape_515460.jpg">
        </li>

        <li>
            <img src="https://cdn-ep19.pressidium.com/wp-content/uploads/2019/08/16-9-original-1.jpg">
        </li>

    </ul>
</div>

Other potential fixes involving your column settings include adding position: absolute to .photos img or using float: left on .photos.

Edit: I noticed that Alessio posted his answer while I was writing mine and he also suggests using float. Consider accepting his solution if you choose the float approach.

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

Tips for effectively deleting a flash object from an HTML page

I recently created a website with AJAX (using jquery) for navigation purposes. The pages on the site slide smoothly, and I have been using remove() to get rid of the old page. Everything was working fine until I encountered an issue where the browser cras ...

Wrap the json response in HTML elements

I'm currently learning about AJAX, and I've encountered a major issue that I can't seem to resolve. I pass several variables to PHP, perform various checks there, and then return string values to AJAX. Strangely, I am able to display these r ...

Scrolling down a jQuery div after each new item is added.OR

I have a div acting as a chat feature, where messages are appended based on user actions. I am trying to make it so that the div automatically scrolls down after each message is added, but I'm encountering some issues with this functionality. You can ...

Submitting forms with Ajax in IE(8)

Sample Google form Related spreadsheet I modified the original code to create two custom forms: First created form Second created form Both forms are functional on most browsers except for IE(8). Any idea why? First form: <!DOCTYPE html> <h ...

Pressing a key once causing two actions when managing content in a separate window

Issue: I am facing a problem where I receive double keypresses from one key event when the event updates content in two separate windows. (Please keep in mind that I am not an expert in this field and appreciate your understanding.) I am attempting to use ...

Transitioning background with background sizing set to cover

I have been searching for an answer to this question, but haven't found one yet. Currently, I have a series of divs with background-images set to 'background-size: cover'. My goal is to make the images zoom in and grow on hover. However, i ...

Import JSON Data into Angular-nvD3 Chart (AngularJS)

I am seeking help to load encoded JSON Data retrieved from a database via queries into an Angular-nvD3 graph. I am unsure about the best approach to achieve this task. The encoded JSON data is fetched using API queries from a database table called PRODUCT ...

Choose all list elements except the final one

I need to display a list of tags horizontally and add a comma after each item except for the last one. Here is how I can achieve this using CSS: ul.tags { list-style: none; } ul.tags > li { display: inline; } ul.tags > li:after { con ...

Styling the tab view in PrimeFaces

I am currently working with the tab view feature in primefaces and I have encountered a couple of issues. 1) When using Internet Explorer, the tabs are displayed vertically instead of horizontally. However, it works fine in Firefox. FireFox : Internet E ...

Concealing information based on the value of a variable

Creating a dynamic price estimator. I need help writing a jQuery function that can hide or show a specific div element based on the value of a variable. For example, let's say I have an HTML div with the ID 'Answer': <div id="answer"&g ...

Modify the td attributes while retaining the extracted data values from the website

I'm currently utilizing this code to extract data from another website: $searchURL = "http://www.anotherwebsite.com"; $html = file_get_contents($searchURL); $patternform = '/(<tbody.*<\/tbody>)/sm'; preg_match_all($patternfor ...

Materialize CSS is throwing an error: 'velocity is not a function'

Encountering an issue with Materialize CSS. A JavaScript error appears 'I('.velocity is not a function', for certain actions. For instance, clicking the collapse icon on the sidenav results in e.velocity is not a function error. Similarly, u ...

Images with full-width will scroll horizontally

Is there a way to fix the horizontal scroll issue? <!DOCTYPE html> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="stylesheet" href="https:// ...

Modify the appearance of a nested div using CSS hover on the main JSX container

Within the material-ui table component, I am dynamically mapping rows and columns. The className is set to clickableRow. The TableRow also includes a hover options div on the right side: const generateTableHoverOptions = () => { if (selected) { ...

Mouse over the background of an image in jQuery to create a fade-in effect, without affecting any content inside

Currently facing a challenge with jquery. Please take a look at jsfiddle.net/7HXEF/1/. I am trying to create a DIV with a background image that fades in and out when the mouse hovers over it. However, the inner div with a link should not be affected by the ...

Having trouble locating and scrolling through elements while webscraping with Python Selenium, encountering the 'cannot focus element' error

I have been trying to extract ticket information from the VividSeats website. The URL I am working with is '' My tools of choice are Selenium and Python. I managed to navigate to the page using Chrome WebDriver and interact with the pop-up tha ...

Tips for keeping a Bootstrap Modal in a fixed position on the screen as you scroll

I am facing an issue with a button that is supposed to trigger a Bootstrap Modal. It seems that the modal is not visible when the page has been scrolled down. Upon clicking the button to show the modal, the background darkens as expected, but the modal its ...

Create a layout using jquery-tokeninput

Currently, my project incorporates jQuery-tokenInput and everything is functioning properly. However, I now have the desire to customize the design of the results that are displayed. For example, I want to adjust the width or make any other design changes. ...

What is the best way to eliminate empty spaces from the container?

click here check out this image too Just getting the hang of Bootstrap. The first picture looks good, with the image as the background. But on the second picture, there are margins and a background color showing up. How can I get rid of those margins? Her ...

transforming unicode into regular characters prior to being presented on a webpage

I am currently utilizing the openinviter class to import contacts from emails. Sadly, it is showing Unicodes of non-English characters, like Polish, such as u0117 (and similar code types), instead of regular characters. Is there a way to convert these Unic ...