Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floating elements in CSS Grid

I'm currently working on my first page where I want to use CSS grid display: grid.

It works very well, but I come across a small problem with an element that I want float classically - text floats around an image, a quote etc.

I simply gave an element a float: left and to my surprise, the float is completely ignored. The element remains as a full "grid-row-item".

Short code example:

main {
  display: grid;
  grid-template-columns: 5% 5% 1fr 5% 5%;
}

main > * {
  grid-column: 3;
}

blockquote {
  grid-column: 2 / -2;
}

blockquote.float-left {
  grid-column: 3;
  float: left;
}

For a larger example, I've created a Codepen.

Unfortunately, I have not found anything about this, so my questions are: Has anyone a solution for this? Have I overlooked something? Or maybe that's not possible yet?

Thank you in advance! :)

Codepen-Link:

https://codepen.io/anon/pen/GQWPWX

like image 994
Max Avatar asked Feb 10 '18 14:02

Max


People also ask

Do people still use float in CSS?

The float property still exists in CSS as it did when Internet Explorer was a young browser, and still works the same.

What is the difference between float flex and grid?

Grid and flexbox. The basic difference between CSS Grid Layout and CSS Flexbox Layout is that flexbox was designed for layout in one dimension - either a row or a column. Grid was designed for two-dimensional layout - rows, and columns at the same time.


2 Answers

There is actually one solution, use:

float: left; in grid works as => justify-self: start;
float: right; in grid works as => justify-self: end;
like image 66
Arvis Iljins Avatar answered Sep 17 '22 16:09

Arvis Iljins


Floats are respected on grid containers, but they are completely ignored on grid items.

This behavior is defined in the CSS Grid spec in this section:

  • https://www.w3.org/TR/css-grid-1/#grid-containers

The problem has been discussed here, but has no solution yet:

  • Text not wrapping in CSS Grid
like image 25
Michael Benjamin Avatar answered Sep 18 '22 16:09

Michael Benjamin