Unable to align buttons with CSS flex or grid - html

I'm trying to create a page where I have 2 lists of buttons, each one in a div, side by side. I tried a CSS grid and flex, as you can see below. However, nothing seems to work, as the first div went all the way to the left margin and the other one under it.
How do I add some space to the left margin for the first column and from the half of the page for the second one?
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-gap: 20px;
}
button {
width: 120px;
height: 60px;
text-align: center;
text-decoration: none;
display: flex;
flex-direction: column;
font-size: 16px;
font color: black;
margin: 4px 2px;
transition-duration: 0.4s;
cursor: pointer;
background-color: white;
color: black;
border: 2px solid #CC0000;
}
<div class="grid-container">
<div class="grid-child">
<a href="createCl.php"> <button style="color: #000000; text-decoration: none;">
<strong>Aggiungi</strong></button></a>Aggiungi</span> <br><br>
</div>
<div class="grid-child">
<a href="map.php"> <button style="color: #000000; text-decoration: none;">
<strong>Mappa</strong></button></a>Mappa Clienti</span>
</div>
</div><br>

I'm a big fan of using flex. So if you're not locked in on using grid, here's a solution:
https://codepen.io/oppo_oskar/pen/yLVJQeL
HTML:
<div class="grid-container">
<div class="left buttons">
<button>Left1</button>
<button>Left2</button>
<button>Left3</button>
<button>Left4</button>
</div>
<div class="right buttons">
<button>Right1</button>
<button>Right2</button>
<button>Right3</button>
<button>Right4</button>
</div>
</div>
CSS:
.grid-container
{
display: flex;
flex-direction: row;
}
.buttons {
display:flex;
flex-direction: column;
}
For quick reading on how flex works, you can check out this site:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/

All your buttons must be inside a container, for example: <div class="grid-container">.
In CSS, grid-template-columns: ; defines how many columns you will have in your .grid-container.
For every 1fr that you type into grid-template-columns: ; a new column will be created.
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 20px;
}
<div class="grid-container">
<button>Button1</button>
<button>Button2</button>
<button>Button3</button>
<button>Button4</button>
<button>Button5</button>
<button>Button6</button>
</div>
I hope it helped a bit, please let me know. ^^'

Related

CSS Grid Styling

I am trying to understand how CSS grids work. I've tried to make an example of a store item as practice, but I am at a loss.
Here's my how my CSS currently looks. Cut off at the top, weird spacing, and the right side is not coming together at all.
How's how it would ideally look
Here is my current CSS, I hope someone can help explain where I am misunderstanding the use of
CSS grids.
.store-currency {
height: 3vh;
}
.item {
display: flex;
align-items: center;
grid-row: 1 / span 2;
}
.currency {
display: flex;
align-items: center;
}
#num-bought-item0 {
display: flex;
align-items: center;
justify-content: right;
margin-right: 10px;
grid-column: 1 / span 2;
}
.store-item {
height: 15vh;
width: 100%;
display: grid;
grid-template-columns: 2fr;
grid-template-rows: 1fr 1fr;
font-size: 24px;
color: white;
border: 5px white solid;
justify-content: left;
align-items: center;
}
.store-item img {
margin: 10px;
height: 8vh;
}
.store-container {
display: flex;
height: 100%;
width: 30vw;
z-index: 0;
background-color: saddlebrown;
left: 0;
position: absolute;
text-align: center;
}
HTML:
<div class="store-container">
<div class="store-item" id="item0">
<div class ="item">
<img src="dumbell.png" alt="">
<span>Dumbbell</span>
</div>
<div id="num-bought-item0">
<span>Owned</span>
<span id="count-item0">0</span>
</div>
<div class="currency">
<img class="store-currency" src="coin.png" alt="">
<span>100000</span>
</div>
</div>
you did the first steps.
To get started you have to define a container element as a grid with display: grid, set the column and row sizes with grid-template-columns and grid-template-rows, and then place its child elements into the grid with grid-column and grid-row.
.store-container {
display: grid | inline-grid;
}
grid – generates a block-level grid
inline-grid – generates an inline-level grid
With grid-template-columns you can define how many columns will appear in your layout.
P.S Fr unit is a fractional unit and 1fr is for 1 part of the available space. In this example each column would take ~ 25% from the available space.
.container {
grid-template-columns: 1fr 1fr 1fr 1fr;
}
For your task, you can use grid-template-areas feature.
The grid-template-areas CSS property specifies named grid areas,
establishing the cells in the grid and assigning them names.
For example:
.item-a {
grid-area: header;
}
.item-b {
grid-area: main;
}
.item-c {
grid-area: sidebar;
}
.item-d {
grid-area: footer;
}
.container {
display: grid;
grid-template-columns: 50px 50px 50px 50px;
grid-template-rows: auto;
grid-template-areas:
"header header header header"
"main main . sidebar"
"footer footer footer footer";
}
This will generates something like that in modern browsers:
If you need more examples, take a look here:
https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-areas
https://css-tricks.com/snippets/css/complete-guide-grid/
Some of the examples are taken from the second site.
It looks like you are mixing flex and grid properties. grid-row and grid-column are only avalaible for a grid display (2D), not a flex display (1D).
You can try to play around with flex (worse choice since it is drawing a 1D layout) , you can use grid , which is made for this kind of layout.
Here a couple example with flex and grid
/* GRID make it simple*/
.grid {display:grid;}
#num-bought-item2 {grid-row:1/3;grid-column:2;}
#num-bought-item2 {display:grid;margin:auto;text-align:center}
/* layout done */
/* some reset for the demo*/
*{box-sizing:border-box;}
.store-container {display:grid;justify-content:center;}
.store-item {border:solid;}
.store-item>div {padding:0.5em;}
img{vertical-align:middle;}
[src="https://dummyimage.com/25/ff0"]{border-radius:50%}
big{color:darkgreen;background:lightyellow;}
/* FLEX make it a mess */
.flex {display:flex}
.column {flex-flow:column wrap;height:120px;}/* here an height is to be set so it wraps*/
/* since it is not made for this, we need to mess around */
.flex #num-bought-item1{order:2}/* reorder item */
.flex .item {height:0;min-height:60%;}/* hide it, then show it */
.flex .currency {height:0;min-height:40%;}/* hide it, then show it */
.flex #num-bought-item1{display:flex;flex-direction:column;justify-content:center;text-align:center;margin:auto;}
/* and flex did not do it */
<p>Let's try via flex</p>
<div class="store-container">
<div class="store-item flex column" id="item1">
<div class="item">
<img src="https://dummyimage.com/50" alt="">
<span>Dumbbell</span>
</div>
<div id="num-bought-item1" >
<span>Owned</span>
<span id="count-item1">0</span>
</div>
<div class="currency">
<img class="store-currency" src="https://dummyimage.com/25/ff0" alt="">
<span>100000</span>
</div>
</div>
</div>
<p>And via <big>grid</big> </p>
<div class="store-container">
<div class="store-item grid" id="item2">
<div class="item">
<img src="https://dummyimage.com/50" alt="">
<span>Dumbbell</span>
</div>
<div id="num-bought-item2" >
<span>Owned</span>
<span id="count-item1">0</span>
</div>
<div class="currency">
<img class="store-currency" src="https://dummyimage.com/25/ff0" alt="">
<span>100000</span>
</div>
</div>
</div>

Vertically center text in a grid layout with grid and items 100% height [duplicate]

This question already has answers here:
How to vertically align text inside a flexbox?
(13 answers)
Closed last year.
Here's an SO question about exactly this, and I've found several others. Many of them have a dozen answers, using many (many!) combinations of align and justify styles, transforms, margins, references to table cells, etc.
I've tried maybe 200 combinations of ideas from references. Is it possible to center the text vertically? Is it possible to do it while maintaining 100% height without adding divs around or inside the boxes?
My main finding after all this is that height: 100% thwarts every other style that can succeed. For example, margin-top and margin-bottom set to auto works, but not with 100% height.
section {
height: 400px;
}
#boxes {
background-color: green;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
gap: 6px;
height: 100%;
min-height: 10em;
min-width: 12em;
}
#boxes>div {
background-color: #8ca0ff;
text-align: center;
border: 1px solid red;
height: 100%;
}
<section>
<div id="boxes">
<div id="00">I'm not</div>
<div id="01">vertically</div>
<div id="02">centered.</div>
<div id="10">I</div>
<div id="11">am not</div>
<div id="12">either!</div>
<div id="20">Was CSS designed</div>
<div id="21">by a psychopath?</div>
<div id="22">Seems like it!</div>
</div>
</section>
I am astonished at how non-simple this simple-seeming thing is.
Put flex on the boxes, just like in an answer on the post you linked.
https://css-tricks.com/snippets/css/a-guide-to-flexbox
section {
height: 400px;
}
#boxes {
background-color: green;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(3, 1fr);
gap: 6px;
height: 100%;
min-height: 10em;
min-width: 12em;
}
#boxes>div {
background-color: #8ca0ff;
text-align: center;
border: 1px solid red;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
}
<section>
<div id="boxes">
<div id="00">I'm not</div>
<div id="01">vertically</div>
<div id="02">centered.</div>
<div id="10">I</div>
<div id="11">am not</div>
<div id="12">either!</div>
<div id="20">Was CSS designed</div>
<div id="21">by a psychopath?</div>
<div id="22">Seems like it!</div>
</div>
</section>

How to left-align items with each other in a grid that used justify-items: center

I am using grid. I centered my items but i want left position to be same on cross axis.
.box {
display: grid;
grid-template-columns: 1fr;
padding: 10px;
border: 1px solid blue;
gap: 1rem;
justify-items: center;
}
img {
width: 200px;
}
<div class="box">
<img src="img/featured.jpg">
<div class="item2">Item 2 Lorem</div>
</div>
Note : i want solution in only grid. There is many temporary fixes for that but i don't want that because i am looking for a perfect standard grid alignment solution for it.
You can do it a few ways:
1. Fixed Width Text (Not Responsive)
Based on the information you gave in your question. you can simply set the width of the item2 div to be the same as the image, e.g.:
.box {
display: grid;
grid-template-columns: 1fr;
padding: 10px;
border: 1px solid blue;
gap: 1rem;
justify-items: center;
}
.item2,
img {
width: 200px;
}
<div class="box">
<img src="https://via.placeholder.com/200x150">
<div class="item2">Item 2 Lorem</div>
</div>
2. The Responsive Way
This will allow us to responsively set up the 2 separate alignments you require: centre the container element, and left-align its contents, e.g.
<div class="box">
<div class="boxcontent">
Content here...
</div>
</div>
CSS: .boxcontent { text-align: left; }
Why this works:
You must have a defined relationship between the image and text, otherwise there is no way to tell the grid that these 2 individual elements must be positioned in relation to each other. You can do this by putting them in a container.
If you think about it, you are trying to do 2 separate alignments here:
make the image and text be centred in relation to the grid but also
make them be in alignment with each other so they are left aligned.
Putting them in a container achieves both of these objectives by creating a relationship between the image and text so that:
they act as a single unit in relation to the grid and also
allow them to be positioned in relation to each other regardless of the grid
Working Example:
.box {
display: grid;
grid-template-columns: 1fr;
padding: 10px;
border: 1px solid blue;
gap: 1rem;
justify-items: center;
}
.boxcontent {
text-align: left;
}
img {
width: 200px;
}
<div class="box">
<div class="boxcontent">
<img src="https://via.placeholder.com/200x150">
<div class="item2">Item 2 Lorem</div>
</div>
</div>
.box {
display: block;
margin: 0 auto;
width: max-content;
padding: 10px;
border: 1px solid blue;
}
img {
width: 200px;
}
.item2 {
margin-top: 1rem;
}
<div class="box">
<img src="pic_trulli.jpg" alt="Hi dear">
<div class="item2">Item 2 Lorem</div>
</div>

How to make divs not move during zooming in grid layout

I have the problem during zooming in browser. I split "details__view" to two grid columns but during zooming these columns do not keep their position and width. I tried to use position: absolute, and position:relative but then the division into columns completely disappears. Here is my html:
<div className="box__details">
<div className="details__container">
<div className="details__container--title">Details</div>
<div className="details__view">
<div className="label">Name</div>
<div className="value">XYZ</div>
</div>
<div className="details__view">
<div className="label">Second Name</div>
<div className="value">XYZ</div>
</div>
<div className="details__view">
<div className="label">Country</div>
<div className="value">XYZ</div>
</div>
</div>
</div>
and CSS:
.box__details {
display: grid;
grid-template-columns: 1fr 1fr;
column-gap: 30px;
}
.details__container {
border: 1px solid #eee;
padding: 10px;
border: 1px solid #eee;
}
.details__container--title {
padding-bottom: 15px;
}
.details__view {
display: grid;
grid-template-columns: .4fr .4fr;
padding-bottom: 15px;
}
.label {
padding-left: 5px;
}
.value {
text-align: left;
}
Do you have any ideas how can I make labels and values ​​do not change during zooming? Thank you in advance for help!
The columns do not keep their position because you're using fr. This makes the box stay in the place you put it in on the screens proportion thus moving the right a little left when zooming in (Kinda like using %). To make it stay in place you may have to use something like px;
By changing grid-template-columns: 1fr 1fr; to something like grid-template-columns: 427px 427px;

How to perfectly center grid-items while using CSS Grid auto-fit property? [duplicate]

This question already has answers here:
Aligning grid items across the entire row/column (like flex items can)
(3 answers)
Closed 4 years ago.
I have this Grid:
You can see the full code in Codepen
The problem is that, due to auto-fit property, when changing screen size, some columns go down. I need that even if new rows appear, all the grid items should be centered in the grid container.
This is the scenario:
body {margin: 0}
.about-us__block {
width: 150px;
height: 200px;
background: yellow;
}
.about-us__container {
background: black;
padding: 20px 0;
display: grid;
justify-items: center;
align-items: center;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
grid-gap: 10px;
}
.about-us__text {
padding: 5px;
text-align: center;
background-color: #35306B;
color: #ebecec;
}
.about-us__text h6 {
font-size: 1.5rem;
font-weight: 600;
margin: 0;
}
.about-us__text p {
font-family: Helvetica;
margin: 0;
}
.about-us__item:nth-of-type(4) {
grid-column: 2;
}
<div class="about-us__container">
<div class="about-us__item">
<div class="about-us__block"></div>
<div class="about-us__text">
<h6>John Doe</h6>
<p>BlaBla</p>
</div>
</div>
<div class="about-us__item">
<div class="about-us__block"></div>
<div class="about-us__text">
<h6>John Doe</h6>
<p>BlaBla</p>
</div>
</div>
<div class="about-us__item">
<div class="about-us__block"></div>
<div class="about-us__text">
<h6>John Doe</h6>
<p>BlaBla</p>
</div>
</div>
<div class="about-us__item">
<div class="about-us__block"></div>
<div class="about-us__text">
<h6>John Doe</h6>
<p>BlaBla</p>
</div>
</div>
</div>
I'm trying a lot of solutions, but none of them works.
I tried forcing the fourth grid-item to place in the 2nd column, but this occurs:
I had this same layout on my project.
On the container try:
display: flex;
flex-wrap: wrap;
justify-content: center;