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

Stop the inclusion of the scrollbar in the mat-drawer-inner-container within the Angular mat-drawer Component

Background Story: Working on designing a screen layout that includes the use of a mat-drawer to display a custom component. The challenge arises when the custom component gets nested inside a div (with class="mat-drawer-inner-container") automatically adde ...

Modify the height of an element in real-time using jQuery

I'm looking to dynamically adjust the height of a div based on another element, but only if that element does not have the class collapsed (which is used in a Bootstrap toggle collapse feature). The initial setup seems to work fine, however, when I i ...

Exploring the application of the PUT method specific to a card ID in vue.js

A dashboard on my interface showcases various cards containing data retrieved from the backend API and stored in an array called notes[]. When I click on a specific card, a pop-up named updatecard should appear based on its id. However, I am facing issues ...

Starting the download of the canvas featuring a stylish border design

I'm facing an issue where the canvas border is not showing up when I download the canvas with a border. The border appears on the screen, but it doesn't get included in the downloaded canvas. let canvas=qrRef.current.querySelector("canvas&qu ...

Maintain the style of the main navigation menu when hovering over the sub-navigation items

I am currently in the process of redesigning the navigation bar for a specific website. The site utilizes Bootstrap, with a main navbar and a secondary navbar-subnav. While I have been able to style both navs accordingly, I am encountering difficulties whe ...

Using setInterval together with jQuery to animate CSS based on changes in window size

I've been troubleshooting this issue and trying various methods, but despite it seeming logical, nothing is working. Any assistance would be greatly appreciated since I'm not very skilled in coding. My current challenge involves animating an obj ...

The issue with data targeting in Bootstrap 3 persists

header.php code <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN"> <html lang="EN"> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta http-equiv="Content-Type" content="text/html; charset=UT ...

The functionality of onClick or onChange seems to be malfunctioning in the preline user interface select component

I recently integrated the preline UI library into my React project to enhance the design of my website. However, I encountered an issue where using the "select" element with the data-hs-select attribute as suggested by preline UI seemed to cause problems w ...

Adjusting the line-height in CSS dynamically based on the length of characters using jQuery

I am facing an issue with the Twitter widget on my website where our company's latest tweet sometimes gets cut off if it exceeds a certain length. I want to dynamically adjust the line-height CSS property of the element based on the tweet's chara ...

What are the steps to create a responsive form using CSS?

I have a screenshot below that needs to be recreated using HTML/CSS. This design should be consistent in both desktop and mobile views. https://i.stack.imgur.com/cnfHt.png Currently, I have managed to replicate this in a JSFiddle which looks good on desk ...

Extracting textual information from Wikipedia through iframes?

Currently, I am working on a website project utilizing Squarespace. This site will feature multiple pages dedicated to individuals who have reached a level of notability worthy of having their own Wikipedia page. With over 150 pages planned, manually writi ...

Can you provide guidance on how to showcase the chat date and time for each message in a webchat created with the bot framework SDKV4 using C#

I've successfully created a chatBot using bot framework SDKV4 in C# with the WebChannel as the channel. Everything is functioning properly, but I have a specific goal in mind: For each message that the user types or the Bot replies to, I want a time ...

Should we make it a routine to include shared sass variables in each vue component for better practice?

Within my Vue application, there are numerous components that rely on shared variables like colors. I'm considering having each component import a global "variables.scss" file. Would this approach have any adverse effects? ...

Illustrating SVG links

I'm working on a basic svg animation project where I want to create a simple shape by animating a line under a menu link. The goal is to have a single line consisting of a total of 7 anchors, with the middle 3 anchors (each offset by 2) moving a few p ...

Enhance your webpage design with stylish CSS formatting using Bulma cards

My HTML output is as follows: https://i.stack.imgur.com/aBdEF.jpg It seems that the footer is not matching up with the cards... The CSS component I am using looks like this: .card-equal-height { display: flex; flex-direction: column; height: 100%; ...

Emphasizing Text Within Div Element

Imagine having a div element structured as such: <div id="test" class="sourcecode"> "Some String" </div> Can CSS and JavaScript be utilized to highlight a specific portion of that string based on a search query? For instance, if the search q ...

Delete the CSS class from a specific HTML element

Having just started learning HTML and CSS, I encountered an issue. I applied a CSS class to a table element. Subsequently, I inserted a child table within the parent table. The problem arose when the CSS class of the parent table also influenced the child ...

(CSS) Unusual phantom white outline surrounding menu with no visible border

I've just finished creating my first multi-level drop-down menu, but I'm encountering two white border issues. The first white border appears at the bottom of the menu and seems to be related to the padding of the a-elements. The specific area in ...

Bootstrap tab toggle feature

I'm currently facing an issue with Bootstrap's tab component. I need help figuring out how to hide a lorem ipsum section and show a hidden div when a specific tab is clicked, and then revert the changes when a different tab is selected. $(func ...

Customize the appearance of a hyperlink

I am having difficulty styling a link's title using jQuery-mobile. Each link represents a player with unique abilities. When the user hovers over a player, I want a tooltip to display their special ability. Despite trying various code samples, none ha ...