When arranging elements using the @media feature in CSS grid, a phantom element seems to mysteriously appear

Apologies for the extensive uploads, I'm not sure how else to proceed. I'm attempting to rearrange my grid layout at a specific width so that the paragraphs repeat with the images. However, I'm encountering a random space between 1 and 2 when I do this. Upon inspecting with F12, it appears as if there is a space element, even though it is part of the .content section. Can someone help me understand what's happening?

  * {
  font-family: 'Courier New', Courier, monospace;
  margin: 0;
  padding: 0;
}

body {
  width: 100%;
  height: 2600px;
}

header {
  background-color: black;
  padding: 50px 0;
}

h1 {
  font-size: xx-large;
  text-align: center;
  color: white;
}

.content {
  height: 1000px;
  display: grid;
  grid-template: repeat(3, 1fr)/repeat(2, 1fr);
  justify-content: center;
  align-items: center;
  margin-top: 100px;
  row-gap: 4em;
}

.content-image {
  margin: auto;
  width: 300px;
  height: 300px;
}

.aparagraph {
  width: 300px;
  height: 300px;
  font-weight: 700;
  font-size: larger;
  text-align: center;
  vertical-align: middle;
  margin: 0 auto;
}

@media (max-width: 965px) {
  .content {
    max-height: 2600px;
    display: grid;
    grid-template: repeat(5, 1fr)/1fr;
    justify-content: center;
    align-items: center;
    margin-top: 100px;
  }
  .p1 {
    order: 1;
  }
  .p2 {
    order: 2;
  }
  .p3 {
    order: 4;
  }
  .img1 {
    order: 2;
  }
  .img2 {
    order: 3;
  }
  .img3 {
    order: 5;
  }
<body>
  <header>
    <h1>Grid-Pratice</h1>
  </header>

  <main>
    <section class="content">

      <p class="aparagraph p1">Lorem ipsum dolor sit amet consectetur adipisicing elit. Harum soluta atque voluptatibus amet sint voluptatem illo, eos blanditiis. Ducimus dicta deleniti cum est soluta repellat aliquid asperiores nesciunt recusandae eos?</p>
      <img src="images/Black and White Four Arm Monster Clip Art - Black and White Four Arm Monster Image.png" alt="" class="content-image img1">

      <img src="images/Graphics by Ruth - Monster.jpg" alt="" class="content-image img2">
      <p class="aparagraph p2">Lorem ipsum dolor sit amet consectetur adipisicing elit. Harum soluta atque voluptatibus amet sint voluptatem illo, eos blanditiis. Ducimus dicta deleniti cum est soluta repellat aliquid asperiores nesciunt recusandae eos?</p>

      <p class="aparagraph p3">Lorem ipsum dolor sit amet consectetur adipisicing elit. Harum soluta atque voluptatibus amet sint voluptatem illo, eos blanditiis. Ducimus dicta deleniti cum est soluta repellat aliquid asperiores nesciunt recusandae eos?</p>
      <img src="images/Mono Monster Urban Threads Unique and Awesome Embroidery Designs.jpg" alt="" class="content-image img3"> '
    </section>

  </main>

</body>

It seems that the @media code is changing to u/media when copied and pasted.

Click here to see how it appears in my standalone browser

Answer №1

Within your code, you have applied the margin-top : 100px attribute to the content class. This value of 100px may not appear as a significant space on larger screens, but on smaller screens, it can create a larger gap.

If desired, you can reduce this to margin-top:50px as seen in the following code snippet. The value of 50px seems appropriate in my opinion.

Additionally, when incorporating the media query, it is recommended to set

margin-top:0px</code to eliminate any unexpected spacing.</p>
<p>Furthermore, I noticed that a height is set for the paragraph in your code. While this may not cause issues in a two-column display on larger screens, it can lead to additional space on smaller screens with a single column. It would be beneficial to remove this height setting.</p>
<p>I hope the following code provided resolves your concerns.</p>
<p><div>
<div>
<pre class="snippet-code-css lang-css"><code>* {
    font-family:'Courier New', Courier, monospace; 
    margin:0; 
    padding:0; 
   }
    
    body{ 
    width:100%; 
    height: 100%; 
    }
    
    header{ 
    background-color: black; 
    padding: 50px 0; 
    }
    
    h1{ 
    font-size:xx-large; 
    text-align: center; 
    color:white; 
    }
    
    .content{ 
    height:1000px; 
    display:grid; 
    grid-template-columns: repeat(2, 1fr); 
    justify-content: center; 
    align-items: center; 
    margin-top:50px; 
    row-gap: 4em; 
    }
    
    .content-image{
    margin:auto; 
    width:300px; 
    height:300px; 
    }
    
    .aparagraph{ 
    width:300px; 
    font-weight: 700; 
    font-size:larger; 
    text-align: center; 
    vertical-align: middle; 
    margin: 0 auto; 
    }
    
@media (max-width: 965px){ 
.content{ 
max-height:2600px; 
display:grid; 
grid-template:repeat(5, 1fr)/1fr; 
justify-content: center; 
align-items: center; 
margin-top:0px;     
}
    
    .p1{ order:1;     }
    
    .p2{ order:2;     }
    
    .p3{ order:4;     }
    
    .img1{ order:2;     }
    
    .img2{ order:3;     }
    
    .img3{ order:5;     }
<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" type="text/css" href="style.css">

</head>
<body>
    <header>
        <h1>Grid-Pratice</h1>
    </header>
    
            <main>
                <section class="content">
            
                    <p class="aparagraph p1">Lorem ipsum dolor sit amet consectetur adipisicing elit. Harum soluta atque voluptatibus amet sint voluptatem illo, eos blanditiis. Ducimus dicta deleniti cum est soluta repellat aliquid asperiores nesciunt recusandae eos?</p>
                    <img  src="images/Black and White Four Arm Monster Clip Art - Black and White Four Arm Monster Image.png" alt="" class="content-image img1">
    
                    <img  src="images/Graphics by Ruth - Monster.jpg" alt="" class="content-image img2">
                    <p class="aparagraph p2">Lorem ipsum dolor sit amet consectetur adipisicing elit. Harum soluta atque voluptatibus amet sint voluptatem illo, eos blanditiis. Ducimus dicta deleniti cum est soluta repellat aliquid asperiores nesciunt recusandae eos?</p>
    
                    <p class="aparagraph p3">Lorem ipsum dolor sit amet consectetur adipisicing elit. Harum soluta atque voluptatibus amet sint voluptatem illo, eos blanditiis. Ducimus dicta deleniti cum est soluta repellat aliquid asperiores nesciunt recusandae eos?</p>
                    <img  src="images/Mono Monster Urban Threads Unique and Awesome Embroidery Designs.jpg" alt="" class="content-image img3">
                '
                </section>
    
            </main>
    
        </body>


</html>

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

I'm curious as to why a webpage tends to load more quickly when CSS files are loaded before script files. Can anyone shed some

While watching a video, I came across some concepts that were a bit difficult for me to grasp. The video mentions that scripts are 'serialized' and can block subsequent files from loading. According to this explanation, Script 1 would load first ...

Using scale transformations to animate SVG group elements

I am currently experimenting with an SVG example where I hover over specific elements to expand or scale them. However, I seem to have made a mistake somewhere or missed something important. Can someone offer me assistance? View the demo on JSFiddle here ...

What could be the reason behind the occurrence of an error after deleting certain lines of code

The code below is functioning correctly. obj = { go: function() { alert(this) } } obj.go(); // object (obj.go)(); // object (a = obj.go)(); // window (0 || obj.go)(); // window However, an error arises when I comment out the first two lines. obj ...

Having difficulty changing placeholder text style in Scss

I'm a newcomer to SCSS and I'm attempting to customize the color of my placeholder text from light gray to dark gray. Below is the HTML code snippet: <div class="form-group"> <textarea class="thread-textarea" ng-maxlength="255" ma ...

Deciphering HTML with the Eyes

So, I find myself in a bit of a bind trying to come up with a title for this question. I have stumbled upon some HTML files that seem so complex, they could only have been crafted by the one and only Lord Lucifer himself. These files contain segments like ...

Display a div based on search results

I recently encountered an issue with a script that needs modification to display different divs based on search criteria. Originally, I used this script for a contact list but now need it to perform another function. View the original code (JSFiddle) Here ...

Implementation demonstration / library for complete URL encoding

Currently, I am developing a Java application that extracts links from HTML and utilizes them to request their content. One particularly challenging area is URL encoding when the intent of the URL author is unknown. Determining whether to use %20 or + can ...

Semantic UI utilizes floating elements to position them within a

As a beginner with Semantic UI, I have been unable to find any information in the documentation on how to float a simple element without it needing to be nested within another element like a button or a segment. For example, at the bottom of a page, I hav ...

show: alongside move: towards the left

Reviewing some vintage CSS code from 2008, I stumbled upon the following CSS snippet for a menu/navigation: #menu li { display: inline; } #menu a { display: block; float: left; height: 32px; margin: 0; padding: 18px 30px 0 30px; ...

Navigating to the end of a list using Angular's scroll feature

I'm trying to figure out how to automatically scroll to the bottom of a list in TypeScript, can someone help me with this? Here's a similar example I found that uses jQuery (I specifically need it in TypeScript): Scroll to bottom of list wit ...

Place a Button or Link Right Below the Title on the Custom Post Type Editing Page

I am currently working on customizing the backend of a WordPress site for a client by adding multiple custom post types. These custom posts are only meant to be displayed on the front-end through a specific loop on one page, preventing users from accessing ...

By simply clicking a button in a React component, I aim to alter the font style of the text

function makeTextBold() { const boldText = document.querySelector('.form-control'); boldText.style.fontWeight = 'bold'; setText(boldText); } When I click the button, it redirects me to a blank page in the browser. ...

Issue in Opera with the $(window).load(function(){}); script

In order to display the body's main div height correctly after all content (images) have loaded, I utilized a combination of jQuery and CSS. //The CSS used is as follows body {display: none;} /* Function to calculate div block height */ function re ...

Load a form using ajax and submit it using jQuery

For a while now, I have been facing challenges in figuring out what's going wrong with my code. The issue arises when I try to submit a form using jQuery that was loaded through ajax. The form loads perfectly fine within a div after invoking the ajax ...

Conceal a different div unless it includes

Hi everyone, I need help with a filter code snippet: $(".title").not(":contains('" + $("[name=filter]").val() + "')").hide() The issue I'm facing is that the .title class is nested within the .sortAll class along with many other divs. I w ...

Is there a way to personalize the link in a Cognito confirmation email?

Users who register through my application will receive an email from Cognito containing a verification link. AWS Cognito offers a template ({## text ##}) that will be converted into a link, such as <a href='link-to-confirm'> text </a> ...

What is preventing the JQuery dialog from opening?

When I try to trigger a dialog box by pressing the enter key, it is not working as expected. Instead of opening the dialog, it just hides the text. Can someone help me understand what might be causing this issue? <!doctype html> <html lang="en" ...

Banner with video background (not taking up the entire page)

I'm currently working on a website project that involves using a YouTube video as the banner, but I'm facing some challenges in making it work correctly. So far, this is what I have: #video-background { position: absolute; right: 0; top:1 ...

What causes an absolutely positioned element to be positioned by its sibling rather than at the top corner of the page?

Can someone explain to me why my absolutely positioned element is displaying after my child_static div? I'm under the impression that absolutely positioned elements are removed from the normal flow. So why isn't child_absolute covering the child_ ...

Display only the initial word in a hidden input value of an HTML document using NodeJS Express

I have a Node.js application that utilizes EJS as its template engine. On multiple pages, I incorporate hidden inputs and retrieve their values using req.body. Although my body parser in Express is configured correctly, I've noticed an issue where hid ...