CSS Grid Columns stacking with large gap under each column - html

I'm relatively new to CSS grid, I'm having issues with my columns. On desktop, columns 2 and 4 have a large gap between them. I'm struggling to close this gap as they must sit directly under each other. So, when each column has different heights of text in, all columns must sit nicely under each other with no gap. Then on mobile they all just stack nicely.
I have created a demo:
http://jsfiddle.net/1uvwyL7h/
.one {
background: red;
}
.two {
background: pink;
}
.three {
background: green;
}
.four {
background: yellow;
}
.wrapper section {
height: fit-content;
}
#media (max-width: 768px) {
.wrapper section:nth-child(2) {
margin: 32px 0 40px;
}
.wrapper section:nth-child(4) {
margin: 24px 0 40px;
}
}
#media (min-width: 768px) {
.wrapper {
display: grid;
gap: 1rem 4rem;
grid-template-columns: repeat(2, 1fr);
}
.wrapper section {
grid-column: 1/2;
}
.wrapper section.sp {
grid-column: 2/3;
grid-row: 1 / span 3;
}
.wrapper section.sa {
grid-column: 2/2;
grid-row: 3 / span 1;
}
}
#media (min-width: 1280px) {
.wrapper {
padding: 32px 24px 24px;
}
}
<div class="wrapper">
<section class="one">
<p>
MOBILE ORDER 1
</p>
<P>
DESKTOP COL 1 TOP LEFT
</P>
<p>
bla bla<br />bla bla<br />bla bla<br />bla bla<br />
</p>
<p>
bla bla<br />bla bla<br />bla bla<br />bla bla<br />
</p>
</section>
<section class="two sp">
<p>
MOBILE ORDER 2
</p>
<P>
DESKTOP COL 2 TOP RIGHT
</P>
<p>
bla bla<br />bla bla<br />bla bla<br />bla bla<br />
</p>
<p>
bla bla<br />bla bla<br />bla bla<br />bla bla<br />
</p>
<p>
bla bla<br />bla bla<br />bla bla<br />bla bla<br />
</p>
<p>
bla bla<br />bla bla<br />bla bla<br />bla bla<br />
</p>
</section>
<section class="three">
<p>
MOBILE ORDER 3
</p>
<P>
DESKTOP COL 1 BOTTOM LEFT
</P>
<p>
bla bla<br />bla bla<br />bla bla<br />bla bla<br />
</p>
<p>
bla bla<br />bla bla<br />bla bla<br />bla bla<br />
</p>
<p>
bla bla<br />bla bla<br />bla bla<br />bla bla<br />
</p>
<p>
bla bla<br />bla bla<br />bla bla<br />bla bla<br />
</p>
</section>
<section class="four sa">
<p>
MOBILE ORDER 4
</p>
<P>
DESKTOP COL 2 BOTTOM RIGHT
</P>
<p>
bla bla<br />bla bla<br />bla bla<br />bla bla<br />
</p>
<p>
bla bla<br />bla bla<br />bla bla<br />bla bla<br />
</p>
</section>
</div>
Thanks

Revised the CSS. Some factors in your grid are not needed.
We have a CSS called 'grid-container' next to your 'wrapper' in DIV, but you can merge both of those together (though its ideal to place all grid setup in the 'grid-container'.
Making a starter grid from scratch with all the basic parameters helps you to catch certain errors, such as loosing the grid box auto height on the text (longer text in one grid and shorter text in other grid).
Additional 'How To' info:
mozilla
/*--====Grid====--*/
.grid-container {
display: grid;
grid-area: auto / auto / auto / auto;
gap: 10px;
padding: 10px;
/*--background-color: #2196F3;--*/ /*--If you want background color--*/
}
.one {background: red;}
.two {background: pink;}
.three {background: green;}
.four {background: yellow;}
/*--This not needed--*/
xxxx.wrapper section {height: fit-content;}
/*--This not needed--*/
xxx#media (max-width: 768px) {
.wrapper section:nth-child(2) {
margin: 32px 0 40px;
}
.wrapper section:nth-child(4) {
margin: 24px 0 40px;
}
}
#media (min-width: 768px) {
.wrapper {
display: grid;
gap: 1rem 1rem;
grid-template-columns: repeat(2, 1fr);
}
section.one {
grid-area: 1 / 1;
}
section.two {
grid-area: 1 / 2;
}
section.three {
grid-area: 2 / 1;
}
section.four {
grid-area: 2 / 2;
}
}
}
/*--This not needed--*/
xxx#media (min-width: 1280px) {
.wrapper {
padding: 32px 24px 24px;
}
}
h1, h2, h3, h4, p {
color: black;
padding: 10px;
margin: 0;
}
<div class="grid-container wrapper">
<section class="one">
<h2> MOBILE ORDER 1 </h2>
<h4> DESKTOP COL 1 TOP LEFT </h4>
<p> bla bla<br />bla bla<br />bla bla<br />bla bla<br /> </p>
<p> bla bla<br />bla bla<br />bla bla<br />bla bla<br /> </p>
</section>
<section class="two sp">
<h2> MOBILE ORDER 2 </h2>
<h4> DESKTOP COL 2 TOP RIGHT </h4>
<p> bla bla<br />bla bla<br />bla bla<br />bla bla<br /> </p>
<p> bla bla<br />bla bla<br />bla bla<br />bla bla<br /> </p>
<p> bla bla<br />bla bla<br />bla bla<br />bla bla<br /> </p>
<p> bla bla<br />bla bla<br />bla bla<br />bla bla<br /> </p>
</section>
<section class="three">
<h2> MOBILE ORDER 3 </h2>
<h4> DESKTOP COL 1 BOTTOM LEFT </h4>
<p> bla bla<br />bla bla<br />bla bla<br />bla bla<br /> </p>
<p> bla bla<br />bla bla<br />bla bla<br />bla bla<br /> </p>
<p> bla bla<br />bla bla<br />bla bla<br />bla bla<br /> </p>
<p> bla bla<br />bla bla<br />bla bla<br />bla bla<br /> </p>
</section>
<section class="four sa">
<h2> MOBILE ORDER 4 </h2>
<h4> DESKTOP COL 2 BOTTOM RIGHT </h4>
<p> bla bla<br />bla bla<br />bla bla<br />bla bla<br /> </p>
<p> bla bla<br />bla bla<br />bla bla<br />bla bla<br /> </p>
</section>
</div>

Related

CSS grid: appearance of no grid-row-gap depending on content of adjacent cells

main {
display: grid;
grid-template-columns: 1fr 1fr;
grid-column-gap: 60px;
grid-row-gap: 60px;
}
p {
grid-column: 2 / span 1;
margin-bottom: 0;
background-color: yellow;
}
p + p {
margin-top: -60px;
}
<main>
<h3>Heading</h3>
<p>Some text, blah, blah blah</p>
<h3>Heading</h3>
<p>Some text, blah, blah blah</p>
<h3>Heading</h3>
<p>Some text, blah, blah blah</p>
<p>Another paragraph of text</p>
<p>Possibly another paragraph of text</p>
<h3>Heading</h3>
<p>Some text, blah, blah blah</p>
</main>
I have a simple two column grid, with headings in the left column and paragraph text in the right hand column. I also have grid-row-gap set to give a nice gap between the rows. This all works fine, until after a heading, there is more than one paragraph <p> element. This second <p> element goes into a new row. I want all the <p> paragraph text to be inside one cell. Or for there to be no grid-row-gap between rows containing adjacent <p> elements. Or some sort of fudge.
This is what I want. Two columns. <h3> in left column. Text in right column. And if there is more than one <p> for there to be no grid-row-gap (or the appearance of no gap) between them.
<h3> | <p>
==========
<h3> | <p>
==========
<h3> | <p>
| <p>
| <p>
=========
<h3> | <p>
Here is my code
<main>
<h3>Heading</h3>
<p>Some text, blah, blah blah</p>
<h3>Heading</h3>
<p>Some text, blah, blah blah</p>
<h3>Heading</h3>
<p>Some text, blah, blah blah</p>
<p>Another paragraph of text</p>
<p>Possibly another paragraph of text</p>
<h3>Heading</h3>
<p>Some text, blah, blah blah</p>
</main>
main {
display: grid;
grid-template-columns: 1fr 1fr;
grid-column-gap: 60px;
grid-row-gap: 60px;
}
p {
grid-column: 2 / span 1;
margin-bottom: 0;
background-colour: yellow;
}
I would do the reasoning on the first child like below
main {
display: grid;
grid-template-columns: 1fr 1fr;
grid-column-gap: 60px;
}
h3 {
margin: 0;
}
p {
grid-column: 2;
margin: 0;
background-color: yellow;
}
h3:not(:first-child),
h3:not(:first-child) + p{
margin-top: 60px
}
<main>
<h3>Heading</h3>
<p>Some text, blah, blah blah</p>
<h3>Heading</h3>
<p>Some text, blah, blah blah</p>
<h3>Heading</h3>
<p>Some text, blah, blah blah</p>
<p>Another paragraph of text</p>
<p>Possibly another paragraph of text</p>
<h3>Heading</h3>
<p>Some text, blah, blah blah</p>
</main>
I guess I can do something like this:
p + p {
margin-top: -60px;
}
Easiest and most reasonable thing to do seems to be just wrapping your paragraphs in div elements.
main {
display: grid;
grid-template-columns: 1fr 1fr;
grid-column-gap: 60px;
grid-row-gap: 60px;
}
main div {
grid-column: 2 / span 1;
}
p {
margin-bottom: 20px;
}
<main>
<h3>Heading</h3>
<div>
<p>Some text, blah, blah blah</p>
</div>
<h3>Heading</h3>
<div>
<p>Some text, blah, blah blah</p>
</div>
<h3>Heading</h3>
<div>
<p>Some text, blah, blah blah</p>
<p>Another paragraph of text</p>
<p>Possibly another paragraph of text</p>
</div>
<h3>Heading</h3>
<div>
<p>Some text, blah, blah blah</p>
</div>
</main>

align-items: stretch doesn't work when an item is overflow

In this flexbox skeleton, I have the .body that is containing 3 rows.
The .main row (blue) contains a content that will always overflow.
I want to keep scroll in body (like it is now), but I want also that the 3 rows including (.main) grow and fill all the .body(green) section.
So my problem, when the body overflow correctly, its children doesn't stretch with it.
You can have the code directly here:
https://jsfiddle.net/garalimedkarim/5khms9Lx/16/
Html
<div class="container">
<div class="header">
Header
</div>
<div class="body">
<div class="left-sidebar"></div>
<div class="main">
<div class="content">
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
<p> Karim </p>
</div>
</div>
<div class="right-sidebar"></div>
</div>
</div>
css:
html,
body {
margin: 0;
background-color: red;
}
.container {
background-color: cyan;
height: 20rem;
display: flex;
flex-flow: column nowrap;
}
.header {
flex: 0 0 2rem;
background-color: bisque;
}
.body {
background-color: green;
flex: 1;
overflow: auto;
display: flex;
flex-flow: row nowrap;
}
.left-sidebar {
background-color: blueviolet;
flex: 0 0 25%;
}
.main {
background-color: blue;
flex: 0 0 50%;
}
.right-sidebar {
background-color: black;
flex: 0 0 25%;
}
Flex is kinda funky when you're managing sizing in 2D
Grid is better for that
.body {
background-color: green;
flex: 1;
overflow: auto;
display: grid;
grid-template-columns: 0.25fr 0.5fr 0.25fr;
}

Div Not Floating When Text Wraps to Second Line?

I ran into a floating/alignment issue and can't seem to figure it out. When I float some DIVs next to each other they all line up like they should... until the text from one of the DIVs falls to a second line.
If you look below you can see how Line 1 shows up. As you can see, ABCD sits next to the other DIVs on the same line.
On Line 2 you can see how EFGH falls to another line, even though there's plenty of space for it on the previous line.
How can I make it so the DIVs always sit next to each other regardless of how many lines of text there are?
Any help would be greatly appreciated!
SCREENSHOT
#sample-list {
margin-bottom: 35px;
float: left;
width: 400px;
background: orange;
word-wrap: break-word;
}
.sample-overview {
width: 100%;
float: left;
font-family: "open sans", "arial";
font-size: 12px;
line-height: 14px;
margin-bottom: 20px;
word-wrap: break-word;
}
.sample-name-wrap {
width: 100%;
white-space: normal;
}
.sample-name {
float: left;
}
.sample-dash {
float: left;
margin: 0 5px;
}
.sample-div {
float: left;
background: #ccc;
}
<div id="sample-list">
<div class="sample-line">
<div class="sample-overview">
<div class="sample-name-wrap">
<div class="sample-name">
Line 1: ostrum exercitationem ullam corporis
</div>
<div class="sample-dash">–</div>
<div class="sample-div">ABCD</div>
</div>
</div>
</div>
<div class="sample-line">
<div class="sample-overview">
<div class="sample-name-wrap">
<div class="sample-name">
Line 2: atis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab sdaf sfffdaf ntoowerf jdj
</div>
<div class="sample-dash">–</div>
<div class="sample-div">EFGH</div>
</div>
</div>
</div>
<div class="sample-line">
<div class="sample-overview">
<div class="sample-name-wrap">
<div class="sample-name">
Line 3: vitae dicta sunt explicabo. Nemo enim
</div>
<div class="sample-dash">–</div>
<div class="sample-div">IJKL</div>
</div>
</div>
</div>
</div>
CODEPEN
https://codepen.io/anon/pen/aEqjRo
Try the code below. I think you dont need to use floats at all. Use spans instead. They have inline display property.
.container {
max-width: 300px;
}
<div class="container">
<div>
Lorem Ipsum bla bla bla bla bla bla bla bla bla bla bla blabla bla bla
<span> - </span>
<span> ABCD </span>
</div>
<div>
Lorem Ipsum bla bla bla bla bla bla bla bla bla bla bla blabla bla bla
<span> - </span>
<span> EFG </span>
</div>
<div>
Lorem Ipsum bla bla bla bla bla bla bla bla bla bla bla blabla bla bla
<span> - </span>
<span> HIJ </span>
</div>
</div>
Using inline elements instead of floated divs fixes the issue. See https://codepen.io/anon/pen/VyQGWM?editors=1100.
<div id="sample-list">
<div class="sample-line">
<div class="sample-overview">
<div class="sample-name-wrap">
Line 1: ostrum exercitationem ullam corporis
<span class="sample-dash">
–
</span>
<span class="sample-span">
ABCD
</span>
</div>
</div>
</div>
<div class="sample-line">
<div class="sample-overview">
<div class="sample-name-wrap">
Line 2: atis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab sdaf sfffdaf ntoowerf jdj
<span class="sample-dash">
–
</span>
<span class="sample-span">
EFGH
</span>
</div>
</div>
</div>
<div class="sample-line">
<div class="sample-overview">
<div class="sample-name-wrap">
Line 3: vitae dicta sunt explicabo. Nemo enim
<span class="sample-dash">
–
</span>
<span class="sample-span">
IJKL
</span>
</div>
</div>
</div>
</div>
#sample-list {
margin-bottom: 35px;
float: left;
width: 400px;
background: orange;
word-wrap: break-word;
}
.sample-overview {
width: 100%;
float: left;
font-family: "open sans", "arial";
font-size: 12px;
line-height: 14px;
margin-bottom: 20px;
word-wrap: break-word;
}
.sample-name-wrap {
width: 100%;
white-space: normal;
}
.sample-dash {
margin: 0 5px;
}
.sample-span {
background: #ccc;
}
If you'd like to see why this is happening visit https://codepen.io/anon/pen/QaQVqP. The only change is a CSS border which illustrates why your floated div is wrapping to the next line.
Try With css Display property.
set Display: inline; to container so that DIVs inside container div sit next to each other.
Hope this helps!

Fixed element positioning relative to container

For a child div if I provide position fixed, it is positioning itself relative to the parent div. As per my understanding, a fixed positioned element is taken out of the flow so that it is relative to the viewport.
My question is why child div is coming inside main div?
Below is my code.
#main {
margin-left : 30px;
background-color : red;
width : 100px;
height : 100px
}
#child {
position : fixed;
background-color : yellow;
width : 50px;
height : 50px;
}
<div id="main">
<div id="child">Child Div</div>
</div>
It's not. It just looks that way. Set the top property to 0 and you'll see it sticks to the top of the viewport. You just didn't move it anywhere.
#main {
margin-left : 30px;
background-color : red;
width : 100px;
height : 100px
}
#child {
position : fixed;
background-color : yellow;
width : 50px;
height : 50px;
top:0;
}
<div id="main">
<div id="child">Child Div</div>
</div>
Another way to see that it's actually fixed would be to add enough content after it so that you can scroll down and see that it is indeed fixed.
#main {
margin-left: 30px;
background-color: red;
width: 100px;
height: 100px
}
#child {
position: fixed;
background-color: yellow;
width: 50px;
height: 50px;
}
<div id="main">
<div id="child">Child Div</div>
</div>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>

Max-width responsive width issues

I'm using max-width and width to make my website responsive to window size.
My content div is not downsizing when the website is made smaller.
The content is told that its max width is 800px, and its set to be 100%.
However its not downsizing when outercontainer does.
Any thoughts?
#charset "utf-8";
/* CSS Document */
#font-face {
font-family: 'Bebas';
src: url(Website%20Specific%20Resources/BEBAS.TTF)
}
body {
background-image:url(Website%20Specific%20Resources/Background.png);
max-width:1920px;
width:100%;
margin: auto;
}
.outercontainer{
height:100%;
max-width:1920px;
width:100%
}
.container {
max-width:960px;
width:100%;
max-height:300px;
height:100%;
margin-top: 100px;
margin:auto;
}
.header {
background-image:url(Website%20Specific%20Resources/New_Banner_.png);
max-height: 300px;
max-width: 1920px;
width: 100%;
height: 100%;
top: 0;
position: fixed;
z-index:5;
}
A {
text-decoration:none;
font-family: "bebas";
color: #fff;
}
li {
z-index:2;
list-style:none;
float:left;
padding-right:50px;
margin-left:25px;
padding-top:15px;
font-size:24px;
}
.nav {
z-index:2;
position: absolute;
}
.content {
font-family: "bebas";
max-width: 800px;
width: 100%;
height: 100%;
margin: auto;
background-color: #009999;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>index</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
</head>
<body>
<div class="outercontainer">
<div class="header">
<div class="container">
<div class="nav">
<ul>
<li>Home </li>
<li>About Us </li>
<li>Products </li>
<li>Gallery </li>
<li>Contact Us </li>
</ul>
</div>
</div>
</div>
<div class="content" >
<p>asdasdasdasdaf</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p>asdasdasdasdafasdasdasdasdafasdasdasdasdafasdasdasdasdafasdasda</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p>sdasdafasdasdasdasdafasdasdasdasdafasdasdasdasdafasdasdasdasdafasdasdasdasdaf</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
</div>
</div>
</body>
</html>
so far i can see it's resizing well in chrome. however i think you forgot to add the meta tag for responsive layout <meta name="viewport" content="width=device-width, initial-scale=1.0">
Ur content is responsive. Since you are not giving space b/w letters that why it is not coming down. Add some space and check it out
<p>sdasdafasdasda sdasdafasdasdasdasdafa sdasdasdasdafas dasdasdasdafasd asdasdasdaf</p>
If you want break the word even without space. U can add the following one
word-wrap: break-word;
Solution: Apply Word-Wrap to your paragraph
Word-wrap:
Usage:
Allow long words to be able to break and wrap onto the next line:
Implementation:
p {
word-wrap: break-word;
}
Working Fiddle: https://jsfiddle.net/rittamdebnath/uy2r6jh7/