Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS grid-column Property

Posted in CSS Tutorial
Updated on Oct 03, 2024
By Mari Selvan
👁️ 68 - Views
⏳ 4 mins
💬 1 Comment
CSS grid-column Property

Photo Credit to CodeToFun

🙋 Introduction

The grid-column property in CSS is used in the context of CSS Grid Layout. It allows you to define how many columns an element should span within a grid container, and which columns it should start and end at.

This property simplifies the process of positioning elements within a grid, making layout design more flexible and efficient.

💡 Syntax

The grid-column property can be defined using two different syntax formats: the shorthand syntax and the longhand syntax.

Shorthand Syntax:

Syntax
Copied
Copy To Clipboard
element {
  grid-column: start / end;
}
  • start: The starting grid line for the column.
  • end: The ending grid line for the column.

Longhand Syntax:

The grid-column property can also be set using the longhand properties grid-column-start and grid-column-end.

Syntax
Copied
Copy To Clipboard
element {
  grid-column-start: start;
  grid-column-end: end;
}

🎛️ Default Value

The default value for the grid-column property is auto, which means the item will be placed in the next available grid cell.

🏠 Property Values

ValueDescription
autoThe element will be placed in the next available grid cell.
span nThe element will span n columns.
line numberSpecifies the starting or ending line number of the grid.
nameA named grid line defined in the grid-template-areas.

📄 Example

In this example, we'll create a simple grid layout and use the grid-column property to position items within the grid.

index.html
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS grid-column Example</title>
  <style>
    .grid-container {
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      gap: 10px;
    }
    .item1 {
      grid-column: 1 / 3; /* Spans from the first to the third column */
    }
    .item2 {
      grid-column: 3 / 5; /* Spans from the third to the fifth column */
    }
    .item3 {
      grid-column: span 2; /* Spans two columns */
    }
  </style>
</head>
<body>
  <h1>Grid Layout with Custom Column Spans</h1>
  <div class="grid-container">
    <div class="item1">Item 1</div>
    <div class="item2">Item 2</div>
    <div class="item3">Item 3</div>
    <div class="item4">Item 4</div>
  </div>
</body>
</html>

🖥️ Browser Compatibility

The grid-column property is widely supported in modern browsers, including the latest versions of Chrome, Firefox, Safari, Edge, and Opera. It's recommended to test your grid layouts across different browsers to ensure consistent rendering.

🎉 Conclusion

The grid-column property is a powerful feature of CSS Grid Layout that provides precise control over the placement of elements within a grid.

By defining the start and end positions of an element, you can create complex and responsive layouts with ease. Experiment with different grid configurations to see how this property can enhance your web designs.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy