CSS grid-auto-columns Property
CSS grid-auto-columns Property
CSS grid-auto-columns
property sets a size for the columns in a grid container.
Example
<!DOCTYPE html>
<html lang="en-US">
<head>
<style>
h2+div div:nth-child(1) { grid-area: 1/ 1/ 2/ 2; }
h2+div div:nth-child(2) { grid-area: 1/ 2/ 2/ 3; }
h2+div div:nth-child(3) { grid-area: 1/ 3/ 2/ 4; }
h2+div div:nth-child(4) { grid-area: 2/ 1/ 3/ 2; }
h2+div div:nth-child(5) { grid-area: 2/ 2/ 3/ 3; }
h2+div{
display: grid;
grid-auto-flow: row;
border: 1px solid black;
}
h2+div div{
border: 1px solid #00f;
background-color: rgba(0,0,255,.2);
padding: 25px;
margin: 8px;
text-align: center;
}
.point(
grid-auto-columns: 150px;
}
.point1{
grid-auto-columns: 33%;
}
</style>
</head>
<body>
<h1>CSS grid-auto-columns Property</h1>
<h2>grid-auto-columns: 150px;</h2>
<div class="point">
<div>Div 1</div>
<div>Div 2</div>
<div>Div 3</div>
<div>Div 4</div>
<div>Div 5</div>
</div>
<h2>grid-auto-columns: 33%;</h2>
<div class="point1">
<div>Div 1</div>
<div>Div 2</div>
<div>Div 3</div>
<div>Div 4</div>
<div>Div 5</div>
</div>
</body>
</html>
Syntax
Using CSS
element{
grid-auto-columns: 100px;
}
Using Javascript
object.style.gridAutoColumns="100px";
Animatable
Yes, grid-auto-columns property is animatable. CSS Animatable Properties Reference.
Default Value
Default value for CSS grid-auto-columns property is auto.
Property Value
The following table provides a list of values for CSS grid-auto-columns
property.
Value | Explanation |
---|
length | Allows you to define the column size of the grid in any CSS length unit. |
% | Allows you to define the column size of the grid in percentage. |
max-content | Sets the size of each column depending on the largest item in the column. |
min-content | Sets the size of each column depending on the smallest item in the column. |
minmax(min, max) | Sets a size range greater than or equal to min and less than or equal to max. |
auto | The size of the columns is determined by the size of the container (div). |
Using JavaScript
In the following example, we will demonstrate how to change the CSS grid-auto-columns
property of an element using JavaScript.
Example
<!DOCTYPE html>
<html lang="en-US">
<head>
<style>
#outerDiv div:nth-child(1) { grid-area: 1/ 1/ 2/ 2; }
#outerDiv div:nth-child(2) { grid-area: 1/ 2/ 2/ 3; }
#outerDiv div:nth-child(3) { grid-area: 1/ 3/ 2/ 4; }
#outerDiv div:nth-child(4) { grid-area: 2/ 1/ 3/ 2; }
#outerDiv div:nth-child(5) { grid-area: 2/ 2/ 3/ 3; }
#outerdiv{
display: grid;
grid-auto-flow: row;
border: 1px solid black;
}
#outerDiv div{
border: 1px solid #00f;
background-color: rgba(0,0,255,.2);
padding: 25px;
margin: 8px;
text-align: center;
}
</style>
</head>
<body>
<h1>CSS grid-auto-columns Property</h1>
<div id="outerDiv">
<div>Div 1</div>
<div>Div 2</div>
<div>Div 3</div>
<div>Div 4</div>
<div>Div 5</div>
</div>
<button onclick="myFunction()">Click Me</button>
<script>
var x = document.getElementById("outerDiv");
function myFunction(){
x.style.gridAutoColumns = "150px";
}
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 98.7% of CSS Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in CSS.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.
Share this Page
Meet the Author