CSS grid items based on minimum width and percentage

Grant Emerson

I currently have a div style as a grid display. The grid-template-columns are each 1/3 of the body. However, I would like to limit each box to a minimum width of, say, 200px. This way, on mobile I won't have three extremely skinny boxes. Is there any way to do this, or am I approaching the problem in completely the wrong way?

.wrapper {
  display: grid;
  grid-template-columns: 33% 33% 33%;
  grid-gap: 10px;
  background-color: #fff;
  color: #444;
}

.box {
  background-color: #444;
  border-color: #Dd6161;
  border-style: solid;
  border-width: 2px;
  color: #fff;
  border-radius: 5px;
  padding: 20px;
  font-size: 14px;
}
Chris Boon

This might be what you're looking for if you only have three boxes in the wrapper and you want them to wrap onto another row if the wrapper is less than 600px.

.wrapper {
  display: grid;
  grid-gap: 10px;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}

What this does is:

  1. repeat - this just tells the gird to do the following multiple times.
  2. auto-fit - this is the first argument inside the 'repeat' and tells the grid to 'fit the columns in the grid by expanding them to take the entire space of the grid. It also means if the items don't fit in a single row they will wrap.
  3. minmax(200px, 1fr) means that each column in the grid will be a minimum of 200px and a max of an equal amount of space (so if there are three items, it will be 33.333% each).

Here is an example: https://codepen.io/chrisboon27/pen/RMjGme

Alternately if you want them to all become full width (so one column of three rows) you would use grid-template-columns: repeat(3, minmax(200px, 1fr)); and then use a media query to change it to grid-template-columns: 1fr; to make it a single column:

.wrapper {
  display: grid;
  grid-gap: 10px;
  grid-template-columns: repeat(3, minmax(200px, 1fr));
}
@media (max-width: 600px){
  .wrapper{
    grid-template-columns: 1fr;
  }
}

Here is an example of this one: https://codepen.io/chrisboon27/pen/pLdNjb

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Span grid items to the full width of the CSS Grid

CSS Grid minmax - minimum width not being adhered to

Clipping CSS linear gradient based on width percentage

Flexbox grid with maximum 3 columns and minimum 2? With equal width items?

Achieving min-content and percentage width in css grid?

multiple colors on progress bar with css or jquery based on width percentage

CSS width percentage accuracy

percentage width in css not working

How does percentage work in grid items min-width [when using fit-content]?

Set minimum width of column in grid

How to set minimum percentage width of View in a ConstraintLayout

CSS Grid Columns limit card width based on background image

css percentage based background

UITableViewCell Percentage Based Height with Minimum Height

Xaml Xamarin grid column fill with minimum width

Overlapping items in CSS Grid

Reordering items with CSS Grid

CSS Grid Center (Justify) Item Based on Row Width Instead of Item Width

Percentage based height and width of jquery ui slider

Alignment of fixed width item in percentage based parent

progress bar changing color based on percentage width

Change TextView background based on width percentage

Jetpack Compose: same width of row items with minimum width of the whole row

Why doen't width with percentage work in CSS?

CSS ellipsis not working when width is set to a percentage

CSS: Setting width/height as Percentage minus pixels

Understanding CSS table-cell and percentage width

CSS min-width fixed and percentage

CSS vw but in percentage of containing block width?