Is the scaling of a div with an image affected by odd pixel sizes?

My goal is to create a responsive grid with square images, where the first image is double the size of the others. While I can achieve this using jQuery, I am curious if there's a way to accomplish this purely with CSS.

Grid: Here's an example of the grid layout with red squares representing divs containing images:

The Expected Behavior:

On every screen size, each div should have a width of 20%, except for the :first-item which should be 40%. The height of all divs is set to auto as the images are all 500 x 500 px. Consequently, the height of the first image should also scale accordingly by two times.

To my surprise, this setup doesn't work consistently across all screen sizes. On certain screens, the first image ends up being slightly larger than intended by 1 pixel. For instance, when the first image has a height of 533px, the adjacent images have heights of 266px each. This discrepancy leads to issues in the alignment of subsequent rows of images.

You can view an online example of the grid here. Try resizing the browser window to observe how the image behavior changes.

If you have any insights into why this isn't working, please share your knowledge!

I've included the HTML and CSS code below for reference:

Thank you,

Wnd

HTML:

<div class="container">

    <header id="header">
    </header>

    <nav id="leftmenu">
    </nav>

    <section id="content">
            <article class="item">
                <img src="img/item.jpg" alt=""/>
            </article>
          <!-- More article items go here -->
          
    </section>

</div>

CSS:

html, body{margin: 0; padding: 0;}

#header{
    width: 100%;
    height: 100px;
    background: #222;
}

/* Additional styling rules go here */

#content .item img{
    width: 100%;
    display: block;
    height: auto;
}
   
/* Media queries for different screen widths */
 
@media only screen and (max-width: 2700px) {
   /* Adjust widths for large screens */
}

@media only screen and (max-width: 1920px) {
   /* Adjust widths for medium-large screens */
}

@media only screen and (max-width: 1400px) {
   /* Adjust widths for medium screens */
}

@media only screen and (max-width: 1024px) {
   /* Adjust widths for small-medium screens */
}

@media only screen and (max-width: 720px) {
   /* Adjust widths for small screens */
}

@media only screen and (max-width: 520px) {
   /* Further adjustments for extra small screens */
}

Answer №1

Percentages can often be unpredictable. One approach could be to avoid aiming for a perfect 100%. Instead, consider using something like: width:25%-2px as an alternative,

or strive for a slightly smaller percentage (not quite 100%, but 99%). For instance, your divs could add up to 39.5%+19.5%+19.5%+19.5% rather than 40%+20%+20%+20% (please note that these numbers are purely hypothetical and may not apply directly to your specific code).

Answer №2

If you want to achieve square article tags, you can do so by setting the padding-bottom to match the width percentage. This will ensure that the tags remain square regardless of their size.

#content .item{
    position: relative;
    float: left;
    margin: 0px;
    width: 50%;
    padding-bottom: 50%;
}

Afterwards, you can position the image using absolute positioning within the tag.

#content .item img{
    width: 100%;
    display: block;
    position: absolute;
    top: 0;
    left: 0;
}

Take a look at this demo on jsfiddle where I have provided placeholders for a clearer visual representation.

http://jsfiddle.net/EmilySmith/GHJ2Z/

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

What is the best way to make a block fill 100% of a container-fluid?

Here is a block: <div class="container-fluid"> <div class="content_wrapper"> </div> </div> What's the best way to display the .content_wrapper block with 100% content width and margins from the body borders? ...

Tips on deactivating automatic email client-specific content (such as Gmail or Outlook) in your emails

I have inserted the following code => <span style="font-size:12px;"><b>Name:</b></span></font><font size="1"><span style="font-size:12px;">Pratik</span> However, when viewed in a browser (such as Outlook ...

Ways to apply a box shadow exclusively to specific elements

Is there a way to apply a box-shadow to a div without it affecting specific other divs? In simpler terms, is it possible to prevent shadows from being cast on a particular div? Take for example this scenario: http://jsfiddle.net/Cd6fE/ How can I prevent ...

Having trouble with script tag not loading content in Next.js, even though it works perfectly fine in React

Currently, I am attempting to utilize a widget that I have developed in ReactJS by utilizing script tags as shown below- React Implementation import React from "react"; import { Helmet } from "react-helmet"; const Dust = () => { ...

The external Jquery file is being successfully loaded, however, the Jquery functions are failing to execute

I'm having an issue with my HTML partial, main index.html, and external JQuery file. Even though the file is being loaded successfully (verified through an alert function), the JQuery functions are not executing as expected. Upon checking the resourc ...

Utilizing Jquery to enhance slide image transitions with navigational links

As a newcomer to jQuery, I am attempting to create a slider using jQuery. Here is the script I have so far: $(function() { var bgCounter = 0, text = [ 'some html code here', 'some html code here', 'some ...

Looking to retrieve a specific data element within Vue.js?

Here's a thought-provoking query for you. Imagine I have some data stored in an element in the form of an array like this: data: { product: socks, variants: [ { variantId: 2234, variantColor: 'Green', varian ...

Altering Hues with a Click

One thing I wanted to achieve was changing the color of a hyperlink once it's clicked. I managed to make it work by using the code below: var current = "home"; function home() { current = "home"; update2(); } function comp() { current ...

Modifying button appearance based on state change in AngularJS

I have a button with a grey color and I want to create two different states for it: one that is selected (blue) and another that is in an idle state (grey). Currently, I am working on Angularjs but I am fairly new to this platform. Could you kindly provid ...

What is the best way to center a rotated element and have it aligned to the top?

* { box-sizing: border-box; } body { font-family: 'Poppins', sans-serif; margin: 0px; } .we-adopt { background-color: #8a8484; color: #ffffff; padding: 88px 0px; height: 100px; } .we-adopt-list span { display: i ...

What is the reason for the crossed out background image CSS in the Datatables header within Zurb Foundation 5.5.3?

I am facing an issue with displaying images in a wide Datatables header. The design is based on the Zurb Foundation 5.5.3 framework. Does anyone know why this CSS code is being strikethrough in the image below? (you can view it at which uses Zurb Founda ...

Guide on defining keyframes in a CSS animation based on a specific property value

In CSS animations, keyframes are defined using percentages to determine their position: @keyframes foo { 0% { ... } 50% { ... } 100% { ... } } I'm working on a basic animation where an object smoothly moves across the screen. However, I ...

What steps do I need to take to create a custom image for a website?

I'm looking for a way to create a website image using just code, without the need for an actual image file or image tag. Is there a method to do this? Would I use CSS, Javascript, or HTML5 for this task? If using JavaScript to dynamically generate the ...

The arrows on the Slick Slider appear cropped on ios devices like the iphone 8, however, they display perfectly when tested on Chrome and Firefox

I am currently facing an issue with my slick slider setup at https://www.labtag.com/shop/product/clear-cryogenic-labels-for-frozen-vials-1-75-x-1-aha-224/. It is configured for 4 slides on desktop and 2 slides on mobile devices (768px breakpoint). The pr ...

I can't understand why this question continues to linger, I just want to remove it

Is there a valid reason for this question to persist? I'm considering removing it. ...

Styling with CSS: Shifting an Element with each adjustment in page width

Within my project, there is a basic div that serves as a navigation bar. Inside this div rests an image portraying a logo. My goal is for the position of the logo to adjust every time the page width is altered. I attempted using media queries for this pu ...

Tips on how to horizontally center align a div when using the flex direction column technique

In my design, there is a container that consists of both a table and a div. The div has its display property set to flex with the flex-direction as column. I am trying to horizontally center align the div within the container. Below is the code snippet: ...

HTML: arranged <pre> with fixed positioning

I currently have a centered column of text with a fixed width. However, I am looking to break that fixed width for certain tags like <pre> so that they can fill the full screen width while maintaining flow with the rest of the text. My CSS snippet s ...

What could be causing the visibility issue with my navigation items in the bootstrap navigation bar?

Currently, I am experiencing a puzzling issue with the navigation bar on my live website. The navigation items seem to flicker for a brief moment and then disappear entirely. This unexpected behavior is certainly causing me some concern. I crafted the us ...