Here are my code snippets:
.grades_dashboard_m {}
.three_separation {
width: 100%;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-gap: 60px;
}
.grades_dashboard_box {
height: 130px;
width: 160px;
background-color: #000000;
color: #ffff;
}
<div class="grades_dashboard_m" id="co_1">
<div class="three_separation">
<div class='grades_dashboard_box'>
<h1>12</h1>
</div>
<div class='grades_dashboard_box'>
<h1>12</h1>
</div>
<div class='grades_dashboard_box'>
<h1>12</h1>
</div>
</div>
</div>
As you see the grid div (grades_dashboard_m) is not centered in grades_dashboard_m. How to center it without center the elements in grades_dashboard_m? I already tried this:
.grades_dashboard_m {
display:flex;
justify-content:center;
align-items:center;
}
But without success.
EDIT: I figured out that this is not the problem. The problem is that the content of the 3 grids is not centered. But how to center the content of the grids?
Simply apply margin: 0 auto; to .grades_dashboard_box
likr this
.grades_dashboard_box {
margin: 0 auto;
}
this shall center the element not text.
Pure, CSS Grid Solution
In short, don't use margin: 0 auto to center grid children—use justify-items: center.
.three_separation {
…
justify-items: center;
}
A few words about justify-items from MDN:
In grid layouts, it aligns the items inside their grid areas on the
inline axis
Demo
.grades_dashboard_m {}
.three_separation {
width: 100%;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-gap: 60px;
justify-items: center; /* <-- Added */
}
.grades_dashboard_box {
height: 130px;
width: 160px;
background-color: #000000;
color: #ffff;
}
<div class="grades_dashboard_m" id="co_1">
<div class="three_separation">
<div class='grades_dashboard_box'>
<h1>12</h1>
</div>
<div class='grades_dashboard_box'>
<h1>12</h1>
</div>
<div class='grades_dashboard_box'>
<h1>12</h1>
</div>
</div>
</div>
Related
How do I get rid of the space between the menu bar and the sidebar caused by typing hello.
I have tried
display:inline-block; and overflow:hidden; which got rid of the white space that was there previously and now filled it up with a color. I have also tried taking the content div and moving it so it isn't a parent(?) of .sidebar but then "hello" just ends up on the bottom of the page. I want to keep the "hello" text centered on the yellow area without having a space between the side bar and the menu bar.
Picture of the website
.menucontain{
display:grid;
grid-template-columns:1fr 1fr 1fr 1fr 1fr 1fr;
grid-column-gap:5px;
color:#F2F0D0;
text-align:center;
background-color:#204959;
font-family:helvetica;
padding:15px;
}
.sidebar{
background:#204959;
width:18%;
height:800px;
text-align:center;
color:#F2F0D0;
font-family:helvetica;
display:grid;
grid-template-rows: repeat(6 ,50px);
grid-gap:2px;
}
.side1{
background:gray;
padding-top:15px;
}
.content{
background-color:#F2F0D0;
text-align:center;
overflow:hidden;
}
<div class="menucontain">
<div class="menu1">Menu1</div>
<div class="menu2">Menu2</div>
<div class="menu3">Menu3</div>
<div class="menu4">Menu4</div>
<div class="menu5">Menu5</div>
<div class="menu6">Menu6</div>
<!--menu contain div on next line-->
</div>
<div class="content">
<p>hello</p>
<div class="sidebar">
<div class="side1">About</div>
<div class="side1">Blog</div>
<div class="side1">Sales</div>
<div class="side1">Partners</div>
<div class="side1">Portfolio</div>
<div class="side1">Contact</div>
</div>
</div>
Here is the correct solution to your problem.
First, move the tag <p> in sequence for sidebar. Like this:
...
<div class="sidebar">
<div class="side1">About</div>
<div class="side1">Blog</div>
<div class="side1">Sales</div>
<div class="side1">Partners</div>
<div class="side1">Portfolio</div>
<div class="side1">Contact</div>
</div>
<p>hello</p>
...
Secondly, assign the grid rules for the .content class by adding this to your css:
.content {
display: grid;
grid-template-columns: 18% 1fr;
}
And remove the width rules - width: 18% out of .sidebar selector. Because we defined the width as 18% in the grid rule above.
.menucontain {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr;
grid-column-gap: 5px;
color: #f2f0d0;
text-align: center;
background-color: #204959;
font-family: helvetica;
padding: 15px;
}
.content {
display: grid;
grid-template-columns: 18% 1fr;
}
.sidebar {
background: #204959;
/*width: 18%;*/
height: 800px;
text-align: center;
color: #f2f0d0;
font-family: helvetica;
display: grid;
grid-template-rows: repeat(6, 50px);
grid-gap: 2px;
}
.side1 {
background: gray;
padding-top: 15px;
}
.content {
background-color: #f2f0d0;
text-align: center;
overflow: hidden;
}
<div class="menucontain">
<div class="menu1">Menu1</div>
<div class="menu2">Menu2</div>
<div class="menu3">Menu3</div>
<div class="menu4">Menu4</div>
<div class="menu5">Menu5</div>
<div class="menu6">Menu6</div>
<!--menu contain div on next line-->
</div>
<div class="content">
<div class="sidebar">
<div class="side1">About</div>
<div class="side1">Blog</div>
<div class="side1">Sales</div>
<div class="side1">Partners</div>
<div class="side1">Portfolio</div>
<div class="side1">Contact</div>
</div>
<p>hello</p>
</div>
The p tag is a block element, to remove the space you have to remove hello <\p> from the 'content' class
Use flex display or grid display on the 'content' class
Re-aling the 3 different parts (menucontain, sidebar, content) into a grid by declaring the body as a grid. Sicne you already use grids to style the menucontain and sidebar, you have to switch them to a subgrid.
body {
margin: 0;
display: grid;
grid-template-columns: 18vw auto;
grid-template-rows: min-content auto;
min-height: 100vh;
}
.menucontain {
grid-column: span 2;
display: grid;
grid-template-columns: repeat(6, 1fr);
grid-template-rows: subgrid;
grid-column-gap: 5px;
color: #F2F0D0;
text-align: center;
background-color: #204959;
font-family: helvetica;
padding: 15px;
}
.sidebar {
background: #204959;
height: 800px;
text-align: center;
color: #F2F0D0;
font-family: helvetica;
display: grid;
grid-template-columns: subgrid;
grid-template-rows: repeat(6, 50px);
grid-gap: 2px;
}
.side1 {
background: gray;
padding-top: 15px;
}
.content {
background-color: #F2F0D0;
text-align: center;
overflow: hidden;
}
<div class="menucontain">
<div class="menu1">Menu1</div>
<div class="menu2">Menu2</div>
<div class="menu3">Menu3</div>
<div class="menu4">Menu4</div>
<div class="menu5">Menu5</div>
<div class="menu6">Menu6</div>
</div>
<div class="sidebar">
<div class="side1">About</div>
<div class="side1">Blog</div>
<div class="side1">Sales</div>
<div class="side1">Partners</div>
<div class="side1">Portfolio</div>
<div class="side1">Contact</div>
</div>
<div class="content">
<p>hello</p>
</div>
I'm working with CSS-grids and I want the last row of my CSS-grid to use all remaining space in the wrapper but at the same time I want all other rows to follow the min-content-strategy.
CSS for the wrapper:
.grid {
width: 100%;
height: 100%;
display: grid;
grid-template-columns: 100px auto;
grid-auto-rows: min-content;
row-gap: 7px;
justify-items: start;
align-items: start;
}
My initial idea how to achieve this was to simply add margin-top: auto to the button as one would do with flexbox. I have also tried to set grid-auto-rows: auto for the grid wrapper but it does not work either.
Edit: Example code: https://jsfiddle.net/xt139o2g/ (I want button to appear in lower right corner of the wrapper div)
.wrapper {
border: 1px solid blue;
height: 300px;
width: 250px;
padding: 10px;
}
.grid {
width: 100%;
height: 100%;
display: grid;
grid-template-columns: 100px auto;
grid-auto-rows: min-content;
row-gap: 7px;
justify-items: start;
align-items: start;
}
.button {
grid-column: 1 / -1;
justify-self: end;
align-self: end;
}
<div class="wrapper">
<div class="grid">
<div>
R1C1
</div>
<div>
R1C2
</div>
<div>
R2C1
</div>
<div>
R2C2
</div>
<div class="button">
Button
</div>
</div>
</div>
Thanks in advance!
You can use the repeat function to set the first two rows to min-content:
grid-template-rows: repeat(2, min-content);
Then, use display: flex on .button and shift its content to the bottom right using justify-content and align-items:
display: flex;
align-items: flex-end;
justify-content: flex-end;
Demo: (I've added borders to demonstrate the spacing.)
.wrapper {
border: 1px solid blue;
height: 300px;
width: 250px;
padding: 10px;
}
.grid {
width: 100%;
height: 100%;
display: grid;
grid-template-columns: 100px auto;
grid-template-rows: repeat(2, min-content);
row-gap: 7px;
}
.grid>div {
width: 100%;
height: 100%;
border: 1px solid;
}
.button {
display: flex;
align-items: flex-end;
justify-content: flex-end;
grid-column: 1 / -1;
}
<div class="wrapper">
<div class="grid">
<div>
R1C1
</div>
<div>
R1C2
</div>
<div>
R2C1
</div>
<div>
R2C2
</div>
<div class="button">
Button
</div>
</div>
</div>
I don't think there is a trivial way to achieve this but you can appromxiate it like below:
.wrapper {
display:inline-block;
border: 1px solid blue;
height: 300px;
width: 200px;
padding: 10px;
}
.grid {
width: 100%;
height: 100%;
display: grid;
grid-template-columns: 100px auto;
grid-template-rows:repeat(100,min-content) 1fr; /* big number here*/
}
.grid *:not(:last-child) {
margin-bottom:7px; /* replace the gap */
}
.button {
grid-column: 1 / -1;
grid-row: 101; /* place it at the 1fr template */
margin:auto 0 0 auto;
}
<div class="wrapper">
<div class="grid">
<div>
R1C1
</div>
<div>
R1C2
</div>
<div>
R2C1
</div>
<div>
R2C2
</div>
<div class="button">
Button
</div>
</div>
</div>
<div class="wrapper">
<div class="grid">
<div>
R1C1
</div>
<div>
R1C2
</div>
<div class="button">
Button
</div>
</div>
</div>
<div class="wrapper">
<div class="grid">
<div>
R1C1
</div>
<div>
R1C2
</div>
<div>
R1C1
</div>
<div>
R1C2
</div>
<div>
R1C1
</div>
<div>
R1C2
</div>
<div>
R1C1
</div>
<div>
R1C2
</div>
<div class="button">
Button
</div>
</div>
</div>
I am trying to display div inside a parent div but want the children divs to be in the centre and not from the beginning of the parent div. The HTML/CSS is given below along with a screenshot of how it looks on iPad (seems alright) and computer(not something I want).
.inner-tile {
display: inline-grid;
grid-gap: 5px;
grid-template-columns: repeat(auto-fill, minmax(240px, 1fr ));
grid-template-rows: 1fr;
justify-items: center;
width: 100%;
height: auto;
font-size: 18px;
padding-bottom: 10px;
}
.event {
border: 1px solid grey;
border-radius: 4px;
min-width: 240px;
min-height: 270px;
width: 90%;
height: auto;
}
<div class = "inner-tile">
<div class = "event">
<div class="event-image"></div>
event
</div>
<div class = "event">
<div class="event-image"></div>
event
</div>
<div class = "event">
<div class="event-image"></div>
event
</div>
<div class = "event">
<div class="event-image"></div>
event
</div>
</div>
If you will always have 4 items you can conside max-width and use grid instead of inline-grid then you can center using margin auto
.inner-tile {
display: grid;
max-width: calc(240px * 4 + 5px * 3);
margin: auto;
grid-gap: 5px;
grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
justify-items: center;
font-size: 18px;
padding-bottom: 10px;
}
.event {
border: 1px solid grey;
border-radius: 4px;
min-width: 240px;
min-height: 270px;
width: 90%;
}
<div class="inner-tile">
<div class="event">
<div class="event-image"></div>
event
</div>
<div class="event">
<div class="event-image"></div>
event
</div>
<div class="event">
<div class="event-image"></div>
event
</div>
<div class="event">
<div class="event-image"></div>
event
</div>
</div>
use
grid-template-columns: 1fr 1fr 1fr 1fr;
justify-items: center;
for more see https://alligator.io/css/align-justify/
Use justify-content: center;css code instead of justify-items: center; for outer div
I want to grow a grid to fill the remaining vertical space. There are a couple divs above a calendar of fixed height, and I'd like those to remain a fixed height. As the window grows vertically, I'd like only the calendar to change, with each row growing evenly, stopping at the bottom of the window with no scrollbars.
* {
box-sizing: border-box;
padding: 0px;
margin: 0px;
}
html,
body {
background: #92bde7;
color: #485e74;
font-family: Tahoma, Geneva, Verdana, sans-serif;
height: 100%;
}
.selection {
padding: 5px;
}
.header {
display: grid;
grid-template-columns: repeat(7, 1fr);
text-align: center;
}
.header>div {
padding: 4px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
/* this is the element that needs to stretch to remaining available window space */
.calendar {
display: grid;
grid-template-columns: repeat(7, 1fr);
}
/* these elements should stretch evenly as the window grows */
.calendar>div {
text-align: right;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
padding: 0.5em;
min-height: 100px;
}
.prev>div,
.next>div,
.date>div {
text-align: left;
}
.prev,
.next {
background: #c9e6ff;
color: #666;
}
.date {
background: #f9feff;
}
<div class="selection">
<h1><a class="back">❮</a> 6 / 2018 <a class="forward">❯</a></h1>
</div>
<div class="header">
<div>Sunday</div>
<div>Monday</div>
<div>Tuesday</div>
<div>Wednesday</div>
<div>Thursday</div>
<div>Friday</div>
<div>Saturday</div>
</div>
<div class="calendar">
<div class="prev">30</div>
<div class="prev">31</div>
<div class="date">1
<div class="item">Stuff to do...</div>
</div>
<div class="date">2</div>
<div class="date">3</div>
<div class="date">4</div>
<div class="date">5</div>
<div class="date">6</div>
<div class="date">7</div>
<div class="date">8</div>
<div class="date">9</div>
<div class="date">10</div>
<div class="date">11</div>
<div class="date">12</div>
<div class="date">13</div>
<div class="date">14</div>
<div class="date">15</div>
<div class="date">16</div>
<div class="date">17</div>
<div class="date">18</div>
<div class="date">19</div>
<div class="date">20</div>
<div class="date">21</div>
<div class="date">22</div>
<div class="date">23</div>
<div class="date">24</div>
<div class="date">25</div>
<div class="date">26</div>
<div class="date">27</div>
<div class="date">28</div>
<div class="date">29</div>
<div class="date">30</div>
<div class="next">1</div>
<div class="next">2</div>
<div class="next">3</div>
</div>
Try wrapping the whole thing a in a flex container, then using flex properties to set your lengths. Something like this:
body {
display: flex;
flex-direction: column;
height: 100vh;
}
.selection {
flex: 0 0 50px; /* flex-grow, flex-shrink, flex-basis */
}
.header {
flex: 0 0 25px;
display: grid;
grid-template-columns: repeat(7, 1fr);
}
.calendar {
flex: 1; /* dynamic length; consumes all remaining space */
overflow: auto;
display: grid;
grid-template-columns: repeat(7, 1fr);
grid-auto-rows: 1fr;
}
jsFiddle demo
There are several ways to accomplish this. One way to do this is to leverage vh units. I made a CodePen with your code and made a slight tweak to the height of your rows in CSS:
.calendar > div {
...
height: calc(20vh - 17px)
}
This means that each row will take up 20% of the vertical space of the screen minus 17px, which allows for the header. This assumes there will always be five rows.
This question already has answers here:
Centering in CSS Grid
(9 answers)
Closed 3 years ago.
Using CSS-Grid, I try to put my items in the center of a grid cell, without shrinking them completely to only the content. Is this possible?
I made a simple example on stackblitz. You can see that the items there don't fill the entire grid-cell with the background color. What is the proper way to get that working? I can remove the justify-items/align-items classes, but then the content isn't centered anymore.
https://stackblitz.com/edit/angular-8bggtq?file=app/app.component.html
Cells filled, but content not in center:
Cells not filled, but content is centered:
.wrapper {
display: grid;
height: 100vh;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
justify-items: center;
align-items: center;
}
.item {
//justify-self: stretch;
//align-self: stretch;
}
.one {
background: red;
}
.two {
background: pink;
}
.three {
background: violet;
}
.four {
background: yellow;
}
.five {
background: brown;
}
.six {
background: green;
}
html,
body {
margin: 0;
}
<div class="wrapper">
<div class="item one">1</div>
<div class="item two">2</div>
<div class="item three">3</div>
<div class="item four">4</div>
<div class="item five">5</div>
<div class="item six">6</div>
</div>
The HTML structure of a grid container has three levels:
the container
the items (the children of the container)
the content (the grandchildren of the container and children of the items)
The problem you're having is that you're taking a two-level approach instead of the correct three-level approach. When you set align-items and justify-items on the container, they apply to the grid items, not to the content.
That's exactly what you are seeing: The grid items are being vertically and horizontally centered.
If you want to center the grid item children (the content), you need to specify that on the items. You can repeat on the items what you did on the container:
.item {
display: grid;
justify-items: center;
align-items: center;
}
Or, if you don't need grid layout in the items, here's another simple method:
.item {
display: flex;
justify-content: center;
align-items: center;
}
The above concepts apply to flex containers, as well.
For a more complete explanation and other centering methods see this post: Centering in CSS Grid
.wrapper {
display: grid;
height: 100vh;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
}
.item {
display: flex;
align-items: center;
justify-content: center;
}
.one { background: red; }
.two { background: pink; }
.three { background: violet; }
.four { background: yellow; }
.five { background: brown; }
.six { background: green; }
body { margin: 0; }
<div class="wrapper">
<div class="item one">1</div>
<div class="item two">2</div>
<div class="item three">3</div>
<div class="item four">4</div>
<div class="item five">5</div>
<div class="item six">6</div>
</div>
I would say the only way to do that just with CSS-grid is to insert a additional element- / grid-level.
However, I would also say that here - as #Zuber has already showed - the combination between grid and flexbox is the best way to achieve what you want.
Grid is designed to be used with flexbox, not instead of it
Ollie Williams: Things I’ve Learned About CSS Grid Layout
Pure Grid-example:
* { margin: 0; padding: 0; box-sizing: border-box; }
body { margin: 20px; }
.wrapper {
height: 100vh;
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-gap: 20px;
}
.wrapper__item {
display: grid;
align-items: center;
justify-items: center;
background: gray;
color: white;
font-size: 2em;
}
<div class="wrapper">
<div class="wrapper__item"><span>1</span></div>
<div class="wrapper__item"><span>2</span></div>
<div class="wrapper__item"><span>3</span></div>
<div class="wrapper__item"><span>4</span></div>
<div class="wrapper__item"><span>5</span></div>
<div class="wrapper__item"><span>6</span></div>
</div>
Grid- & Flexbox-example:
* { margin: 0; padding: 0; box-sizing: border-box; }
body { margin: 20px; }
.wrapper {
height: 100vh;
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-gap: 20px;
}
.wrapper__item {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
background: gray;
color: white;
font-size: 2em;
}
<div class="wrapper">
<div class="wrapper__item">1</div>
<div class="wrapper__item">2</div>
<div class="wrapper__item">3</div>
<div class="wrapper__item">4</div>
<div class="wrapper__item">5</div>
<div class="wrapper__item">6</div>
</div>
you need to add some css in the class "item"
.item {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
Try with justify-self: center or text align:center if it is only text.