So I was wondering if it was possible for display: grid to center its items at the center like a flexbox if the row isn't filled up.
Example:
for (let i = 0; i < 3; i++)
{
$("#main").append($("<div class='item'>test</div>"))
$("#flex").append($("<div class='flex-item'>test</div>"))
}
#main {
display: grid;
grid-template-columns: repeat(6, 1fr);
width: 100%;
height: 100%;
align-items: center;
justify-content: center;
}
.item {
border: 1px solid black;
height: 100px;
}
#flex {
display: flex;
width: 100%;
justify-content: center;
}
.flex-item {
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main">
</div>
<div id="flex">
</div>
Apply auto instead 1fr in grid-template-columns. Then apply your desired width for your items. It will make the desired result.
#main {
display: grid;
grid-template-columns: repeat(6, auto);
width: 100%;
height: 100%;
align-items: center;
justify-content: center;
}
.item {
border: 1px solid black;
height: 100px;
width:100px;
}
DEMO
UPDATE:
If you don't want to fix the column width then use the vw to apply dynamic width based on your screen.
.item {
border: 1px solid black;
content: "test";
height: 100px;
width:16vw;
}
DEMO
Related
I've been trying to get familiar with grid while making the sidebar, and I encountered the problem where my grid items/children aren't equal to each-other in height even though they're supposed to be the same size.
* {
margin: 0;
padding: 0;
}
.mainContainer {
height: 500px;
gap: 30px;
display: grid;
grid-row-template: repeat(auto-fill, 1fr);
background-color: black;
color: white;
justify-items: center;
align-items: start;
}
.mainContainer div {
display: grid;
align-content: center;
justify-content: center;
width: 60%;
border: 1px solid yellow;
height: 60%;
border-radius: 10px;
}
.mainContainer img {
height: 50%;
width: 100%;
object-fit: contain;
}
<div class="mainContainer">
<div> <img src="https://pbs.twimg.com/profile_images/1455185376876826625/s1AjSxph_400x400.jpg"> </div>
<div> Box 1 </div>
<div> Box 2 </div>
<div> Box 3 </div>
</div>
Focusing on the image
.mainContainer img{
height: 30px; // set it to any size
object-fit: contain;
}
I think you should use px instead of %
You can use this, it is done without grid, but with a flex-column.
* {
margin: 0;
padding: 0;
}
.mainContainer{
height: 500px;
display: flex;
flex-direction: column;
align-items: center;
gap: 30px;
background-color: black;
color: white;
}
.mainContainer > * {
height: 25%;
width: 60%;
border: 1px solid yellow;
/* center image / text in children boxes */
display: flex;
justify-content: center;
align-items: center;
}
.mainContainer img {
object-fit: contain;
}
How do you have a CSS grid that is responsive for its child container when centered horizontally and vertically, I want to do this with CSS grid only. The code below doesn't work at all but it's the basics of what I want, can someone please get my code to work and make the box responsive, please!
html,
body {
display: grid;
justify-items: center;
align-items: center;
height: 100vh;
margin: 0;
}
.box {
height: 500px;
width: 100%;
max-width: 800px;
background: red;
border: 3px solid blue;
}
<div class="box"></div>
remove html
body {
display: grid;
justify-items: center;
align-items: center;
height: 100vh;
margin: 0;
}
.box {
height: 500px;
width: 100%;
max-width: 800px;
background: red;
border: 3px solid blue;
box-sizing:border-box;
}
<div class="box"></div>
just remove HTML from your CSS target element cause HTML is the root element of your page
body {
display: grid;
justify-items: center;
align-items: center;
height: 100vh;
margin: 0;
}
.box {
height: 500px;
width: 100%;
max-width: 800px;
background: red;
border: 3px solid blue;
}
<div class="box"></div>
Ok, so during the last weeks I've been trying to understand how html and CSS work, right now I'm struggling trying to make a grid layout. I'm trying to make my web responsive but when I work with percentages the percentages don't seem to apply correctly. For example there's an orange rectangle that's supposed to fit all the width of the screen, but with my code when the screen is pretty small it isn't large enough.
Here's the HTML and CSS (there are different settings depending on the media):
#media screen and (max-width:480px) {
body {
background-color: mediumspringgreen;
display: grid;
grid-template-columns: 100%;
grid-template-rows: 30% 6% 60% 4%;
grid-template-areas: "header" "nav" "main" "footer";
}
header img {
display: block;
align-self: center;
justify-self: center;
max-height: 5%;
display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
}
.rectangle {
width: 100%;
height: 4%;
background: gold;
position: absolute;
display: flex;
align-items: center;
justify-content: center;
}
.button1 {
background-color: green;
color: gold;
border: 3px solid mediumspringgreen;
border-radius: 12px;
display: inline-block;
cursor: pointer;
}
nav {
display: flex;
width: 100%;
align-items: center;
justify-content: center;
}
main {
display: grid;
grid-template-columns: 100%;
grid-template-rows: 70% 30%;
grid-template-areas: "imatge" "descripcio";
}
.imatge {
background-color: mediumspringgreen;
display: inline-block;
}
.descripcio {
background: gold;
justify-content: center;
align-items: center;
display: inline-block;
width: 100%;
height: 50%;
}
footer {
display: inline-block;
width: 100%;
align-items: center;
justify-content: center;
}
}
#media screen and (min-width:481px) {
body {
background-color: mediumspringgreen;
display: grid;
grid-template-columns: 100%;
grid-template-rows: 30% 6% 60% 4%;
grid-template-areas: "header" "nav" "main" "footer";
width: 100%;
height: 100%;
}
header img {
display: block;
align-self: center;
justify-self: center;
max-height: 5%;
display: block;
margin-left: auto;
margin-right: auto;
width: 20%;
}
.rectangle {
width: 100%;
height: 4%;
background: gold;
position: absolute;
display: flex;
align-items: center;
justify-content: center;
}
.button1 {
background-color: green;
color: gold;
border: 3px solid mediumspringgreen;
border-radius: 12px;
display: inline-block;
cursor: pointer;
}
nav {
display: flex;
width: 100%;
align-items: center;
justify-content: center;
}
main {
display: grid;
grid-template-columns: 50% 50%;
grid-template-rows: 100%;
grid-template-areas: "imatge descripcio";
}
.imatge {
background-color: mediumspringgreen;
}
.descripcio {
background: gold;
justify-content: center;
align-items: center;
display: flex;
position: absolute;
width: 100%;
height: 50%;
}
footer {
display: inline-block;
width: 100%;
align-items: center;
justify-content: center;
}
span {
color: gold;
}
}
<header style="width: 100%;">
<p style="text-align:center;">
<a href="pàgina%20home.html">
<img src="https://i.imgur.com/BSHMhUe.png" alt="logo">
</a>
</p>
</header>
<nav style="width: 100%;">
<p class="rectangle">
<button class="button1" onclick="location.href='pàgina%20productes.html'">Productes</button>
<span> </span>
<button class="button1" onclick="location.href='pàgina%20contacte.html'">Contacte</button>
</p>
</nav>
<main>
<section style="text-align:center;" class="Imatge">
<h2 style="font-size:150%;">Producte Destacat</h2>
<a href="pàgina%20producte.html">
<p style="text-align:center;">
<img src="https://clubtech.es/wp-content/uploads/2018/12/playstation_4_console_controller_ps4_92842_3840x2160.jpg" alt="Producte destacat" style="width:40%">
</p>
</a>
</section>
<article style="text-align:center;" class="Descripcio">
<p></p>Description</article>
</main>
<footer>
<p style="text-align:center;">
Avís Legal-Privadesa-Termes d'ús
</p>
<p style="text-align:center; font-size:75%;">
Logo vector created by freepik - www.freepik.com
</p>
</footer>
Right now my problems are that elements don't change their size proportionally to the resolutions of the screen, they decrease or increase their size at different speeds. Also, sometimes elements from a grid cell overlap with elements of another cell, like if I leave the header img at 100% it overlaps with the rest of the elements in the html.
JSFiddle: https://jsfiddle.net/915seaLc/
Reset the margin of the body, and I recommend reset the padding to
body {
margin: 0;
padding: 0;
}
This question already has answers here:
Stretch columns in two columns layout with shared header using flexbox
(2 answers)
Closed 4 years ago.
I have this layout, where a row wrap flex container has a first child with 100% width and 2 more children on the second row. The container has a fixed height and the first child (Filters block below) is collapsible (i.e. has 2 possibles values for height).
I would like the blocks on the last line to take all available height in all cases (filters block collapsed or expanded), but I can't find a solution.
I've tried various combinations of height, align-items/align-self: stretch, to no avail. Setting the pdt/list blocks height to 100% makes them effectively 100% of parent container, so they overflow due to the filters.
I know I could achieve it by making the first container flex column and throw in a second one with flex row,but I'd like to keep the current markup if possible. Any idea?
JSfiddle: https://jsfiddle.net/Lp4j6cfw/34/
HTML
<div id="lp-tag">
<div id="header">HEADER</div>
<div id="lp-ctnr">
<div id="filters" onclick="toggle()">FILTERS</div>
<div id="pdt">PDT</div>
<div id="list">LIST</div>
</div>
CSS
#lp-tag{
display: flex;
flex-flow: column nowrap;
width: 350px;
margin: auto;
border: 1px solid red;
height: 250px;
}
#header{
background: lightblue;
height: 80px;
}
#lp-ctnr{
display: flex;
flex-flow: row wrap;
justify-content: space-between;
align-content: flex-start;
align-items: stretch;
border: 1px solid green;
flex: 1;
}
#filters{
width: 100%;
background: lightgreen;
height: 45px;
}
.close{
height: 20px !important;
}
#pdt, #list {
flex: 1;
border: 1px solid blue;
text-align: center;
align-self: stretch;
}
#pdt{
background: yellow;
}
#list{
background: pink;
}
If you are open to alternative layout methods, I'd recommend CSS-Grid
.lp-tag {
width: 250px;
margin: 1em auto;
border: 1px solid red;
height: 250px;
display: inline-grid;
grid-template-rows: auto 1fr;
}
.header {
background: lightblue;
height: 80px;
}
.header.small {
height: 40px;
}
.lp-ctnr {
display: grid;
grid-template-rows: auto 1fr;
grid-template-columns: repeat(2, 1fr);
border: 1px solid green;
flex: 1;
}
.filters {
grid-column: 1 / span 2;
background: lightgreen;
height: 45px;
}
.filters.large {
height: 80px;
}
.pdt,
.list {
border: 1px solid blue;
text-align: center;
}
.pdt {
background: yellow;
}
.list {
background: pink;
}
<div class="lp-tag">
<div class="header">HEADER</div>
<div class="lp-ctnr">
<div class="filters" onclick="toggle()">FILTERS</div>
<div class="pdt">PDT</div>
<div class="list">LIST</div>
</div>
</div>
<div class="lp-tag">
<div class="header small">HEADER</div>
<div class="lp-ctnr">
<div class="filters large" onclick="toggle()">FILTERS</div>
<div class="pdt">PDT</div>
<div class="list">LIST</div>
</div>
</div>
This is the only solution I can see without an intermediary container. https://jsfiddle.net/5j38ouvs/
However, I would probably do like Nandita and add a surrounding container like here: https://jsfiddle.net/8md4oyLx/
CSS
#lp-ctnr{
display: flex;
flex-direction: column;
border: 1px solid green;
height: 550px;
width: 350px;
margin: auto;
}
#filters{
width: 100%;
background: lightgreen;
}
.close{
height: 20px !important;
}
#pdt{
flex-grow: 1;
background: yellow;
}
#list{
flex-grow: 1;
background: pink;
}
.list-container {
flex-grow: 1;
border: 1px solid blue;
text-align: center;
display: flex;
flex-direction: row;
}
HTML
<div id="lp-ctnr">
<div id="filters" onclick="toggle()">FILTERS</div>
<div class="list-container">
<div id="pdt">PDT</div>
<div id="list">LIST</div>
</div>
</div>
I have a flex item that is also a flex container .sub-con, problem is the flex item of .sub-con is refusing to wrap, even after adding : flex-flow: row wrap.
Can anyone fix this for me, or point out what I'm doing wrong.
.container {
display: flex;
justify-content: center;
align-items: center;
flex-flow: row wrap;
height: 100vh;
}
.sub-con {
margin-right: 50px;
margin-left: 50px;
height: 500px;
background-color: #fff;
display: flex;
justify-content: center;
flex-flow: row wrap;
}
.col-one {
height: 100px;
border: 1px solid lightgreen;
flex-grow: 2;
}
.col-two {
height: 100px;
border: 1px solid red;
flex-grow: 1;
}
<div class="container">
<div class="sub-con">
<div class="col-one"></div>
<div class="col-two"></div>
</div>
</div>
Your flex items in the nested container are sized with percentages.
.col-one{
width: 40%;
height: 100px;
border: 1px solid lightgreen;
}
.col-two{
width: 40%;
height: 100px;
border: 1px solid red;
}
Because percentage lengths are based on the length of the parent they have no reason to wrap. They will always be 40% of the parent, even if the parent has a width of 1%.
If you use other units for length, such as px or em, they will wrap.
jsFiddle demo
.container {
display: flex;
justify-content: center;
align-items: center;
flex-flow: row wrap;
height: 100vh;
}
.sub-con {
flex: 1; /* for demo only */
align-content: flex-start; /* for demo only */
margin-right: 50px;
margin-left: 50px;
height: 500px;
background-color: #fff;
display: flex;
justify-content: center;
flex-flow: row wrap;
}
.col-one {
width: 200px;
height: 100px;
border: 1px solid lightgreen;
}
.col-two {
width: 200px;
height: 100px;
border: 1px solid red;
}
<div class="container">
<div class="sub-con">
<div class="col-one"></div>
<div class="col-two"></div>
</div>
</div>