Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 months ago.
Improve this question
This table layout using CSS GridI am new to CSS Grid and would like to know if the following can be achieved using grid layout.
Yes, you can do it.
You can generate CSS Grid layouts here
I have also generated one for you which would represent current situation.
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
gap: 0px 0px;
grid-auto-flow: row;
grid-template-areas: "empty1 empty1 header1 header2 header3 empty2 empty2" "text1 text2 text3 text4 text5 text6 text7" "text8 text9 text10 text11 text12 text13 text14";
}
.text1 {
grid-area: text1;
}
.text2 {
grid-area: text2;
}
.text3 {
grid-area: text3;
}
.text4 {
grid-area: text4;
}
.text5 {
grid-area: text5;
}
.text6 {
grid-area: text6;
}
.text7 {
grid-area: text7;
}
.text14 {
grid-area: text14;
}
.text13 {
grid-area: text13;
}
.text12 {
grid-area: text12;
}
.text11 {
grid-area: text11;
}
.text10 {
grid-area: text10;
}
.text9 {
grid-area: text9;
}
.text8 {
grid-area: text8;
}
.empty1 {
grid-area: empty1;
}
.empty2 {
grid-area: empty2;
}
.header1 {
grid-area: header1;
}
.header2 {
grid-area: header2;
}
.header3 {
grid-area: header3;
}
<div class="container">
<div class="text1">text1</div>
<div class="text2">text2</div>
<div class="text3">text3</div>
<div class="text4">text4</div>
<div class="text5">text5</div>
<div class="text6">text6</div>
<div class="text7">text7</div>
<div class="text14">text14</div>
<div class="text13">text13</div>
<div class="text12">text12</div>
<div class="text11">text11</div>
<div class="text10">text10</div>
<div class="text9">text9</div>
<div class="text8">text8</div>
<div class="empty1"></div>
<div class="empty2"></div>
<div class="header1">HDR Item1</div>
<div class="header2">HDR Item2</div>
<div class="header3">HDR Item3</div>
</div>
Related
I training by create the Google Homepage and I have an issue on the footer which has a line break in 2 columns.
#footer{
display: grid;
grid-template-columns: repeat(auto-fit, minmax(50px,1fr));
grid-template-rows: 50px;
grid-template-areas:
"pub entreprise propos comment neutre infos confidentialite conditions parametres";
background-color: rgb(242, 242, 242);
}
<div id="footer">
Publicité
Entreprise
A propos
Comment fonctionne la recherche Google ?
Neutre en carbone depuis 2007
Infos consommateurs
Confidentialité
Conditions
Paramètres
You can add a white-space : nowrap and not use grid-template-columns to enforce fitting of text within fraction of screen. But also you will have to take care of the responsive behaviour when width decreases. If that's not the requirement then this shall work. Maybe on shrinking of screen use a media query to have 2 rows instead of just one.
#footer {
display: grid;
grid-template-rows: 50px;
grid-template-areas: "pub entreprise propos comment neutre infos confidentialite conditions parametres";
background-color: rgb(242, 242, 242);
white-space: nowrap;
}
<div id="footer">
Publicité
Entreprise
A propos
Comment fonctionne la recherche Google ?
Neutre en carbone depuis 2007
Infos consommateurs
Confidentialité
Conditions
Paramètres
First, it's important to note that your grid-template-areas definition is not mapped to the grid items. This is because you haven't defined the grid-area property for each item.
The wrapping problem can be solved with white-space: nowrap.
#footer {
display: grid;
grid-template-rows: 50px;
grid-template-areas: "pub entreprise propos comment neutre infos confidentialite conditions parametres";
grid-column-gap: 10px;
}
#pub { grid-area: pub; }
#entreprise { grid-area: entreprise; }
#propos { grid-area: propos; }
#comment { grid-area: comment; }
#neutre { grid-area: neutre; }
#infos { grid-area: infos; }
#confidentialite { grid-area: confidentialite; }
#conditions { grid-area: conditions; }
#parametres { grid-area: parametres; }
#footer > a {
white-space: nowrap;
padding: 0 25px;
background-color: lightgray;
display: flex;
justify-content: center;
align-items: center;
}
<div id="footer">
Publicité
Entreprise
A propos
Comment fonctionne la recherche Google ?
Neutre en carbone depuis 2007
Infos consommateurs
Confidentialité
Conditions
Paramètres
</div>
I want to make a grid with different column sizes ('ITEM = V' covers all width 'ITEM A' or 'P' or 'T' cover each 50 percent of the grid width), as shown in the image. Any help?
i ve been tryin to solve this for a week now. Really what's wrong with this. (video should take two columns)
<div class="grid-container">
<div *ngFor="let media of allMedia">
<div *ngIf="media.type==='V'" class="item1">
{{media.title}}
</div>
<div *ngIf="media.type==='A'" >
{{media.title}}
</div>
<div *ngIf="media.type==='P'" >**Bold Text Here**
{{media.title}}
</div>
<div *ngIf="media.type==='T'">
{{media.title}}
</div>
</div>
</div>
CSS
.grid-container {
display: grid;
background-color: #2196F3;
grid-template-columns: 50% 50%;
}
.item1 {
grid-area: 2 / 1 / span 2 / span 2 !important;
border-style:solid;
text-align:center
}
It'd be best to use display: grid; for something like this.
You'd have to add unique classes/id's for the children elements and then set the grid-area property on each of them.
The layout of grid-area is:
grid-area: row-start / column-start / row-end / column end;
You can also use shorthand if two properties have the same value:
grid-area: 2 / 1
/* Equates to: */
grid-area: 2 / 1 / 2 / 1
Example:
.outer-grid {
display: grid;
height: 100vh;
}
.newspaper1 {
grid-area: 1 / 3 / 2 / 1;
border-style: solid;
}
.newspaper2 {
grid-area: 2 / 1;
border-style: solid;
}
.newspaper3 {
grid-area: 2;
border-style: solid;
}
.newspaper4 {
grid-area: 3 / 1;
border-style: solid;
}
.newspaper5 {
grid-area: 3 / 2;
border-style: solid;
}
.newspaper6 {
grid-area: 4 / 3 / 4 / 1;
border-style: solid;
}
<div class="outer-grid">
<div *ngIf="item==='V'" class="newspaper1">
some content
</div>
<div *ngIf="item==='A'" class="newspaper2">
some content
</div>
<div *ngIf="item==='P'" class="newspaper3">
some content
</div>
<div *ngIf="item==='T'" class="newspaper4">
some content
</div>
<div *ngIf="item==='A'" class="newspaper5">
some content
</div>
<div *ngIf="item==='V'" class="newspaper6">
some content
</div>
</div>
Here's a good interface for learning CSS Grid if you want to learn more: https://alialaa.github.io/css-grid-cheat-sheet/
I am trying to break some elements in different order on mobile, i have something like this now
And my html
<main>
<div data-color="yellow"></div>
<div class="wrapper">
<div data-color="orange"></div>
<div data-color="purple"></div>
</div>
</main>
Here is my css
.wrapper {
flex-direction: column;
}
div {
flex: 1;
}
[data-color=purple] {
order: 3;
}
main.mobile {
flex-direction: column;
}
main.mobile .wrapper {
display: contents;
}
main.mobile [data-color=orange] {
order: -2;
}
I made it simple that you can help me, what i need is that now palge lpaut is half and half, i need yellow container to be 75% and right 25%, i am using boostrap 4 but I am not so good at fex layout
Thanks
main {
display: flex;
}
[data-color="yellow"] {
flex: 0 0 75%;
}
.wrapper{
flex: 0 0 25%;
}
you can use flex: 1 percentage(); for example flex: 1 percentage(1/4);
Here I made an example in codepen for you:
https://codepen.io/Alirezaaraby/pen/JjYJMZW?editors=1100
I'm trying to accomplish the following layout:
My first thought is to use flexbox to achieve this layout. I currently have the following HTML:
<section>
<div class"item">box1</div>
<div class"item">box2</div>
<div class"item">box3</div>
<div class"item">box4</div>
<div class"item">box5</div>
</section>
How can I achieve the desired layout with my HTML? I can add line break div elements in between items like this as well:
<div class"break"> </div>
Unfortunately I am still not able to achieve the required layout. Please help
A simple CSS-grid based approach would be to use a "template of named grid areas".
CSS grid allows for named areas, which dictate the placement of children based on the grid-area of those grid children. In the case of your requirements, a template based on named areas could be defined as:
grid-template-areas:
"a a b b c c"
". . e f . .";
These template areas work by causing:
child elements with grid-area of a, b, and c to occupy the top row of the template layout, where each spans two columns of the 6 column grid
child elements with grid-area of e and f to occupy the bottom row of the template, at the third and fourth column respectively. The . on this row configuration specifies that no child apples to that area of the template
Note that template area strings can be written on the same line for the grid-template-areas property as shown below:
section {
/* Specify that CSS grid is to be used for layout of children */
display: grid;
/* Specify spacing between children */
grid-gap:1rem;
/* Wrap against six evenly spaced columns of this grid */
grid-template-columns: repeat(6, 1fr);
/* Define the area names of the grid template */
grid-template-areas: "a a b b c c" ". . e f . .";
}
section div:nth-child(1) {
grid-area: a;
}
section div:nth-child(2) {
grid-area: b;
}
section div:nth-child(3) {
grid-area: e;
}
section div:nth-child(4) {
grid-area: f;
}
section div:nth-child(5) {
grid-area: c;
}
/* Optional aesthetics to better match your example */
div {
background: darkgrey;
border-radius: 5px;
color: white;
text-align: center;
}
<section>
<div class "item">box1</div>
<div class "item">box2</div>
<div class "item">box3</div>
<div class "item">box4</div>
<div class "item">box5</div>
</section>
Updates
section {
display: grid;
grid-gap:1rem;
grid-template-columns: repeat(5, auto);
grid-template-areas: "a b c c d e" ". . f g . .";
}
section div:nth-child(1) {
grid-area: a;
}
section div:nth-child(2) {
grid-area: b;
}
section div:nth-child(3) {
grid-area: c;
}
section div:nth-child(4) {
grid-area: f;
}
section div:nth-child(5) {
grid-area: g;
}
section div:nth-child(6) {
grid-area: d;
}
section div:nth-child(7) {
grid-area: e;
}
/* Optional aesthetics to better match your example */
div {
background: darkgrey;
border-radius: 5px;
color: white;
text-align: center;
}
<section>
<div class "item">box1</div>
<div class "item">box2</div>
<div class "item">box3</div>
<div class "item">box4 lots of content causes uneven column distribution</div>
<div class "item">box5</div>
<div class "item">box6</div>
<div class "item">box7</div>
</section>
You Can use CSS Grid Instead:
Complete Grid Guide
Working Demo:
.grid {
display: grid;
grid-template-areas: "i1 i1 i2 i2 i3 i3" ". . i4 i5 . .";
grid-template-columns: repeat(6,1fr); /* to make all boxes same with */
grid-gap: 10px;
}
.i1 {
grid-area: i1
}
.i2 {
grid-area: i2
}
.i3 {
grid-area: i3
}
.i4 {
grid-area: i4
}
.i5 {
grid-area: i5
}
.item {
min-height: 40px;
background-color: #7D7D7D;
border-radius: 10px;
display: flex;
justify-content: center;
;
align-items: center;
color: white;
font-size: 1.5rem;
font-weight: bolder;
}
.i4,.i5 {
border-radius: 8px;
}
<section class="grid">
<div class="item i1">box1</div>
<div class="item i2">box2</div>
<div class="item i3">box3</div>
<div class="item i4">box4</div>
<div class="item i5">box5</div>
</section>
With flexbox you can adjust the order of the element and rely on wrapping:
section {
display:flex;
flex-wrap:wrap;
justify-content:center;
}
section > .item {
width:calc(100%/3 - 10px);
margin:5px;
}
section > .item:nth-child(3),
section > .item:nth-child(4){
order:1;
width:calc((100%/3 - 20px) /2);
}
/* Irrelevant styles */
section > .item {
padding:10px;
box-sizing:border-box;
text-align:center;
background:#000;
color:#fff;
border-radius:10px;
}
<section>
<div class="item">box1</div>
<div class="item">box2</div>
<div class="item">box3</div>
<div class="item">box4</div>
<div class="item">box5</div>
</section>
I created a new Angular 5.2 project using the CLI (e.g. ng new MyApp)
Changing to the folder and running the app works fine. (e.g ng serve)
I made the following changes to the generated code (see below). There are only HTML and CSS code changes, very minor, what I posted is the entirety of the changes.
When I save the code it recompiles, and a warning is thrown:
ErrorEmittedError: (Emitted value instead of an instance of Error)
autoprefixer: D:\MyApp\src\app\app.component.css:51:7: Can not find
grid areas: header, nav, content, sidebar, ad, footer
The error seems to be related to the media query section of the CSS. If I remove that section the error goes away.
I don't remember this happening in Angular 4.x? Any ideas what's going on?
app.component.html
<div class="wrapper">
<header class="main-head">The header</header>
<nav class="main-nav">
<ul>
<li>Nav 1</li>
<li>Nav 2</li>
<li>Nav 3</li>
</ul>
</nav>
<article class="content">
<h1>Main article area</h1>
<p>In this layout, we display the areas in source order for any screen less that 500 pixels wide. We go to a two column layout, and then to a three column layout by redefining the grid, and the placement of items on the grid.</p>
</article>
<aside class="side">Sidebar</aside>
<div class="ad">Advertising</div>
<footer class="main-footer">The footer</footer>
</div>
app.compnent.css
.main-head {
grid-area: header;
}
.content {
grid-area: content;
}
.main-nav {
grid-area: nav;
}
.side {
grid-area: sidebar;
}
.ad {
grid-area: ad;
}
.main-footer {
grid-area: footer;
}
.wrapper {
display: grid;
grid-gap: 20px;
grid-template-areas:
"header"
"nav"
"content"
"sidebar"
"ad"
"footer";
}
#media (min-width: 700px) {
.wrapper {
grid-template-columns: 1fr 4fr 1fr;
grid-template-areas:
"header header header"
"nav content sidebar"
"nav content ad"
"footer footer footer"
}
nav ul {
flex-direction: column;
}
}
I am having a similar problem and the solution I have found so far isn't a great one since it duplicates code, but it may help you.
First I realize that the error is just a warning and the code complies without a problem however it is worrisome so I added the classes that I defined outside of the #media within the curly braces so with your code it would look something like this:
.main-head {
grid-area: header;
}
.content {
grid-area: content;
}
.main-nav {
grid-area: nav;
}
.side {
grid-area: sidebar;
}
.ad {
grid-area: ad;
}
.main-footer {
grid-area: footer;
}
.wrapper {
display: grid;
grid-gap: 20px;
grid-template-areas:
"header"
"nav"
"content"
"sidebar"
"ad"
"footer";
}
#media (min-width: 700px) {
.wrapper {
grid-template-columns: 1fr 4fr 1fr;
grid-template-areas:
"header header header"
"nav content sidebar"
"nav content ad"
"footer footer footer"
}
nav ul {
flex-direction: column;
}
.main-head {
grid-area: header;
}
.content {
grid-area: content;
}
.main-nav {
grid-area: nav;
}
.side {
grid-area: sidebar;
}
.ad {
grid-area: ad;
}
.main-footer {
grid-area: footer;
}
}
Again I don't like this solution but it gets rid of the error.
If you're using Sass, to not repeat yourself as much, create a partial (_grid-areas.scss) with a mixin:
#mixin grid-areas {
body {
.leftbar { grid-area: leftbar; }
.rightbar { grid-area: rightbar; }
.main { grid-area: main;
header { grid-area: header; }
#content {grid-area: content; }
}
}
}
Then import it as needed:
#import 'grid-areas';
#media screen and (max-width: 80em) {
#include grid-areas;
}
Clears my errors in CLI 1.7.2
This warning occurred while using named grid-areas in Sass.
According to the CSS Tricks article posted by iwis, the issue is due to a conflict with Autoprefixer and the IE browser's support for the grid property.
To resolve the warning from Sass I simply replaced the property grid-template-areas: with the propertygrid-template:.