
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
Difference between Auto-fit vs Auto-fill property in CSS grid
A responsive webpage is a necessary key point which has to be always kept in mind while developing a website. The grid module enables the developer to design webpages easily without using a lot of positioning, as the id module provides a grid type layout system in which there are rows and columns.
The auto-fill property
The auto-fill property is used to fill the rows with possible columns, the column which is added will occupy the space and the other column will be empty. The auto-fill property is an important property of the CSS grid and is mostly used so as to create a responsive layout without using media queries.
Syntax
Let’s look at the syntax of the auto-fill property.
:auto-fill;
Example
In the following program,
We first made a class “autofill” and then within the class we placed 3 items, each having different colors showcasing different boxes.
We set the “display property” to grid and assigned a height and width to the container. Later, auto-fill property is used to set its minimax value.
Finally, we gave dimensions to the 3 items that we made earlier.
<!DOCTYPE html> <html lang="en"> <head> <title>Example of using the auto-fill property</title> <style> .first-item { background-color: violet; border: solid black 4px ; max-width: 100%; min-height: 150px; height: auto; } .second-item { background-color: blue; border: solid black 5px; min-height: 150px; max-width: 100%; height: auto; } .third-item { background-color: maroon; border: solid black 5px; min-height: 150px; max-width: 100%; height: auto; } .autfill { margin-top: 4%; border: solid black 3px; width: 80vh; grid-template-columns: repeat( auto-fill, minmax(190px, 1fr)); display: grid; gap: 5px; } .para{ text-align: center; } </style> </head> <body> <div class="content"> <div class="para"><h1>This is an example!!</h1></div> <div class="autfill"> <div class="first-item"><h2 class="group">1</h1></div> <div class="second-item"><h2 class="group">2</h1></div> <div class="third-item"><h2 class="group">3</h1></div> </div> </div> </body> </html>
The auto-fit property
The “auto-fit” property is similar to the “auto-fill” property the only difference is that the property will expand its size by occupying the space available to it, depending up on the width of the device. If all the items of the grid are empty, then all of them are treated as a single track.
Example
In the following example, we set the property value to auto-fit. The auto-fit property will stretch itself, so as to fill the width of the entire row and fills any empty spaces by stretching itself.
<!DOCTYPE html> <html lang="en"> <head> <title>Example of using the auto-fit property</title> <style> #container { display:grid; width:100%; border:2px solid blue; grid-template-columns: repeat(auto-fit, minmax(100px, 2fr)); } #first-item,#second-item,#third-item { height:100px; margin:3px 15px 15px 2px; background-color: blue; } </style> </head> <body> <div id="container"> <div id="first-item">1</div> <div id="second-item">2</div> <div id="third-item">3</div> </div> </body> <html>
In the above example you can see that the items have filled the whole width of the row and the area where there is any space left.
The auto-fit vs the auto-fill property
The grid layout has different properties. Both auto-fit and auto-fill properties are part of the CSS grid module. The syntax of the grid layout is as follows −
.container-grid{ display: grid; }
Above is the syntax for the grid layout which sets the display property of a HTML element to grid layout.
Conclusion
The auto-fit and the auto-fill are both properties of the CSS-Grid and are used to create responsive layouts without using media query in which the auto-fill property will allow empty spaces in a row whereas in the auto-fit property the content will stretch itself that it can fill the row entirely. In this article, we covered the two properties auto-fill and the auto-fit which are used to create responsive layouts.
- Related Articles
- Usage of CSS grid-auto-rows property
- Usage of CSS grid-auto-columns property
- Usage of CSS grid-auto-flow property
- How does auto property work in margin: 0 auto in CSS?
- Set how auto-placed items are inserted in the CSS grid
- CSS overflow: auto
- What is the difference between overflow: auto and overflow: scroll in CSS?
- How to Auto-Fit Column Width in Excel?
- Role of margin property with value auto using CSS
- How to auto-resize an image to fit a div container using CSS?
- Difference between auto, 0 and no z-index
- How to Auto-Fit Row Height of Merged Cells in Excel?
- Difference between static, auto, global and local variable in C++
- CSS object-fit Property
- Difference between CSS Grid and Bootstrap
