Achieve an overlapping effect with grid items

I'm in the process of creating a CSS grid layout where the header overlaps the next row.

Below is a snippet of my code with the header positioned on top, and the linked image should give you an idea of what I'm aiming for.

Thank you!

https://i.sstatic.net/8DJf4.jpg

body { 
  display: grid;
  grid-template-areas: 
    "header header header"
    "nav article ads"
    "footer footer footer";
  grid-template-rows: 80px 1fr 70px;  
  grid-template-columns: 20% 1fr 15%;
  grid-row-gap: 10px;
  grid-column-gap: 10px;
  height: 100vh;
  margin: 0;
  }  
/* Stack the layout on small devices/viewports. */
@media all and (max-width: 575px) {
  body { 
    grid-template-areas: 
      "header"
      "article"
      "ads"
      "nav"
      "footer";
    grid-template-rows: 80px 1fr 70px 1fr 70px;  
    grid-template-columns: 1fr;
 }
}
header, footer, article, nav, div {
  padding: 1.2em;
  background: gold;
  }
#pageHeader {
  grid-area: header;
  }
#pageFooter {
  grid-area: footer;
  }
#mainArticle { 
  grid-area: article;      
  }
#mainNav { 
  grid-area: nav; 
  }
#siteAds { 
  grid-area: ads; 
  }
<header id="pageHeader">Header</header>
<article id="mainArticle">Article</article>
<nav id="mainNav">Nav</nav>
<div id="siteAds">Ads</div>
<footer id="pageFooter">Footer</footer>

Answer №1

You are utilizing the grid-template-areas attribute to organize the layout using ASCII art.

body { 
  display: grid;
  grid-template-areas: 
    "header header header"
    "nav article ads"
    "footer footer footer";
  ...
}

This technique is effective for non-overlapping grid areas.

However, since strings cannot overlap in the code, this method does not support overlaying grid areas.

In simpler terms, "header header header" and "nav article ads" can be stacked above and below each other on the X and Y axes, but not the Z axis.

To achieve overlapping grid areas, an alternative method is required.

CSS Grid offers line-based placement as a substitute for grid-template-areas.

Line-based placement involves utilizing the following properties to specify a grid item's size and position:

  • grid-row-start
  • grid-row-end
  • grid-column-start
  • grid-column-end

and the shorthands:

  • grid-column
  • grid-row

body {
  display: grid;
  grid-template-rows: 80px 1fr 70px;
  grid-template-columns: 20% 1fr 15%;
  grid-gap: 10px;       /* shorthand for grid-row-gap and grid-column-gap */
  height: 100vh;
  margin: 0;
}
#pageHeader {
  grid-row: 1;          /* see notes below */
  grid-column: 1 / 4;   /* see notes below */
  background-color: crimson;
  z-index: 1;
  opacity: .5;
}
#pageFooter {
  grid-row: 3;
  grid-column: 1 / 4;
}
#mainArticle {
  grid-row: 1 / 3;
  grid-column: 2 / 3;
}
#mainNav {
  grid-row: 1 / 3;
  grid-column: 1 / 2;
}
#siteAds {
  grid-row: 1 / 3;
  grid-column: 3 / 4;
}
header, footer, article, nav, div {
  padding: 1.2em;
  background: gold;
}
<header id="pageHeader">Header</header>
<article id="mainArticle">Article</article>
<nav id="mainNav">Nav</nav>
<div id="siteAds">Ads</div>
<footer id="pageFooter">Footer</footer>

The grid-row and grid-column properties determine the location and dimensions of grid items.

  • grid-column: 1 / 4 indicates that the grid item spans from column lines one to four (covering the first, second, and third columns)
  • grid-row: 3 specifies that the grid item is positioned on row three (from row line 3 to auto, default when the second value is omitted.)

Using this positioning method for grid items allows for easy overlapping.

jsFiddle

Answer №2

To accomplish your desired outcome, consider implementing multiple solutions.

Solution one
One approach is to utilize the CSS styling position: fixed on the header. This will keep the header at the top of the page even when scrolling down. You can enhance this by setting the header's opacity to 0.8 and increasing it to 1 on hover for a nice effect.

Example of Solution one

    body {
        display: grid;
        grid-template-areas:
                "header header header"
                "nav article ads"
                "footer footer footer";
        grid-template-rows: 80px 1fr 70px;
        grid-template-columns: 20% 1fr 15%;
        grid-row-gap: 10px;
        grid-column-gap: 10px;
        height: 100vh;
        margin: 0;
    }
    /* Responsive layout for small devices/viewports */
    @media all and (max-width: 575px) {
        body {
            grid-template-areas:
                    "header"
                    "article"
                    "ads"
                    "nav"
                    "footer";
            grid-template-rows: 80px 1fr 70px 1fr 70px;
            grid-template-columns: 1fr;
        }
    }
    header, footer, article, nav, div {
        padding: 1.2em;
        background: gold;
    }
    #pageHeader {
        grid-area: header;
        z-index: 1999;
        position: fixed;
        background-color: orange;
        width: 100%;
        opacity: 0.8;
    }
    #pageHeader:hover {
        opacity: 1;
    }
    #pageFooter {
        grid-area: footer;
    }
    #mainArticle {
        grid-area: article;
    }
    #mainNav {
        grid-area: nav;
        height: 2000px;
    }
    #siteAds {
        grid-area: ads;
    }
<header id="pageHeader">Header</header>
<article id="mainArticle">Article</article>
<nav id="mainNav">Nav</nav>
<div id="siteAds">Ads</div>
<footer id="pageFooter">Footer</footer>

Solution two
Another method involves using negative margins beneath the header. By applying <div id="main-content"> to content below the header with a negative top margin such as margin-top: -100px;, you can achieve the desired effect.

Example of Solution two

    body {
        display: grid;
        grid-template-areas:
                "header header header"
                "nav article ads"
                "footer footer footer";
        grid-template-rows: 80px 1fr 70px;
        grid-template-columns: 20% 1fr 15%;
        grid-row-gap: 10px;
        grid-column-gap: 10px;
        height: 100vh;
        margin: 0;
    }
    /* Responsive layout for small devices/viewports */
    @media all and (max-width: 575px) {
        body {
            grid-template-areas:
                    "header"
                    "article"
                    "ads"
                    "nav"
                    "footer";
            grid-template-rows: 80px 1fr 70px 1fr 70px;
            grid-template-columns: 1fr;
        }
    }
    header, footer, article, nav, div {
        padding: 1.2em;
        background: gold;
    }
    #pageHeader {
        grid-area: header;
        background-color: orange;
        opacity: 0.8;
    }
    #pageFooter {
        grid-area: footer;
    }
    #mainArticle {
        margin-top: -100px;
        grid-area: article;
    }
    #mainNav {
        margin-top: -100px;
        grid-area: nav;
    }
    #siteAds {
        margin-top: -100px;
        grid-area: ads;
    }
<header id="pageHeader">Header</header>
<article id="mainArticle">Article</article>
<nav id="mainNav">Nav</nav>
<div id="siteAds">Ads</div>
<footer id="pageFooter">Footer</footer>

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

What is the best way to implement a required input field in Vue.js?

I need some assistance with my chat functionality. I want to prevent users from sending empty messages, so I want to make the input field required. Can you please help me with this? I have already tried adding "required='required'" to the input ...

Tips for safeguarding primary design elements from modifications by user-contributed styles in a text entry box

Currently utilizing Wordpress, but I believe my inquiry pertains to any platform where users are granted frontend posting capabilities. I am interested in enabling users to customize the style of their posts, specifically limited to the description area. ...

Creating aesthetically pleasing class names using SASS and CSS modules

I'm in the midst of working on a Next.js project and experimenting with SCSS/SASS for my styling needs. I'm currently grappling with how to streamline the process of applying class names. Here's the issue at hand: Within one of my componen ...

Animation using jQuery is functional on most browsers, however, it seems to

After creating an animation to simulate an opening door using jQuery, I discovered that it works perfectly on Firefox 24, Chrome 28, and IE 8. However, Safari presents a problem - the door opens but then the "closed" door reappears at the end of the animat ...

the quickest method to apply font-weight:bold; to the text within the component

Is there a way to apply font-weight: bold; only to the inner content of a component in a scss file while avoiding affecting the component tag itself? :host { font-weight: bold; } Although this code works, it also affects the component tag itself. What ...

Various Mobile Devices Display Website in Unique Ways

I've been having trouble getting my website to display consistently across different phones and could use some assistance. You can view the site at www.fpphotos.store. I have set overflow-x:auto; for the table containing the images, with CSS styles de ...

Centered HTML Columns on the Screen

Figuring out this simple html + css issue is proving to be quite challenging for me. I am attempting to create a '3 column' layout. I envision a central column (approximately 10em in width) with two additional columns flanking it on each side (e ...

Using jQuery UI to create a bouncing effect on a CSS sprite image

Want to give your social icons a bounce effect using jQuery UI Bounce? I'm following a template and some jQuery documentation, but having trouble getting it right. It seems like the sprite image for the icons might be causing the issue. Can someone h ...

How can I alter the image using the success function in jQuery?

I'm encountering an issue with my jQuery voting script. Everything seems to be working correctly, except for the fact that the image in the success function of the AJAX request is not updating as expected. jQuery: $("a.vote_down").click(function(){ ...

Strategies for Locating XPath Using Text Content within Table Cells (td / tr)

Need help with searching for specific text within a large HTML table of emails and selecting a button within the element? You can easily find the table body via XPATH by using: //*[@id="reportList"]/tbody Within this table, there are multiple rows (tr). ...

When hovering over one div, both it and another div should be displayed simultaneously. The second div should continue to be displayed even when hovered over

I am looking to keep another div displayed when hovering over it - in this example, it says "hello." The div I want to remain visible is not a child element of the main div where I initiate the hover event. document.write("<base href=\"" + docum ...

Having trouble with button alignment in the header with Bootstrap 3?

Using Bootstrap 3 and looking to adjust the alignment of a star icon with the title in my HTML page. Here is the current setup: <p class="pull-right visible-xs"> <h3><center>2000 Cadillac Eldorado Esc (Prospect heights) $3500 ...

Developing a customized WordPress walker to specifically target the first and last links

I've implemented a custom walker in my Wordpress site: class Custom_Walker_Nav_Menu extends Walker_Nav_Menu { function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) { if ( $depth ) $indent = str_repeat("& ...

To style a text box next to text within a paragraph in CSS, simply create an empty box

I have some CSS classes defined as follows: .p_box { position: relative; } .p_box span { background-color: white; padding-right: 10px; } .p_box:after { content:""; position: absolute; bottom: 0; left: 0; right: 0; top: ...

The loading indicator is not appearing inside the designated box

I have successfully implemented a loader using jQuery and CSS to display a gray background during an AJAX call. However, I am encountering an issue where the loader and background are being displayed across the entire page instead of just within a specific ...

Display a label upon hovering over an image

Is there a way to create an effect where upon hovering over an image, a pop-up image is displayed like this: https://i.sstatic.net/OloUH.png Any suggestions on how I can achieve this using HTML and CSS? Thanks in advance! ...

Disable error display using PHP for the image field with the value stored in $.row['img']

Currently seeking assistance. I have utilized [ onerror=this.style.display="none"] but it suddenly stopped functioning. Unable to find a solution, is there an alternative to the following method? <img class="basicimg" src='.$row['anncimg&apos ...

Is it time to go back to the parent selector using the ampersand symbol?

I recently started using Less for writing CSS, and it has definitely been a time-saver for me. However, I have encountered a small issue with my code: <a href="#" class="btn-black-bg btn-right-skew">largest fight gym in Australia</a> Less .b ...

What is the best way to center my image both vertically and horizontally?

Utilizing react.js to develop a template website has been my current project. The issue arose when I began constructing the first component of the site. The dilemma: The picture was not centered on the page, neither vertically nor horizontally. Despite ...

Error messages are being generated by Angular CLI due to an unclosed string causing issues with the

After incorporating styles from a Bootstrap theme into my Angular 8 project and updating angular.json, I encountered a compile error displaying the following message: (7733:13) Unclosed string 7731 | } 7732 | .accordion_wrapper .panel .panel-heading ...