Centering the element horizontally is the basic task while developing the web pages. You can use CSS Grid layout for this purpose, and it is considered to be a modern approach for this task. The Grid layout is used to create a two-dimensional layout system, and it supports the position of the elements in both rows and columns. This method is considered to be the most structured way of organising the elements.
Table of Contents:
What is CSS Grid Layout?
Grid layout in CSS is used for dividing your webpage into rows and columns to align the items accurately in a particular position. With some simple CSS rules, you can control the size, alignment, and spacing of elements within the grid.
How to Horizontally Center an Element Using CSS Grid?
CSS Grid can be used for horizontally centering an element. You can do this by setting the parent container to display: grid and using justify-content: center for centering the child element horizontally. You can also use grid-template-columns to define the columns and position the element to center using grid-column. There are many CSS Grid examples for centering input fields; some of them are mentioned below. The modern approach to aligning elements is to use CSS Grid center horizontally techniques, which offer flexibility and cleaner layout structures for responsive design.
There are some examples that can be used to center the input field using CSS grid.
Example 1: Center a Div Horizontally Using CSS Grid
The <div> is created for centering the elements horizontally. You can apply CSS properties like background color and padding to the centered div.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Grid Example</title>
<style>
.box {
display: grid;
place-items: center;
height: 100vh;
background-color: pink;
}
.center {
background-color: orange;
padding: 30px;
}
</style>
</head>
<body>
<div class="box">
<div class="center">
Element is Centered Using Grid
</div>
</div>
</body>
</html>
Output:
Explanation: This code can be used to create a simple div for centering the element horizontally. The class .container centers the content horizontally, and the inner content gets styled using the .centered class.
Industry-Aligned Curriculum to Master Modern Web Technologies
Professional Web Development Courses
The button is created in the center of the page horizontally. You can apply some CSS properties like background color, text color, padding, etc., for styling the button.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Centered Button</title>
<style>
.container {
display: grid;
place-items: center;
height: 100vh;
background-color: black;
}
.button {
background-color: blue ;
color: whitesmoke;
padding: 15px 25px;
font-size: 16px;
border: none;
border-radius: 6px;
cursor: pointer;
transition: background 0.4s;
}
.button:hover {
background-color: lightblue;
}
</style>
</head>
<body>
<div class="container">
<button class="button">Intellipaat</button>
</div>
</body>
</html>
Output:
Explanation: The code creates the button with a horizontal centering property. The CSS properties are added to the button, and the hover makes the color of the button change when you click on it.
You can use CSS grid to center horizontally in the input field. Some styling properties can be applied further.
Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Centered Input Field</title>
<style>
.container {
display: grid;
place-items: center;
height: 90vh;
background-color: black;
}
.input-field {
width: 200px;
padding: 10px;
font-size: 18px;
border: 3px solid #ccc;
border-radius: 6px;
outline: none;
transition: border-color 0.3s;
}
.input-field:focus {
border-color: lightblue;
}
</style>
</head>
<body>
<div class="container">
<input type="text" class="input-field" placeholder="Enter text...">
</div>
</body>
</html>
Output:
Explanation: You can use this code for centering the input field by applying CSS grid to the .container, which contains the place-items: center property.
Advantages of Using CSS Grid for Centering Elements
- Consistent Behavior Across Browsers
CSS Grid is well-supported and behaves predictably across modern browsers, making it a reliable choice for centering elements.
- Two-Dimensional Layout Capability
CSS Grid allows you to manage both rows and columns, making it inherently suitable for centering in both horizontal and vertical directions.
- Concise and Readable Syntax
Centering elements with CSS Grid requires minimal code, improving readability and reducing the chance of layout bugs.
- Native Alignment Properties
Grid provides properties like justify-content
, align-items
, and place-items
that are specifically designed for alignment and centering.
- No Extra Markup Required
Unlike older techniques, CSS Grid eliminates the need for additional wrapper elements or hacks to achieve centering.
CSS Grid vs Flexbox for Centering Elements
Feature |
CSS Grid |
Flexbox |
Layout Type |
Two-dimensional (rows and columns) |
One-dimensional (row or column) |
Ease of Horizontal Centering |
Very easy with justify-items or place-items |
Very easy with justify-content |
Vertical + Horizontal Centering |
Vertical and Horizontal Centering |
Requires both justify-content and align-items |
Best Use Case |
Complex layouts with multiple items aligned in two directions |
Simple linear layouts (single row or column) |
Item-Level Control |
Use justify-self and align-self on grid items |
Use align-self and margin: auto on flex items |
Get 100% Hike!
Master Most in Demand Skills Now!
Conclusion
CSS Grid centering techniques provide a flexible and efficient way to achieve CSS Grid center horizontally. Whether you need to center an element using CSS Grid or horizontally align with grid layout, this method simplifies alignment without extra markup. The above CSS Grid layout examples demonstrate how to horizontally center a div using CSS Grid, making it an excellent choice for modern web design. When comparing CSS Grid vs Flexbox for centering elements, Grid offers a structured approach. You can explore more CSS Grid examples for centering input fields to optimize your layout effectively.
Alternative Methods to Horizontally Center the Element
Check out these resources on mastering Backend logic and SQL data handling
Make A Div Fill The Height Of The Remaining Screen Space – Improve your knowledge of making a div fill the height of the remaining screen space in this blog.
Maintain The Aspect Ratio Of A Div With CSS – Improve your knowledge of maintaining the aspect ratio of a div with CSS in this blog.
How To Center Absolutely Positioned Element In Div Using CSS – Improve your knowledge of how to center absolutely positioned element in div using CSS in this blog.
Merging Objects JavaScript – Improve your knowledge of merging objects JavaScript in this blog.
Automatic Semicolon Insertion In JavaScript – Improve your knowledge of automatic semicolon insertion in JavaScript in this blog.
CSS Margin 0 Auto To Horizontally Center Element – Improve your knowledge of CSS margin 0 auto to horizontally center element in this blog.
Are Non Void Self Closing Tags Valid In HTML5 – Improve your knowledge of are non void self closing tags valid in HTML5 in this blog.
CSS Masonry Layout – Improve your knowledge of CSS masonry layout in this blog.
Html Computer Code Elements – Improve your knowledge of HTML computer code elements in this blog.
CSS Grid to Horizontally Center Element – FAQs
Q1. How to horizontally center a div using CSS Grid?
Use ‘place-items: center;’ on the grid container to center the div both horizontally and vertically.
Q2. What are CSS Grid centering techniques?
Use properties like ‘place-items’, ‘place-content’, ‘justify-content’, and ‘align-items’ to control centering in CSS Grid.
Q3. How to Center element using CSS Grid?
Apply ‘display: grid;’ and use ‘justify-content: center;’ and ‘align-items: center;’ to center the element within the grid container.
Q4. How to position a grid in CSS?
To position a grid in CSS: Use ‘display: grid;’ on a container and position it using properties like ‘justify-content’, ‘align-items’, or ‘position’.
Q5. How to apply inline CSS?
To apply inline CSS: Add the ‘style’ attribute directly to an HTML element with CSS rules, e.g., ‘<div style=”color: red;”>Text</div>’.