how to display cards on the same line using display flex? - html

I need to display cards on the same line and use flex-wrap: wrap to wrap. But it's not working, what's wrong with my code?
Code
return(
<div className='container-poster'>
<div className='poster'>
<div className='poster-img'>
<img src={poster} />
<i>
{isFavorite ?
<FaHeart className='heart-icon' style={{ color: 'red' }} /> : <FaHeart className='heart-icon' style={{ color: '#BABABA' }} />
}
</i>
</div>
<div className='poster-title-vote'>
<h4 className='title-movie'>
{title}
</h4>
<div className='box-note'>
<span className='rating'>{rating}</span>
<i className='icon-vote-like'>
<img src={IconLike} alt="icon like"></img>
</i>
</div>
</div>
<p className='overview'>
{overview}
</p>
</div>
</div>
)
Reality
I want it to be a maximum of 4 cards in the same row and then break it when there's no space
SandBox
https://codesandbox.io/s/jolly-waterfall-10s5gx?file=/src/Poster.js

You'll want to add display: flex; on your parent .container-poster. This will make all elements nested in each flex-item .poster row item. You can then use display: flex; with flex-direction: column on your .poster to get the title underneath.
See below:
.container-poster {
display: flex;
justify-content: space-between;
}
.poster {
text-align: center;
display: flex;
flex-direction: column;
width: calc(100%/4.1); /* .1 for spacing */
}
img {
max-width: 100%;
}
<div class='container-poster'>
<div class='poster'>
<div class='poster-img'>
<img src="https://dummyimage.com/300/000/fff" />
</div>
<div class='poster-title-vote'>
<h4 class='title-movie'>Title
</h4>
</div>
<div class='box-note'>
<span class='rating'></span>
</div>
</div>
<div class='poster'>
<div class='poster-img'>
<img src="https://dummyimage.com/300/000/fff" />
</div>
<div class='poster-title-vote'>
<h4 class='title-movie'>Title
</h4>
</div>
<div class='box-note'>
<span class='rating'></span>
</div>
</div>
<div class='poster'>
<div class='poster-img'>
<img src="https://dummyimage.com/300/000/fff" />
</div>
<div class='poster-title-vote'>
<h4 class='title-movie'>Title
</h4>
</div>
<div class='box-note'>
<span class='rating'></span>
</div>
</div>
<div class='poster'>
<div class='poster-img'>
<img src="https://dummyimage.com/300/000/fff" />
</div>
<div class='poster-title-vote'>
<h4 class='title-movie'>Title
</h4>
</div>
<div class='box-note'>
<span class='rating'></span>
</div>
</div>
</div>

Related

How to dynamically display cards in a grid

I am designing the UI for a web app which will dynamically display data from my database in form of cards and will be arranged in a grid. For testing purposes I have put two cards , one after the other , however, only the first one is being displayed.
How can I get them to be displayed in a 3 column row after which if there are more than 3, they overflow to the next row dynamically?
<div class="container_grid">
<div class="product-card" style="margin: 1rem">
<div class="product-front">
<div class="shadow"></div>
<img src="https://veenaazmanov.com/wp-content/uploads/2019/08/Chocolate-Birthday-Cake5-500x500.jpg" alt="" />
<div class="image_overlay"></div>
<div class="view-details">View details</div>
<div class="stats">
<div class="stats-container">
<span class="product_price">Ksh.500</span>
<span class="product_name">Chocolate Cake</span>
<p>Tasty cake</p>
<div class="product-options">
<strong>Available in</strong>
<span>1kg, 2kg, 3kg, 4kg, 5kg</span>
<button class="btn">Add To Cart</button>
<!-- <div class="cart_btn"><p>Add to Cart</p></div> -->
</div>
</div>
</div>
</div>
</div>
<div class="product-card" style="margin: 1rem">
<div class="product-front">
<div class="shadow"></div>
<img src="https://veenaazmanov.com/wp-content/uploads/2019/08/Chocolate-Birthday-Cake5-500x500.jpg" alt="" />
<div class="image_overlay"></div>
<div class="view-details">View details</div>
<div class="stats">
<div class="stats-container">
<span class="product_price">Ksh.500</span>
<span class="product_name">Chocolate Cake</span>
<p>Tasty cake</p>
<div class="product-options">
<strong>Available in</strong>
<span>1kg, 2kg, 3kg, 4kg, 5kg</span>
<button class="btn">Add To Cart</button>
</div>
</div>
</div>
</div>
</div>
</div>
Link to the full JFiddle: https://jsfiddle.net/5yoerguh/3/
What am I doing wrong?
Both are being displayed if you check the console both div class='product-card' is there.
The problem is in the css your class .propduct-card is displayed with position:absolute;
.product-card {
width: 20rem;
max-width: 95%;
width: 325px;
height: 490px;
--> take this out /* position: absolute; */
overflow: hidden;
}
Now you will see both items

Gap between elements and wrapping not consistent

I have a page where the left and right are split evenly. On the right side, I have a bunch of div elements containing an img and a span. What I'm trying to do is show four of these div elements at MOST per row, and start to wrap on smaller screens, with a specific gap.
The problem I'm having is the gap is wider on bigger screens, and the elements squish together on smaller screens instead of wrapping, regardless of what gap value I set.
Here's a gif showing what's happening: https://gfycat.com/remorsefulglossyherring
Here's the relevant HTML (only the right side of the page which contains these divs):
<div
class="about"
fxFlex="50%"
fxLayout="row wrap"
fxLayoutGap="10px grid"
fxLayoutAlign="center center"
>
<div fxFlex="25%" class="item">
<img class="img-skills" src="assets/c-sharp.png" alt="C#" />
<span class="caption">C#</span>
</div>
<div fxFlex="25%" class="item">
<img class="img-skills" src="assets/.net-core.png" alt=".NET Core" />
<span class="caption">.NET Core</span>
</div>
<div fxFlex="25%" class="item">
<img class="img-skills" src="assets/wpf.png" alt="WPF" />
<span class="caption">WPF</span>
</div>
<div fxFlex="25%" class="item">
<img class="img-skills" src="assets/winforms.jpg" alt="WinForms" />
<span class="caption">WinForms</span>
</div>
<div fxFlex="25%" class="item">
<img class="img-skills" src="assets/angular.png" alt="Angular" />
<span class="caption">Angular</span>
</div>
<div fxFlex="25%" class="item">
<img class="img-skills" src="assets/nodejs.png" alt="NodeJS" />
<span class="caption">Node.js</span>
</div>
<div fxFlex="25%" class="item">
<img class="img-skills" src="assets/html5.png" alt="HTML5" />
<span class="caption">HTML5</span>
</div>
<div fxFlex="25%" class="item">
<img class="img-skills" src="assets/css3.png" alt="CSS3" />
<span class="caption">CSS3</span>
</div>
<div fxFlex="25%" class="item">
<img class="img-skills" src="assets/mongodb.png" alt="MongoDB" />
<span class="caption">MongoDB</span>
</div>
<div fxFlex="25%" class="item">
<img
class="img-skills"
src="assets/mssql.png"
alt="Microsoft SQL Server"
/>
<span class="caption">Microsoft SQL Server</span>
</div>
</div>
Here's the css:
.about {
min-height: 100vh;
justify-content: center;
height: 100%;
}
div.item {
vertical-align: top;
display: inline-block;
text-align: center;
width: 120px;
}
.img-skills {
width: 100px;
height: 100px;
background-color: grey;
}
.caption {
display: block;
}
This is the simple idea, but this will help you for sure to start with:
Play around here : fiddle
.MainDiv {
border: 1px solid red;
display: flex;
flex-wrap: wrap;
}
.first {
border: 1px solid black;
height: 100px;
width: 100px;
background-color: gray;
margin: 5px;
}
<div class="MainDiv">
<div class="first"></div>
<div class="first"></div>
<div class="first"></div>
<div class="first"></div>
<div class="first"></div>
<div class="first"></div>
<div class="first"></div>
<div class="first"></div>
</div>
Make it both left and right divs separated with the main container.
Your right side div should be like this below.
<div fxLayout="row" fxLayoutGap="20px">
<div>1. One</div>
<div>2. Two</div>
<div>3. Three</div>
<div>4. Four</div>
</div>

Placing a vertical divider between text

I need a vertical divider and the text to line up exactly like this on the top right hand corner of my screen.
I am having some trouble even getting the divider to show and the text to pull right
<div class="row">
<div class="float-right">
<span>Profile & Settings</span>
</div>
<div class="float-right sep">
<div class="septText"></div>
</div>
<div class="float-right">
<span>Logout</span>
</div>
</div>
Css
.sep {
display: flex;
flex-direction: column;
justify-content: center;
}
.sepText {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
flex: 1;
}
.sepText::before,
.sepText::after {
content: '';
flex: 1;
width: 1px;
background: currentColor;
/* matches font color */
margin: .25em;
}
I can see that bootstrap 4 tag is added to your question. If you are using bootstrap 4, there is a in-built flex utility https://getbootstrap.com/docs/4.0/utilities/flex/
<div class="d-flex flex-row justify-content-end">
<div class="">
<span>Profile & Settings</span>
</div>
<div class="mx-2">|</div>
<div class="">
<span>Logout</span>
</div>
</div>
JS Fiddle: https://jsfiddle.net/8p27xwcs/
use "container" instead of "row", or set display:inline or block on the row div.
<div class="row" style="display:inline;" >
<div class="float-right">
<span>Profile & Settings</span>
</div>
<div class="float-right sep">
<div class="septText"></div>
</div>
<div class="float-right">
<span>Logout</span>
</div>
</div>
or
<div class="container">
<div class="float-right">
<span>Profile & Settings</span>
</div>
<div class="float-right sep">
<div class="septText"></div>
</div>
<div class="float-right">
<span>Logout</span>
</div>

I have multiple blocks in design need to write the html code for that with the help divs

I am using this code to develop HTML code for the image below, but I am overlapping the divs, and stuck with code. I need to develop the solution with the help of divs in HTML.
<div style="width:80%; height: 80px;padding-right:110px;margin-right:40px">
<div style="width:26%; float: left;float:left">
<div class="pointer-blue-small1">Steps & User Requirements Definition</div>
</div>
<div style="width:15%; float: left;">
<div class="pointer-blue-small1">High Level Design</div>
</div>
</div>
<div style="background:rgb(93,188,210);width:500px;height:110px;color:white;line-height:1.2;padding:10px;margin: 5px;">
<div>
<div style="background:rgb(143,63,123);width:390px; height:40px;color:white;text-align:center;line-height:1.2;float:left;padding-left:50px;margin-left:100px">A1901 Integrated Planning</div>
</div>
<br />
<br />
<br />
<div>
<div class="pointer-purple-small" style="width:374px;height:40px;color:white;text-align:center;float:left;padding-left:50px;margin-left:100px;line-height:1.2;display:inline-block">A1102 Update Project Plan</div>
</div>
</div>
<div style="background:rgb(93,188,210);width:500px;height:60px;color:white;line-height:1.2;padding:10px;margin: 15px;display:inline-block;margin-left:6px">
<div style="background:rgb(143,63,123);width:390px; height:40px;color:white;text-align:center;line-height:1.2;float:left;padding-left:50px;margin-left:100px">A1919 Change Request Control & Monitor and Report Risks and Issues</div>
</div>
<div style="background:rgb(93,188,210);width:500px;height:175px; color:white;line-height:1.2;padding:10px;margin: 5px;" ;display: inline-block;margin-left:35px">
<div>
<div style="background:rgb(143,63,123);width:390px; height:40px;color:white;text-align:center;line-height:1.2;float:left;padding-left:50px;margin-left:100px">A1105 Quality Assurance </div>
</div>
<br />
<br />
<br />
<div>
<div style="background:rgb(143,63,123);width:390px;height:40px;color:white;text-align:center;float:left;padding-left:50px;margin-left:100px;line-height:1.2;display:inline-block">A1922 StackHolder Management</div>
</div>
<br />
<br />
<br />
<div>
<div class="pointer-purple-small" style="width:374px;height:40px;color:white;text-align:center;flo[![enter image description here][1]][1]at:left;padding-left:50px;margin-left:100px;line-height:1.2;display:inline-block">A1104 Update Quality Plan</div>
</div>
</div>
</div>
.left-parent div {
width: 40%;
height: 50px;
}
.row {
justify-content: space-between;
}
.left-parent {
justify-content: space-between;
}
.border {
border: 1px solid;
}
.flex {
display: flex
}
.left,
.right {
width: 50%;
margin: 10px
}
.child {
height: 50px;
margin-top: 20px;
}
<div class="row flex ">
<div class="left">
<div class="flex left-parent">
<div class="border left-parent-child">
</div>
<div class="border left-parent-child">
</div>
</div>
<div class="border child">
</div>
<div class="border child">
</div>
<div class="border child">
</div>
</div>
<div class="right border">
</div>
</div>

Break line using Flex keeping elements centered

This is another CSS Flex question, I'm sorry but I've been struggling a lot using Flex, I'm trying to make a kind of slider, in which it will have a left and right arrow and elements in between, like so:
The problem I'm facing is that in certain number of elements I need to break a line, keeping the alignment center both vertically and horizontally, like this: (paint pro editing)
I can't find a way to do this, I'm lost.
This is my actual code for the first image:
HMTL:
<div class="main allin">
<div class="left-arrow">
<i class="material-icons">keyboard_arrow_left</i>
</div>
<div class="flex-con ">
<div class="item-1">
<img src="https://via.placeholder.com/250" />
</div>
<div class="item-2">
<img src="https://via.placeholder.com/250" />
</div>
<div class="item-3">
<img src="https://via.placeholder.com/250" />
</div>
</div>
<div class="right-arrow">
<i class="material-icons">keyboard_arrow_right</i>
</div>
</div>
<div class="main break">
<div class="left-arrow">
<i class="material-icons">keyboard_arrow_left</i>
</div>
CSS:
.main{
display:flex;
justify-content: center;
align-items: center;
}
.flex-con{
display:flex;
align-items: center;
}
.flex-con div{
padding:20px;
}
And this is a CodePen
How can I achieve this? thanks.
Put all your item-x elements within the same flex-con wrapper.
Then, simply add the following properties:
flex-wrap: wrap; // to wrap its children into multiple lines
justify-content: center; // to center horizontally
.main {
display: flex;
justify-content: center;
align-items: center;
}
.flex-con {
display: flex;
align-items: center;
flex-wrap: wrap;
justify-content: center;
}
.flex-con div {
padding: 20px;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/material-design-icons/3.0.1/iconfont/material-icons.min.css" />
<div class="main break">
<div class="left-arrow">
<i class="material-icons">keyboard_arrow_left</i>
</div>
<div class="flex-con ">
<div class="item-1">
<img src="https://via.placeholder.com/250" />
</div>
<div class="item-2">
<img src="https://via.placeholder.com/250" />
</div>
<div class="item-3">
<img src="https://via.placeholder.com/250" />
</div>
<div class="item-4">
<img src="https://via.placeholder.com/250" />
</div>
<div class="item-5">
<img src="https://via.placeholder.com/250" />
</div>
</div>
<div class="right-arrow">
<i class="material-icons">keyboard_arrow_right</i>
</div>
</div>
Click Full page for better demonstration.