Elements within the CSS grid are misaligned

After spending hours on it, aligning my grid elements vertically seems like a simple task that I just can't seem to crack. Check out the fiddle for a better idea of what I'm talking about. Any help with this issue would be greatly appreciated.

HTML

 <section class="top">
   <div></div>
   <div></div>
 </section>

 <section class="bottom">
   <div></div>
   <div></div>
   <div></div>
 </section>

CSS

 section {
   display: grid; 
   grid-template-rows:auto; 
   margin: 40px 0 0 0
 }

 section.top {
   grid-template-columns:2fr 1fr;
   grid-column-gap: 50px;
 }

 section.bottom {
   grid-template-columns:1fr 1fr 1fr;
   grid-column-gap: 50px;
 }

 section div {
   background:lightblue;
   height:400px
 }

https://jsfiddle.net/ecj1wrae/

Answer №1

After some thoughtful consideration and calculations, I managed to find a solution.

CSS

section.top {
  grid-template-columns:calc(66% + 2vw) 34%;
  grid-column-gap: 2vw;
}

section.bottom {
  grid-template-columns:33% 33% 34%;
  grid-column-gap: 2vw;
}

Check out the result here!

Answer №2

While this question may be considered old, I faced a similar issue and wanted to share the solution that worked for me.

I came across a helpful comment that pointed me in the right direction.

The use of fr units for dividing space means only free space within the container is considered. Since the bottom section has 50px less free space than the top, alignment using fr units is not possible in this scenario.

To address the gap between columns, I found it more effective to approach the problem by having the layout span 2 columns out of a 3 column setup instead of focusing on occupying 2fr of a 3fr layout.

Thankfully, CSS grid allows us to specify the number of columns an element can span using grid-column: span 2;

By utilizing the provided HTML from the question, we can apply a span to the first div in the top section:

section {
  display: grid; 
  margin: 40px 0 0 0;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-column-gap: 50px;
}

section.top div:first-child {
   grid-column: span 2;
}

Answer №3

If you want to achieve this with just one parent element, here is a possible solution:

.container {
  display: grid;
  grid-template-columns: auto auto auto auto;
  grid-row-gap: 50px;
  grid-column-gap: 10px;
  grid-template-areas: 
    'item1 item1 item1 item1 item2'
    'item3 item3 item4 item4 item5';
}
.container > div {
  background: red;
  height: 400px;
}

.item1 {
  grid-area: item1;
}
.item2 {
  grid-area: item2;
}
.item3 {
  grid-area: item3;
}
.item4 {
  grid-area: item4;
}
.item5 {
  grid-area: item5;
}
<div class="container">
  <div class="item1"></div>
  <div class="item2"></div>
  <div class="item3"></div>
  <div class="item4"></div>
  <div class="item5"></div>
</div>

Answer №4

An easy workaround would be to adjust the spacing between columns in the grid layout by using margins instead of the grid-column-gap property, especially if having extra space on the sides is not an issue:

Below is the HTML structure:

<section class="top">
   <div></div>
   <div></div>
</section>

<section class="bottom">
   <div></div>
   <div></div>
   <div></div>
</section>

This is how the CSS styling looks like:

section {
   display: grid; 
   grid-template-rows:auto; 
   margin: 4px 0 0 0
 }

 section.top {
   grid-template-columns:2fr 1fr;
 }

 section.bottom {
   grid-template-columns:1fr 1fr 1fr;
 }

 section div { 
   background:lightblue;
   height:400px;
   margin: 5px;
 }

You can view the code example here: https://codepen.io/fillsanches/pen/oNEpKWN

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

Once the image is loaded, cropped, and displayed on the canvas, what is the best way to implement image rotation functionality for the user before converting it to base64?

My process involves cropping images to a square shape and drawing them onto a canvas. if(img.height >= img.width) { ctx.drawImage(img, 0, 0, 110, 110 * img.height / img.width); } else { ctx.drawImage(img, 0 , 0, 110 * img.width ...

Customize the appearance of a <label> tag when it is inside an <input> tag that is marked as invalid

In an ideal scenario, I wish for the text on a label to change color when the input value is considered invalid. For instance: <form> <label> Enter your name: <input type="text"> </label> </form> Ideall ...

Using Bootstrap, creating a full-height body with multiple scrollable divs

Struggling to replicate a HTML layout similar to the one shown in Figure 1 using bootstrap. Figure 1 Despite numerous attempts, I am still unable to fully achieve the layout using bootstrap's row and column classes. If anyone can offer guidance, it ...

Combining input elements and captchas in one line within Bootstrap

I am currently working on a form group that includes a label, a captcha image, and an input field for the captcha code. My goal is to have the label positioned at the top of the form group, with the captcha image and input field aligned in one line. I have ...

Deleting a division with the help of media queries

As a challenge, I am attempting to eliminate the .navbar-brand>img element once the screen size exceeds 768px by using media queries. Unfortunately, my efforts have not been successful so far. Although the styling applied to the img is successfully remo ...

Expanding Tile Size in Grids Using CSS and JavaScript

I have a strong grasp of CSS and JS, utilizing jQuery for this particular project. The design includes a fluid grid made up of an unordered list with 'li' tags. Each tile has uniform width and height, adjusting to the browser window size by wrapp ...

Ways to enhance an image by zooming in when the user reaches a designated area on the webpage

I have implemented a feature where an image zooms in to letter L when the user scrolls on the page. However, I want this zoom effect to occur only when the user reaches a specific section of the site, rather than immediately when the image loads. In my exa ...

Finding all instances of a specific class in Sublime Text 2

Is there a way in Sublime Text 2 to search for every instance of a specific class applied to an element? For example, if I have 20 .jsp's and I want to find every element with the class "sample," is there a method to do this beyond just searching for ...

Adjust the vertical position of the image with HTML and CSS

I want to adjust the positioning of the image below the blue navigation bar so that it aligns with the main content area below, creating the appearance of a shaded box around the webpage's content. The image in question is boy_top.png. Is there a way ...

Modifying the URL of an image based on screen size with the @media property

Currently working on developing a responsive webpage. An interesting challenge is presenting two distinct images for the desktop and mobile views. Attempted to switch between images by utilizing the @media feature within CSS, as shown below. #login-top-s ...

CSS horizontal scrolling for unaligned unordered lists

If you take a look at this code snippet, you will notice an issue with the layout http://jsfiddle.net/qo9czztj/ The problem arises when the title of an image is longer than one line, causing the image to shift and creating an uneven appearance within the ...

Content within the two inline divs is experiencing alignment problems?

I am experiencing alignment issues with the content inside two divs that contain multiple nested divs. The content is not properly aligned. CSS Code: .breadcrumb-nav{ margin-left: 230px; left: 70px; top: 70px; width: 1291px; height: 6 ...

Using Javascript or jQuery to Enhance the Appearance of Text on a Website

Is there a way to automatically apply styling to specific phrases on our website by searching for instances of those phrases? For example: <div> This is what you get from <span class="comp">Company Name</span>. We do all kin ...

Alignment problem with email signatures on MAC devices

Recently, I designed a straightforward email signature and successfully copied and pasted it into the appropriate section. However, while it works perfectly in Outlook, there is an issue with MAC Mail (Version 7.3) where the image extends beyond the table. ...

Alter the border hue of the checkbox and radio button

Is there a way to modify the border color of checkboxes and radio buttons (both circle and rectangle) using CSS? I've attempted several methods with no success. Can someone provide guidance on this? Any advice would be greatly appreciated. Thank you ...

how can you make keyframe animation pause at the last stage?

Here is an example of utilizing CSS animations: .show-left-menu { -webkit-animation-name: leftSidebarAni; -webkit-animation-duration: .25s; -webkit-animation-timing-function: ease-out; -webkit-animation-iteration-count: 1; } @-webkit-keyframes l ...

Issue with flexbox max-width property not being applied when resizing the page width

I'm struggling to make a flex box with max and min width properties work properly. When I reduce the page size, it ends up showing blank space instead of resizing. How can I troubleshoot this issue? .parent{ border: 1px solid red; width: 50%; ...

Ways to prevent the :link style being applied to every link

Is there a way to apply this CSS styling to only one specific link and not all the links on my page? Here is the CSS code that is currently being applied to all links: a:visited { font-family: "Trebuchet MS", Arial, Helvetica, sans-serif; font-size: 14px ...

Stop CSS tooltip from overflowing outside of the page or window

One issue I am facing is with a CSS-only tooltip that uses a span to display the tooltip when hovering over a link. The problem arises when the link is positioned near the top or side of a page, causing the tooltip to extend beyond the edge of the page. I ...

Ways to design a parent element based on the presence of a particular child element

I am facing an issue with applying a default background color to #parent in HTML when it contains a #child. <div id="parent"> <div id="child"></div> </div> Even though I tried using the CSS code below, the :contains pseudo selec ...