css grid: centerin column/row [duplicate] - css
I'm trying to create a simple page with CSS Grid.
What I'm failing to do is center the text from the HTML to the respective grid cells.
I've tried placing content in separate divs both inside and outside of the left_bg and right_bg selectors and playing with some of the CSS properties to no avail.
How do I do this?
html,
body {
margin: 0;
padding: 0;
}
.container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 100vh;
grid-gap: 0px 0px;
}
.left_bg {
display: subgrid;
background-color: #3498db;
grid-column: 1 / 1;
grid-row: 1 / 1;
z-index: 0;
}
.right_bg {
display: subgrid;
background-color: #ecf0f1;
grid-column: 2 / 2;
grid_row: 1 / 1;
z-index: 0;
}
.left_text {
grid-column: 1 / 1;
grid-row: 1 / 1;
position: relative;
z-index: 1;
justify-self: center;
font-family: Raleway;
font-size: large;
}
.right_text {
grid-column: 2 / 2;
grid_row: 1 / 1;
position: relative;
z-index: 1;
justify-self: center;
font-family: Raleway;
font-size: large;
}
<div class="container">
<!--everything on the page-->
<div class="left_bg">
<!--left background color of the page-->
</div>
</div>
<div class="right_bg">
<!--right background color of the page-->
</div>
<div class="left_text">
<!--left side text content-->
<p>Review my stuff</p>
<div class="right_text">
<!--right side text content-->
<p>Hire me!</p>
</div>
</div>
This answer has two main sections:
Understanding how alignment works in CSS Grid.
Six methods for centering in CSS Grid.
If you're only interested in the solutions, skip the first section.
The Structure and Scope of Grid layout
To fully understand how centering works in a grid container, it's important to first understand the structure and scope of grid layout.
The HTML structure of a grid container has three levels:
the container
the item
the content
Each of these levels is independent from the others, in terms of applying grid properties.
The scope of a grid container is limited to a parent-child relationship.
This means that a grid container is always the parent and a grid item is always the child. Grid properties work only within this relationship.
Descendants of a grid container beyond the children are not part of grid layout and will not accept grid properties. (At least not until the subgrid feature has been implemented, which will allow descendants of grid items to respect the lines of the primary container.)
Here's an example of the structure and scope concepts described above.
Imagine a tic-tac-toe-like grid.
article {
display: inline-grid;
grid-template-rows: 100px 100px 100px;
grid-template-columns: 100px 100px 100px;
grid-gap: 3px;
}
You want the X's and O's centered in each cell.
So you apply the centering at the container level:
article {
display: inline-grid;
grid-template-rows: 100px 100px 100px;
grid-template-columns: 100px 100px 100px;
grid-gap: 3px;
justify-items: center;
}
But because of the structure and scope of grid layout, justify-items on the container centers the grid items, not the content (at least not directly).
article {
display: inline-grid;
grid-template-rows: 100px 100px 100px;
grid-template-columns: 100px 100px 100px;
grid-gap: 3px;
justify-items: center;
}
section {
border: 2px solid black;
font-size: 3em;
}
<article>
<section>X</section>
<section>O</section>
<section>X</section>
<section>O</section>
<section>X</section>
<section>O</section>
<section>X</section>
<section>O</section>
<section>X</section>
</article>
Same problem with align-items: The content may be centered as a by-product, but you've lost the layout design.
article {
display: inline-grid;
grid-template-rows: 100px 100px 100px;
grid-template-columns: 100px 100px 100px;
grid-gap: 3px;
justify-items: center;
align-items: center;
}
article {
display: inline-grid;
grid-template-rows: 100px 100px 100px;
grid-template-columns: 100px 100px 100px;
grid-gap: 3px;
justify-items: center;
align-items: center;
}
section {
border: 2px solid black;
font-size: 3em;
}
<article>
<section>X</section>
<section>O</section>
<section>X</section>
<section>O</section>
<section>X</section>
<section>O</section>
<section>X</section>
<section>O</section>
<section>X</section>
</article>
To center the content you need to take a different approach. Referring again to the structure and scope of grid layout, you need to treat the grid item as the parent and the content as the child.
article {
display: inline-grid;
grid-template-rows: 100px 100px 100px;
grid-template-columns: 100px 100px 100px;
grid-gap: 3px;
}
section {
display: flex;
justify-content: center;
align-items: center;
border: 2px solid black;
font-size: 3em;
}
article {
display: inline-grid;
grid-template-rows: 100px 100px 100px;
grid-template-columns: 100px 100px 100px;
grid-gap: 3px;
}
section {
display: flex;
justify-content: center;
align-items: center;
border: 2px solid black;
font-size: 3em;
}
<article>
<section>X</section>
<section>O</section>
<section>X</section>
<section>O</section>
<section>X</section>
<section>O</section>
<section>X</section>
<section>O</section>
<section>X</section>
</article>
jsFiddle demo
Six Methods for Centering in CSS Grid
There are multiple methods for centering grid items and their content.
Here's a basic 2x2 grid:
grid-container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-rows: 75px;
grid-gap: 10px;
}
/* can ignore styles below; decorative only */
grid-container {
background-color: lightyellow;
border: 1px solid #bbb;
padding: 10px;
}
grid-item {
background-color: lightgreen;
border: 1px solid #ccc;
}
<grid-container>
<grid-item>this text should be centered</grid-item>
<grid-item>this text should be centered</grid-item>
<grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
<grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
</grid-container>
Flexbox
For a simple and easy way to center the content of grid items use flexbox.
More specifically, make the grid item into a flex container.
There is no conflict, spec violation or other problem with this method. It's clean and valid.
grid-item {
display: flex;
align-items: center;
justify-content: center;
}
grid-container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-rows: 75px;
grid-gap: 10px;
}
grid-item {
display: flex; /* new */
align-items: center; /* new */
justify-content: center; /* new */
}
/* can ignore styles below; decorative only */
grid-container {
background-color: lightyellow;
border: 1px solid #bbb;
padding: 10px;
}
grid-item {
background-color: lightgreen;
border: 1px solid #ccc;
}
<grid-container>
<grid-item>this text should be centered</grid-item>
<grid-item>this text should be centered</grid-item>
<grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
<grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
</grid-container>
See this post for a complete explanation:
How to Center Elements Vertically and Horizontally in Flexbox
Grid Layout
In the same way that a flex item can also be a flex container, a grid item can also be a grid container. This solution is similar to the flexbox solution above, except centering is done with grid, not flex, properties.
grid-container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-rows: 75px;
grid-gap: 10px;
}
grid-item {
display: grid; /* new */
align-items: center; /* new */
justify-items: center; /* new */
}
/* can ignore styles below; decorative only */
grid-container {
background-color: lightyellow;
border: 1px solid #bbb;
padding: 10px;
}
grid-item {
background-color: lightgreen;
border: 1px solid #ccc;
}
<grid-container>
<grid-item>this text should be centered</grid-item>
<grid-item>this text should be centered</grid-item>
<grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
<grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
</grid-container>
auto margins
Use margin: auto to vertically and horizontally center grid items.
grid-item {
margin: auto;
}
grid-container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-rows: 75px;
grid-gap: 10px;
}
grid-item {
margin: auto;
}
/* can ignore styles below; decorative only */
grid-container {
background-color: lightyellow;
border: 1px solid #bbb;
padding: 10px;
}
grid-item {
background-color: lightgreen;
border: 1px solid #ccc;
}
<grid-container>
<grid-item>this text should be centered</grid-item>
<grid-item>this text should be centered</grid-item>
<grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
<grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
</grid-container>
To center the content of grid items you need to make the item into a grid (or flex) container, wrap anonymous items in their own elements (since they cannot be directly targeted by CSS), and apply the margins to the new elements.
grid-item {
display: flex;
}
span, img {
margin: auto;
}
grid-container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-rows: 75px;
grid-gap: 10px;
}
grid-item {
display: flex;
}
span, img {
margin: auto;
}
/* can ignore styles below; decorative only */
grid-container {
background-color: lightyellow;
border: 1px solid #bbb;
padding: 10px;
}
grid-item {
background-color: lightgreen;
border: 1px solid #ccc;
}
<grid-container>
<grid-item><span>this text should be centered</span></grid-item>
<grid-item><span>this text should be centered</span></grid-item>
<grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
<grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
</grid-container>
Box Alignment Properties
When considering using the following properties to align grid items, read the section on auto margins above.
align-items
justify-items
align-self
justify-self
https://www.w3.org/TR/css-align-3/#property-index
text-align: center
To center content horizontally in a grid item, you can use the text-align property.
grid-container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-rows: 75px;
grid-gap: 10px;
text-align: center; /* new */
}
/* can ignore styles below; decorative only */
grid-container {
background-color: lightyellow;
border: 1px solid #bbb;
padding: 10px;
}
grid-item {
background-color: lightgreen;
border: 1px solid #ccc;
}
<grid-container>
<grid-item>this text should be centered</grid-item>
<grid-item>this text should be centered</grid-item>
<grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
<grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
</grid-container>
Note that for vertical centering, vertical-align: middle will not work.
This is because the vertical-align property applies only to inline and table-cell containers.
grid-container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-rows: 75px;
grid-gap: 10px;
text-align: center; /* <--- works */
vertical-align: middle; /* <--- fails */
}
/* can ignore styles below; decorative only */
grid-container {
background-color: lightyellow;
border: 1px solid #bbb;
padding: 10px;
}
grid-item {
background-color: lightgreen;
border: 1px solid #ccc;
}
<grid-container>
<grid-item>this text should be centered</grid-item>
<grid-item>this text should be centered</grid-item>
<grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
<grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
</grid-container>
One might say that display: inline-grid establishes an inline-level container, and that would be true. So why doesn't vertical-align work in grid items?
The reason is that in a grid formatting context, items are treated as block-level elements.
6.1. Grid Item
Display
The display value of a grid item is blockified: if the
specified display of an in-flow child of an element generating a
grid container is an inline-level value, it computes to its
block-level equivalent.
In a block formatting context, something the vertical-align property was originally designed for, the browser doesn't expect to find a block-level element in an inline-level container. That's invalid HTML.
CSS Positioning
Lastly, there's a general CSS centering solution that also works in Grid: absolute positioning
This is a good method for centering objects that need to be removed from the document flow. For example, if you want to:
Center text over an image, or
Place asterisks (*) above required form fields
Simply set position: absolute on the element to be centered, and position: relative on the ancestor that will serve as the containing block (it's usually the parent). Something like this:
grid-item {
position: relative;
text-align: center;
}
span {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
grid-container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-rows: 75px;
grid-gap: 10px;
}
grid-item {
position: relative;
text-align: center;
}
span, img {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
/* can ignore styles below; decorative only */
grid-container {
background-color: lightyellow;
border: 1px solid #bbb;
padding: 10px;
}
grid-item {
background-color: lightgreen;
border: 1px solid #ccc;
}
<grid-container>
<grid-item><span>this text should be centered</span></grid-item>
<grid-item><span>this text should be centered</span></grid-item>
<grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
<grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item>
</grid-container>
Here's a complete explanation for how this method works:
Element will not stay centered, especially when re-sizing screen
Here's the section on absolute positioning in the Grid spec:
https://www.w3.org/TR/css3-grid-layout/#abspos
Do not even try to use flex and stay with css grid. Just add the following on the content element:
place-self: center;
and it will do the centring work here.
If you want to center something that is inside div that is inside grid cell, you need to define nested grid in order to make it work.
Both examples shown in the demo.
https://css-tricks.com/snippets/css/complete-guide-grid/
The CSS place-items shorthand property sets the align-items and justify-items properties, respectively. If the second value is not set, the first value is also used for it.
.parent {
display: grid;
place-items: center;
}
You can use flexbox to center your text. By the way no need for extra containers because text is considered as anonymous flex item.
From flexbox specs:
Each in-flow child of a flex container becomes a flex item, and each contiguous run of text that is directly contained inside a flex container is wrapped in an anonymous flex item. However, an anonymous flex item that contains only white space (i.e. characters that can be affected by the white-space property) is not rendered (just as if it were display:none).
So just make grid items as flex containers (display: flex), and add align-items: center and justify-content: center to center both vertically and horizontally.
Also performed optimization of HTML and CSS:
html,
body {
margin: 0;
padding: 0;
}
.container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 100vh;
font-family: Raleway;
font-size: large;
}
.left_bg,
.right_bg {
display: flex;
align-items: center;
justify-content: center;
}
.left_bg {
background-color: #3498db;
}
.right_bg {
background-color: #ecf0f1;
}
<div class="container">
<div class="left_bg">Review my stuff</div>
<div class="right_bg">Hire me!</div>
</div>
We can use place-items: center; property in parent element to make child elements text in center.
.parent {
display:grid;
grid-template-columns: repeat(2,1fr);
border: 3px solid yellow;
grid-gap: 3px;
place-items: center;
}
.parent > div {
background-color: blue;
text-align: center;
color: white;
font-weight: bold;
font-size: 2rem;
}
<div class="parent">
<div class="child1">child1</div>
<div class="child2">child2</div>
</div>
Make parent a grid and justify-content: center; and align-content: center;
.parent {
height: 100px;
width: 100px;
border: 1px solid black;
display: grid;
justify-content: center;
align-content: center;
}
.child {
height: 20px;
width: 20px;
background-color: red;
}
<div class="parent">
<div class="child">child</div>
</div>
Try using flex:
Plunker demo : https://plnkr.co/edit/nk02ojKuXD2tAqZiWvf9
/* Styles go here */
html,
body {
margin: 0;
padding: 0;
}
.container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 100vh;
grid-gap: 0px 0px;
}
.left_bg {
background-color: #3498db;
grid-column: 1 / 1;
grid-row: 1 / 1;
z-index: 0;
display: flex;
justify-content: center;
align-items: center;
}
.right_bg {
background-color: #ecf0f1;
grid-column: 2 / 2;
grid_row: 1 / 1;
z-index: 0;
display: flex;
justify-content: center;
align-items: center;
}
.text {
font-family: Raleway;
font-size: large;
text-align: center;
}
HTML
<div class="container">
<!--everything on the page-->
<div class="left_bg">
<!--left background color of the page-->
<div class="text">
<!--left side text content-->
<p>Review my stuff</p>
</div>
</div>
<div class="right_bg">
<!--right background color of the page-->
<div class="text">
<!--right side text content-->
<p>Hire me!</p>
</div>
</div>
</div>
This worked for me:
.d-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
border-top: 1px solid black;
border-left: 1px solid black;
}
.d-grid div {
height: 50px;
display: flex;
border-bottom: 1px solid black;
border-right: 1px solid black;
align-items: center;
justify-content: center;
}
<div class="d-grid">
<div>text 1</div>
<div>text 2</div>
<div>text 3</div>
<div>text 4</div>
<div>text 5</div>
<div>text 6</div>
</div>
You want this?
html,
body {
margin: 0;
padding: 0;
}
.container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 100vh;
grid-gap: 0px 0px;
}
.left_bg {
display: subgrid;
background-color: #3498db;
grid-column: 1 / 1;
grid-row: 1 / 1;
z-index: 0;
}
.right_bg {
display: subgrid;
background-color: #ecf0f1;
grid-column: 2 / 2;
grid_row: 1 / 1;
z-index: 0;
}
.text {
font-family: Raleway;
font-size: large;
text-align: center;
}
<div class="container">
<!--everything on the page-->
<div class="left_bg">
<!--left background color of the page-->
<div class="text">
<!--left side text content-->
<p>Review my stuff</p>
</div>
</div>
<div class="right_bg">
<!--right background color of the page-->
<div class="text">
<!--right side text content-->
<p>Hire me!</p>
</div>
</div>
</div>
Related
Trying to put 2 links beside each other so they are the same height in css grid [duplicate]
This question already has answers here: Align child elements of different blocks (3 answers) Closed 7 months ago. I'm trying to use css grid to place 2 links beside each other to be the same height with text centered both vertically and horizontally, regardless of how much text is in each link. The grid itself is centered vertically, but the text does not fill the entire grid cell. Is it not possible to make the 2 links the same height? Codepen: https://codepen.io/stevea777/pen/VwXPZQE .box { display: grid; grid-template-columns: 50% 50%; grid-auto-rows: 1fr; height: auto; width: 300px; background: red; } a.button { background: green; color: white; text-align: center; padding: 10px; align-self: center; } a:nth-child(2) { background: blue; } <div class="box"> <a class="button">Batman</a> <a class="button">Spiderman eats Cornbread with Maple Syrup</a> </div>
How about adding height: 100% to a.button class: .box{ display: grid; grid-template-columns: 50% 50%; grid-auto-rows: 1fr; height: auto; width: 300px; background: red; } a.button{ box-sizing: border-box; background: green; color: white; padding: 10px; height: 100%; display: flex; align-items: center; justify-content: center; } a:nth-child(2){ background: blue; } <div class="box"> <a class="button">Batman</a> <a class="button">Spiderman eats Cornbread with Maple Syrup</a> </div>
Prevent CSS Grid Item from expanding to full width based on content/auto-sizing [duplicate]
I'm trying to create a simple page with CSS Grid. What I'm failing to do is center the text from the HTML to the respective grid cells. I've tried placing content in separate divs both inside and outside of the left_bg and right_bg selectors and playing with some of the CSS properties to no avail. How do I do this? html, body { margin: 0; padding: 0; } .container { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 100vh; grid-gap: 0px 0px; } .left_bg { display: subgrid; background-color: #3498db; grid-column: 1 / 1; grid-row: 1 / 1; z-index: 0; } .right_bg { display: subgrid; background-color: #ecf0f1; grid-column: 2 / 2; grid_row: 1 / 1; z-index: 0; } .left_text { grid-column: 1 / 1; grid-row: 1 / 1; position: relative; z-index: 1; justify-self: center; font-family: Raleway; font-size: large; } .right_text { grid-column: 2 / 2; grid_row: 1 / 1; position: relative; z-index: 1; justify-self: center; font-family: Raleway; font-size: large; } <div class="container"> <!--everything on the page--> <div class="left_bg"> <!--left background color of the page--> </div> </div> <div class="right_bg"> <!--right background color of the page--> </div> <div class="left_text"> <!--left side text content--> <p>Review my stuff</p> <div class="right_text"> <!--right side text content--> <p>Hire me!</p> </div> </div>
This answer has two main sections: Understanding how alignment works in CSS Grid. Six methods for centering in CSS Grid. If you're only interested in the solutions, skip the first section. The Structure and Scope of Grid layout To fully understand how centering works in a grid container, it's important to first understand the structure and scope of grid layout. The HTML structure of a grid container has three levels: the container the item the content Each of these levels is independent from the others, in terms of applying grid properties. The scope of a grid container is limited to a parent-child relationship. This means that a grid container is always the parent and a grid item is always the child. Grid properties work only within this relationship. Descendants of a grid container beyond the children are not part of grid layout and will not accept grid properties. (At least not until the subgrid feature has been implemented, which will allow descendants of grid items to respect the lines of the primary container.) Here's an example of the structure and scope concepts described above. Imagine a tic-tac-toe-like grid. article { display: inline-grid; grid-template-rows: 100px 100px 100px; grid-template-columns: 100px 100px 100px; grid-gap: 3px; } You want the X's and O's centered in each cell. So you apply the centering at the container level: article { display: inline-grid; grid-template-rows: 100px 100px 100px; grid-template-columns: 100px 100px 100px; grid-gap: 3px; justify-items: center; } But because of the structure and scope of grid layout, justify-items on the container centers the grid items, not the content (at least not directly). article { display: inline-grid; grid-template-rows: 100px 100px 100px; grid-template-columns: 100px 100px 100px; grid-gap: 3px; justify-items: center; } section { border: 2px solid black; font-size: 3em; } <article> <section>X</section> <section>O</section> <section>X</section> <section>O</section> <section>X</section> <section>O</section> <section>X</section> <section>O</section> <section>X</section> </article> Same problem with align-items: The content may be centered as a by-product, but you've lost the layout design. article { display: inline-grid; grid-template-rows: 100px 100px 100px; grid-template-columns: 100px 100px 100px; grid-gap: 3px; justify-items: center; align-items: center; } article { display: inline-grid; grid-template-rows: 100px 100px 100px; grid-template-columns: 100px 100px 100px; grid-gap: 3px; justify-items: center; align-items: center; } section { border: 2px solid black; font-size: 3em; } <article> <section>X</section> <section>O</section> <section>X</section> <section>O</section> <section>X</section> <section>O</section> <section>X</section> <section>O</section> <section>X</section> </article> To center the content you need to take a different approach. Referring again to the structure and scope of grid layout, you need to treat the grid item as the parent and the content as the child. article { display: inline-grid; grid-template-rows: 100px 100px 100px; grid-template-columns: 100px 100px 100px; grid-gap: 3px; } section { display: flex; justify-content: center; align-items: center; border: 2px solid black; font-size: 3em; } article { display: inline-grid; grid-template-rows: 100px 100px 100px; grid-template-columns: 100px 100px 100px; grid-gap: 3px; } section { display: flex; justify-content: center; align-items: center; border: 2px solid black; font-size: 3em; } <article> <section>X</section> <section>O</section> <section>X</section> <section>O</section> <section>X</section> <section>O</section> <section>X</section> <section>O</section> <section>X</section> </article> jsFiddle demo Six Methods for Centering in CSS Grid There are multiple methods for centering grid items and their content. Here's a basic 2x2 grid: grid-container { display: grid; grid-template-columns: 1fr 1fr; grid-auto-rows: 75px; grid-gap: 10px; } /* can ignore styles below; decorative only */ grid-container { background-color: lightyellow; border: 1px solid #bbb; padding: 10px; } grid-item { background-color: lightgreen; border: 1px solid #ccc; } <grid-container> <grid-item>this text should be centered</grid-item> <grid-item>this text should be centered</grid-item> <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item> <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item> </grid-container> Flexbox For a simple and easy way to center the content of grid items use flexbox. More specifically, make the grid item into a flex container. There is no conflict, spec violation or other problem with this method. It's clean and valid. grid-item { display: flex; align-items: center; justify-content: center; } grid-container { display: grid; grid-template-columns: 1fr 1fr; grid-auto-rows: 75px; grid-gap: 10px; } grid-item { display: flex; /* new */ align-items: center; /* new */ justify-content: center; /* new */ } /* can ignore styles below; decorative only */ grid-container { background-color: lightyellow; border: 1px solid #bbb; padding: 10px; } grid-item { background-color: lightgreen; border: 1px solid #ccc; } <grid-container> <grid-item>this text should be centered</grid-item> <grid-item>this text should be centered</grid-item> <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item> <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item> </grid-container> See this post for a complete explanation: How to Center Elements Vertically and Horizontally in Flexbox Grid Layout In the same way that a flex item can also be a flex container, a grid item can also be a grid container. This solution is similar to the flexbox solution above, except centering is done with grid, not flex, properties. grid-container { display: grid; grid-template-columns: 1fr 1fr; grid-auto-rows: 75px; grid-gap: 10px; } grid-item { display: grid; /* new */ align-items: center; /* new */ justify-items: center; /* new */ } /* can ignore styles below; decorative only */ grid-container { background-color: lightyellow; border: 1px solid #bbb; padding: 10px; } grid-item { background-color: lightgreen; border: 1px solid #ccc; } <grid-container> <grid-item>this text should be centered</grid-item> <grid-item>this text should be centered</grid-item> <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item> <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item> </grid-container> auto margins Use margin: auto to vertically and horizontally center grid items. grid-item { margin: auto; } grid-container { display: grid; grid-template-columns: 1fr 1fr; grid-auto-rows: 75px; grid-gap: 10px; } grid-item { margin: auto; } /* can ignore styles below; decorative only */ grid-container { background-color: lightyellow; border: 1px solid #bbb; padding: 10px; } grid-item { background-color: lightgreen; border: 1px solid #ccc; } <grid-container> <grid-item>this text should be centered</grid-item> <grid-item>this text should be centered</grid-item> <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item> <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item> </grid-container> To center the content of grid items you need to make the item into a grid (or flex) container, wrap anonymous items in their own elements (since they cannot be directly targeted by CSS), and apply the margins to the new elements. grid-item { display: flex; } span, img { margin: auto; } grid-container { display: grid; grid-template-columns: 1fr 1fr; grid-auto-rows: 75px; grid-gap: 10px; } grid-item { display: flex; } span, img { margin: auto; } /* can ignore styles below; decorative only */ grid-container { background-color: lightyellow; border: 1px solid #bbb; padding: 10px; } grid-item { background-color: lightgreen; border: 1px solid #ccc; } <grid-container> <grid-item><span>this text should be centered</span></grid-item> <grid-item><span>this text should be centered</span></grid-item> <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item> <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item> </grid-container> Box Alignment Properties When considering using the following properties to align grid items, read the section on auto margins above. align-items justify-items align-self justify-self https://www.w3.org/TR/css-align-3/#property-index text-align: center To center content horizontally in a grid item, you can use the text-align property. grid-container { display: grid; grid-template-columns: 1fr 1fr; grid-auto-rows: 75px; grid-gap: 10px; text-align: center; /* new */ } /* can ignore styles below; decorative only */ grid-container { background-color: lightyellow; border: 1px solid #bbb; padding: 10px; } grid-item { background-color: lightgreen; border: 1px solid #ccc; } <grid-container> <grid-item>this text should be centered</grid-item> <grid-item>this text should be centered</grid-item> <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item> <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item> </grid-container> Note that for vertical centering, vertical-align: middle will not work. This is because the vertical-align property applies only to inline and table-cell containers. grid-container { display: grid; grid-template-columns: 1fr 1fr; grid-auto-rows: 75px; grid-gap: 10px; text-align: center; /* <--- works */ vertical-align: middle; /* <--- fails */ } /* can ignore styles below; decorative only */ grid-container { background-color: lightyellow; border: 1px solid #bbb; padding: 10px; } grid-item { background-color: lightgreen; border: 1px solid #ccc; } <grid-container> <grid-item>this text should be centered</grid-item> <grid-item>this text should be centered</grid-item> <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item> <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item> </grid-container> One might say that display: inline-grid establishes an inline-level container, and that would be true. So why doesn't vertical-align work in grid items? The reason is that in a grid formatting context, items are treated as block-level elements. 6.1. Grid Item Display The display value of a grid item is blockified: if the specified display of an in-flow child of an element generating a grid container is an inline-level value, it computes to its block-level equivalent. In a block formatting context, something the vertical-align property was originally designed for, the browser doesn't expect to find a block-level element in an inline-level container. That's invalid HTML. CSS Positioning Lastly, there's a general CSS centering solution that also works in Grid: absolute positioning This is a good method for centering objects that need to be removed from the document flow. For example, if you want to: Center text over an image, or Place asterisks (*) above required form fields Simply set position: absolute on the element to be centered, and position: relative on the ancestor that will serve as the containing block (it's usually the parent). Something like this: grid-item { position: relative; text-align: center; } span { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); } grid-container { display: grid; grid-template-columns: 1fr 1fr; grid-auto-rows: 75px; grid-gap: 10px; } grid-item { position: relative; text-align: center; } span, img { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); } /* can ignore styles below; decorative only */ grid-container { background-color: lightyellow; border: 1px solid #bbb; padding: 10px; } grid-item { background-color: lightgreen; border: 1px solid #ccc; } <grid-container> <grid-item><span>this text should be centered</span></grid-item> <grid-item><span>this text should be centered</span></grid-item> <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item> <grid-item><img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt=""></grid-item> </grid-container> Here's a complete explanation for how this method works: Element will not stay centered, especially when re-sizing screen Here's the section on absolute positioning in the Grid spec: https://www.w3.org/TR/css3-grid-layout/#abspos
Do not even try to use flex and stay with css grid. Just add the following on the content element: place-self: center; and it will do the centring work here. If you want to center something that is inside div that is inside grid cell, you need to define nested grid in order to make it work. Both examples shown in the demo. https://css-tricks.com/snippets/css/complete-guide-grid/
The CSS place-items shorthand property sets the align-items and justify-items properties, respectively. If the second value is not set, the first value is also used for it. .parent { display: grid; place-items: center; }
You can use flexbox to center your text. By the way no need for extra containers because text is considered as anonymous flex item. From flexbox specs: Each in-flow child of a flex container becomes a flex item, and each contiguous run of text that is directly contained inside a flex container is wrapped in an anonymous flex item. However, an anonymous flex item that contains only white space (i.e. characters that can be affected by the white-space property) is not rendered (just as if it were display:none). So just make grid items as flex containers (display: flex), and add align-items: center and justify-content: center to center both vertically and horizontally. Also performed optimization of HTML and CSS: html, body { margin: 0; padding: 0; } .container { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 100vh; font-family: Raleway; font-size: large; } .left_bg, .right_bg { display: flex; align-items: center; justify-content: center; } .left_bg { background-color: #3498db; } .right_bg { background-color: #ecf0f1; } <div class="container"> <div class="left_bg">Review my stuff</div> <div class="right_bg">Hire me!</div> </div>
We can use place-items: center; property in parent element to make child elements text in center. .parent { display:grid; grid-template-columns: repeat(2,1fr); border: 3px solid yellow; grid-gap: 3px; place-items: center; } .parent > div { background-color: blue; text-align: center; color: white; font-weight: bold; font-size: 2rem; } <div class="parent"> <div class="child1">child1</div> <div class="child2">child2</div> </div>
Make parent a grid and justify-content: center; and align-content: center; .parent { height: 100px; width: 100px; border: 1px solid black; display: grid; justify-content: center; align-content: center; } .child { height: 20px; width: 20px; background-color: red; } <div class="parent"> <div class="child">child</div> </div>
Try using flex: Plunker demo : https://plnkr.co/edit/nk02ojKuXD2tAqZiWvf9 /* Styles go here */ html, body { margin: 0; padding: 0; } .container { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 100vh; grid-gap: 0px 0px; } .left_bg { background-color: #3498db; grid-column: 1 / 1; grid-row: 1 / 1; z-index: 0; display: flex; justify-content: center; align-items: center; } .right_bg { background-color: #ecf0f1; grid-column: 2 / 2; grid_row: 1 / 1; z-index: 0; display: flex; justify-content: center; align-items: center; } .text { font-family: Raleway; font-size: large; text-align: center; } HTML <div class="container"> <!--everything on the page--> <div class="left_bg"> <!--left background color of the page--> <div class="text"> <!--left side text content--> <p>Review my stuff</p> </div> </div> <div class="right_bg"> <!--right background color of the page--> <div class="text"> <!--right side text content--> <p>Hire me!</p> </div> </div> </div>
This worked for me: .d-grid { display: grid; grid-template-columns: repeat(3, 1fr); border-top: 1px solid black; border-left: 1px solid black; } .d-grid div { height: 50px; display: flex; border-bottom: 1px solid black; border-right: 1px solid black; align-items: center; justify-content: center; } <div class="d-grid"> <div>text 1</div> <div>text 2</div> <div>text 3</div> <div>text 4</div> <div>text 5</div> <div>text 6</div> </div>
You want this? html, body { margin: 0; padding: 0; } .container { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 100vh; grid-gap: 0px 0px; } .left_bg { display: subgrid; background-color: #3498db; grid-column: 1 / 1; grid-row: 1 / 1; z-index: 0; } .right_bg { display: subgrid; background-color: #ecf0f1; grid-column: 2 / 2; grid_row: 1 / 1; z-index: 0; } .text { font-family: Raleway; font-size: large; text-align: center; } <div class="container"> <!--everything on the page--> <div class="left_bg"> <!--left background color of the page--> <div class="text"> <!--left side text content--> <p>Review my stuff</p> </div> </div> <div class="right_bg"> <!--right background color of the page--> <div class="text"> <!--right side text content--> <p>Hire me!</p> </div> </div> </div>
A first element should take a free space and next elements should have fixed size
I want to set for fist element '1fr' and any other elements should have fixed size I'm tried this and it works. grid-template-columns: 500px repeat(auto-fill, 50px); This, what I'm trying to do. .container { display: grid; grid-template-columns: 1fr repeat(auto-fill, 50px); > div { border: 1px solid red; } } A first element should have a 1fr (any available space) enter image description here
I think flexbox would be more appropriate here. .container { display: flex; height: 98vh; margin: 1vh 1vw; } .item { border: 1px solid red; flex: 0 0 50px; margin: .5em; } .wide { flex: 1 } <div class="container"> <div class="item wide">Auto Remaining Width</div> <div class="item">50px</div> <div class="item">50px</div> <div class="item">50px</div> </div>
How to overlap image tag as a background over two CSS Grid areas
The image is an img tag and needs to be stretched as a background image over two areas 'img' and 'content'. The text has to go above the stretched image in the 'content' area. Simple but how? I can't find any obvious answers online. .media { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; width: 100% } .media { display: grid; grid-template-columns: 1fr 1fr; grid-template-areas: "img content"; margin-bottom: 1em; } .image { grid-area: img; background-color: #ffd8a8; } .text { grid-area: content; padding: 10px; } <div class="media"> <img class="image" src="https://loremflickr.com/500/200" /> <div class="text">This is a media object example. We can use grid-template-areas to switch around the image and text part of the media object. </div> </div>
Modify your code as follows: .media { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; width: 100% } .media { display: grid; grid-template-columns: 1fr 1fr; grid-template-areas: "img content"; margin-bottom: 1em; } .image { grid-area: img; background-color: #ffd8a8; } .text { grid-area: content; padding: 10px; } .media { background-image: url("https://loremflickr.com/700/200"); background-repeat: no-repeat; height: 150px; } <div class="media"> <div class="text">This is a media object example. We can use grid-template-areas to switch around the image and text part of the media object. </div> </div> Here is a screenshot of the output:
There are many ways to get the desired result. You could also make the image a background of the DIV rather than having an IMG tag inside the DIV. But I sticked to your code below and just added CSS to place the text DIV on top of the image and stretch the image to 100% with hidden overflow. .media { border: 2px solid #f76707; border-radius: 5px; background-color: #fff4e6; width: 100% display: grid; grid-template-columns: 1fr 1fr; grid-template-areas: "img content"; margin-bottom: 1em; overflow: hidden; } .image { grid-area: img; background-color: #ffd8a8; width: 100%; float: left; } .text { grid-area: content; padding: 10px; position: absolute; top: 10px; left: 10px; color: white; text-shadow: 1px 1px 2px black; box-sizing: border-box; } <div class="media"> <img class="image" src="https://loremflickr.com/500/200" /> <div class="text"><b>This is a media object example. We can use grid-template-areas to switch around the image and text part of the media object.</b> </div> </div>
I think, if there is just one part of the page at the background of the page, you should create a style. Then look at the backgammon tag select whatever you want,Later, the ID will have it in created anything on the page, for example Panel, td, div,.... get it there. You do not need to add any image.
Have you tried position: absolute on the text div?
I figured it out if anyone stumbles across it: (make sure you preview with full page) #container { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 100px; grid-template-areas: "empty text" "mobile mobile"; } #container img { grid-column: 1 / span 2; grid-row: 1 / span 1; width: 100%; overflow: hidden; } #container p { grid-area: text; color: red; align-self: center; justify-self: center; z-index: 1; } #media (max-width: 768px) { #container p { grid-area: mobile; color: red; align-self: center; justify-self: center; z-index: 1; } } <div id="container"> <img src="https://loremflickr.com/500/200"> <p>SOME TEXT OVER IMAGE</p> </div>
Center items in grid layout when not enough data to fill entire row
I'm using the new CSS grid layout, basically I'm displaying a series of images, which is fine with six columns, I've gotten that part down, but the content has a variable number, and often times there is not enough content to fill every column, so in the event that there's only say two items in the row, I'd like them to center in the middle two columns. This being a relatively new API though I haven't seen anything like this that I've managed to get working properly. Here is some sample CSS, please excuse my general ignorance if something looks horribly wrong. #result-images{ display: grid; grid-template-columns: 260px 260px 260px 260px 260px 260px; grid-gap: 11px; grid-column-gap:55px; background-color: #959595; border-style:solid; border-width:3px; border-color:#FFFFFF; } #result-images img{ width:100%; height: 100%; background-color: #363636; display:block; margin: 0 auto; }
Flexbox is actually easier to use to achieve this output. Example below. #result-images{ display:flex; flex-flow: row wrap; justify-content:center; align-content:center; align-items:center; border: 2px solid black; max-width:360px; } #result-images img{ min-width: 120px; max-width: 120px; min-height: 120px; max-height: 120px; flex: 0 1 auto align-self:center; } <section id="result-images"> <img src="https://rack.pub/media/ronRoyston.png"> <img src="https://rack.pub/media/ronRoyston.png"> <img src="https://rack.pub/media/ronRoyston.png"> <img src="https://rack.pub/media/ronRoyston.png"> <img src="https://rack.pub/media/ronRoyston.png"> </section> List item
Center an image in the area #result-images { border: 2px solid #fffff; border-radius: 5px; background-color: #959595; } #result-images { display: grid; grid-template-columns: repeat(6, 1fr); grid-gap: 10px; grid-auto-rows: 200px; grid-template-areas: ". a a a a ." ". a a a a ."; } img { grid-area: a; align-self: center; justify-self: center; width: 100px; height: 100px; } <div id="result-images"> <img src="https://c1.staticflickr.com/2/1018/805106121_ab84d1a216_b.jpg" /> </div>
use auto-fit with repeat() in your case grid-template-columns: repeat(auto-fit, 260px)