Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grid Columns Without Rows?

Tags:

css

css-grid

I'm trying to create a 3 column layout using the CSS Grid spec but I'm running into an issue with rows and element sizing. My columns need to contain an unspecified amount of content with varying heights.

This is my ideal layout:

enter image description here

Problems:

1) If I tell element A and B to use row 1, column 1 then they stack on top of one another rather than B below A.

2) If I specify element B to use the second row, then it gets pushed below element C due to element C making row 1 tall.

enter image description here

3) If I specify element B to use the second row, then element A stretches to fill row 1.

enter image description here

Is there any way to get elements to behave like in the first picture?

The only solution I know of is to create "scaffolding" divs inside the columns like this:

<div class="grid">
    <div class="col">
        <div class="itemA"></div>
        <div class="itemB"></div>
    </div>
    <div class="col">
        <div class="itemC"></div>
    </div>
    <div class="col">
        <div class="itemD"></div>
        <div class="itemE"></div>
        <div class="itemF"></div>
    </div>
</div>

But I dislike doing that and I thought the grid spec was supposed to allow for layout creation without scaffolding.

Questions:

1) Is there any way to prevent elements from stretching to fill the row vertically?

2) Is it possible to put two elements on the same row and have them appear one below the other instead of conflicting?

Here's my 2 pens where I'm attempting to find a solution with and without scaffolding:

Without scaffolding

With scaffolding

like image 516
Andronomos Avatar asked Jun 19 '18 13:06

Andronomos


People also ask

What is inline grid?

CSS Inline Level Grid CSS Grid is a two-dimensional CSS layout system. To set an HTML element into a inline-level grid container use display: inline-grid property/value. The nested elements inside this element are called grid items.

What is grid column?

A grid column is a vertical track in a CSS Grid Layout, and is the space between two vertical grid lines. It is defined by the grid-template-columns property or in the shorthand grid or grid-template properties.

What is the difference between grid-column and grid-row?

The grid-column property sets the width of the column of a grid item. The grid-row property sets the height of the row of a grid item. The grid-area property sets the area of a grid item. It consists of the width of the column & height of the row of a grid item. The Columns & Rows are different from Column Lines & Row Lines respectively.

What is the difference between grid column and row and area?

Grid Columns, Grid Rows and Grid Area The grid-column property sets the width of the column of a grid item. The grid-row property sets the height of the row of a grid item. The grid-area property sets the area of a grid item.

What is a grid column in CSS?

grid-column The grid-column CSS shorthand property specifies a grid item's size and location within a grid column by contributing a line, a span, or nothing (automatic) to its grid placement, thereby specifying the inline-start and inline-end edge of its grid area.

How to create rows and columns without using the <table> tag?

In HTML and CSS, there are a few common ways to create rows and columns without using the <table> tag: <div id="float"> <div>CELL</div> <div>CELL</div> </div>


1 Answers

The closest i could have, but i do recommend you to use flexbox, You can't only define columns in CSS grids, the browsers will define rows for you, if you don't, and you don't manage them, you will get weird layout.

I hard coded the difference in heights between elements.

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.grid {
  background: brown;
  display: grid;
  grid-template-columns: auto 1fr auto;
  grid-gap: 10px;
  padding: 10px;
}

.grid>div {
  background-color: orange;
}

.itemA {
  grid-column-start: 1;
  grid-row: 1 / 1;
  width: 100px;
  height: 100px;
}

.itemB {
  grid-column-start: 1;
  grid-row: 2 / 4;
  height: 200px;
}

.itemC {
  grid-column-start: 2;
  grid-row: 1 / 3;
  height: 200px;
}

.itemD {
  grid-column-start: 3;
  width: 100px;
  height: 100px;
}

.itemE {
  grid-column-start: 3;
  height: 100px;
}

.itemF {
  grid-column-start: 3;
  grid-row: 3 / 4;
  height: 200px;
}
<div class="grid">
  <div class="itemA">A</div>
  <div class="itemB">B</div>
  <div class="itemC">C</div>
  <div class="itemD">D</div>
  <div class="itemE">E</div>
  <div class="itemF">F</div>
</div>
like image 159
Nothing Mattress Avatar answered Oct 12 '22 20:10

Nothing Mattress