Align div at bottom of flex - html

I have this card:
.do_card {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
transition: 0.3s;
width: 220px;
height: 320px;
}
.do_container {
padding: 2px 16px;
}
<div class="do_card">
<img src="https://picsum.photos/200/300" style="width:220px;height:150px;margin-left:auto;margin-right:auto;display:block;">
<div class="do_container">
<p style="text-align: center;">Lorem Ipsum</p>
<div style="display: flex; justify-content: space-between;">
<p><i class="test"></i> GET IT
<p>Like</p>
</div>
</div>
</div>
How can I get the "GET IT" and "Like" to always be at the bottom of the card?
Setting the do_card to position: relative; and the div with "Get it" and "Like"
to position: absoulute; bottom:0px; does not work

To make sure the content set to absolute remains within the desired element (card in your case), make sure to set its parent to position: relative; (.do_card in your case)
It does seem to work for me as mentioned in the comment:
<style>
.do_card {
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
transition: 0.3s;
width: 220px;
height: 320px;
position: relative;
}
.do_container {
padding: 2px 16px;
}
</style>
<div class="do_card">
<img src="https://picsum.photos/200/300" style="width:220px;height:150px;margin-left:auto;margin-right:auto;display:block;">
<div class="do_container">
<p style="text-align: center;">Lorem Ipsum</p>
<div style="display: flex; justify-content: space-between; position: absolute; bottom: 0; width: 100%; box-sizing: border-box; padding: 0 16px; left: 0;">
<p><i class="test"></i> GET IT
<p>Like</p>
</div>
</div>
</div>

I would prefer a "non-absolute" solution whenever possible. In fact, you're able to achieve the layout with flexbox. Here's how:
First, you need to put your content inside the card into 2 blocks (top and bottom).
Then, you can use flex-direction: column; and justify-content: space-between; on the card.
.do_card {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
transition: 0.3s;
width: 220px;
height: 320px;
/* vertical stretched alignment */
display: flex;
flex-direction: column;
justify-content: space-between;
}
.do_card img {
max-width: 100%;
}
.do_container {
padding: 2px 16px;
}
.do_container_bottom {
display: flex;
justify-content: space-between;
align-items: center;
}
<div class="do_card">
<!-- top -->
<div class="do_container">
<img src="https://picsum.photos/200/300" style="width:220px;height:150px;margin-left:auto;margin-right:auto;display:block;">
<p style="text-align: center;">Lorem Ipsum</p>
</div>
<!-- bottom -->
<div class="do_container_bottom">
<i class="test"></i> GET IT
<p>Like</p>
</div>
</div>

if you want button "GET IT" and "Like" to always be at the bottom of the card, you can set do_container to flex_grow: 1 to make it take as much space as possible and set the element inside with justify-content: space-between
.do_card {
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
transition: 0.3s;
width: 220px;
height: 320px;
/* add flex */
display:flex;
flex-direction: column;
}
img {
width:220px;
height:150px;
margin-left:auto;
margin-right:auto;
display:block;
}
.do_container {
padding: 2px 16px;
/* add flex-grow to make it fill the height of the container */
flex-grow: 1;
display: flex;
flex-direction: column;
/* make it space-between to set get_it and like button on bottom of card */
justify-content: space-between;
}
.title {
text-align: center;
}
.action_container {
display: flex;
justify-content: space-between;
}
p {
color:black
}
<div class="do_card">
<img src="https://picsum.photos/200/300">
<div class="do_container">
<p class="title">Lorem Ipsum</p>
<div class="action_container">
<p><i class="test"></i> GET IT
<p>Like</p>
</div>
</div>
</div>

Related

Add a box to the bottom right of the screen using CSS

I need to display a list of cards with maximize, minimize and close button to the bottom right of the screen. The cards should be displayed from bottom rigght to left. I am having trouble displaying to the bottom
Code that I tried
.card {
padding: 24px;
box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.13);
background: skyblue;
border-radius: 4px;
font-family: "Helvetica", sans-serif;
display: flex;
flex-direction: column;
height: 280px;
width: 160px;
}
.card-body {
display: flex;
flex: 1 0 auto;
align-items: flex-end;
justify-content: center;
}
.card-actions {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 16px;
}
<div class="card">
<div class="card-actions">
Close
</div>
<div class="card-body">
Card content
</div>
</div>
* {
box-sizing: border-box;
/* this is necessary, so we can't have the scrollbar appearing after setting the height of body*/
}
body {
margin: 0;
/* deleting the default margin in body (that create the bug of scrollbar appearing) */
}
.cards-container {
display: flex;
/* making element one near the other */
height: 100vh;
/* setting the height of div to device_height (so we can put them in the end of the page) */
align-items: flex-end;
/* aligning the cards to the end of the page */
justify-content: flex-end;
/* aligning the cards to the end of the page */
padding: 1rem;
/* adding some padding to the end of the page */
gap: 1rem;
/* adding some gap between the cards */
}
.card-actions {
display: flex;
/* making element one near the other */
gap: 1rem;
/* adding some gap between the icons */
position: absolute;
/* making the actions appear on the top of the card */
top: 0.5rem;
/* setting the top position of the actions to 0.5rem (so they are not overlapping with the card) */
right: 0.5rem;
/* setting the right position of the actions to 0.5rem (so they are not overlapping with the card) */
}
.card {
position: relative;
/* making the card relative so we can use position absolute in the childs (so we can put the actions on the top of the card) */
padding: 24px;
box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.13);
background: skyblue;
border-radius: 4px;
font-family: "Helvetica", sans-serif;
display: flex;
flex-direction: column;
height: 280px;
width: 160px;
}
.card-body {
display: flex;
flex: 1 0 auto;
align-items: flex-end;
justify-content: center;
}
<head>
<!-- I used FONTAWESOME for the icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.1/css/all.min.css" />
</head>
<body>
<div class="cards-container">
<!-- 1 -->
<div class="card">
<div class="card-actions">
<!-- minimize --><i class="fa-regular fa-square-minus"></i>
<!-- maximize --><i class="fa-regular fa-window-maximize"></i>
<!-- close btn--><i class="fa-regular fa-rectangle-xmark"></i>
</div>
<div class="card-body">Card content</div>
</div>
<!-- 2 -->
<div class="card">
<div class="card-actions">
<!-- minimize --><i class="fa-regular fa-square-minus"></i>
<!-- maximize --><i class="fa-regular fa-window-maximize"></i>
<!-- close btn--><i class="fa-regular fa-rectangle-xmark"></i>
</div>
<div class="card-body">Card content</div>
</div>
<!-- 3 -->
<div class="card">
<div class="card-actions">
<!-- minimize --><i class="fa-regular fa-square-minus"></i>
<!-- maximize --><i class="fa-regular fa-window-maximize"></i>
<!-- close btn--><i class="fa-regular fa-rectangle-xmark"></i>
</div>
<div class="card-body">Card content</div>
</div>
</div>
</body>
you can try using Position property.
For example your HTML content is in a parent div named mainBody
<div class="mainBody">
<div class="card">
<div class="card-actions">
Close
</div>
<div class="card-body">
Card content
</div>
</div>
</div>
So, you will have to give postion:relative; to the mainBody and then position:absolute; to the child elements and then use top, bottom, right, left properties to adjust the position.
.mainBody{
height: 100vh;
width: 100vw;
position:relative;
}
.card {
padding: 24px;
box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.13);
border-radius: 4px;
font-family: "Helvetica", sans-serif;
background: skyblue;
width:30%;
height:50%;
display: flex;
flex-direction: column;
justify-content: flex-end;
position:absolute;
bottom:0;
right:0;
}
.card-body {
display: flex;
flex: 1 0 auto;
align-items: center;
justify-content: center;
}
.card-actions {
position:absolute;
top:0;
right:0;
}
You can try to do this, very simple and easy CSS.
position: fixed;
bottom: 0;
right: 0;
bottom and right attributes can be changed to suit your needs. Remember, think of these attributes as a margin for the element but doesn't effect other elements.
Thank you for considering my answer.
.main {
height: 100vh;
display: flex;
justify-content: end;
align-items: end;
}
.card {
padding: 24px;
box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.13);
background: skyblue;
border-radius: 4px;
font-family: "Helvetica", sans-serif;
display: flex;
flex-direction: column;
height: 280px;
width: 160px;
}
.card-body {
display: flex;
flex: 1 0 auto;
align-items: flex-end;
justify-content: center;
}
.card-actions {
display: flex;
align-items: center;
justify-content: space-between;
margin-top: 16px;
}
<div class="main">
<div class="card">
<div class="card-actions">
Close
</div>
<div class="card-body">
Card content
</div>
</div>
</div>
You can also do this using flex property of css

Why my Icons are getting extra height ? I have attached sandbox link at the bottom [duplicate]

This question already has answers here:
How to make a flex item not fill the height of the flex container? [duplicate]
(2 answers)
Closed 8 months ago.
I have a div which has height and width of 500px, inside this div I have 8 icons with font-size: 60px and margin: 0 30px, I have applied display flex to the div and justify-content: space-between, flex-wrap: wrap but my Icons are getting all the remaining height in the div.
<div id="container">
<nav id="navbar">
<a href="j" className="link">
Site Name
</a>
<a href="f">
<FaRegUserCircle className="user-icon" />
</a>
</nav>
{/* navbar above */}
<div id="playlist-container">
<div id="playlist">
<div className="icons">
<BsCloudRain />
</div>
<div className="icons">
<GiForest />
</div>
<div className="icons">
<MdOutlineWaterDrop />
</div>
<div className="icons">
<BiWind />
</div>
<div className="icons">
<GiCampfire />
</div>
<div className="icons">
<GiThreeLeaves />
</div>
<div className="icons">
<BsMoonStars />
</div>
<div className="icons">
<BiWater />
</div>
</div>
</div>
</div>
Css
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
#container {
height: 100vh;
width: 100%;
background-color: rgb(10, 116, 80);
}
#navbar {
height: 76px;
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
box-shadow: 0px 0.5px 10px 1px rgb(32, 32, 32);
}
.link {
font-size: 30px;
margin-left: 30px;
}
.user-icon {
margin-right: 70px;
font-size: 30px;
}
a {
color: #fff;
text-decoration: none;
}
/* Navbar */
#playlist-container {
display: flex;
justify-content: center;
}
#playlist {
height: 500px;
width: 500px;
background-color: aquamarine;
margin: 80px auto;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
}
.icons {
font-size: 60px;
margin: 0 30px;
}
Sandbox
The CSS align-content property sets the distribution of space between and around content items along a flexbox's cross-axis.
Add align-content: flex-start; to #playlist:
#playlist {
height: 500px;
width: 500px;
background-color: aquamarine;
margin: 80px auto;
display: flex;
justify-content: space-between;
flex-wrap: wrap;
align-content: flex-start;
}

Can't figure out how to cover a div with an image correctly for website

Title poorly describes the issue, sorry.
I have div's ("cards") in my website that contain a lot of content. One of the important thing's I wanted was to have an image covering the "cards", however every time I've attempted to fit them properly, they either don't change from their current setup or they take on massive size relative to the page size and not the "card" size.
I've read other threads regarding similar issues, but I haven't gotten anything to work yet.
Image not fitting into Card correctly
/* Entire div for card base + image inside it */
.card {
display: flex;
flex-flow: row no-wrap;
justify-content: space-between;
width: 275px;
height: 150px;
font-size: 25px;
padding: 20px;
background-color: #f1f1f1;
border-radius: 30px;
box-shadow: 0px 0px 18px #888888;
transition: all 0.5s ease;
}
.card:hover {
background-color: #daeaf5;
}
.card-image {
display: flex;
justify-content: center;
align-items: center;
overflow: hidden
}
.card-img:hover {
-webkit-filter: grayscale(0%);
filter: greyscale(0%);
}
.card-img {
width: 100%;
height: 100%;
flex-shrink: 0;
min-width: 100%;
min-height: 100%;
border-radius: 30px;
-webkit-filter: grayscale(100%);
filter: greyscale(100%);
-webkit-mask-image:-webkit-gradient(linear, left top, left bottom, from(rgba(0,0,0,1)), to(rgba(0,0,0,0)));
mask-image: linear-gradient(to left, rgba(0,0,0,1), rgba(0,0,0,0));
transition: all 0.5s ease;
}
/* ignore this stuff, it's just styling for the rest of the card */
.card-title {
font-size: 10px;
}
.card-content {
font-size: 18px;
margin-bottom: 5px;
white-space: nowrap;
}
.card-a {
color: black;
text-decoration: none;
border-radius: 30px;
margin: 15px;
}
.main {
margin-left: 10px;
}
.flex-container {
display: flex;
flex-flow: row wrap;
justify-content: flex-start;
align-items: flex-start;
align-content: flex-start;
}
/* Entire card div, yes I know my code looks like a goblin wrote it, I am a goblin. */
/*So far, this was the only way I have found to get it to work how I wanted to*/
<div class="main">
<ul type="none">
<div class="flex-container">
<a class="card-a" href="###">
<li>
<div class="card">
<div class="left-side">
<div class="card-title">Make</div>
<div class="card-content">Template</div>
<div class="card-title">Model</div>
<div class="card-content">Temp</div>
<div class="card-title">Trim</div>
<div class="card-content">Temp</div>
<div class="card-title">Year</div>
<div class="card-content">Temp</div>
</div>
<div class="right-side">
<img class="card-img" src="https://static.turbosquid.com/Preview/001308/648/FU/_D.jpg" alt="Temporary Picture provided by Turbosquid 1308648"/>
</div>
</div>
</li>
</a>
</div>
</ul>
</div>
Ideally I want the image to fit snugly into the card, hence the corners on the image matching the corners on the card. I've tried to relocate the image into various places, re-sorting the div's and attempting to structure everything differently. Unfortunately, every time I've tried that, I get farther and farther away from the ideal card design. Yes I know, the code looks like chicken scratch compared to anyone else's work, please excuse my poor man's amount of experience.
/* Entire div for card base + image inside it */
.card {
display: flex;
flex-flow: row no-wrap;
justify-content: space-between;
width: 275px;
height: 150px;
font-size: 25px;
padding: 20px;
background-color: #f1f1f1;
border-radius: 30px;
box-shadow: 0px 0px 18px #888888;
transition: all 0.5s ease;
position: relative;
overflow: hidden;
}
.card:hover {
background-color: #daeaf5;
}
.card-image {
display: flex;
justify-content: center;
align-items: center;
overflow: hidden
}
.card-img:hover {
-webkit-filter: grayscale(0%);
filter: greyscale(0%);
}
.card-img {
width: 100%;
height: 100%;
flex-shrink: 0;
min-width: 100%;
min-height: 100%;
border-radius: 30px;
-webkit-filter: grayscale(100%);
filter: greyscale(100%);
-webkit-mask-image:-webkit-gradient(linear, left top, left bottom, from(rgba(0,0,0,1)), to(rgba(0,0,0,0)));
mask-image: linear-gradient(to left, rgba(0,0,0,1), rgba(0,0,0,0));
transition: all 0.5s ease;
}
/* ignore this stuff, it's just styling for the rest of the card */
.card-title {
font-size: 10px;
}
.card-content {
font-size: 18px;
margin-bottom: 5px;
white-space: nowrap;
}
.card-a {
color: black;
text-decoration: none;
border-radius: 30px;
margin: 15px;
}
.main {
margin-left: 10px;
}
.flex-container {
display: flex;
flex-flow: row wrap;
justify-content: flex-start;
align-items: flex-start;
align-content: flex-start;
}
.right-side{
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
}
/* Entire card div, yes I know my code looks like a goblin wrote it, I am a goblin. */
/*So far, this was the only way I have found to get it to work how I wanted to*/
<div class="main">
<ul type="none">
<div class="flex-container">
<a class="card-a" href="###">
<li>
<div class="card">
<div class="left-side">
<div class="card-title">Make</div>
<div class="card-content">Template</div>
<div class="card-title">Model</div>
<div class="card-content">Temp</div>
<div class="card-title">Trim</div>
<div class="card-content">Temp</div>
<div class="card-title">Year</div>
<div class="card-content">Temp</div>
</div>
<div class="right-side">
<img class="card-img" src="https://static.turbosquid.com/Preview/001308/648/FU/_D.jpg" alt="Temporary Picture provided by Turbosquid 1308648"/>
</div>
</div>
</li>
</a>
</div>
</ul>
</div>
use the CSS background-image property
then play with its properties till you get what you want I.E:
<div id="card_image">
<h1>hello World</h1>
<p>this Section Has a background image</p>
</div>
<style>
#card_image {
background-image:url("https://static.turbosquid.com/Preview/001308/648/FU/_D.jpg");
background-size:25%;
backgroun-position:top right;
}
</style>
play with this

Css flex left and center alignments [duplicate]

This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 1 year ago.
I have flex container and first element needs to align left and second center. I try different options with justify-content and align items => center so that the second element of this paging box is in the middle but there is no help.
This is css and html code
paging-section {
display: flex;
height: 60px;
}
/* .paging-section div {
flex: 1;
} */
.pagination-info {
align-items: flex-start;
margin-top: 15px;
background-color: red;
}
.pagination-box {
width: auto;
height: 38px;
margin: 20px 308px 369px 215px;
padding: 0 16px 0 16px;
border-radius: 4px;
box-shadow: 0px 1px 2px 0 rgba(0, 0, 0, 0.08);
border: solid 1px #d8dce6;
background-color: #ffffff;
display: flex;
background-color: yellow;
// justify-content: center;
// align-items: center;
}
<div class="paging-section">
<div class="pagination-info">
<p>
{{ size }}
{{ 'ADMIN.PAGINATION.RESULTS' | translate }}
</p>
</div>
<div class="pagination-box">
....
</div>
</div>
How to achieve that this yellow element is in the middle of the container and the first one remains on the left side ? Thanks
ive set fixed width to .pagination-info and also set margin: 0px auto; to .pagination-box at the end ive translate the .pagination-box to the left on half width of .pagination-info there is transform: translateX(-50px);
.paging-section {
display: flex;
flex-direction: row;
align-items: center;
justify-content: flex-start;
height: 60px;
}
/* .paging-section div {
flex: 1;
} */
.pagination-info {
align-items: flex-start;
background-color: red;
max-width: 100px;
width: 100px;
min-width: 100px;
word-break: break-all;
}
.pagination-box {
margin: 0px auto;
transform: translateX(-50px);
width: auto;
height: 38px;
padding: 0 16px 0 16px;
border-radius: 4px;
box-shadow: 0px 1px 2px 0 rgba(0, 0, 0, 0.08);
border: solid 1px #d8dce6;
background-color: #ffffff;
display: flex;
background-color: yellow;
// justify-content: center;
// align-items: center;
}
<div class="paging-section">
<div class="pagination-info">
<p>
{{ size }}
{{ 'ADMIN.PAGINATION.RESULTS' | translate }}
</p>
</div>
<div class="pagination-box">
....
</div>
</div>

Flex item when wrap, change margins and paddings

is there a way to change margin or padding when a flex item is wrapped?
This is my problem:
I have a div 'buy' with margin: 0 auto, because i want it to be always at the right, but when this div is wrapped, i want to change that margin.
Before wrap:
After wrap:
This is my code:
HTML:
.container {
cursor: grab;
padding-left: 10px;
padding-right: 10px;
padding-bottom: 10px;
width: 70%;
background: #24252A;
margin: 20px auto;
height: auto;
border-radius: 5px;
box-shadow: 3px 3px 3px 3px #0d0d0d;
display: flex;
flex-wrap: wrap;
}
.container > div {
margin-right: 10px;
}
#imgBox {
width: 200px;
height: 200px;
object-fit: contain;
align-self: flex-start;
margin-top: 10px;
}
#content {
max-width: 50%;
min-width: 200px;
margin-top: 10px;
}
#buy {
margin-left: auto;
display: flex;
flex-direction: column;
justify-content: center;
}
.buy-specs {
display: flex;
flex-direction: column;
align-items: center;
}
#buy > * {
margin-left: auto;
}
<div id="container0" class="container" draggable="true" ondragstart="drag(event)">
<div id="imgBox">
<img src="../img/pinguini.jpg" alt="" draggable="false">
</div>
<div id="content">
<h2>Pinguini Tattici Nucleari</h2>
<p id="luogo">Torino - Pala Alpitour</p><p>04/10/2021 | 21:00</p>
</div>
<div id="buy">
<div class="buy-specs">
<p>Prezzo: 39,10 €</p>
<div id="quantityDiv"><p>Quantità: </p>
</div>
<button id="button0">Aggiungi al carrello</button>
</div>
</div>
Thank you!
Another approach could be to let the middle one to grow and push the last one.
last one can receive : margin:auto to be centered if alone on its row:
possible example:
.container {
cursor: grab;
padding-left: 10px;
padding-right: 10px;
padding-bottom: 10px;
width: 70%;
background: #24252A;
margin: 20px auto;
height: auto;
border-radius: 5px;
box-shadow: 3px 3px 3px 3px #0d0d0d;
display: flex;
flex-wrap: wrap;
}
.container>div {
margin-right: 10px;
}
#imgBox {
width: 200px;
height: 200px;
margin:auto;
margin-top: 10px;
}
#content {
flex-grow: 1;
min-width: 200px;
margin-top: 10px;
}
#buy {
display: flex;
flex-direction: column;
justify-content: center;
margin:auto;/* optionnal to center if alone*/
}
.buy-specs {
display: flex;
flex-direction: column;
align-items: center;
}
#buy>* {
margin-left: auto;
}
<div id="container0" class="container" draggable="true" ondragstart="drag(event)">
<div id="imgBox">
<img src="https://dummyimage.com/200/349&text=pinguini" alt="" draggable="false">
</div>
<div id="content">
<h2>Pinguini Tattici Nucleari</h2>
<p id="luogo">Torino - Pala Alpitour</p>
<p>04/10/2021 | 21:00</p>
</div>
<div id="buy">
<div class="buy-specs">
<p>Prezzo: 39,10 €</p>
<div id="quantityDiv">
<p>Quantità: </p>
</div>
<button id="button0">Aggiungi al carrello</button>
</div>
</div>
Note that first an last one can be centered if standing alone on a row: