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:
Related
I made this layout but I can't find a way to align the circles and the text on the same line. As you can see in the first picture there's a problem both with circles and text. I'd like to achive the result you see in the 2nd picture.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.grid {
display: flex;
flex-wrap: wrap;
max-width: 980px;
width: 100%;
margin: auto;
padding-top: 5%;
padding-bottom: 5%;
}
.cell {
flex-basis: 33.3%;
display: flex;
justify-content: center;
align-items: center;
}
.cell:before {
padding-bottom: 100%;
display: block;
content: '';
}
.circle {
width: 250px;
height: 250px;
border: 0.5px solid;
border-radius: 50%;
background-position: center;
background-size: contain;
background-repeat: no-repeat;
box-shadow: 0px 0px 15px 0px;
overflow: hidden;
margin: 0 auto 1em;
}
.circle img {
width: 100%;
position: relative;
height: 100%;
}
h3 {
text-align: center;
}
.inner {
text-align: start;
padding-bottom: 15%;
}
<div class="grid">
<div class="cell">
<div class="inner">
<div class="circle" style="background-image:url('https://freight.cargo.site/t/original/i/655f74e74d3b88cc9d367ba8cccd79680c3837a84a547f9e03b6f39981f424e0/3.png');"></div>
<div class="caption">
<h3>Chiara Bersani <br> Marta Montanini</h3>
</div>
</div>
</div>
First, you will have to remove your manual line breaks <br>. Once you do this, the text can wrap automatically without having odd breaks at specific breaking points. If you want to force a line break, use horizontal padding on .caption h3.
Once you do this, just use align-items: self-start; on .grid.
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.grid {
display: flex;
flex-wrap: wrap;
align-items: self-start;
max-width: 980px;
width: 100%;
margin: auto;
padding-top: 5%;
padding-bottom: 5%;
}
.cell {
flex-basis: 33.3%;
display: flex;
justify-content: center;
align-items: center;
}
.cell:before {
padding-bottom: 100%;
display: block;
content: '';
}
.circle {
width: 250px;
height: 250px;
border: 0.5px solid;
border-radius: 50%;
background-position: center;
background-size: contain;
background-repeat: no-repeat;
box-shadow: 0px 0px 15px 0px;
overflow: hidden;
margin: 0 auto 1em;
}
.circle img {
width: 100%;
position: relative;
height: 100%;
}
.caption h3 {
text-align: center;
padding: 0 2em;
}
.inner {
text-align: start;
padding-bottom: 15%;
}
<div class="grid">
<div class="cell">
<div class="inner">
<div class="circle" style="background-image:url('https://freight.cargo.site/t/original/i/655f74e74d3b88cc9d367ba8cccd79680c3837a84a547f9e03b6f39981f424e0/3.png');"></div>
<div class="caption">
<h3>Chiara Bersani Marta Montanini</h3>
</div>
</div>
</div>
<div class="cell">
<div class="inner">
<div class="circle" style="background-image:url('https://freight.cargo.site/t/original/i/655f74e74d3b88cc9d367ba8cccd79680c3837a84a547f9e03b6f39981f424e0/3.png');"></div>
<div class="caption">
<h3>Chiara Bersani Longer TEXT...........................</h3>
</div>
</div>
</div>
<div class="cell">
<div class="inner">
<div class="circle" style="background-image:url('https://freight.cargo.site/t/original/i/655f74e74d3b88cc9d367ba8cccd79680c3837a84a547f9e03b6f39981f424e0/3.png');"></div>
<div class="caption">
<h3>Chiara Bersani Marta Montanini more text lorem foo</h3>
</div>
</div>
</div>
</div>
Goal: I have a main flex box with row display. Within each of these flexbox I will have card with text inside. When I create the card inside the flexbox I place p tags inside the card with the text, but it does not reflect. I want the text to dsiplay within each card.
Code
.flex-container {
display: flex;
flex-direction: row;
background-color: DodgerBlue;
}
.flex-container>div {
background-color: #f1f1f1;
margin: 10px;
/*text-align: center;*/
line-height: 70vh;
font-size: 30px;
}
.funds-card {
display: flex;
flex-direction: column;
width: 200px;
}
.transaction-card {
width: 700px
}
.amount-card {
width: 70%;
height: 70px;
max-width: 100px;
border-radius: 5px;
margin: 5px auto 0 auto;
padding: 1.5em;
background-color: green;
text-align: center;
}
<body>
<h1>The flex-direction Property</h1>
<p>The "flex-direction: row;" stacks the flex items horizontally (from left to right):</p>
<div class="flex-container">
<div class="funds-card">
<div class="amount-card">
<p>Hello</p>
</div>
</div>
<div class="transaction-card">
2
</div>
</div>
</body>
Remove line-height: 70vh; property from the .flex-container>div
.flex-container {
display: flex;
flex-direction: row;
background-color: DodgerBlue;
}
.flex-container>div {
background-color: #f1f1f1;
margin: 10px;
/*text-align: center;*/
/*line-height: 70vh;*/
font-size: 30px;
}
.funds-card {
display: flex;
flex-direction: column;
width: 200px;
}
.transaction-card {
width: 700px
}
.amount-card {
width: 70%;
height: 70px;
max-width: 100px;
border-radius: 5px;
margin: 5px auto 0 auto;
padding: 1.5em;
background-color: green;
text-align: center;
}
<body>
<h1>The flex-direction Property</h1>
<p>The "flex-direction: row;" stacks the flex items horizontally (from left to right):</p>
<div class="flex-container">
<div class="funds-card">
<div class="amount-card">
<p>Hello</p>
</div>
</div>
<div class="transaction-card">
2
</div>
</div>
</body>
I have 2 flex box containers. One of these is in row for and the other is in column for. What I am need to do is to have the items be in a cascading way. All items should have the same height and width but should be leveled in different positions.
Adjusting them margin of the container and items is not working.
HTML
<div class="flex-review">
<div class="review-card"></div>
<div class="review-card"></div>
<div class="review-card"></div>
</div>
<div class="flex-container">
<div class="main-card" id="higest-card"></div>
<div class="main-card"></div>
<div class="main-card"></div>
</div>
CSS (firts part is for bottom cotainer with big items, second part is for top cotainer with small items)
.flex-container{
display: flex;
justify-content: center;
align-content: space-between;
}
.main-card{
height: 250px;
width: 400px;
background-color: antiquewhite;
box-shadow: 5px 10px 18px #b8b5b5;
border-radius: 5px;
margin-top: 400px;
margin-right: 40px;
}
.flex-review{
display: flex;
flex-direction: column;
align-items: flex-end;
}
.review-card{
height: 60px;
width: 500px;
background-color: antiquewhite;
box-shadow: 5px 10px 18px #b8b5b5;
border-radius: 5px;
margin-right: 100px;
margin-top: 50px;
}
Currently looks like this:
Is this what you're after?
I set the divs to have relative positioning, and simply moved them using right/bottom.
.flex-container{
display: flex;
justify-content: center;
align-content: space-between;
}
.main-card{
height: 250px;
width: 400px;
background-color: antiquewhite;
box-shadow: 5px 10px 18px #b8b5b5;
border-radius: 5px;
margin-top: 400px;
margin-right: 40px;
position: relative;
}
.main-card:nth-child(1) {
bottom: 100px;
}
.main-card:nth-child(2) {
bottom: 50px;
}
.flex-review{
display: flex;
flex-direction: column;
align-items: flex-end;
}
.review-card{
height: 60px;
width: 500px;
background-color: antiquewhite;
box-shadow: 5px 10px 18px #b8b5b5;
border-radius: 5px;
margin-top: 50px;
position: relative;
}
.review-card:nth-child(1) {
right: 100px;
}
.review-card:nth-child(2) {
right: 50px;
}
<div class="flex-review">
<div class="review-card"></div>
<div class="review-card"></div>
<div class="review-card"></div>
</div>
<div class="flex-container">
<div class="main-card" id="higest-card"></div>
<div class="main-card"></div>
<div class="main-card"></div>
</div>
I am try to write markup and css for table based design in flexbox module !
Here is the Design
Here is the Markup
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.header-area {
margin: 50px auto;
max-width: 800px;
}
.flextable-header {
width: 100%;
background: #f7f7f7;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
height: 45px;
}
.flextable-header span {
width: 20%;
text-align: center;
}
.flextable-header .amount {
flex: auto;
text-align: right;
padding-right: 30px;
}
.flextable-content {
height: 100px;
width: 100%;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.flextable-content div {
width: 20%;
text-align: center;
}
.text-left {
display: flex;
flex-direction: column;
justify-content: center;
font-size: 15px;
height: 100%;
}
#dates {
text-align: left;
display: inline-block;
}
.flextable-content .money {
flex: auto;
text-align: right;
padding-right: 30px;
}
.flextable-content div img {
width: 120px;
height: 80px;
border-radius: 5px;
object-fit: cover;
object-position: center;
}
<div class="header-area">
<div class="flextable-header">
<span>Photos</span>
<span>Date</span>
<span class="amount">Amount</span>
</div>
<div class="flextable-content">
<div><img src="https://i.imgur.com/VzV3NwJ.jpg" alt=""></div>
<div class="text-left">
<div>Business</div><br/>
<div id="dates">25 December 2019</div>
</div>
<div class="money">3500 <small>Taka</small></div>
</div>
</div>
I'm stack at date alignment and bottom border as per design which I put on top !
You can simplify your code like below and use a simple gradient for the line:
* {
box-sizing: border-box;
}
.header-area {
margin: 50px auto;
max-width: 800px;
}
.flextable-header,
.flextable-content{
background: #f7f7f7;
display: flex;
align-items: center;
height: 45px;
}
.flextable-header > span:first-child {
min-width:130px;
text-align:center;
}
.flextable-header .amount,
.flextable-content .money{
margin-left:auto;
padding-right: 30px;
}
.flextable-content {
height: 100px;
background:linear-gradient(grey,grey) bottom right/calc(100% - 130px) 1px no-repeat;
padding-bottom:10px;
margin:10px 0;
}
.flextable-content:last-child {
background:none;
}
.flextable-content > img {
max-width:120px;
border-radius: 5px;
}
.text-left {
padding-left:10px;
font-size: 15px;
}
<div class="header-area">
<div class="flextable-header">
<span>Photos</span>
<span>Date</span>
<span class="amount">Amount</span>
</div>
<div class="flextable-content">
<img src="https://i.imgur.com/VzV3NwJ.jpg" alt="">
<div class="text-left">
<div>Business</div>
<div class="dates">25 December 2019</div>
</div>
<div class="money">3500 <small>Taka</small></div>
</div>
<div class="flextable-content">
<img src="https://i.imgur.com/VzV3NwJ.jpg" alt="">
<div class="text-left">
<div>Business</div>
<div class="dates">25 December 2019</div>
</div>
<div class="money">3500 <small>Taka</small></div>
</div>
<div class="flextable-content">
<img src="https://i.imgur.com/VzV3NwJ.jpg" alt="">
<div class="text-left">
<div>Business</div>
<div class="dates">25 December 2019</div>
</div>
<div class="money">3500 <small>Taka</small></div>
</div>
</div>
Add width: 100% to your dates (use it as class, not id, id should be unique)
Add a spacer div at the bottom.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.header-area {
margin: 50px auto;
max-width: 800px;
}
.flextable-header {
width: 100%;
background: #f7f7f7;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
height: 45px;
}
.flextable-header span {
width: 20%;
text-align: center;
}
.flextable-header .amount {
flex: auto;
text-align: right;
padding-right: 30px;
}
.flextable-content {
height: 100px;
width: 100%;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.flextable-content div {
width: 20%;
text-align: center;
}
.text-left {
display: flex;
flex-direction: column;
justify-content: center;
font-size: 15px;
height: 100%;
}
div.dates {
text-align: left;
display: inline-block;
width: 100%;
}
.flextable-content .money {
flex: auto;
text-align: right;
padding-right: 30px;
}
.flextable-content div img {
width: 120px;
height: 80px;
border-radius: 5px;
object-fit: cover;
object-position: center;
}
.spacer{
height: 1px;
background-color: #e5e5e5;
width: 78%;
float: right;
margin-right: 2%;
}
<div class="header-area">
<div class="flextable-header">
<span>Photos</span>
<span>Date</span>
<span class="amount">Amount</span>
</div>
<div class="flextable-content">
<div><img src="https://i.imgur.com/VzV3NwJ.jpg" alt=""></div>
<div class="text-left">
<div>Business</div><br/>
<div class="dates">25 December 2019</div>
</div>
<div class="money">3500 <small>Taka</small></div>
</div>
<div class="spacer"></div>
</div>
And to give some rest for other users, you can also change your HTML structure and use a border as a separator
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.header-area {
margin: 50px auto;
max-width: 800px;
}
.flextable-header {
width: 100%;
background: #f7f7f7;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
height: 45px;
}
.flextable-header span {
width: 20%;
text-align: center;
}
.flextable-header .amount {
flex: auto;
text-align: right;
padding-right: 30px;
}
.flextable-content {
height: 100px;
width: 100%;
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}
.img-container {
flex-basis: 20%;
flex-shrink: 0;
flex-grow: 0;
text-align: center;
}
.border-container{
flex-basis: 78%;
flex-shrink: 0;
flex-grow: 0;
display: flex;
align-items: center;
border-bottom: 1px solid #e5e5e5;
margin-right: 2%;
padding-bottom: 15px;
height: 100%;
}
.text-left {
display: flex;
flex-direction: column;
justify-content: center;
font-size: 15px;
height: 100%;
}
div.dates {
text-align: left;
display: inline-block;
width: 100%;
}
.flextable-content .money {
flex: auto;
text-align: right;
padding-right: 30px;
}
.flextable-content div img {
width: 120px;
height: 80px;
border-radius: 5px;
object-fit: cover;
object-position: center;
}
<div class="header-area">
<div class="flextable-header">
<span>Photos</span>
<span>Date</span>
<span class="amount">Amount</span>
</div>
<div class="flextable-content">
<div class="img-container"><img src="https://i.imgur.com/VzV3NwJ.jpg" alt=""></div>
<div class="border-container">
<div class="text-left">
<div>Business</div><br/>
<div class="dates">25 December 2019</div>
</div>
<div class="money">3500 <small>Taka</small></div>
</div>
</div>
</div>
I have a comment section where the user can enter comments, it automatically displays comments and the time the comment was entered.
I want author name to be to the left and date to the right and it should be responsive using flex-box
Here is jsfiddle:
https://jsfiddle.net/68vt3c7s/
Here is what I would like to have
Here is a snippet of what i have:
.comments-description {
display: flex;
flex-direction: row;
padding: 23px 0px;
border-bottom: 2px solid #EBEBEB;
}
.comments_details {
display: flex;
align-items: center;
justify-content: space-between;
flex-basis: 100%;
margin-top: 27px;
}
.comments_wrapper {
padding: 32px 21px;
}
.comments-photo img {
border-radius: 300px;
width: 80px;
height: 80px;
position: relative;
top: 37px;
}
<div class="comments-description">
<div class="comments-photo"><img alt="" src="https://randomuser.me/api/portraits/men/84.jpg"></div>
<div class="comments_wrapper">
<div class="comments_details">
<h1>Mike Ross</h1>
<span class="days">4 minutes ago</span>
</div>
<div class="comments_text">
<p>dingi
</p>
</div>
</div>
</div>
what do I need to change to get what I want ?
Add for .comments_wrapper the flex: auto; in order to align time to right
Means:
.comments_wrapper {
padding: 32px 21px;
flex: auto; /* here I added */
}
Hope this will help.
.comments_wrapper{
-webkit-box-flex:1;
-ms-flex-positive:1;
flex-grow:1;
}
comments_wrapper selector not taking rest of the place that's why you not seeing the effect. Try like following way:
.comments_wrapper {
padding: 32px 21px;
flex-grow: 1; /* Key Line */
}
You need to set the width of the .comments_wrapper
.comments-description {
display: flex;
flex-direction: row;
padding: 23px 0px;
border-bottom: 2px solid #EBEBEB;
}
.comments_details {
display: flex;
align-items: center;
justify-content: space-between;
flex-basis: 100%;
margin-top: 27px;
}
.comments_wrapper {
padding: 32px 21px;
width: 100%; /*add this*/
}
.comments-photo img {
border-radius: 300px;
width: 80px;
height: 80px;
position: relative;
top: 37px;
}
.days{
margin-left: auto;
}
Add flex: auto; to .comments_wrapper css
.comments_wrapper {
padding: 32px 21px;
flex: auto;
}
.comments-description {
display: flex;
flex-direction: row;
padding: 23px 0px;
border-bottom: 2px solid #EBEBEB;
}
.comments_details {
display: flex;
align-items: center;
justify-content: space-between;
flex-basis: 100%;
margin-top: 27px;
}
.comments_wrapper {
padding: 32px 21px;
flex: auto;
}
.comments-photo img {
border-radius: 300px;
width: 80px;
height: 80px;
position: relative;
top: 37px;
}
<div class="comments-description">
<div class="comments-photo"><img alt="" src="https://randomuser.me/api/portraits/men/84.jpg"></div>
<div class="comments_wrapper">
<div class="comments_details">
<h1>Mike Ross</h1>
<span class="days">4 minutes ago</span>
</div>
<div class="comments_text">
<p>dingi
</p>
</div>
</div>
</div>