Align div to start at the center of row - html

My intention is this:
Sadly I cannot figure out how to align this the way I want to, other than with margin.
The issue is with the row with button.
I want to align the div with id="info" to start at the center of the row (id="row").
This is the current state and the code, below.
<div class="d-flex align-items-center" id="row">
<div class="radio-btn">
</div>
<div style="margin-left: 2em; margin-right: 2em; width: 50px;">
<img src='xy.png' style="max-width: 100%; max-height: 100%;">
</div>
<div id="info">
<div> SAMPLE TEXT </div>
<div><button>SELECT PICKUP</button></div>
</div>
</div>
Since I am trying to generate multiple lines like this in a for loop, I do not want to complicate things with if / else statements.
Is there a different way to achieve this, some kind of align option I am not aware of?
Or should I just use margin for the rows with buttons?

Here's the code for your question. You can add just a few css for this.
The screenshot of the result:
Check the full code below. I used SVG instead of img since it doesn't work in the current code.
#row {
display: flex;
flex-direction: row;
align-items: center;
}
#info {
position: relative;
}
#info>div:last-child {
position: absolute;
width: max-content;
margin-top: 5px;
}
<div class="d-flex align-items-center" id="row">
<div class="radio-btn">
</div>
<div style="margin-left: 2em; margin-right: 2em; width: 50px;">
<svg class="Icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" style="height: 50px; width: 50px;"><path d="M0 0h512v512H0z" fill="#000" fill-opacity="1"></path><g class="" transform="translate(0,0)" style=""><path d="M256 24.585L51.47 118.989 256 213.394l204.53-94.405zM38.998 133.054v258.353L247 487.415V229.063zm434.004 0L265 229.062v258.353l208.002-96.008z" fill="#fff" fill-opacity="1"></path></g></svg>
</div>
<div id="info">
<div> SAMPLE TEXT </div>
<div><button>SELECT PICKUP</button></div>
</div>
</div>

You could use grid. Make a grid as a "card" to insert in a container.
.container {
width: 500px;
}
.card {
display: grid;
grid-template-rows: 1fr 1fr;
grid-template-columns: 1fr 1fr 1fr;
gap: 0;
width: 100%;
height: 100%;
}
#div1 {
grid-area: 1 / 1 / 3 / 2;
align-self: center;
justify-self: center;
}
#div2 {
grid-area: 1 / 2 / 3 / 3;
align-self: center;
justify-self: center;
}
#div3 {
grid-area: 1 / 3 / 2 / 4;
align-self: center;
justify-self: center;
}
#div4 {
grid-area: 2 / 3 / 3 / 4;
align-self: center;
justify-self: center;
}
<div class="container">
<div class="card">
<div id="div1">
<input type="radio" id="radio" value="HTML">
</div>
<div id="div2">
<svg id="logo-84" width="40" height="28" viewBox="0 0 40 28" fill="none" xmlns="http://www.w3.org/2000/svg"><path class="ccustom" fill-rule="evenodd" clip-rule="evenodd" d="M9.98578 4.11462L0 14C1.99734 15.9773 4.27899 17.6437 6.76664 18.9474C7.45424 20.753 8.53203 22.4463 10 23.8995C15.5229 29.3668 24.4772 29.3668 30 23.8995C31.468 22.4463 32.5458 20.753 33.2334 18.9473C35.721 17.6437 38.0027 15.9773 40 14L30.0223 4.12266C30.0149 4.11527 30.0075 4.10788 30 4.1005C24.4772 -1.36683 15.5229 -1.36683 10 4.1005C9.99527 4.10521 9.99052 4.10991 9.98578 4.11462ZM29.0445 20.7309C26.1345 21.7031 23.0797 22.201 20 22.201C16.9203 22.201 13.8656 21.7031 10.9556 20.7309C11.2709 21.145 11.619 21.5424 12 21.9196C16.4183 26.2935 23.5817 26.2935 28 21.9196C28.381 21.5424 28.7292 21.145 29.0445 20.7309ZM12.2051 5.8824C12.9554 6.37311 13.7532 6.79302 14.588 7.13536C16.3038 7.83892 18.1428 8.20104 20 8.20104C21.8572 8.20104 23.6962 7.83892 25.412 7.13536C26.2468 6.79302 27.0446 6.3731 27.795 5.88238C23.4318 1.77253 16.5682 1.77254 12.2051 5.8824Z" fill="#3B4158"></path></svg>
</div>
<div id="div3">
SAMPLE TEXT
</div>
<div id="div4">
<button>SELECT PICKUP</button>
</div>
</div>
</div>
Here the grid has 2 rows and 3 cols.
First div is taking the 2 rows and 1rst col. Second div is taking 2 rows and 2 col. 3rd div taking first row on 3rd col. 4th div 2nd row 3rd col.
Each div is align-self and justify-self center, in case you prefer to change positioning on a div. If you want like this, you can align and justify directly in card grid definition.
You can change names, and put divs from id to class if needed.

Related

Grid items wont center align despite using align and justify items

I created a grid using 6 images and structured it the way how i want it. The only issue im having with my grid is that I cannot center it. It's leaning too far to the left and have a huge gap on the right. I tried using align items: Center and justify content: center but my grid does not move at all. If i were to use padding it will move but it does not translate well when i look at my project from a different device. What can i do to fix this issue? Thanks.
.PophikeMargin{
margin-top: 50px;
margin-bottom: 50px;
text-align: center;
}
.PophikeMargin h1{
margin: 30px;
}
.Hiking-grid-container{
display: grid;
grid-template-columns: repeat(9, 1fr);
grid-template-rows: repeat(4, 1fr);
grid-column-gap: 10px;
grid-row-gap: 10px;
justify-content: center
}
.Hiking-item1 {
grid-area: 1 / 1 / 3 / 2;
height: 400px;
width: 350px;
}
.Hiking-item2 {
grid-area: 3 / 1 / 5 / 2;
height: 400px;
width: 350px;
}
.Hiking-item3 {
grid-area: 1 / 2 / 6 / 4;
height: 800px;
width: 700px;
}
.Hiking-item4 {
grid-area: 1 / 4 / 3 / 6;
height: 400px;
width: 705px;
}
.Hiking-item5 {
grid-area: 3 / 4 / 5 / 5;
height: 400px;
width: 350px;
}
.Hiking-item6 {
grid-area: 3 / 5 / 5 / 6;
height: 400px;
width: 350px;
}
<section class="Popular-Hiking-Trails">
<div class="PophikeMargin">
<h1>Popular Hiking Destinations</h1>
<div class="Hiking-grid-container">
<div class="Hiking-item1">
<img src="Images/****" width="350" height="400">
</div>
<div class="Hiking-item2">
<img src="Images/****" width="350" height="400">
</div>
<div class="Hiking-item3">
<img src="Images/****" width="350" height="400">
</div>
<div class="Hiking-item4">
<img src="Images/****" width="350" height="400">
</div>
<div class="Hiking-item5">
<img src="Images/****" width="350" height="400">
</div>
<div class="Hiking-item6">
<img src="Images/****" width="350" height="400">
</div>
</div>
</div>
</section>
Several points in your code:
you defined a 9 cols and 4 rows grid
you position the item inside with their row-start, col-start, row-end, col-end but not with the good values. If you want to design grid with grid-area check here: http://cssgridbuilder.com/
if you want to center the element use ...-self for the element to center
Here a snippet with 2 cols, 2 rows and images inside with grid-area. If you need dynamic numbering of images, it's more an approach with span to take.
.PophikeMargin {
margin-top: 50px;
margin-bottom: 50px;
text-align: center;
}
.PophikeMargin h1 {
margin: 30px;
}
.Hiking-grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(2, 1fr);
gap: 10px;
}
.Hiking-item1 {
grid-area: 1 / 1 / 2 / 2;
}
.Hiking-item2 {
grid-area: 1 / 2 / 2 / 3;
}
.Hiking-item3 {
grid-area: 1 / 3 / 2 / 4;
}
.Hiking-item4 {
grid-area: 2 / 1 / 3 / 2;
}
.Hiking-item5 {
grid-area: 2 / 2 / 3 / 3;
}
.Hiking-item6 {
grid-area: 2 / 3 / 3 / 4;
}
.Hiking-grid-container>div {
height: auto;
width: 33%;
justify-self: center;
align-self: center;
}
<section class="Popular-Hiking-Trails">
<div class="PophikeMargin">
<h1>Popular Hiking Destinations</h1>
<div class="Hiking-grid-container">
<div class="Hiking-item1">
<img src="https://picsum.photos/id/237/350/400">
</div>
<div class="Hiking-item2">
<img src="https://picsum.photos/id/184/350/400">
</div>
<div class="Hiking-item3">
<img src="https://picsum.photos/id/125/350/400">
</div>
<div class="Hiking-item4">
<img src="https://picsum.photos/id/85/350/400">
</div>
<div class="Hiking-item5">
<img src="https://picsum.photos/id/51/350/400">
</div>
<div class="Hiking-item6">
<img src="https://picsum.photos/id/100/350/400">
</div>
</div>
</div>
</section>

Get an element (img/svg/canvas) to not affect the height calculation of a flexbox and then vertically fill the result [duplicate]

This question already has answers here:
How can you set the height of an outer div to always be equal to a particular inner div?
(2 answers)
Closed 7 months ago.
I want to make an img/canvas/svg fit in a flexbox without effecting the flexbox's size. AND, I don't want to have to specify the size of the flexbox nor the image/svg/canvas (because I don't know it)
In other words, here's 3 flexboxes each with 2 children, the child on the right is an image/canvas/svg
And here's the code for that.
.outer,
.outer2 {
display: flex;
background-color: orange;
margin-bottom: 10px;
align-items: stretch;
}
.outer>*,
.outer2>* {
flex: 1 1 auto;
max-height: 100%;
overflow: auto;
}
img,
svg,
canvas {
display: block;
background-color: purple;
max-height: 100%;
height: calc(1em + 2.5px); /* had to guess this. It's brittle */
}
<div class="outer">
<div>An Image</div><img src="https://via.placeholder.com/300x150">
</div>
<div class="outer">
<div>A Canvas</div><canvas></canvas>
</div>
<div class="outer">
<div>An SVG</div><svg viewBox="0 0 300 150"><circle cx="150" cy="75" r="48" fill="red"/></svg>
</div>
That's what I want except without having to specify the heights.
Notice I had to specify height: calc(1em + 2.5px). That height is only a guess. It might be too small or to big depending on other settings. For example if I expand the size of the first child.
To put it another way, I only want to the first child to affect the size of the flexbox. The second child I just want to stretch or shrink to fit the height of the parent.
Is it possible?
Here's one try but it fails. Though it's interesting to me that surrounding the img/canvas/svg by an extra div makes them smaller, it doesn't make them small enough.
.outer,
.outer2 {
display: flex;
background-color: orange;
margin-bottom: 10px;
align-items: stretch;
}
.outer>*,
.outer2>* {
flex: 1 1 auto;
max-height: 100%;
overflow: auto;
}
img,
svg,
canvas {
display: block;
background-color: purple;
max-height: 100%;
object-fit: contain;
}
<div class="outer">
<div>An Image</div><img src="https://via.placeholder.com/300x150">
</div>
<div class="outer">
<div>A Canvas</div><canvas></canvas>
</div>
<div class="outer">
<div>An SVG</div><svg viewBox="0 0 100 100"><circle cx="50" cy="50" r="48" fill="red"/></svg>
</div>
<div class="outer2">
<div>An Image</div><div><img src="https://via.placeholder.com/300x150"></div>
</div>
<div class="outer2">
<div>A Canvas</div><div><canvas></canvas></div>
</div>
<div class="outer2">
<div>An SVG</div><div><svg viewBox="0 0 100 100"><circle cx="50" cy="50" r="48" fill="red"/></svg></div>
</div>
If changing to a grid is acceptable here is a snippet which wraps the img etc in a div. That div is positioned relative and the img etc within it is positioned absolute, so it doesn't influence the height of the grandparent/parent.
In this way the height is determined by the first child of outer.
.outer {
display: grid;
grid-template-columns: 1fr 1fr;
background-color: orange;
margin-bottom: 10px;
}
.outer :nth-child(2) {
position: relative;
}
img,
svg,
canvas {
display: block;
background-color: purple;
object-fit: contain;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
<div class="outer">
<div>An Image</div>
<div><img src="https://via.placeholder.com/300x150"></div>
</div>
<div class="outer">
<div>A Canvas</div>
<div><canvas></canvas></div>
</div>
<div class="outer">
<div>An SVG</div>
<div><svg viewBox="0 0 100 100"><circle cx="50" cy="50" r="48" fill="red"/></svg></div>
</div>

Design a layout with one div on 2 columns and 2 rows at the left, and 4 divs on 2 columns and 2 rows at the right, similar to BBC website

I want to design a hero section similar to BBC website. I started working on this using CSS Grid which I thought could get the same design with minimal code.
I have managed to base design but I want item1 to take 50% of the container's width and rest of the items to take 25% of space each so that it looks like the image above. I can span rows but I am not sure how I can span columns correctly, I tried this :
.item1 {
grid-column-end: span 2;
}
But it broke the design and same did happen with:
.item1 {
grid-column: auto / span 2;
}
My code:
.grid-container {
display: grid;
grid-template-columns: auto auto auto;
grid-gap: 10px;
background-color: #2196F3;
padding: 0px;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
padding: 0px 0;
font-size: 30px;
}
.item1 {
grid-row-end: span 2;
}
.grid-container div img {width:100%; max-width:600px;}
<div class="grid-container">
<div class="item1">
<img src="https://dummyimage.com/600x400/f09c00/fafafa&text=1"/>
</div>
<div class="item2">
<img src="https://dummyimage.com/600x400/0010f0/fafafa&text=2"/>
</div>
<div class="item3">
<img src="https://dummyimage.com/600x400/5310f0/fafafa&text=3"/>
</div>
<div class="item4">
<img src="https://dummyimage.com/600x400/0010f0/fafafa&text=4"/>
</div>
<div class="item5">
<img src="https://dummyimage.com/600x400/0010f0/fafafa&text=5"/>
</div>
</div>
You could do it as below. I simplified the code by removing irrelevant code for the desired layout and added comments.
.grid-container {
background-color: #2196f3;
/* with these 3 lines below, I'm creating a grid of 4 columns and 2 rows*/
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: auto auto;
grid-gap: 10px;
}
.grid-container > div:nth-of-type(1){ /* I grab the first div */
grid-column: 1 / 3; /* I tell him to take 2 columns, 1 -> 3 with 3 excluded */
grid-row: 1 / 3; /* I tell him to take 2 rows, 1 -> 3 with 3 excluded */
}
.grid-container div img {
width: 100%;
height:100%;
object-fit:cover;
}
<div class="grid-container">
<div class="item1">
<img src="https://dummyimage.com/600x400/f09c00/fafafa&text=1" />
</div>
<div class="item2">
<img src="https://dummyimage.com/600x400/0010f0/fafafa&text=2" />
</div>
<div class="item3">
<img src="https://dummyimage.com/600x400/5310f0/fafafa&text=3" />
</div>
<div class="item4">
<img src="https://dummyimage.com/600x400/0010f0/fafafa&text=4" />
</div>
<div class="item5">
<img src="https://dummyimage.com/600x400/0010f0/fafafa&text=5" />
</div>
</div>

Grid table jumps out of the main body

I am new CSS, HTML if this is dumb question sorry.
I am trying to make a list of items using grid by providing image and text next to it. After finally doing so my grid jumped out of my main part.
Here is the example of what my code looks like:
.container {
display: grid;
grid-template-columns: 1fr 1fr;
max-width: 80vw;
max-height: 50vh;
background-color: red;
grid-row-gap: 3%;
}
.box-gap {
grid-column-start: 1;
grid-column-end: 3;
border: black solid;
}
.box-image {
object-fit: cover;
flex: 1 1 0px;
width: 100%
}
.box-text {
flex: 1 1 0px;
width: 100%
}
<div class="container">
<!-- Item 1 -->
<div class="box-gap"></div>
<image src="https://cdn.pixabay.com/photo/2014/10/26/14/36/light-bulb-503881_1280.jpg" class="box-image"></image>
<div class="box-text">
Text 1
</div>
<!-- Item 2 -->
<div class="box-gap"></div>
<image src="https://cdn.pixabay.com/photo/2014/10/26/14/36/light-bulb-503881_1280.jpg" class="box-image"></image>
<div class="box-text">
Text 2
</div>
</div>
https://jsfiddle.net/9jL4tw7m/9/
I want my container to cover whole grid, right know red container do not fit grid table. I managed to solve this by adding overflow: auto, but it looks ugly with two scrollbars.

Make child flex measure the same height and width different row

I have 4 items inside a flex container. I want to make the 4 of them the same width and height, but always having them in 2 different rows (so a 2 X 2 grid).
Since the children are in different flex containers, they do not obey flex-grow: 1; Even the children of the same row do not obey the rule. And if I put them in the same container, they put themselves in the same row, and I need the 2 X 2 grid.
You can find a codepen here with the same code: https://codepen.io/mongolhippie/pen/yLYQbVd?editors=1100
.tile {
display: flex;
flex-direction: column;
border: 2px solid #A97C50;
border-radius: 20px;
padding: 20px;
margin: 25px;
/* THESE 3 OPTIONS TO CONTROLL HAVING THE SAME SPACE FOR EVERY TILE */
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0;
flex: 1;
}
.tile p {
font-size: min(calc( 1.125vw + 1.2rem ), 3.9rem);
}
.flex-center{
display: flex !important;
justify-content: center !important;
align-items: center;
}
.manual-link {
text-decoration: none !important;
color: var(--brown-dark);
}
.manual-link:hover {
text-decoration: none !important;
color: var(--brown-dark);
}
.icon-and-title{
display: flex;
align-items: center;
}
.icon-and-title p{
font-size: min(calc( 1.125vw + .9rem ), 3.9rem);
}
.icon-and-title img{
max-width: 100%;
height: auto;
transition: all .3s;
max-height: 80px;
margin-right: 20px;
margin-bottom: 10px;
}
<div class="flex-center">
<a class="manual-link" href="/manuals/manual-it.pdf">
<div class="tile">
<div class="icon-and-title">
<img src="https://i.pinimg.com/originals/06/bc/e8/06bce81285badba0c3becd273ca67f95.png" alt="">
<p>ADMIN</p>
</div>
<div class="links-manuals">
<p>For the administrator of the app</p>
</div>
</div>
</a>
<a class="manual-link" href="/manuals/manual-developers.pdf">
<div class="tile">
<div class="icon-and-title">
<img src="https://i.pinimg.com/originals/06/bc/e8/06bce81285badba0c3becd273ca67f95.png" alt="">
<p>DEVELOPERS</p>
</div>
<div class="links-manuals">
<p>To upgrade the Code</p>
</div>
</div>
</a>
</div>
<div class="flex-center">
<a class="manual-link" href="/manuals/manual-design.pdf">
<div class="tile">
<div class="icon-and-title">
<img src="https://i.pinimg.com/originals/06/bc/e8/06bce81285badba0c3becd273ca67f95.png" alt="">
<p>DESIGNERS</p>
</div>
<div class="links-manuals">
<p>Style guide</p>
</div>
</div>
</a>
<a class="manual-link" href="/manuals/ngo.pdf">
<div class="tile">
<div class="icon-and-title">
<img src="https://i.pinimg.com/originals/06/bc/e8/06bce81285badba0c3becd273ca67f95.png" alt="">
<p>NGOs</p>
</div>
<div class="links-manuals">
<p>The project</p>
</div>
</div>
</a>
</div>
Which is the secret to having them in a 2 X 2 disposition with the same width and height??
Thanks a lot!
Simply move from Flexbox to CSS GRID.
So change the CSS like this:
.flex-center{
display: grid;
grid-template-columns: 1fr 1fr;
}
Since you are handling your layout in 2 different rows we'll play with the grid-template-columns.
The fr unit tells the grid to have to "cells" with the same width.
Better renaming the class from .flex-center to something else since you'll use CSS GRID.
Here the updated working Codepen
Flebox are very good to make a 1D flexible container (vertically or horizontally) and can also handle multi-lines when they is no more space for all elements (instead of overflow the container)
BUT They is another display type used mainly for 2D flexible grid. And it's called grid, you should look at this guid in order to use it right.
As you want by default a 2D (2x2) grid, it'll be way easier to use it, This is a very basic grid you can set in order to have both columns and rows to take half of available space (50%)
.container {
display: grid;
grid-template-columns: 50% 50%;
grid-template-rows: 50% 50%;
}