1

I have in my HTML page grids contained in grids contained in grids, all contained in kind of a mother-grid. It may be ugly but it's a structural requirement of my website.

When the window is small, the grids get smaller as well, and unreadable at one point.

I would like that, when the window gets too small, a single (horizontal) scrollbar appears in order to make the whole readable.

I've read a book about CSS and it didn't give me the answer :(

Any ideas ?

Zlotz

Zlotz
  • 147
  • 1
  • 2
  • 12
  • 1
    Maybe `@media` queries will help you https://www.w3schools.com/cssref/css3_pr_mediaquery.asp – Sa1m0n Sep 07 '20 at 22:33
  • It seems that I can do things with @media, but I hoped not to have to compute the size I need to display my grids. There must be a simpler way, no ? – Zlotz Sep 07 '20 at 22:39
  • @Zlotz please add your html structure ... or what have you already try ? – نور Sep 12 '20 at 05:08
  • 1
    do you have a code snippet to demonstrate your issue and show us how you have set your grid layout (row/colums or area) ? auto-fill/auto-fit could be an option, minmax() or width:min-content could also be another option but also none of this could match what you have done. ... Answers from the little of your question can be guesses or opinions and maybe you'll be lucky if one of these comes to be efficient... my guess `display:inline-grid;min-width:100%;` – G-Cyrillus Sep 12 '20 at 22:25
  • My HTML structure is Javascript dependent. The user provokes the creation of grids, and of grids inside grids, and so on. – Zlotz Sep 14 '20 at 15:45
  • please check the answer, if it covers the solution area you were targeting to achieve @Zlotz – Ritika Gupta Sep 18 '20 at 07:45

6 Answers6

1

Imho, best answer is Christoper's. You can take a look at CSS units here: CSS Units to choose which one fits best for you.

Best choices for your case should be vw (relative to viewport width), % (relative to parent width) or em/rem (relative to font sizes, the first for the current element and the second for the root element)

If you will try vw unit, remember to define viewport meta tag on header. In example:

<meta name="viewport" content="width=device-width, initial-scale=1.0">
Dharman
  • 30,962
  • 25
  • 85
  • 135
Mansemino
  • 36
  • 2
1

As inferred from the problem statement, please find the below code running for large-screens and small-screen breakpoints, a mother grid containing grids inside grids.... and a single scroll controlling the view of grids in clean manner on smaller screen sizes, screenshots of which have been attached in support of the same.

div{
border: 1px solid black;
padding: 12px;
margin: 0.5px;
}
div.mother-grid{
display: grid;
grid-template-columns: 350px 350px;

}
div.sub-grid{
  display: grid;
  grid-template-columns: 150px 150px;
}
div.mother-grid{
  max-width: 100%;
  overflow: auto;
}
<div class="mother-grid">
<div class="sub-grid">

<div class="sub-sub-grid">This is sub grid level 2</div>
<div class="sub-sub-grid">This is sub grid level 2</div>
</div>
<div class="sub-grid">

<div class="sub-sub-grid">This is sub grid level 2</div>
<div class="sub-sub-grid">This is sub grid level 2</div>
</div>
</div>
enter image description hereenter image description here
Ritika Gupta
  • 376
  • 1
  • 16
0

Specify min-width for every cell (of course, depending on the the cell class) rather than for the whole page. Then effective min-width for the whole page will be internally calculated by the layout engine.

0

The best way is using both media queries and possibly auto-fill auto-fit for responsive development.

//mobile/tablet portrait
@media only screen and (max-width: 768px) {
    .inner-grid {
         grid-template-columns: repeat(auto-fill, 186px); // you can try
     }
}
@media only screen and (max-width: 1024px) {
    .inner-grid {
     }
}

This link might be helpful CSS grid wrapping

Fawn
  • 73
  • 1
  • 4
  • 20
0

If you want to see the horizontal scroll bars, you need to make the grid cell scrollable in x axis, use the overflow-x property below. But if you want the children grid not to shrink, either you need to remove the grid layout, or to provide a minimum height and width to the child so that it can create scroll bars.

overflow-x : scroll;
Vijay122
  • 884
  • 8
  • 12
-1

why don't you use these in css

min-width:100px;

//or 

max-width:800px;
Drust Taib
  • 29
  • 7