grid-template-areas: none vs grid-template-areas: dot? - html

csstricks says, we have three property values for grid-template-areas, out of which two are . and none. I can't see any difference in the way . and none affect the grid. See following:
.grid-container {
display: grid;
grid-template-areas: "one none two three"
"four five . six" ;
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
.item1 {
grid-area: one;
}
.item2 {
grid-area: two;
}
.item3 {
grid-area: three;
}
.item4 {
grid-area: four;
}
.item5 {
grid-area: five;
}
.item6 {
grid-area: six;
}
<div class="grid-container">
<div class="item1">1</div>
<div class="item2">2</div>
<div class="item3">3</div>
<div class="item4">4</div>
<div class="item5">5</div>
<div class="item6">6</div>
</div>
As you can see above . and none both leave empty blocks in the grid. I also searched on MDN, but theres no explanation for the difference. So,
What is the difference between grid-template-areas: none and grid-template-areas: .?

From the specification:
Name: grid-template-areas
Value: none | <string>+
Initial: none
none is a value on its own (grid-template-areas:none) and not a value to be used like grid-template-areas:" none one". the latter will fall under the <string>+ and "none" will become a named area and is different from .
A sequence of name code points, representing a named cell token with a name consisting of its code points.
A sequence of one or more "." (U+002E FULL STOP), representing a null cell token.
Below, I am placing 6 at "none" area
.grid-container {
display: grid;
grid-template-areas: "one . two three"
"four five none six" ;
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
.item1 {
grid-area: one;
}
.item2 {
grid-area: two;
}
.item3 {
grid-area: three;
}
.item4 {
grid-area: four;
}
.item5 {
grid-area: five;
}
.item6 {
grid-area: none;
}
<div class="grid-container">
<div class="item1">1</div>
<div class="item2">2</div>
<div class="item3">3</div>
<div class="item4">4</div>
<div class="item5">5</div>
<div class="item6">6</div>
</div>
This said, your example will show no difference because you are explicitly placing all your items so "none" and "." will remain empty in all the case. You can put anything there, the result will be the same.

Related

Can you stretch the CSS grid to fill the width of the screen?

I'm wanting to stretch out the grid to look like the second image attached in the imgur below (sorry I can't post directly on I don't have enough points to post the image directly)
My code:
.container {
display: grid;
grid-template-columns: 0.5fr 2.4fr 1.3fr 1fr;
grid-template-rows: 1fr 1fr 1fr [F] 1fr;
/* gap: 1px 0px; */
grid-auto-flow: row;
grid-template-areas:
"One oneTitle oneDescrip Picture"
"Two twoTitle twoDescrip Picture"
"Three threeTitle threeDescrip Picture"
"Four fourTitle fourDescrip Picture";
}
.One { grid-area: One; }
.Two { grid-area: Two; }
.Three { grid-area: Three; }
.Four { grid-area: Four; }
.oneTitle { grid-area: oneTitle; }
.twoTitle { grid-area: twoTitle; }
.threeTitle { grid-area: threeTitle; }
.fourTitle { grid-area: fourTitle; }
.oneDescrip { grid-area: oneDescrip; }
.twoDescrip { grid-area: twoDescrip; }
.threeDescrip { grid-area: threeDescrip; }
.fourDescrip { grid-area: fourDescrip; }
.Picture { grid-area: Picture; }
.One,.Two,.Three,.Four {
font-size: 60px;
color: lightgray;
}
.oneTitle, .twoTitle, .threeTitle, .fourTitle, .oneDescrip, .twoDescrip, .threeDescrip, .fourDescrip {
/* padding-top: 35px; */
color: black;
padding-right: 30px;
padding-left: 20px;
}
.oneTitle, .twoTitle, .threeTitle, .fourTitle {
font-weight: bold;
padding-top: 17px;
font-size: 25px;
}
<div class="container">
<div class="One">1</div>
<hr>
<div class="Two">2</div>
<div class="Three">3</div>
<div class="Four">4</div>
<div class="oneTitle">Connect your Crowds</div>
<div class="twoTitle">Gamify the Experience</div>
<div class="threeTitle">Engage with Business Growth</div>
<div class="fourTitle">Continuous Interaction</div>
<div class="oneDescrip">Descript</div>
<div class="twoDescrip">Descript</div>
<div class="threeDescrip">whehwehahha</div>
<div class="fourDescrip">Descriptjwsenwejwje</div>
<div class="Picture"><img id="block3img" src="https://i.imgur.com/PEbCBjt.png")></div>
</div>
What it looks like:
What I want it to look like:
In the .container rule set make this declaration. width: 100%;

grid-template-area not placing elements in the expected positions

I've checked multiple times I swear I have the same amount of columns in every row. I've also check that every grid-are corresponds to its due position but I still get some pretty weird extra cells.
html:
<main>
<div class="calculator">
<div class="calculator-screen">0</div>
<div class="calculator-delete">C</div>
<div class="calculator-percent">%</div>
<div class="calculator-negative">+/-</div>
<div class="calculator-division">/</div>
<div class="calculator-7">7</div>
<div class="calculator-8">8</div>
<div class="calculator-9">9</div>
<div class="calculator-multiply">*</div>
<div class="calculator-4">4</div>
<div class="calculator-5">5</div>
<div class="calculator-6">6</div>
<div class="calculator-subtract">-</div>
<div class="calculator-1">1</div>
<div class="calculator-2">2</div>
<div class="calculator-3">3</div>
<div class="calculator-add">+</div>
<div class="calculator-dot">.</div>
<div class="calculator-zero">0</div>
<div class="calculator-squareRoot">&#8730</div>
<div class="calculator-equals">=</div>
</div>
</main>
here's the scss:
*{
box-sizing: border-box;
padding: 0;
margin: 0;
}
html{
font-size: 62.5%;
}
body{
font-family: 'Roboto';
font-size: 1.6rem;
}
main{
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.calculator{
width: 50%;
min-height: 75%;
display: grid;
grid-template-areas:
"screen screen screen screeen"
"delete percent negative division"
"7 8 9 multiply"
"4 5 6 subtract"
"1 2 3 add"
"dot zero squareRoot equals";
text-align: center;
outline: 1.5px solid black;
& > *{
outline: 1.5px solid black;
display: flex;
align-items: center;
justify-content: center;
font-size: 2.4rem;
}
&-screen{
grid-area: screen;
justify-content: end;
align-items: flex-end;
padding: 0px 20px 20px 0px;
font-size: 3rem;
}
&-delete{
grid-area: delete;
}
&-percent{
grid-area: percent;
}
&-negative{
grid-area: negative;
}
&-division{
grid-area: division;
}
&-7{
grid-area: 7;
}
&-8{
grid-area: 8;
}
&-9{
grid-area: 9;
}
&-multiply{
grid-area: multiply;
}
&-4{
grid-area: 4;
}
&-5{
grid-area: 5;
}
&-6{
grid-area: 6;
}
&-subtract{
grid-area: subtract;
}
&-1{
grid-area: 1;
}
&-2{
grid-area: 2;
}
&-3{
grid-area: 3;
}
&-add{
grid-area: add;
}
&-dot{
grid-area: dot;
}
&-zero{
grid-area: zero;
}
&-squareRoot{
grid-area: squareRoot;
}
&-equals{
grid-area: equals;
}
}
Here's the unwanted result:
Where did that 5th column and the 3 rows come from? I am honestly lost here.
#JHeth solved my question. Here's the answer:
Might be because you're using numbers as grid-area names in grid-template-areas which only accept a string. Try switching 1-7 to one-seven and see if it works.
.calculator{
width: 50%;
min-height: 75%;
display: grid;
grid-template-areas:
"screen screen screen screen"
"delete percent negative division"
"seven eight nine multiply"
"four five six subtract"
"one two three add"
"dot zero squareRoot equals";
text-align: center;
outline: 1.5px solid black;
& > *{
outline: 1.5px solid black;
display: flex;
align-items: center;
justify-content: center;
font-size: 2.4rem;
}
&-screen{
grid-area: screen;
justify-content: end;
align-items: flex-end;
padding: 0px 20px 20px 0px;
font-size: 3rem;
}
&-delete{
grid-area: delete;
}
&-percent{
grid-area: percent;
}
&-negative{
grid-area: negative;
}
&-division{
grid-area: division;
}
&-7{
grid-area: seven;
}
&-8{
grid-area: eight;
}
&-9{
grid-area: nine;
}
&-multiply{
grid-area: multiply;
}
&-4{
grid-area: four;
}
&-5{
grid-area: five;
}
&-6{
grid-area: six;
}
&-subtract{
grid-area: subtract;
}
&-1{
grid-area: one;
}
&-2{
grid-area: two;
}
&-3{
grid-area: three;
}
&-add{
grid-area: add;
}
&-dot{
grid-area: dot;
}
&-zero{
grid-area: zero;
}
&-squareRoot{
grid-area: squareRoot;
}
&-equals{
grid-area: equals;
}
Wrote down the name of the numbers instead of the numbers themselves for the grid-areas

How to split a screen into a number of equal sized boxes all facing outwards?

I have a mobile site which aims to be placed in the center of a table with boxes equal to the number of players around the table. On it are a series of scores that refer to each players score and face that player.
I think I might have to use grid in order to lay these out. My question is whether there's an easier way built in? I'm struggling combining both grid and transform: rotate(90deg).
Without rotation:
With rotation:
Here's a code snippet:
body {
font-family: segoe-ui_normal,Segoe UI,Segoe,Segoe WP,Helvetica Neue,Helvetica,sans-serif;
display: grid;
grid-template-areas:
"one two three six"
"one two three six"
"one four five six"
"one four five six";
grid-template-rows: 25% 25% 25% 25%;
grid-template-columns: 25% 25% 25% 25%;
grid-gap: 5px;
height: 100vh;
margin: 10px;
}
one {
background: #707070;
grid-area: one;
transform: rotate(90deg);
}
two {
background: #707070;
grid-area: two;
transform: rotate(180deg);
}
three {
background: #707070;
grid-area: three;
transform: rotate(180deg);
}
four {
background: #707070;
grid-area: four;
}
five {
background: #707070;
grid-area: five;
}
six {
background: #707070;
grid-area: six;
transform: rotate(-90deg);
}
<one>20</one>
<two>20</two>
<three>20</three>
<four>20</four>
<five>20</five>
<six>20</six>
I think you will find it easier to rotate the contents of the divs rather than the divs themselves. Just use the divs for layout.
Theh you can leverage writing-mode and/or a transform as necessary.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
::before,
::after {
box-sizing: inherit;
}
body {
font-family: segoe-ui_normal, Segoe UI, Segoe, Segoe WP, Helvetica Neue, Helvetica, sans-serif;
display: grid;
grid-template-areas: "one two three six" "one two three six" "one four five six" "one four five six";
grid-template-rows: repeat(4, 1fr);
grid-template-rows: repeat(4, 1fr);
grid-gap: 5px;
height: 100vh;
padding: 5px;
}
body div {
outline: 1px solid grey;
display: flex;
justify-content: center;
align-items: center;
font-size: 2rem;
}
.one {
grid-area: one;
background: green;
}
.one p {
writing-mode: vertical-rl;
}
.two {
grid-area: two;
}
.three {
grid-area: three;
}
.two p,
.three p {
transform: rotate(180deg);
}
.four {
grid-area: four;
}
.five {
grid-area: five;
}
.six {
background: orange;
grid-area: six;
writing-mode: vertical-rl;
}
.six p {
transform: rotate(-180deg);
}
<div class="one">
<p>20</p>
</div>
<div class="two">
<p>20</p>
</div>
<div class="three">
<p>20</p>
</div>
<div class="four">
<p>20</p>
</div>
<div class="five">
<p>20</p>
</div>
<div class="six">
<p>20</p>
</div>

CSS "grid-template-areas" not working as desired

I thought that when I type . (blank field) inside grid-template-areas property, .item5 will show inside under field. However it showing inside . field. Why does it works like that?
body {
margin: 0;
}
.item1 {
grid-area: header;
}
.item2 {
grid-area: content;
}
.item3 {
grid-area: left;
}
.item4 {
grid-area: footer;
}
.grid-container {
display: grid;
grid-template-columns: 300px 1fr;
grid-template-areas: "header header"
"content ."
"left ."
"footer footer"
"under under";
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
<div class="grid-container">
<div class="item1">1</div>
<div class="item2">2</div>
<div class="item3">3</div>
<div class="item4">4</div>
<div class="item5">5</div>
</div>
From the specification:
A sequence of one or more "." (U+002E FULL STOP), representing a null cell token.
and
A null cell token represents an unnamed area in the grid container.
unnamed area doesn't mean that an item cannot be placed there. It simply mean unnamed and the automatic placement algorithm will start from the top to the bottom so the first empty area for item5 is an unnamed area.
You can follow the full algorithm here: https://www.w3.org/TR/css-grid-1/#auto-placement-algo where you will find no restriction about unnamed area or the named one. If your item5 was alone it will get placed into the first row/column (the one named "header")
body {
margin: 0;
}
.item1 {
grid-area: header;
}
.item2 {
grid-area: content;
}
.item3 {
grid-area: left;
}
.item4 {
grid-area: footer;
}
.grid-container {
display: grid;
grid-template-columns: 300px 1fr;
grid-template-areas: "header header"
"content ."
"left ."
"footer footer"
"under under";
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
<div class="grid-container">
<div class="item5">5</div>
</div>
Related question where I am giving a more detailed explanation about the placement algorithm:
CSS Grid : How Auto placement algorithm works
Based on your last comment, I think that you are expecting to have grid-auto-flow: column;, but this is not the default style.
I added it to the snippet:
body {
margin: 0;
}
.item1 {
grid-area: header;
}
.item2 {
grid-area: content;
}
.item3 {
grid-area: left;
}
.item4 {
grid-area: footer;
}
.grid-container {
grid-auto-flow: column;
display: grid;
grid-template-columns: 300px 1fr;
grid-template-areas: "header header"
"content ."
"left ."
"footer footer"
"under under";
grid-gap: 10px;
background-color: #2196F3;
padding: 10px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 20px 0;
font-size: 30px;
}
<div class="grid-container">
<div class="item1">1</div>
<div class="item2">2</div>
<div class="item3">3</div>
<div class="item4">4</div>
<div class="item5">5</div>
</div>

Adaptive 3-columns layout with flexible height of the items without JS?

I have a layout for the whole website which is quite standard - header/footer, two side blocks and the main content in the center. Let's put footer/header away, they're not really interesting. What I want to achieve is two things:
Make adaptive layout on mobile phones by putting red block between cyan and green.
In the same time keep the layout on desktops flexible. Namely, I don't want to have extra spaces between blue and red blocks (when cyan block has a lot of content), nor I want extra spaces between cyan and green blocks (when blue block has a lot of content)
First I did it with flexbox, but bullet #1 was not possible in Flex. Now I rewrote it to using Grid and faced issues with #2.
Question - how do I make grid elements in each column "independent" from each other in terms of height (keeping the deterministic behavior, of course)?
Here is the schematic layout (top - desktop, bottom - mobile):
And here is the CodePen example (click on magenta block to check how does it behave and what's the issue)
var testing = true;
function startTime() {
if (testing) {
document.getElementById('info').innerHTML = "<br>LongLongContent<br>LongLongContent<br><br>LongLongContent<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>";
document.getElementById('content').innerHTML = "Short content (logs block should start just after me)";
} else {
document.getElementById('content').innerHTML = "<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>LongLongContent<br>";
document.getElementById('info').innerHTML = "Short content (photos block should start just after me)";
}
testing = !testing;
}
.grid-container {
display: grid;
grid-template-columns: 210px 1fr 200px;
grid-template-rows: repeat(4, auto);
gap: 0px 0px;
grid-template-areas: "header header header"
"info content sidebar"
"photos logs sidebar"
"footer footer footer";
}
.grid-container>* {
border: 1px solid red;
}
.header {
grid-area: header;
}
.info {
grid-area: info;
background-color: blue;
color: white;
}
.photos {
grid-area: photos;
background-color: red;
}
.footer {
grid-area: footer;
}
.sidebar {
grid-area: sidebar;
background-color: magenta;
}
.content {
grid-area: content;
background-color: cyan;
}
.logs {
grid-area: logs;
background-color: green;
}
<header class="grid-container">
<header class="header">Header</header>
<div class="info" id="info">Info<br>Info<br>Info<br>Info<br>Info<br>Info<br><br>Info<br></div>
<div class="photos">Photos<br>Photos<br>Photos<br>Photos<br>Photos<br></div>
<div class="content" id="content">content<br>asdasd<br><br><br>[I'm some random empty space =(]</div>
<div class="logs">logs</div>
<div class="sidebar" id="sidebar" onclick="startTime()">Click me to toggle between different sizes of content</div>
<footer class="footer">Footer</footer>
</div>
The following code shows the implementation of the structure of a variable height grid. Note that the corresponding columns must be the same height.
Using grid-template-areas: you can implement your desired structure.
Pay attention to how to implement the grid-container class
.grid-container {
display: grid;
grid-template-columns: 25% 50% 25%;
grid-gap: 0px;
background-color: black;
grid-template-areas:
"one two three"
"one four three"
"five six three"
}
With grid-template-columns: 25% 50% 25%; 3 columns are defined.
Then with
grid-template-areas:
"one two three"
"one four three"
"five six three"
3 rows are defined by 3 columns.
I used the following code for the responsive mode
#media only screen and (max-width: 600px) {
.grid-container {
display: grid;
grid-template-columns: 100%;
grid-gap: 10px;
background-color: black;
grid-template-areas:
"two"
"four"
"five"
"six"
}
}
Full code:
.grid-container {
display: grid;
grid-template-columns: 25% 50% 25%;
grid-gap: 0px;
background-color: black;
grid-template-areas: "one two three" "one four three" "five six three"
}
#one {
grid-area: one;
height: auto;
width: 100%;
background-color: blue;
}
#two {
grid-area: two;
height: 50px;
width: 100%;
background-color: yellow;
}
#three {
grid-area: three;
height: auto;
width: 100%;
background-color: #365263;
}
#four {
grid-area: four;
height: auto;
width: 100%;
background-color: aqua;
}
#five {
grid-area: five;
height: auto;
width: 100%;
background-color: red;
}
#six {
grid-area: six;
height: auto;
width: 100%;
background-color: green;
}
.grid-container>div {
text-align: center;
padding: 20px 0;
font-size: 30px;
}
#media only screen and (max-width: 600px) {
.grid-container {
display: grid;
grid-template-columns: 100%;
grid-gap: 10px;
background-color: black;
grid-template-areas: "two" "four" "five" "six"
}
}
<h1>The grid-column Property</h1>
<div class="grid-container">
<div id="one">2<br>2<br>2<br>2<br>2<br>2<br>2<br>2<br>1</div>
<div id="two"></div>
<div id="three">3</div>
<div id="four">4<br>4<br>4<br>4<br>4<br>4<br>4<br>4<br>4<br>4<br>4<br>4<br>4<br>4<br>4<br></div>
<div id="five">5</div>
<div id="six">6</div>
</div>