Unexpected Placement of CSS Grid Items

I am currently facing an issue with aligning items in a nested grid using grid-column/grid-row assignment. After setting the template with grid-template-rows:/grid-template-columns, I expected the $50 and Give Now items to appear on row 3, with one in column 1 and the other in column 2. However, they are displaying in column 2 on rows 1 and 2 instead. I am struggling to understand why this unexpected layout is occurring and would appreciate any assistance with resolving this issue.

Thank you.

.mockup {
  display: grid;
  max-width: 303px;
  position: relative;
  margin: auto;
  grid-gap: 10px;
  grid-template-areas: "infobar infobar" "progbar progbar" "main       main" "button2 button3";
  grid-template-columns: 50% 50%;
  grid-template-rows: 65px 20px 250px 50px 50px;
}

.infobar {
  grid-area: infobar;
  padding: 5px 18px;
  border-radius: 4px;
  color: white;
  background-color: #424242;
  font-family: "rooney-web", "AmericanTypewriter";
  white-space: nowrap;
  text-align: center;
}

.infobar:before {
  content: ' ';
  display: block;
  position: relative;
  bottom: -48px;
  width: 14px;
  height: 18px;
  right: -247px;
  background-color: #424242;
  -webkit-transform: rotate(45deg);
  -moz-transform: rotate(45deg);
}

.progbar {
  grid-area: progbar;
  border: 1px solid #777;
  margin: none;
  background: linear-gradient(to right, #EF5F3C 75%, #eaeaea 25%);
}

.main {
  grid-area: main;
  border: 1px solid #777;
  display: grid;
  grid-template-columns: 50% 50%;
  grid-template-rows:  repeat(4, 25%)
}



.main .input p4{
  border: 1px solid #777;
  border-radius: 4px;
  grid-column: 1;
  grid-row: 3;
  font-size: 20px;
  z-index: 1;
  
}

.main .button1 p3 {
  grid-column: 2;
  grid-row: 3;
  white-space: nowrap;
  background-color: Green;
  border: 1px solid #777;
  border-radius: 4px;
  text-align: center;
  color: white;
  z-index: 1;
}

.main p1 {
  grid-column: 1 / 2;
  grid-row: 1;
}

.main p2 {
  grid-column:1 / 2;
  grid-row: 2;
  
}

.main p5 {
  grid-column: 1 / 2;
  grid-row: 4;
  color: #20A1D4;
}

.button2 {
  grid-area: button2;
  border: 1px solid #777;
  border-radius: 4px;
  text-align: center;
  background-color: #eaeaea;
}

.button3 {
  grid-area: button3;
  white-space: nowrap;
  border: 1px solid #777;
  border-radius: 4px;
  text-align: center;
  background-color: #eaeaea;
}
<!DOCTYPE html>
<html>

  <head>
    <link rel='stylesheet' type='text/css' href='style.css'>
  </head>

  <body>
    <div class="mockup">
      <section class="infobar">$167 still needed for this project</section>
      <section class="progbar"></section>
      <section class="main">
        <p1>
          <span style="color: #EF5F3C">Only 3 days           left</span> to fund this project.
        </p1>
        <p2>
          Join the <strong>42</strong> others who             have already supported this project.               Every dollar helps.
        </p2>
      <section class="input">
        <p4>
          $ <strong>50</strong>
        </p4>
      </section>
      <section class="button1">
        <p3>
          Give Now
        </p3>
      </section>
        <p5>
          Why give $50?
        </p5>
      </section>
      <section class="button2">
        <p>
          Save for later
        </p>
      </section>
      <section class="button3">
        <p>
          Tell your friends
        </p>
      </section>
    </div>
  </body>

</html>

View the code snippet on JSFiddle here

Answer №1

Your grid calculations seem to be a bit off. Instead of counting columns, you should be counting lines. Take a piece of scrap paper and draw 3 vertical lines (representing your two columns) and then horizontal lines for your rows. When you designated p1, p2, and p5 for column spans, you incorrectly stated that columns one and two were 1/2. In reality, you should be focusing on the lines on your scrap paper. These classes should begin on the first line and end on the third line. Therefore, by adjusting your CSS accordingly, you will be good to go. For more information, check out the link below.

.main p1 {
  grid-column: 1 / 3;
  grid-row: 1;
}

.main p2 {
  grid-column:1 / 3;
  grid-row: 2;
}

.main p5 {
  grid-column: 1 / 3;
  grid-row: 4;
  color: #20A1D4;
}

w3schools' CSS Grid Item

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

Steps to Embed a Link in a Jumbotron's Background Image

I'm facing a challenge with my jumbotron that has a background image. I want to make the image clickable so that it redirects to another page when clicked. I attempted to add an image tag with a src attribute, but it ended up overlaying another image ...

What is the best way to show a pop-up model at the bottom of a webpage?

I am working on a project that involves a popup modal that needs to appear after 10 seconds. However, the issue I am facing is that the modal is currently displaying at the top of the page, but I would like it to be shown at the bottom of the page just abo ...

The menu item is having trouble aligning to the right side

Hello, I am currently working on developing a website menu for a project and I am facing an issue with aligning the "Login" Menu item to the right side. Any assistance in resolving this alignment issue would be greatly appreciated. I am also willing to pro ...

Steps to Show an Image When Hovering and Clicking on the Relevant Div

CSS: .imgECheck { position: absolute; top: 0; right: 5px; width: 15px; height: 15px; display: none; } .imgFCheck { position: absolute; top: 0; right: 5px; width: 15px; height: 15px; display: none; } Is ther ...

What are some strategies for disregarding the effects of the !important rule

After incorporating some HTML and CSS styling into a third-party site, I encountered an issue where their styling was conflicting with mine, specifically due to certain !important declarations. I want to avoid this conflict without resorting to using !impo ...

Use CSS bracket attribute to conceal link with no HTML access

I am unable to modify the HTML code, but I need to find a way to hide the link (#wall). <td class="status_value"><span class="online"> - <a href="#wall"> Leave a Comment </a> <a href="#today"> ...

Differences in Print Layout Between Chrome and Firefox when Viewing a PHP Website

I have been working on creating a print command to print a specific div, and I have managed to successfully run the print command using default methods like CTRL + P and also with a button click. The Issue: However, I have encountered a problem where the ...

The display function in Javascript has a tendency to work sporadically

I’ve been tasked with creating my own tic tac toe game through coding. While I am relatively new to programming, I have a strong passion for it. At the moment, I've set up a basic function to hide only the "O", leaving only the "X" visible on the gr ...

What is the best way to place an image above a step progress bar?

I need assistance with adding an image or icon above each step in the progress bar. I attempted to insert an image tag, but it ended up next to 'step 1', which is not the desired outcome. .progressbar { counter-reset: step; } .progressbar li ...

Display or conceal div elements using JavaScript

Is there a way to hide certain divs when clicking on a specific div box? Clicking on the box will trigger the hiding of some application divs. See the first image below: When the boxes are hidden, the other divs will readjust in place of the current divs ...

Recalling the layout following the user's computer restart

I am creating a simple online editing tool (similar to Microsoft Outline) for coursework and I would like the outline details to be saved even if the user restarts the system. How can I achieve this? <html> <head> <title>Editor</ ...

What is the best way to create space between three columns in Bootstrap?

Despite my efforts to find a solution on Stackoverflow, I have not been able to locate exactly what I need. I am trying to evenly space and center three columns across my page. However, when I assign col-md-4 to each column, they end up too close together ...

Issues have been reported regarding compatibility between iOS7, jquery.touchSwipe, on click and webkit-touch-callout

I'm in the process of developing a web application, but I've encountered an issue with iOS7 where there seems to be a conflict between jquery.touchSwipe and my "on click" events. Interestingly, the web app functions perfectly on older iOS versio ...

Jquery plugin experiencing a malfunction

I am encountering an issue with my custom plugin as I am relatively new to this. My goal is to modify the properties of div elements on a webpage. Here is the JavaScript code I am using: (function($) { $.fn.changeDiv = function( options ) { var sett ...

Can you explain the process of gathering texts based on CSS selector?

I wanted to place texts next to the div-class as shown in the following code snippets. <div class="review-contents__text">소재가 좀 저렴해 보이지만 그래도 입으면 휠씬 나아보여요</div> Initially, I wrote a code ...

Unique background image that perfectly aligns with the dimensions of a single full screen

Does anyone know how this webpage achieves the effect of having a full-screen background that ends when scrolling down? I have noticed that the picture of the desert always covers my entire browser screen but is not a typical full-screen background as it ...

Jquery bypasses original stylesheet settings when inline styles are removed

I have a CSS file with various styles defined within it. One style in particular has the property position set to 'fixed', but this only applies to specific combinations of classes. For example: .mystyle.blue { position: fixed; } Here, el ...

React component is unable to identify prop

I'm attempting to send an array of objects from the main App.js file to a smaller component using props. However, for some reason, the prop is not being recognized within the smaller component file. https://i.stack.imgur.com/WuyFr.png https://i.stac ...

Display elements on hover of thumbnails using CSS

I'm struggling with the logic of displaying images when hovering over corresponding thumbnails using only CSS. If necessary, I can do it in JavaScript. Here's my latest attempt. <div id='img-container' class='grd12'> ...

How can I place my container above my header in the layout?

I'm facing difficulty positioning the container above my header. Here is an example of what I am aiming for: No matter what I attempt, strange occurrences like the NAV disappearing or divs overlapping keep happening. HTML: <!DOCTYPE html PUBLIC ...