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

Discover the best practices for integrating media queries using Radium within Material UI components

Having trouble applying a breakpoint to the Material UI component, Raised button. I have managed to make it work for regular divs, but not for the Material UI component. I attempted wrapping the button inside Radium import RaisedButton from 'materia ...

Navigating horizontally with buttons in VueJS

I am searching for a way to implement horizontal scrolling using buttons in VueJS. The idea is to have a container with multiple divs arranged horizontally, and I wish to navigate through them using the buttons. If you want to see a similar solution using ...

Implementing jQuery UI toggleClass method to seamlessly alternate between two distinct CSS classes

My goal is to toggle between two CSS classes on a selector click using Jquery UI .toggleClass(), but unfortunately, it's not producing the desired toggle effect. $(".toggle").click(function () { $(".archivePosts .columns").removeClass( "l ...

Remove the bottom border from the active tab by utilizing the <div> and <a> elements

I am facing an issue with a tabbed menu on my webpage. While the menu is functioning correctly, I am struggling to remove the bottom border from the active tab. You can view all of my test code here. While I have come across solutions using <ul> an ...

Updating the navigation with a dynamic CSS class using jQuery

Hey there, I'm on the lookout for a simple and discreet method to dynamically add an "active" class to a navigation menu based on the current directory. Unfortunately, I am unable to modify the body tag with a custom class, ruling out a pure CSS solut ...

Modifying .vue files within Laravel seems to unexpectedly impact unrelated CSS styles

I've been working on a Laravel project that involves building Vue components. Strangely, every time I update a .vue file and add it for a commit, modifications are also made to the app.css and app.js files. These changes seem to be undoing a particula ...

Align the middle element in a dynamic row

There is a row consisting of three elements: left, center, and right. However, the problem lies in the fact that the center element is not aligned at the screen level, but rather at the row level. Is there a solution to align this element at the screen l ...

Tips on rearranging the location of a div element within a directive

I have created a custom directive that displays two divs, Div1 and Div2, with a splitter in the middle: Splitter Image Now, I am looking for a way to swap the positions of these two divs dynamically using an Angular directive. I thought about using ng-swi ...

How come my wrapper box doesn't extend to the full width of the screen when I set it to width: 100%?

Can someone explain why my wrapper box doesn't fill the whole screen when I use width: 100%, but it does when I use width: 100vw? I have set "width=device-width" on my page and the minimum width for the page is 1400px. Thank you all for your assistan ...

How can I delete a CSS class from an element?

Is there a way to remove the "ui-widget-content" class from the code below? It appears in multiple places throughout my code. Here is an example snippet: <pre> <form id="clientForm"> <div id="clientData"> <div id="gbox_grid_1" class=" ...

Issue with the height of sections in the active menu

While the set-up below is functional for content within section height limits, it fails when exceeding the limit causing overflow. Adding "display: table" or "overflow: hidden" to fix the overflow issue affects the menu's active state behavior. Sett ...

Font animation experiencing limited functionality

As a beginner in the world of programming, I am facing some challenges with my animation. The struggle lies in the fact that only my "Treats" are moving and not the "Tricks". Any guidance or suggestions would be greatly welcomed as I might have jumped into ...

Is it possible to reverse the effects of @media queries for min/max width and height?

I rely on http://www.w3.org/TR/css3-mediaqueries/ to customize my design based on the available viewport size. When I need to adjust the breakpoints, I struggle with negating @media (min-width: 500px), which overlaps with (max-width: 500px). To counter th ...

Is the navigation bar offset causing an issue?

After struggling with my navigation menu in Google Chrome, I finally found a solution. However, the nav bar is now offset on one page but not on another. For the aligned main site view, visit , and for the left-offset forum view, visit . This is the HTML ...

Is it common for functions to be outlined in standard CSS practices?

I am curious if the CSS standard includes definitions for "functions"; I have not come across any information about them on w3schools or other sources. When I refer to "functions," I am talking about calls such as rgb(int, int, int) or url(string) that ar ...

Is all of the CSS stored in one or multiple files?

Will the number of CSS files on my website impact its performance? I understand the necessity of having a separate file for print styles, but I'm wondering about the other CSS files. Is it better to have all my styles in one file or split them into mu ...

The landscape orientation causes the button bar to overflow by 100% of the body height

Imagine the scenario below: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <style type="text/css"> html, body { padding: 0; margin: 0; ...

Testing the screen size automatically using Javascript

Hello everyone, I've implemented a JavaScript code that triggers an image to slowly appear and darken the background when a link is clicked. However, on an iPad, the background doesn't completely turn black as intended. The CSS specifies that the ...

Adjust the vertical size and smoothly lower a text input field

When a user clicks on a textarea, I want it to smoothly change height to 60px and slide down in one fluid animation. Check out the JSFiddle example here: http://jsfiddle.net/MgcDU/6399/ HTML: <div class="container"> <div class="well"> ...

Creating a multi-step progress bar in Yii2 that showcases completed steps using jQuery

Is there a way to automatically transition from one state to another once the order status changes in Yii2? var i = 1; $('.progress .circle').removeClass().addClass('circle'); $('.progress .bar').removeClass().addClass(&a ...