HTML CSS GRID - Need layout good practices and advices [closed] - html

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am starting in front-end and I am currently studying html / css / vanilla javascript.
For the moment it is going well but I have a concern for understanding the design layout organization in general, and then responsive design becomes another problem for me as I cannot define a default layout correctly.
What are the best practices ?
Using CSS GRID, should I only create basic areas like header, nav, main, footer? or should I also create nested grid inside div/section ?
For exemple:
Should I declare in CSS:
#main-grid{
display:grid;
grid-template-areas:
"nav nav"
"head head"
"bio bio"
"picture1 article1"
"article2 article2"
"picture2 picture2"
"article3 picture3"
grid-template-columns: auto auto;
or should I simply declare a main content area (span 1) with CSS GRID and add div inside in html (using flexbox, float, playing with witdh,height, etc...?
#main-grid{
display:grid;
grid-template-areas:
"nav"
"head"
"main-content";
grid-template-columns: auto;
}
and #main-content in html:
<div>picture1 + article1 </div>
<div>article2 + picture2</div>
<div>article3 + picture3</div>
I know that everyone does their own thing and that it depends on the project but I suppose that there are good practices or logic to be applied by default.
Thank you!

I don't think
#main-grid{
display:grid;
grid-template-areas:
"nav nav"
"head head"
"bio bio"
"picture1 article1"
"article2 article2"
"picture2 picture2"
"article3 picture3"
grid-template-columns: auto auto;
}
is a great idea. What if you have 100 articles and pictures ?
You can do a combination of grid for the layout globally and flexbox/grid to manage articles & pictures.
I would do something like that for example to have everything pretty much responsive :
body {
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
}
.mySite {
display: grid;
grid-template-columns: 1fr .5fr;
grid-template-areas:
"header header"
"main aside"
"footer footer";
}
header {
grid-area: header;
height: 40px;
background: red;
display: flex;
align-items: center;
justify-content: center;
}
main {
grid-area: main;
display: grid;
grid-gap: 25px;
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
background: blue;
padding: 25px;
}
article {
background: aliceblue;
max-width: 200px;
height: 100px;
width: 100%;
}
aside {
grid-area: aside;
background: green;
display: flex;
align-items: center;
justify-content: center;
}
footer {
grid-area: footer;
height: 150px;
background: yellow;
display: flex;
align-items: center;
justify-content: center;
}
<div class="mySite">
<header>
<p>HEADER</p>
</header>
<main>
<article></article>
<article></article>
<article></article>
<article></article>
<article></article>
<article></article>
<article></article>
<article></article>
<article></article>
<article></article>
<article></article>
<article></article>
<article></article>
<article></article>
<article></article>
<article></article>
<article></article>
</main>
<aside>
<p>SIDEBAR</p>
</aside>
<footer>
<p>FOOTER</p>
</footer>
</div>
As you can see, the articles will auto adapt thanks to those lines :
display: grid;
grid-gap: 25px;
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
And to auto adapt your layout you can use media queries. For example here, If I want everything to be in block below 767px I would do something like that :
#media only screen and (max-width: 767px) {
.mySite {
grid-template-columns: 1fr;
grid-template-areas:
"header"
"main"
"aside"
"footer";
}
}
You can automatically adapt your layout thanks to the CSS grid directly in the parent.
Globally, you will manage the layout with the CSS Grid when you have delimited areas (typically header, sidebar, footer, ...)
Flexbox will help you to shape the inner content, like for an article, to do for example : the date on the left and the author on the right of the block with this :
display: flex;
justify-content: space-between;
You can consider flexbox as one dimensional. Flexbox can make rows and columns in the sense that it allows elements to wrap, there’s no way to declaratively control where elements end up since the elements merely push along a single axis and then wrap or not wrap accordingly. They do as they do, if you will, along a one-dimensional plane and it’s because of that single dimension that we can optionally do things, like align elements along a baseline — which is something grid is unable to do.
and CSS grid as two dimensional in that we can (if we want to) declare the sizing of rows and columns and then explicitly place things into both rows and columns as we choose.

Related

Split a sidebar in top and bottom on mobile with CSS grid

Is it possible to split a sidebar with css grids on mobile but treat it kind of one on desktop?
On mobile I want everything stacked Intro Part of the sidebar Content and then the rest of the Sidebar.
On desktop I would like Intro and Content stacked in one column and the 2 sidebars stacked next to them.
The closes I got was
grid-template-areas:
"intro"
"sidebarTop"
"content"
"sidebarBottom";
#media (min-width: 600px) {
grid-template-areas:
"intro sidebarTop"
"content sidebarTop"
"content sidebarBottom";
}
But sidebarTop always takes up as much space as it can.
I want it to only use the space it uses and let the sidebarBottom take up the rest.
Any suggestions?
Example
You can start with something like this, then adapt each block to your need:
main {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: repeat(4, 25%);
grid-template-areas: "intro" "sidebarTop" "content" "sidebarBottom";
grid-gap: 5px;
}
section {
box-sizing: border-box;
padding: 15px;
background-color: tomato;
color: white;
}
#media (min-width: 600px) {
main {
grid-template-columns: 1fr 1fr;
grid-template-rows: repeat(3, 33.33%);
grid-template-areas: "intro sidebarTop" "content sidebarTop" "content sidebarBottom";
}
}
<main>
<section id="intro">
intro
</section>
<section id="sidebarTop">
sidebarTop
</section>
<section id="content">
content
</section>
<section id="sidebarBottom">
sidebarBottom
</section>
</main>

Finding a way to properly implement <main> tag in this layout

I'm laying out HTML for a project, and as you can see on the joined picture, I have an issue about properly implementing <main> tag.
My layout has a content header above the sidebar and content, which prevents me from keeping the sidebar outside the main tag.
Would someone have a solution?
I tried having the sidebar code below the content in the HTML structure, and display it as shown in the picture with flexbox reverse row, but as soon as I wrap what I want inside a <main> semantic tag, everything breaks.
Use display: contents; to stop the main element having any effect on layout. Then layout its children as if they were siblings of main instead of children of it.
body {
background: black;
display: grid;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-template-areas: "header header header" "content-header content-header content-header" "sidebar content content";
margin: 0;
padding: 0;
height: 100vh;
}
header {
background: #aaf;
grid-area: header;
}
main {
display: contents;
}
main header {
background: #faa;
grid-area: content-header;
}
main #content {
background: #afa;
grid-area: content;
}
aside {
background: #aff;
grid-area: sidebar;
}
<header>
header
</header>
<main>
<header>
content header
</header>
<div id="content">
content
</div>
</main>
<aside>
sidebar
</aside>

CSS grid dynamic number of columns possible?

i have a web page setup which uses css grid to display the main section centered at 80% width.
<html>
....
<body>
<main>
<section>
</section>
</main>
</body>
....
</html>
main {
display: grid;
justify-items: center;
}
section {
max-width: 80%;
min-height: 100%
}
now I would like to also be able to add a second section using a PHP if statement so that both sections are displayed right next to each other at 50% each. (while not altering the css via PHP)
If I just add another section it will be displayed on top or below the first one.
I've already tried using grid-auto-columns as well as setting grid-template-rows to 100% but both didn't work as expected.
Any Ideas on how to solve this?
I'm not completely sure what you are after, but this will give you side by side,
<html>
<body>
<main>
<section>
section1 stuff
</section>
<section>
section2 stuff
</section>
</main>
</body>
</html>
main{
display: grid;
grid-template-columns: 100px 200px 300px;
grid-auto-rows: minmax(100px, auto);
grid-gap:5px;
}
section{
max-width: 80%;
min-height: 100%;
border:1px solid black;
background:red;
}
https://codepen.io/anon/pen/ZovaVG
Ugly in a Pen, but it does what you asked.

Extracting nested grids from parent grids in CSS Grid

I'm tinkering with CSS Grid and was wondering if it's possible to have nested grids move out of their parent grids when resizing the screen.
I've tried changing the grid-template-areas to include "subcontent" but this seems to be wrong and disrupts the grid.
As an example, I've set up the following template and would like the "subcontent" divs (pink) to replace the "profile" area when the media breakpoint is hit.
Is there any way to do this in pure CSS Grid without roping in JS?
.wrapper {
margin: auto;
width: 80%;
background-color: #e0e0e0;
display: grid;
grid-template-columns: 3fr 1fr;
grid-template-rows: 2fr 1fr 4fr;
grid-template-areas:
" title profile"
" infobar infobar "
" maincontent sidebar ";
grid-gap: 1em;
}
.space {
background-color: #eeeeee;
grid-area: space;
}
.title {
grid-area: title;
background-color: red;
}
.profile {
grid-area: profile;
background-color: orange;
}
.infobar {
grid-area: infobar;
background-color: yellow;
}
.maincontent {
grid-area: maincontent;
display: grid;
grid-template-columns: 2fr 1fr;
grid-template-rows: 1fr 1fr;
grid-template-areas:
"subcontent1 subcontent2"
"subcontent1 subcontent2";
background-color: green;
}
.subcontent1 {
grid-area: subcontent1;
background-color: pink;
margin: 10px;
}
.subcontent2 {
grid-area: subcontent2;
background-color: pink;
margin: 10px;
}
.sidebar {
grid-area: sidebar;
background-color: blue;
}
#media screen and (max-width: 900px) {
.wrapper {
grid-template-columns: 2fr 1fr 1fr;
grid-template-areas:
"title title sidebar"
"infobar infobar infobar"
"profile maincontent maincontent"
"footer footer sidebar"
;
}
.maincontent{
display: block;
}
.subcontent {
grid-area: subcontent;
}
}
<div class="wrapper">
<div class="title">Title</div>
<div class="profile">Profile</div>
<div class="infobar">Info Bar</div>
<div class="maincontent">Main Content
<div class="subcontent1">Main1</div>
<div class="subcontent2">Main2</div>
</div>
<div class="sidebar">Sidebar</div>
</div>
I'm tinkering with CSS Grid and was wondering if it's possible to have nested grids move out of their parent grids when resizing the screen.
Just like you can't extract nested flex containers so they can participate as flex items of an ancestor flex container, you can't extract nested grids to do the same thing.
You would have to make subcontent1 and subcontent2 siblings of the other grid items in your layout to re-position them.
I've tried changing the grid-template-areas to include "subcontent" but this seems to be wrong and disrupts the grid.
There's a good reason for this disruption. You've posted invalid string values.
This is good:
grid-template-areas: " title profile"
" infobar infobar "
" maincontent sidebar "
This is good, as well:
grid-template-areas: "subcontent1 subcontent2"
"subcontent1 subcontent2"
This is bad:
grid-template-areas: "title title sidebar"
"infobar infobar infobar"
"profile maincontent maincontent"
"footer footer sidebar"
Your last declaration is invalid.
String values of the grid-template-areas property must form rectangular blocks. Since sidebar appears twice in a disconnected manner, the entire layout breaks.
In contrast, try these out instead:
grid-template-areas: "title title sidebar"
"infobar infobar infobar"
"profile maincontent maincontent"
"footer footer footer"
jsFiddle demo
OR
grid-template-areas: "title title sidebar"
"infobar infobar infobar"
"profile maincontent maincontent"
"footer footer ..."
jsFiddle demo
Now the layout works. However, there's actually no footer in your HTML, so nothing will render. Regardless, the presence of an invalid string was breaking the layout.
Here are more complete explanations:
grid-template-areas with ASCII art is not working
Grid areas not laying out properly in CSS Grid

Pre tag causing page to scroll horizontally in CSS Grid [duplicate]

This question already has answers here:
Prevent content from expanding grid items
(3 answers)
Closed 5 years ago.
I am working on switching my site to CSS Grid. I found an unexpected behavior.
I have a simple CSS-Grid layout header spanning two columns the next row has the side navigation with a fixed size, and the main content using the remaining space.
In the main element contains a pre tag with demo code. That pre is a rather long line, for some screen sizes it pushes the main area outside the browser view.
What I want is for the pre to have to scroll horizontally.
body {
margin: 40px;
}
pre {
overflow: auto;
background-color: #ffffff;
border: 1px solid #898989;
padding: 5px;
}
header {
background: #e1e1e1;
grid-area: header;
}
aside {
grid-area: aside;
background: #d7d7d7;
}
aside nav {
background: #e1e1e1;
}
aside nav ul {
list-style: none;
padding-top: 10px;
}
aside nav ul li {
margin-bottom: 10px;
}
main {
background: #c2c2c2;
grid-area: main;
}
.box {
border-radius: 5px;
padding: 20px;
}
.grid {
display: grid;
grid-gap: 10px;
grid-template-areas: "header header" "aside main";
grid-template-columns: 200px 1fr;
grid-template-rows: 110px 1fr;
}
<div class="grid">
<header class="box">
<h1>Site Name and Navigation Bar</h1>
</header>
<aside class="box">
<h1>Sidebar</h1>
<p>This is the sidebar. Might have secondary navigation elements.</p>
<nav>
<ul>
<li>Link to nowhere</li>
<li>Link to nowhere</li>
</ul>
</nav>
</aside>
<main class="box">
<h1>Main Content</h1>
<p>The following <code>pre</code> element will be too large for our grid. It will not have a horizontal scrollbar if it has lines that exceed its width.</p>
<h2>Darn Pre</h2>
<pre><code><html>
<body>
<p>I like long sentences and can not lie. Look at it it's so big. That make your page to be far to wide. I want to get horizontal with the freaky line. My homeboys tried to warn me that pre can make you layout funky.
</p>
</body>
</html>
</code></pre>
<main>
</div>
codepen with demo of this problem
A Pre tag will try and fill all available space. The fix is to limit it in some way.
I fixed this by adding another 1fr column and having main span that.
.grid {
display: grid;
grid-gap: 10px;
grid-template-areas: "header header header" "aside main main";
grid-template-columns: 200px 1fr 1fr;
grid-template-rows: 110px 1fr;
}
Codepen example showing my fix