Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Grid and Absolute Positioning on Safari

I have a problem with a css grid layout.

The grid container is absolutely positioned and the grid-template-rows contains an 1fr to allow one row to take all the free space.

This displays correctly on all current browsers except the latest Safari installed with macOS 10.13.2

The issue seems to be caused by it not calculating the freespace if the height is not explicitly set.

Am I missing something or is there a workaround?

jsfiddle example

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}

.wrapper {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  display: grid;
  grid-template-rows: 1fr 150px;
  grid-template-columns: 1fr 150px;
  grid-template-areas: "a b" "c d";
}

.wrapper>div {
  outline: solid 1px #aaa;
  padding: 10px;
}

.game {
  grid-area: a;
}

.player {
  grid-area: b;
}

.rules {
  grid-area: d;
}

.controls {
  grid-area: c;
}
<div class="wrapper">
  <div class="game">game</div>
  <div class="player">player</div>
  <div class="rules">rules</div>
  <div class="controls">controls</div>
</div>
like image 622
blackmamba Avatar asked Dec 19 '17 12:12

blackmamba


People also ask

Is CSS Grid supported in Safari?

The supporting browsers Other than in Internet Explorer, CSS Grid Layout is unprefixed in Safari, Chrome, Opera, Firefox and Edge.

Why is CSS Grid row height different in Safari?

The height of the grid rows is set to 1fr so that it is proportional to the height of the grid. Some grid items have a grid-row: span 2 or grid-row: span 3 . The grid element is absolutely positioned inside of wrapper with padding on it in order to maintain the aspect ratio.

Does CSS Grid work on all browsers?

Most developers are afraid to start using CSS Grid because of browser support, but CSS grid is fully supported in all main browsers: Chrome, Firefox, Safari, Edge including their mobile versions.

Is it bad to use absolute positioning CSS?

As long as you structure your HTML so that the elements follow a logical order that makes sense when rendered without CSS, there is no reason why using absolute positioning should be considered bad practice.


1 Answers

In 2017 top: 0; bottom 0 is more like a dirty hack. Just give the .wrapper a minimum height as 100% of the viewport - 100vh.

.wrapper {
  min-height: 100vh;
}
like image 125
Andrey Fedorov Avatar answered Sep 19 '22 21:09

Andrey Fedorov