Home  >  Article  >  Web Front-end  >  CSS grid functions you may not know about!

CSS grid functions you may not know about!

青灯夜游
青灯夜游forward
2021-03-05 10:48:122027browse

This article will introduce to you the grid functions in CSS: fit-content(), minmax(), repeat(). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

CSS grid functions you may not know about!

These three functions can only be used in grid layout

[Recommended tutorial: CSS video tutorial

fit-content()

fit-content function receives a parameter, a length value, and its function can be explained literally," Adapt to the content".

<div class="fit-content-wrapper">
  <div class="fit-item item1">test1dsssss3333333 sssssssssssssss sssssssssssssssssss sssssssssssssssssss ssssssssssssssssssss 这是用了fit-content(400px)</div>
  <div class="fit-item item2">test2 这是固定宽度width:400px</div>
  <div class="fit-item item3">test3 这是fit-content(400px)</div>
</div>

  .fit-content-wrapper{
    width: 100%;
    height: 200px;
    display: grid;
    grid-template-columns: fit-content(400px) 400px fit-content(400px);
    grid-gap: 10px;
  }
  .fit-item{
    background-color: rgb(20, 106, 177);
  }

Effect

CSS grid functions you may not know about!

##You can see that

When the content length is greater than the given length, the text will automatically wrap and will not exceed the given length. , when the content length is less than the given length, the length will be set according to the given content length.

Compatibility

CSS grid functions you may not know about!

Compatibility has no problem with modern browsers, and the new version of mainstream browsers can basically do it Supported, but cannot be used for projects that need to support IE.

minmax()

The minmax function represents a closed interval range [min,max]. When the value is less than or equal to min, the value is equal to min. When it is greater than or equal to max , the value is equal to max.

min-content, max-content

The minmax function receives the min-content, max-content parameters. These two parameters represent the shortest sum of the content. Maximum content length. See the case below.

    <div class="minmax-wrapper">
      <div class="minmax-item">
        test1dsssss3333333 sssssssssssssss sssssssssssssssssss
        sssssssssssssssssss ssssssssssssssssssss
      </div>
      <div class="minmax-item">
        <p>test2222222222</p>
        <p>test 232232323233</p>
        <p>min-content采用最短的内容长度</p>
      </div>
      <div class="minmax-item">
        <p>test</p>
        <p>test 232232323233222222</p>
        <p>max-content采用最长的内容长度</p>
      </div>
    </div>

      .minmax-wrapper {
        margin-top: 100px;
        width: 100vw;
        display: grid;
        grid-gap: 10px;
        grid-template-columns:
          minmax(300px, 500px) minmax(50px, min-content)
          minmax(100px, max-content);
      }

Effect

CSS grid functions you may not know about!

You can see that the minimum content width of the second item is the first p tag in the second item

CSS grid functions you may not know about!

When set to minmax(50px,min-content), it means that the maximum column width cannot exceed the content width of the first p tag.

The maximum content width of the third item is the content width of the third p tag

CSS grid functions you may not know about!

When set to minmax(100px,max-content) When, the maximum content width will not exceed the width of the third p tag

Compatibility

CSS grid functions you may not know about!

With the fit-content function Same, it doesn't support IE, but it has pretty good support for mainstream modern browsers.

repeat()

The repeat function is used to process grids in batches and receives 2 parameters. The first parameter indicates the number of executions, and the second parameter indicates the length. See the example below

    <div class="repeat-wrapper">
      <div class="repeat-item">test1 3</div>
      <div class="repeat-item">test2 23</div>
      <div class="repeat-item">test3 444</div>
    </div>

      .repeat-wrapper {
        margin-top: 100px;
        display: grid;
        grid-template-columns: repeat(3, 100px);
        grid-gap: 10px;
      }

Effect

CSS grid functions you may not know about!

grid-template-columns: repeat(3, 100px) is equivalent to grid-template-columns: 100px 100px 100px ;

auto-fill,auto-fit

In addition to specifying the specific number of times, repeat also receives these parameters auto-fill,auto-fit. , let’s talk about the concepts of these two parameters.

auto-fill

auto-fill means that the browser automatically fills the number of times based on the project. When the container is very wide, the width of the remaining grid will be automatically reserved. If the grid container has a definite size or a maximum size on the relevant axis, the number of repetitions is the largest possible positive integer that will not cause the grid to overflow its grid container.

    <div class="repeat-wrapper">
      <div class="repeat-item">test1 3</div>
      <div class="repeat-item">test2 23</div>
      <div class="repeat-item">test3 444</div>
      <div class="repeat-item">test3 4444</div>
      <div class="repeat-item">test3 444</div>
      <div class="repeat-item">test3 444</div>
    </div>

grid-template-columns: repeat(auto-fill, minmax(100px,1fr));

Effect


CSS grid functions you may not know about!

##auto-fit

auto-fit will also be automatically calculated, but Different from auto-fill, auto-fit will not retain the remaining empty cells, but will redistribute the remaining empty cells of auto-fill to each cell. See the example below

    <div class="repeat-wrapper">
      <div class="repeat-item">test1 3</div>
      <div class="repeat-item">test2 23</div>
      <div class="repeat-item">test3 444</div>
      <div class="repeat-item">test3 4444</div>
      <div class="repeat-item">test3 444</div>
      <div class="repeat-item">test3 444</div>
    </div>

grid-template-columns: repeat(auto-fit, minmax(100px,1fr));

Effect

CSS grid functions you may not know about!

Compatibility


CSS grid functions you may not know about!The latest versions of mainstream browsers are basically supported, but IE is still not supported.

Summary

These three grid functions greatly enrich the grid layout. I haven’t used many grid layouts before, but today I will learn these three After reading the function and some related parameters, I found that the grid layout is also very convenient compared to other layouts. I can try to use it in some of my own small projects later.

For more programming related knowledge, please visit:

Programming Video

! !

The above is the detailed content of CSS grid functions you may not know about!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete