How to create 1 row below 2 colums with CSS Grid? - html

Using CSS Grid I'm trying to create a blurb layout that has 2 columns at the top with 1 full row below.
This would allow for FA Icon and Title, with full description underneath, like the image below.
I think I've got the grid layout, but I'm unsure how to make the width of the top 2 columns (icon and title) shift together and auto-fit to the contents?
Would this be easier with firebox?
Thanks,
CodePen Link
HMTL
<div class="main_description_container">
<div class="description_gridwrapper">
<div id="icon"><i class="fas fa-info-circle"><!--tr--></i></div>
<div id="title"><h3>Title Text</h3></div>
<div id="text">Long Paragraph Text</div>
</div>
</div>
CSS
.main_description_container{
display: block;
height: 100%;
width: 100%;
padding: 0px;
margin: 0px;
}
.description_gridwrapper {
display: grid;
grid-template-rows: 1fr 1fr 1fr;
grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
gap: 0px;
height: 100%;
}
.description_gridwrapper #icon {
background-color: #68dd99;
grid-row-start: 1;
grid-column-start: 1;
grid-row-end: 2;
grid-column-end: 2;
}
.description_gridwrapper #icon i.fas{
color: #B1DDF1;
font-size: 36px !important;
max-width: 36px !important;
width: 36px !important;
}
.description_gridwrapper #title {
background-color: #D75DDC;
grid-row-start: 1;
grid-column-start: 2;
grid-row-end: 2;
grid-column-end: 3;
color: #31324E;
}
.description_gridwrapper #text {
background-color: #9FDABB;
grid-row-start: 2;
grid-column-start: 1;
grid-row-end: 3;
grid-column-end: 3;
}

Just let the text-block span both columns with grid-column: span 2. See comments within CSS
/* creates a 2 column grid where the first column for the icon only uses as much space as required and the 2nd column the remaining space */
.description_gridwrapper {
display: grid;
grid-template-columns: min-content auto;
}
/* removes the default margin of the title to be in same line as the icon */
#title h3 {
margin: 0;
}
/* lets the tex-block use both columns */
#text {
grid-column: span 2;
}
<div class="main_description_container">
<div class="description_gridwrapper">
<div id="icon"><i class="fas fa-info-circle">ICON</i></div>
<div id="title"><h3>Title Text</h3></div>
<div id="text">Long Paragraph Text</div>
</div>
</div>

Related

How to use CSS Grid to make layout where one column has a maximum size

I'm trying to make a layout with a "page".
I would have 3 columns, 1st and 3th would use only 10% of the space, and the middle 80%. Until there no problem. But I would like that as soon as the middle part reach 64rem, it's only the first and last column that grow.
Currently I've tried this:
.container {
display: grid;
grid-template-columns: 10% minmax(80%, 64rem) 10%;
grid-template-rows: min-content auto;
min-height: 100vh;
background-color: red;
}
.header {
grid-column-start: 2;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 2;
background-color: blue;
}
.content {
grid-column-start: 2;
grid-column-end: 3;
grid-row-start: 2;
grid-row-end: 3;
background-color: yellow;
}
<div class="container">
<div class="header">
header
</div>
<div class="content">
content
</div>
</div>
But it doesn't stops at 64rem. Any idea how to adress this issue?
Replace the 10% with 1fr and consider min() instead of minmax(). I used 32rem instead of 64rem to easily demonstrate the trick
.container {
display: grid;
grid-template-columns: 1fr min(80%, 32rem) 1fr;
grid-template-rows: min-content auto;
min-height: 100vh;
background-color: red;
}
.header {
grid-column: 2;
background-color: blue;
}
.content {
grid-column: 2;
background-color: yellow;
}
<div class="container">
<div class="header">
header
</div>
<div class="content">
content
</div>
</div>
You can also use padding and simplify the code like below:
.container {
display: grid;
padding-inline: max(10%,(100% - 32rem)/2);
grid-template-rows: min-content 1fr;
min-height: 100vh;
background-color: red;
}
.header {
background-color: blue;
}
.content {
background-color: yellow;
}
<div class="container">
<div class="header">
header
</div>
<div class="content">
content
</div>
</div>
You need to think otherwise and set width and max-width on the container itself. your template becomes then : 1fr auto 1fr , wher both sides will grow as much as the middle column will allow them to.
Example below
.container {
display: grid;
grid-template-columns: 1fr auto 1fr;
grid-template-rows: min-content auto;
min-height: 100vh;
background-color: red;
}
.header {
grid-column-start: 2;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 2;
background-color: blue;
}
.content {
grid-column-start: 2;
grid-column-end: 3;
grid-row-start: 2;
grid-row-end: 3;
background-color: yellow;
width:80vw;/* where the parent container fills the whole screen's width */
max-width:64rem;
}
<div class="container">
<div class="header">
header
</div>
<div class="content">
content
</div>
</div>

CSS media query not responding in GRID

index.html
This is the index file with a hero image and hero content(title and subtitle)
<section class= 'container main-section grid'>
<div class="hero-content">
<div class="title">
<h1>Hi, I'm Megha</h1>
</div>
<div class="subtitle">
<p>I’m a software engineer, where I like spending my day with programming and a bit of designing in general.</p>
</div>
</div>
<div class='image-wrapper'>
<div class='girl-image'></div>
</div>
styles.css
Code for overlapping hero content and hero image using CSS grid.
.grid {
display: grid;
grid-template-columns: 2fr 2fr 1fr 1fr;
grid-template-rows: 1fr 2fr;
margin-top: 80px;
gap: 20px;
}
.hero-content {
grid-column: 1 / span 2;
grid-row: 2 / span 2;
z-index: 1;
margin-top: -50px;
align-content: center;
max-width: 80vh;
}
.hero-content .title {
font-family: blackjack;
font-size: 24px;
color: #16161D;
}
.hero-content .subtitle {
font-family: futurapt;
font-size: 22px;
color: #363636
}
.image-wrapper {
grid-column: 2/span 3;
grid-row: 1/span 2;
}
index.css
Code for changing responsive layout with hero content on top and image on the bottom.
#media only screen and (max-width: 1249px) {
header, .hero-content, .social-icons, .image-wrapper {
margin: 0 20px;
}
}
#media only screen and (max-width: 535px) {
.grid {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr 2fr 2fr 2fr;
}
.hero-content {
grid-column: 1;
grid-row: 1 / span 2;
}
.image-wrapper {
grid-column: 1;
grid-row: 3 / span 4;
}
}
The code does not work for responsive design for max-width 535px. I've been looking for a long while. Any help would be much appreciated.
Basically I want to change layout for mobile with a single column and 4 rows. This doesn't work. Why??
I've added a bit of CSS to your girl-image class so we could visualize where it currently lands in your grid. Your hero content DOES overlap your hero image at higher viewport widths. But on mobile, the hero image is under your hero content.
.girl-image {
background-color: cornflowerblue;
height: 100%;
width: 100%;
}
This is what your mobile layout looks like right now:
If you go above 535px, you get the image below:
.grid {
display: grid;
grid-template-columns: 2fr 2fr 1fr 1fr;
grid-template-rows: 1fr 2fr;
margin-top: 80px;
gap: 20px;
}
.hero-content {
grid-column: 1 / span 2;
grid-row: 2 / span 2;
z-index: 1;
margin-top: -50px;
align-content: center;
max-width: 80vh;
}
.hero-content .title {
font-family: blackjack;
font-size: 24px;
color: #16161d;
}
.hero-content .subtitle {
font-family: futurapt;
font-size: 22px;
color: #363636;
}
.image-wrapper {
grid-column: 2 / span 3;
grid-row: 1 / span 2;
}
.girl-image {
background-color: cornflowerblue;
height: 100%;
width: 100%;
}
#media only screen and (max-width: 1249px) {
header,
.hero-content,
.social-icons,
.image-wrapper {
margin: 0 20px;
}
}
#media only screen and (max-width: 535px) {
.grid {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr 2fr 2fr 2fr;
}
.hero-content {
grid-column: 1;
grid-row: 1 / span 2;
}
.image-wrapper {
grid-column: 1;
grid-row: 3 / span 4;
}
}
<section class='container main-section grid'>
<div class="hero-content">
<div class="title">
<h1>Hi, I'm Megha</h1>
</div>
<div class="subtitle">
<p>I’m a software engineer, where I like spending my day with programming and a bit of designing in general.</p>
</div>
</div>
<div class='image-wrapper'>
<div class='girl-image'></div>
</div>
</section>

How do I equally space out css grid columns?

I have a grid that displays data in a tabular format. The previous developer at my company used the grid-template-columns and minmax properties to implement the view but one of the tables has a a wide first column and every other column is alright. How do I equally space them out so they're the same width?
I've tried using minmax(auto, 1fr) and repeat(auto-fill, minmax(max-content, 1fr)) but it just messes up the entire structure of the view
The main container has the following CSS:
display: grid;
grid-template-columns: minmax(min-content, 30%) 1fr auto;
justify-content: center;
And the prt of the page that displays the table data is this :
display: grid;
grid-template-columns: minmax(auto, 1fr);
I expect the spacing to be equal across every column
View image
Here's an example of 3 equal columns using grid:
.parent {
background: blue;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
height: 100%;
width: 100%;
}
.child-1 {
background: red;
grid-column-start: 1;
grid-column-end: 2;
grid-row-start: 1;
grid-row-end: 3 end;
}
.child-2 {
background: green;
grid-column-start: 2;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 3 end;
}
.child-3 {
background: black;
grid-column-start: 3;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 3 end;
}
#-- Non-Grid Styles --#
body {
height: 100vh;
margin: 0;
width: 100vw;
}
.child-1,
.child-2,
.child-3 {
align-items: center;
color: white;
display: flex;
justify-content: center;
}
<div class="parent">
<div class="child-1">child 1</div>
<div class="child-2">child 2</div>
<div class="child-3">child 3</div>
</div>
This link has excellent explanations of how to use grid: Grid Info

CSS Grid - auto column height

The cart form is stretching vertically and the thumbs are positioning in the bottom left corner, when I'd like them to sit directly under the cart form like so:
.product-page--main-content {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: auto 1fr auto;
}
.product-page--main-content>* {
padding: 50px 100px;
border: 1px solid #ccc;
}
.product-page--cart-form-block {
grid-row: 2;
}
.product-page--thumbs {
grid-row: 3;
}
.product-page--images {
grid-column: span 2;
grid-row: span 3;
height: 400px;
}
.product-page--description {
grid-row: span 3;
}
<div class="product-page--main-content">
<div class="product-page--title-n-vendor">Title</div>
<div class="product-page--cart-form-block">Cart form</div>
<div class="product-page--thumbs">Thumbs</div>
<div class="product-page--images">Images</div>
<div class="product-page--description">Description</div>
</div>
Codepen: https://codepen.io/paulmason/pen/rYXyYW
The code you have is working perfectly, as written.
Your image grid item is set to height: 400px.
.product-page--images {
grid-column: span 2;
grid-row: span 3;
height: 400px;
}
Then you have 50px in top and bottom padding.
.product-page--main-content > * {
padding: 50px 100px;
border: 1px solid #ccc;
}
So the image grid item is 500px tall, in a row set to 1fr, in a grid with three rows. It all works perfectly, as specified.
Maybe what you want is four rows:
.product-page--main-content {
display: grid;
grid-template-columns: repeat(4, 1fr);
/* grid-template-rows: auto 1fr auto; */ /* now defaults to grid-auto-rows: auto */
}
.product-page--main-content > * {
padding: 50px 100px;
border: 1px solid #ccc;
}
.product-page--cart-form-block {
grid-row: 2;
}
.product-page--thumbs {
grid-row: 3;
}
.product-page--images {
grid-column: span 2;
grid-row: span 4; /* changed from 3 */
height: 400px;
}
.product-page--description {
grid-row: span 4; /* changed from 3 */
}
<div class="product-page--main-content">
<div class="product-page--title-n-vendor">Title</div>
<div class="product-page--cart-form-block">Cart form</div>
<div class="product-page--thumbs">Thumbs</div>
<div class="product-page--images">Images</div>
<div class="product-page--description">Description</div>
</div>
I'm not sure this is what you're asking but this seems to fit: change
grid-template-rows: auto 1fr auto;
to
grid-template-rows: 3;

Flexbox/Float anchor items to top [duplicate]

This question already has answers here:
Create a Masonry grid with flexbox (or other CSS)
(3 answers)
Closed 5 years ago.
Consider this example:
Notice that that 4th item is pushed to top instead of aligning with the 3rd item. I can't achieve this using flexbox's align-items: flex-end, neither with floats.
I am aware of achieving this by using masonry/isotope, but I would like to avoid using javascript just for this layout.
Is it possible to achieve using only CSS?
Yes, it's possible via CSS Grid Layout:
.grid {
display: grid;
grid-gap: 10px 30px;
grid-template-rows: auto 1fr auto;
}
/* styles just for demo */
.grid__item {
background-color: #e0e0e0;
font-size: 30px;
padding: 10px;
}
.b, .d {
align-self: flex-start;
}
.a {
grid-row: 1 / span 2;
/* setting height just for demo */
height: 200px;
}
.b {
grid-column: 2;
}
.c {
grid-row: 3;
}
.d {
grid-column: 2;
}
<div class="grid">
<div class="grid__item a">1</div>
<div class="grid__item b">2</div>
<div class="grid__item c">3</div>
<div class="grid__item d">4</div>
</div>
If you need IE\Edge support you should use old grid syntax. You can fake grid-gap using additional grid columns and rows. Demo:
.grid {
display: -ms-grid;
display: grid;
-ms-grid-columns: 1fr 30px 1fr;
grid-template-columns: 1fr 30px 1fr;
-ms-grid-rows: auto 10px 1fr 10px auto;
grid-template-rows: auto 10px 1fr 10px auto;
}
/* styles just for demo */
.grid__item {
background-color: #e0e0e0;
font-size: 30px;
padding: 10px;
}
.b, .d {
-ms-grid-row-align: start;
align-self: flex-start;
}
.a {
-ms-grid-row-span: 3;
grid-row: 1 / span 3;
/* setting height just for demo */
height: 200px;
}
.b {
-ms-grid-column: 3;
grid-column: 3;
}
.c {
-ms-grid-row: 5;
grid-row: 5;
}
.d {
-ms-grid-column: 3;
grid-column: 3;
-ms-grid-row: 3;
grid-row: 3;
}
<div class="grid">
<div class="grid__item a">1</div>
<div class="grid__item b">2</div>
<div class="grid__item c">3</div>
<div class="grid__item d">4</div>
</div>