CSS Grid Media Query not working (Beginner) - css

The standard Pc pixel version is working fine! Pc Version image.
The mobile version the right sidebar should go under the content but its still on the right Mobile version, and i do not know what seems to be the problem,in the home.css media query grid-template-areas i guess im missing something, im new on Css Grid
home.css
#media only screen and (min-width: 768px) {
.grid{
display: grid;
grid-template-areas:
'lsidebar content content'
'lsidebar rsidebar rsidebar'
'footer footer footer';
grid-template-columns:1fr 4fr;
}
}
.grid{
display: grid;
padding: 20px;
grid-template-areas:
'lsidebar content rsidebar'
'lsidebar content rsidebar'
'footer footer footer';
grid-template-columns:1fr 3fr 1fr;
}
.lsidebar{
grid-area:lsidebar;
}
.content{
grid-area:content;
}
.rsidebar{
grid-area:rsidebar;
}
.footer{
grid-area:footer;
}
home.blade.php:
#extends('layouts.app')
#section('content')
<link rel="stylesheet" href="{{ asset('css/home.css') }}">
<div class="grid">
<div class="lsidebar">
<ul>
<li>
Home
</li>
<li>
Messages
</li>
<li>
Games
</li>
<li>
Pages
</li>
</ul>
</div>
<div class="content">
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Quos voluptatum vitae rem harum hic. Molestias laudantium
dolore quod modi repudiandae, ullam molestiae tempore sed ratione minus nihil pariatur fugit veniam?
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Voluptas ex soluta nihil molestias asperiores repudiandae. Aspernatur accusamus, enim alias quod, non neque aliquam, soluta aut autem mollitia eaque
impedit assumenda.
</div>
<div class="rsidebar">
Show Friends of this profile
</div>
<div class="footer">
<p style="text-align: center"> This is the footer copyright 2020 This is the footer copyright 2020 This is the footer copyright 2020</p>
</div>
</div>
#endsection

I made a demo of what you want in code pen.
grid demo
resize the browser to see grid changes in different media query.
.lsidebar {
grid-area: lsidebar;
background-color: red;
}
.content {
grid-area: content;
background-color: green;
}
.rsidebar {
grid-area: rsidebar;
background-color: blue;
}
.footer {
grid-area: footer;
background-color: pink;
}
.grid {
display: grid;
grid-gap: 16px;
grid-template-areas:
"lsidebar"
"content"
"rsidebar"
"footer";
}
#media (min-width: 768px) {
.grid {
grid-template-columns: 1fr 3fr;
grid-template-areas:
"lsidebar content"
"lsidebar content"
"lsidebar rsidebar"
"footer footer";
}
}
#media (min-width: 1100px) {
.grid {
grid-template-columns: 1fr 3fr 1fr;
grid-template-areas:
'lsidebar content rsidebar'
'lsidebar content rsidebar'
'footer footer footer';
}
}

Related

Running into grid item text overflow vertically on my sidebar

Running into an issue: I have a total of five grid items, but the problem lies within two items: leftbar & rightbar - the text overflows vertically when I scale the page down. Instead, I would like for the text to remain wthin the two grid items and mold around the brand shown in the middle. I understand that I can use overflow:scroll and it would solve my issue, but that's not a solution I'm looking for. Ideally, I want the the left/rightbars to adjust around the other grid items.
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
</head>
<body>
<header>
<div class = "header">
Hello, World.
</div>
</header>
<div class = "container flex">
<nav class="nav">
<ul>
<li>Home</li>
<li>Gallery</li>
<li>Options</li>
<li>Contact</li>
</ul>
</nav>
<main class="logo-center">
<div class= "logo-center-grid-items">
<h1></h1>
<h1></h1>
<h1></h1>
<h1></h1>
</div>
</main>
<div class="leftbar">
<h3>the art.</h3><br>
<p> Lorem ipsum dolor sit amet consectetur adipisicing elit. Minima nostrum facere eum laborum blanditiis exercitationem doloremque. Veritatis suscipit sunt a earum error illum quam, adipisci, fuga corporis in distinctio molestias!
</p> <br>
<div class = "creations-btn">
creations
</div><br>
</div>
<div class="rightbar">
<h3>the craft.</h3><br>
<p> Lorem ipsum dolor sit amet consectetur adipisicing elit. Minima nostrum facere eum laborum blanditiis exercitationem doloremque. Veritatis suscipit sunt a earum error illum quam, adipisci, fuga corporis in distinctio molestias!
</p><br>
<div class = "order-btn">
order
</div><br>
</div>
<footer>
<div class="social-media">
<i class="fab fa-facebook"></i>
<i class="fab fa-instagram"></i>
<i class="fab fa-discord"></i>
<i class="fab fa-youtube"></i>
</div>
</footer>
</div>
</body>
</html>
My CSS:
:root{
--main-radius: 25px;
--main-padding: 40px;
box-sizing: border-box;
}
.container{
display: grid;
height: 100vh;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: auto 35% 35% auto;
grid-template-areas:
"nav nav nav nav"
"leftbar main main rightbar"
"leftbar main main rightbar"
"footer footer footer footer";
grid-gap: 1.5rem;
text-align: center;
margin: 15px;
margin-bottom: 45px;
}
/* grid items (5) */
/* nav (deleted for the sake of unimportance) */
/* logo center */
.logo-center {
grid-area: main;
display: grid;
}
.logo-center > div {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-template-rows: repeat(2, 1fr);
}
.logo-center h1 {
background-attachment: fixed;
}
/* left sidebar */
.leftbar{
grid-area: leftbar;
}
/* right sidebar*/
.rightbar{
grid-area: rightbar;
}
.rightbar > p{
margin: 20px;
}
/* footer (deleted for the sake of unimportance) */
I did find one solution that allowed the items to form around the grid like I want, adding inline-table to the parents...
display:inline-table;
width: 100%;
but ultimately this solution comes with the price of the sidebars digging into the footer.

Left align div to centered container

I have a container centered with a max width as follow:
#container {
margin-left: auto;
margin-right: auto;
max-width: 900px;
}
Now I want to create a new div just below the contained one, but I want this new div to align to the left and expand beyond the right side of the container. Something like that:
|||||||||||||||||||||||||||||||||||| #container
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| #div 2
The difficulty is that #container margins are auto, so how can I force #div to follow #container left margin as the browser resizes?!
NOTE: I am looking for a pure CSS solution WITHOUT JAVASCRIPT
EDIT: It was not clear in my explanation but, my goal was to make the #div ADJACENT to the #container. Like this:
<div id="container"></div>
<div id="div"></div>
I ended up refactoring my html to use #bananabran solution with absolute positioning which simply uses parent-child structure:
<div id="container">
<div id="div"></div>
</div>
You don't have to force the div to follow its container's left margin. Divs naturally start at the top-left of their container (unless otherwise specified or affected by). You also do not need to use Grid or FlexBox. CSS3, and even CSS2 can do this natively.
See working CodePen example: https://codepen.io/bananabrann/pen/QWWdXQZ
Assuming you have no other code affecting your code...
<div id="container" />
<div id="my-div" />
#container {
background-color: blue;
position: relative;
margin: 0 auto;
width: 900px;
height: 300px;
}
#my-div {
background-color: red;
position: absolute;
bottom: 0;
width: 500px;
height: 20px;
}
CSS-Grid can do that:
.wrap {
display: grid;
grid-template-columns: 1fr minmax(auto, 400px) 1fr;
/* 400px for demo purposes */
}
.container {
padding: 1em;
background: pink;
grid-column-start: 2;
}
.wide-r {
padding: 1em;
background: lightgreen;
grid-column: 2 / span 2;
}
<div class="wrap">
<div class="container">Container
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates, similique, maxime aspernatur dolorum quod recusandae possimus fuga blanditiis laudantium delectus quis magni. Veniam, consequuntur dolores facilis cupiditate fugiat ullam aspernatur!
Corporis excepturi quos esse voluptatem voluptatibus corrupti ea, tempora culpa magni, hic aspernatur pariatur molestias itaque doloremque assumenda ad fugiat!</p>
</div>
<div class="wide-r">Wide Right</div>
</div>

Problem with positioning elements using Grid

I am trying to place a section with 4x4 fields. Each field will hold a picture with header and paragraph centered horizontally and vertically on a picture. I would like to do that by not implementing pictures as a backgrounds for each field. I am having problem centering header and paragraph in a field.
.container-2 {
width: 100%;
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(2, 1fr);
margin-bottom: 100px;
}
#pic-1 {
grid-column: 1/3;
grid-row: 1/2;
width: 100%;
height: 100%;
}
#pic-1 img {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#pic-1 h3 {
z-index: 1;
text-align: center;
}
#pic-2 {
grid-column: 3/5;
grid-row: 1/2;
}
#pic-3 {
grid-column: 1/3;
grid-row: 2/3;
}
#pic-4 {
grid-column: 3/5;
grid-row: 2/3;
}
<div class="container-2">
<div id="pic-1">
<img src="./img/practise-areas.jpg" alt="">
<h3>USLUGE KOJE PRUZAMO</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Animi velit, consequatur veniam dolorem eligendi tenetur ex?</p>
</div>
<div id="pic-2">
<img src="./img/who-we-are.jpg" alt="">
<h3>KO SMO MI?</h3>
</div>
<div id="pic-3">
<img src="./img/getting-started2.jpg" alt="">
<h3>KONTAKTIRAJTE NAS</h3>
</div>
<div id="pic-4">
<img src="./img/how-we-work.jpg" alt="">
<h3>NAS NACIN RADA</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Animi velit, consequatur veniam dolorem eligendi tenetur ex?</p>
</div>
</div>
[...] not implementing pictures as a backgrounds
One approach which creates the effect you are looking to achieve (ie. employing marked up images as element backgrounds) is to give each image which precedes the heading and the paragraph a position: absolute (to take it out of document flow) and then apply the styles: top: 0, left: 0 and width: 100%, height: 100%.
I am having problem centering header and paragraph in a field.
To horizontally center the heading and the paragraph in each field, you simply need to apply the style: text-align: center.
Working Example:
.container-2 {
display: grid;
grid-template-columns: repeat(2, 50%);
grid-template-rows: repeat(2, 50%);
}
.container-2 div {
position: relative;
text-align: center;
}
.container-2 img {
position: absolute;
display: block;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
<div class="container-2">
<div id="pic-1">
<img src="./img/practise-areas.jpg" alt="">
<h3>USLUGE KOJE PRUZAMO</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Animi velit, consequatur veniam dolorem eligendi tenetur ex?</p>
</div>
<div id="pic-2">
<img src="./img/who-we-are.jpg" alt="">
<h3>KO SMO MI?</h3>
</div>
<div id="pic-3">
<img src="./img/getting-started2.jpg" alt="">
<h3>KONTAKTIRAJTE NAS</h3>
</div>
<div id="pic-4">
<img src="./img/how-we-work.jpg" alt="">
<h3>NAS NACIN RADA</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Animi velit, consequatur veniam dolorem eligendi tenetur ex?</p>
</div>
</div>

How do I show a "2xN" grid from desktop "1x2*N" on mobile?

I want to make layout like this
On PC:
List 1 |List2
L1.Item1|L2.Item1
L1.Item2|L2.Item2
L1.ItemN|L2.ItemN.
Show on mobile like this:
List 1
L1.Item1
L1.Item2
L1.ItemN
List2
L2.Item1
L2.Item2
L2.ItemN
html, body {
height: 100%;
}
* {
margin: 0;
padding: 0;
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 50px repeat(auto-fit, auto);
background-color: #1aaa00;
height: 100%;
text-align: center;
}
.grid > * {
outline: 1px dashed #666;
}
.head {
grid-area: 1 / 1 / 3 / 3;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="grid">
<div class="head">Header</div>
<div>List 1</div>
<div>List 2</div>
<div>Position 1. List 1 <br> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Culpa aliquam ipsa alias molestias accusamus enim veritatis! Pariatur fugiat maiores fuga sed nisi itaque quisquam, recusandae, accusamus cumque. Facilis, ipsum, vitae?</div>
<div>Position 1. List 2</div>
<div>Position 2. List 1</div>
<div>Position 2. List 2 <br> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Obcaecati, dolorum!</div>
<div>Position 3. List 1</div>
<div>Position 3. List 2</div>
</div>
</body>
</html>
Every item must be opposite the same item at neighbouring list. The number of rows is not fixed but same at both lists. Height of rows can be different also. It depends on content.
How can I show on mobile List 1 first and List 2 under it?
I tried grid-auto-flow: column, and reorder DIVs in HTML, but I need to specify grid-template-rows to make it work. And I don't know how much rows will it be so don't know how much 1fr set there.
You can adjust the order of only the second elements of the column using nth-child() then simply make your layout one column on mobile:
/*html,
body {
height: 100%;
} no more needed with 100vh*/
* {
margin: 0;
padding: 0;
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
background-color: #1aaa00;
min-height: 100vh; /*better use min-height*/
text-align: center;
}
.grid>* {
outline: 1px dashed #666;
}
.head {
grid-column: 1 / -1; /*use -1 to avoid creating a extra column on mobile*/
min-height: 50px;
}
#media all and (max-width:800px) {
.grid {
grid-template-columns: 1fr; /*one column*/
}
.grid> :nth-child(2n+1) {
order: 2; /*all the list2 at the bottom*/
}
.grid>div.head {
order: -1; /*the head should stay on the top*/
}
}
<div class="grid">
<div class="head">Header</div>
<div>List 1</div>
<div>List 2</div>
<div>Position 1. List 1 <br> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Culpa aliquam ipsa alias molestias accusamus enim veritatis! Pariatur fugiat maiores fuga sed nisi itaque quisquam, recusandae, accusamus cumque. Facilis, ipsum, vitae?</div>
<div>Position 1. List 2</div>
<div>Position 2. List 1</div>
<div>Position 2. List 2 <br> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Obcaecati, dolorum!</div>
<div>Position 3. List 1</div>
<div>Position 3. List 2</div>
</div>
In case you want to have the good order in your HTML code you can try this:
/*html,
body {
height: 100%;
} no more needed with 100vh*/
* {
margin: 0;
padding: 0;
}
.grid {
display: grid;
grid-template-columns: 1fr 1fr;
background-color: #1aaa00;
grid-auto-flow:dense; /*to fill all the space*/
min-height: 100vh; /*better use min-height*/
text-align: center;
}
.grid>* {
outline: 1px dashed #666;
grid-column:1; /*list one column 1*/
}
.l2,
.l2 ~ * {
grid-column:2; /*list two column 2*/
}
.head {
grid-column: 1 / -1; /*use -1 to avoid creating a extra column on mobile*/
min-height: 50px;
}
#media all and (max-width:800px) {
.grid {
grid-template-columns: 1fr; /*one column*/
}
.l2,
.l2 ~ * {
grid-column:1; /*list two column 1*/
}
}
<div class="grid">
<div class="head">Header</div>
<div>List 1</div>
<div>Position 1. List 1 <br> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Culpa aliquam ipsa alias molestias accusamus enim veritatis! Pariatur fugiat maiores fuga sed nisi itaque quisquam, recusandae, accusamus cumque. Facilis, ipsum, vitae?</div>
<div>Position 1. List 1</div>
<div>Position 3. List 1</div>
<div class="l2">List 2</div>
<div>Position 1. List 2</div>
<div>Position 2. List 2 <br> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Obcaecati, dolorum!</div>
<div>Position 3. List 2</div>
</div>

Two columns with one header layout with CSS3 Flexbox

Like at picture above I need such layout. DIV1 contains static text and to DIV2 data (text) coming from other files (EX.JSON) so it's variable. The point is to keep both divs always with same height based on height of heigher div.
Note: I don't want this with float.
Image courtesy: One of Test I Given Online.
Hi
You can do this easly with CSS3 Flexbox like you asking.
Solution
Here is snippet with working example. I used Flexbox and detalils you have in comments in the code.
#main {
/*Styles for sample presentation*/
padding: 20px;
border: 1px solid tomato;
}
#wrapper {
display: flex;
flex-flow: row wrap;
/* The remaining place (horizontaly) will be spread out around divs in wrapper. */
justify-content: space-around;
/*Styles for sample presentation*/
border: 1px solid royalblue;
}
#wrapper>header {
/* To keep header 100% width. */
flex: 0 0 100%;
text-align: center;
/*Styles for sample presentation*/
background-color: sandybrown;
}
#wrapper>div {
margin: 10px;
padding: 20px;
/* To center the text vertically. */
display: flex;
flex-flow: column nowrap;
justify-content: center;
/*Styles for sample presentation*/
border: 1px solid maroon;
text-align: justify;
}
#text-static {
/*Low flex basis values to keep it next to each other divs*/
flex: 1 0 30%;
}
#wrapper>div#text-json {
/*Low flex basis values to keep it next to each other divs*/
flex: 0 0 25%;
margin-left: 0;
}
<div id="main">
<div id="wrapper">
<header>
Sample header
</header>
<div id="text-static">
Lorem Ipsum jest tekstem stosowanym jako przykładowy wypełniacz w przemyśle poligraficznym. Został po raz pierwszy użyty w XV w. przez nieznanego drukarza do wypełnienia tekstem próbnej książki. Pięć wieków później zaczął być używany przemyśle elektronicznym,
pozostając praktycznie niezmienionym. Spopularyzował się w latach 60. XX w. wraz z publikacją arkuszy Letrasetu, zawierających fragmenty Lorem Ipsum, a ostatnio z zawierającym różne wersje Lorem Ipsum oprogramowaniem przeznaczonym do realizacji
druków na komputerach osobistych, jak Aldus PageMaker
</div>
<div id="text-json">
a Lorem Ipsum a Lorem Ipsum a Lorem Ipsum
</div>
</div>
</div>
If you want to try case when right div has more text than left you can edit same snippet as above there.
Knowledge
More informations about CSS3 Flexbox you have e.g. on this W3Schools site.
A nice learning tool that I found recently flexboxfroggy.com .
Hope that was helpful.
Cheers
Here is my implementation using CSS Grid which in my opinion makes this a lot easier especially when working with layouts. I am using SASS for styling. I hope this helps. Here is a link of the snippet on codepen.io
HTML CODE
<div class="main-div">
<div class="wrapper-div">
<div class="sample-header">Header</div>
<div class="div1">Lorem ipsum dolor sit amet consectetur adipisicing elit. Dicta blanditiis, error dolorem, velit tempora, magni ea officiis itaque voluptates aliquid consectetur deserunt quisquam tenetur dolor! Labore assumenda iusto debitis autem. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nulla, velit cumque quaerat optio vero sed dolores maxime dolorum aut itaque? Asperiores, esse. Nihil dignissimos nisi debitis molestiae facilis accusamus non! Lorem ipsum dolor sit amet consectetur, adipisicing elit. Architecto quisquam corrupti error nesciunt pariatur quidem, voluptates similique obcaecati magni aperiam autem aliquam ex, ducimus, distinctio amet labore vel blanditiis sapiente. </div>
<div class="div2">Little bit of text here </div>
</div>
</div>
CSS STYLES USING CSS GRID
.main-div{
border:1px solid #000;
width:500px;
margin:0 auto;
padding:10px;
.wrapper-div{
display: grid;
grid-gap: 10px;
grid-template-columns: 3fr 1fr;
background-color: #fff;
// align-items:center;
color: #444;
margin:10px 0;
.sample-header {
grid-column: 1 / 3;
grid-row: 1;
background-color:lightgrey;
padding:10px;
text-align:center;
}
.div1,.div2{
border:1px solid #000;
padding:10px;
display:grid;
align-items:center;
}
.div1 {
grid-column: 1 ;
grid-row: 2 ;
}
.div2 {
grid-column: 2;
grid-row: 2;
}
}
}
https://codepen.io/anon/pen/OQZNgX
On the container for Div 1 & Div 2 apply this CSS
display: flex;
justify-content: center;
And then on the child divs, use flex-basis to specify their width
flex-basis: 75%;
/* and / or */
flex-basis: 25%;
See above code pen link for a working demo!
You can use bootstrap to achieve this... You can see below code for this type of design.
<div class='container'>
<div class='col-md-12 customHeaderclass'>
Your header
</div>
<div class='row'>
<div class='col-md-8'>
Your big content
</div>
<div class='col-md-4'>
Your small content
</div>
</div>
</div>
I hope this code will help you
Thanks & Regards.

Resources