Home > Article > Web Front-end > CSS grid functions you may not know about!
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.
These three functions can only be used in grid layout
[Recommended tutorial: CSS video tutorial 】
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
##You can see thatWhen 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.
<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 You can see that the minimum content width of the second item is the first p tag in the second item 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 When set to minmax(100px,max-content) When, the maximum content width will not exceed the width of the third p tag
<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 grid-template-columns: repeat(3, 100px) is equivalent to grid-template-columns: 100px 100px 100px ;
<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
<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
CompatibilityThe latest versions of mainstream browsers are basically supported, but IE is still not supported.
SummaryFor more programming related knowledge, please visit:
Programming VideoThe 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!