I'm building a responsive template with CSS Grid Layout (still learning) and thanks to a few of you on here, I've got most of it working.
mobile (max-width: 767px)
tablet (min-width: 768px)
desktop (min-width: 992px)
xl desktop (min-width: 1920px)
The thing is I'm using a header
tag to color the spacing to the left and right of the nav
. Whether I use a header or a div, it doesn't seem right having an empty container just for coloring the empty space.
Is there a way of doing this in a way that lets me apply position: fixed
on the entire top section?
* {
margin: 0;
padding: 0;
}
body {
display: grid;
font-family: sans-serif;
}
a {
text-decoration: none;
color: white;
}
header,
nav {
background: blue;
color: #fff;
}
nav {
display: flex;
flex-wrap: wrap;
align-items: center;
}
nav span {
margin-right: auto;
}
header {
display: none;
}
aside {
background: lightgreen;
}
main {
background: pink;
}
/* mobile */
@media (max-width: 767px) {
body {
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr 1fr;
}
nav,
aside,
main {
grid-column: 1 / 1;
padding: 0 15px;
}
}
/* tablets */
@media (min-width: 768px) {
body {
grid-template-columns: 275px 1fr;
grid-template-rows: 1fr 1fr;
}
nav {
grid-column: 1 / 4;
grid-row: 1;
height: 50px;
grid-row: 1;
}
aside {
grid-column: 1;
}
main {
grid-column: 2;
}
nav,
aside,
main {
padding: 0 15px;
}
}
/* desktops */
@media (min-width: 992px) {
body {
grid-template-columns: 10% 275px 1fr 10%;
grid-template-rows: 1fr 1fr;
}
header {
display: block;
grid-column: 1 / -1;
grid-row: 1;
}
nav {
grid-column: 2 / 4;
grid-row: 1;
}
aside {
grid-column: 2 / 3;
}
main {
grid-column: 3 / 4;
}
}
/* xl desktops */
@media (min-width: 1920px) {
body {
grid-template-columns: 1fr minmax(auto, 300px) minmax(auto, 1620px) 1fr;
grid-template-rows: 1fr 1fr;
}
}
<header></header>
<nav>
<span>Logo</span>
<a href="#">login</a>
</nav>
<aside>aside</aside>
<main>main</main>
https://jsfiddle.net/90kotz8d/1/
Since position fixed will mess up any grid layout I tried to use a hacking putting two divs around each container where the second div would have a position fixed but it is still having some weird effects.
Building a Responsive Grid-View First ensure that all HTML elements have the box-sizing property set to border-box . This makes sure that the padding and border are included in the total width and height of the elements. Read more about the box-sizing property in our CSS Box Sizing chapter.
Using transform. The translate() CSS function moves an element along the x- and y-axes. Using it together with the transform property allows you to change the position of a grid item.
If I understand your intent, you are not really using header for spacing, you are really using it to get the blue background to cover all the space.
Since you seem also wanting the logo and login to vertically align with the boundaries, I don't think that there is any posible solution to do this with the nav.
So, the only solution that I could find involves using a pseudo element. At least this is more semantic. I got rid of the media queries, since now they don't play a role here:
* {
margin: 0;
padding: 0;
}
.container {
display: grid;
grid-template-columns: 10% 275px 1fr 10%;
grid-template-rows: 1fr 1fr;
}
.container::after {
content: "";
background-color: blue;
grid-column: 1 / 5;
grid-row: 1;
z-index: -1;
}
a {
text-decoration: none;
color: white;
}
nav {
background: blue;
color: #fff;
display: flex;
flex-wrap: wrap;
align-items: center;
}
nav span {
margin-right: auto;
}
header {
display: none;
}
aside {
background: lightgreen;
}
main {
background: pink;
}
nav {
grid-column: 2 / 4;
grid-row: 1;
}
aside {
grid-column: 2 / 3;
}
main {
grid-column: 3 / 4;
}
<div class="container">
<nav>
<span>Logo</span>
<a href="#">login</a>
</nav>
<aside>aside</aside>
<main>main</main>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With