Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide elements not specified in grid-template-areas?

Tags:

css

css-grid

I'm trying to understand grid-template-areas.

I've this HTML

<div class="wrapper">
  <div class="d">D</div>
  <div class="b">B</div>
  <div class="c">C</div>
  <div class="a">A</div>
</div>

and this CSS

.wrapper {
    grid-template-areas: "areaA areaB areaC areaD" 
}

.A { grid-area: areaA; }
.B { grid-area: areaB; }
.C { grid-area: areaC; }
.D { grid-area: areaD; }

I get the (expected) following result

A B C D

now if I add a media query, and wanted to hide column B, C and D

@media (min-width: 500px) {
    .wrapper {
        grid-template-areas: "areaA";
    }

    .B {
        display: none;
    }

    .C {
        display: none;
    }

    .D {
        display: none;
    }
}

this also works :

A

now, I then removed the display:none entries, hoping that because there was no mention of the elements in grid-template-areas that they would not show. I was wrong ;)

Is it possible to specify just using css-grid that elements not specified are hidden by default ? I can't seem to find anything that mentions this

like image 924
jmls Avatar asked Feb 07 '18 09:02

jmls


People also ask

How do you hide the grid element in CSS?

You can achieve this by setting that row's height to 0 via the grid-template-rows declaration, and then setting the visibility of the element you want to hide to hidden . If you need your grid to utilize a gap between columns / rows, you won't be able to use the gap property when using this method.

Should I use grid template areas?

CSS Grid Template Area BasicsA CSS grid template area makes a visual representation of the grid using both columns and rows. This makes for a faster design process than when line-based placement or grid-column and grid-row values are used.

How do I declare grid template areas?

The grid-template-areas property specifies areas within the grid layout. You can name grid items by using the grid-area property, and then reference to the name in the grid-template-areas property. Each area is defined by apostrophes. Use a period sign to refer to a grid item with no name.


1 Answers

The grid-template-areas property cannot hide grid items. It is designed to create grid areas.

But your media query can still be very simple.

This is all you need:

@media (max-width: 500px) {

   section:not(.a) { display: none; }

}

jsFiddle demo

article {
  display: grid;
  grid-template-areas: "areaA areaB areaC areaD";
}

@media (max-width: 500px) {
  section:not(.a) { display: none; }
}

.a { grid-area: areaA; }
.b { grid-area: areaB; }
.c { grid-area: areaC; }
.d { grid-area: areaD; }

/* non-essential demo styles */

section {
  height: 50px;
  width: 75px;
  background-color: lightgreen;
  border: 1px solid #ccc;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 1.2em;
}
<article>
  <section class="d">D</section>
  <section class="b">B</section>
  <section class="c">C</section>
  <section class="a">A</section>
</article>
like image 167
Michael Benjamin Avatar answered Sep 19 '22 23:09

Michael Benjamin