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.
Related
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>
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 am trying to vertically center my header elements, but the following code that I've tried isnt working. What am I doing wrong? Please let me know where I can add padding or something else.
Here is the website: https://eagleroofingcontractor.com/
What I have so far:
<div class="col-md-12">
<aside id="text-3" class="widget header-right widget_text">
<div class="textwidget">
<div class="container extra-info">
<div class="row">
<div class="col-md-4">
<i class="fa fa-phone"></i>
<div class="phone">
<h3>631-209-7377</h3>
<span>info#eagleroofingcontractor.com</span>
</div>
</div>
<div class="col-md-2">
<div>
<img src="https://eagleroofingcontractor.com/wp-content/uploads/2019/02/Better-Business-Bureau-A-Logo.png">
</div>
</div>
<div class="col-md-2">
<div>
<img src="https://eagleroofingcontractor.com/wp-content/uploads/2019/02/gaf-master-elite-gold800px-800x292.jpg">
</div>
</div>
<div class="col-md-2">
<div>
<img src="https://eagleroofingcontractor.com/wp-content/uploads/2019/02/Google_Partners_logo_blogpage.jpg">
</div>
</div>
<div class="col-md-2">
<div>
<img src="https://eagleroofingcontractor.com/wp-content/uploads/2019/02/googole-guaranteed-min.png">
</div>
</div>
</div>
</div>
</div>
</aside>
</div>
You need to change the elements .textwidget, .extra-info and the col-md-2 columns inside .extra-info to flex items and then vertically center the columns using the css flex property align-items:center.
Add the following to your CSS:
.textwidget {
display: flex;
}
.extra-info {
display: flex;
}
.extra-info .col-md-2 {
display: flex;
align-items: center;
}
N.B. Your site has a lot of elements using a common ID (e.g. all your <aside> elements in your header uses the same #text-3 ID).
You should not use the same ID for more than one elements. Replace them with a common class-name or use a different ID for each element instead.
I have an image, and I display a text to the right and bottom of it , as can be seen in the first snippet(view snippet in full window). Now I want to add another text, and that it will be to the top right of the image, above the other text, but it pushes the bottom text to the right. I want both texts to be relative to the image, but the last paragraph seems to be relative to the sibling.
(**Sorry about the many html tags, I'm using a css framework also)
.justify-content-end {
align-self: flex-end;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.css" rel="stylesheet"/>
<div class="level">
<div class="level-left">
<div class="level-item">
<figure class="image is-128x128">
<img src="https://bulma.io/images/placeholders/128x128.png">
</figure>
</div>
<div class="level-item justify-content-end">
<p>Hello World</p>
</div>
</div>
</div>
And this is the snippet depicting the problem:
.justify-content-start {
align-self: flex-start;
}
.justify-content-end {
align-self: flex-end;
}
#click {
text-align: center;
width: 128px;
cursor: pointer;
border: 1px solid red;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.css" rel="stylesheet" />
<div class="level">
<div class="level-left">
<div class="level-item">
<figure class="image is-128x128">
<img src="https://bulma.io/images/placeholders/128x128.png">
</figure>
</div>
<div class="level-item justify-content-start">
<label class="label">
<p id="click">Click Me!</p>
</label>
</div>
<div class="level-item justify-content-end">
<p>Hello World</p>
</div>
</div>
</div>
Here is my solution
Had to change your structure
<div class=" par">
<label class="label">
<p id="click">Click Me!</p>
</label>
<p>Name Placeholder</p>
</div>
Then
added this to css
.main .stretch{
align-items: stretch;
}
.par{
display:flex;
flex-direction:column;
justify-content:space-between;
}
.justify-content-start {
align-self: flex-start;
}
.justify-content-end {
align-self: flex-end;
}
.main .stretch {
align-items: stretch;
text-align: center;
}
#click {
text-align: center;
width: 128px;
cursor: pointer;
border: 1px solid red;
margin: auto;
}
.par {
display: flex;
flex-direction: column;
justify-content: space-between;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.css" rel="stylesheet" />
<div class="level main">
<div class="level-left stretch">
<div class="level-item">
<figure class="image is-128x128">
<img src="https://bulma.io/images/placeholders/128x128.png">
</figure>
</div>
<div class=" par">
<label class="label">
<p id="click">Click Me!</p>
</label>
<p>Name Placeholder</p>
</div>
</div>
</div>
I usually dislike position:absolute; but it looks like it could be a fair use here .
.justify-content-start {
align-self: flex-start;
min-height: 1.4em; /* keep room for the absolute child */
}
.justify-content-end {
align-self: flex-end;
}
#click {
text-align: center;
width: 128px;
cursor: pointer;
border: 1px solid red;
}
#media (min-width: 768px) {
#click {
position: absolute;
}
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.css" rel="stylesheet" />
<div class="level">
<div class="level-left">
<div class="level-item">
<figure class="image is-128x128">
<img src="https://bulma.io/images/placeholders/128x128.png">
</figure>
</div>
<div class="level-item justify-content-start">
<label class="label">
<p id="click">Click Me!</p>
</label>
</div>
<div class="level-item justify-content-end">
<p>Hello World</p>
</div>
</div>
</div>
This question already has answers here:
How to center a flex container but left-align flex items
(9 answers)
Closed 6 years ago.
I would like child elements with a fixed width and height to be placed in flexbox one by one with a small indentation between them and a parent element to be centered, but not centering it's child elements.
All my attempts to force the parent element to be centered and stop centering it's child elements failed, among them:
align-self: center;
margin: 0 auto;
justify-content: space-between;
Here is the link for a preview. All I need is parent element to be centered.
justify-content: center; aligns parent div, but I dont want last row (if it isn't completely filled with elements) to be centered.
So how can I do that? Thank you in advance.
Use justify-content: center; on the parent .grid-view.
Have a look at this snippet below:
.grid-view {
display: flex;
padding: 0px;
margin: 0px;
align-self: center;
flex-wrap: wrap;
align-items: flex-start;
justify-content: center;
}
.grid-view .item {
flex-shrink: 1;
margin: 5px;
border: 1px solid #333;
align-self: flex-start;
}
.grid-view .item .item-cover {
width: 200px;
border: 3px solid #006699;
border-radius: 3px;
}
<div class="grid-view">
<div class="item">
<img class="item-cover" alt="attēls" src="https://pbs.twimg.com/profile_images/660946436801101824/niM7azZS.jpg" />
<h2 class="item-title">{{Title}}</h2>
<p class="item-description">{{item description}}</p>
</div>
<div class="item">
<img class="item-cover" alt="attēls" src="https://pbs.twimg.com/profile_images/660946436801101824/niM7azZS.jpg" />
<h2 class="item-title">{{Title}}</h2>
<p class="item-description">{{item description}}</p>
</div>
<div class="item">
<img class="item-cover" alt="attēls" src="https://pbs.twimg.com/profile_images/660946436801101824/niM7azZS.jpg" />
<h2 class="item-title">{{Title}}</h2>
<p class="item-description">{{item description}}</p>
</div>
<div class="item">
<img class="item-cover" alt="attēls" src="https://pbs.twimg.com/profile_images/660946436801101824/niM7azZS.jpg" />
<h2 class="item-title">{{Title}}</h2>
<p class="item-description">{{item description}}</p>
</div>
<div class="item">
<img class="item-cover" alt="attēls" src="https://pbs.twimg.com/profile_images/660946436801101824/niM7azZS.jpg" />
<h2 class="item-title">{{Title}}</h2>
<p class="item-description">{{item description}}</p>
</div>
<div class="item">
<img class="item-cover" alt="attēls" src="https://pbs.twimg.com/profile_images/660946436801101824/niM7azZS.jpg" />
<h2 class="item-title">{{Title}}</h2>
<p class="item-description">{{item description}}</p>
</div>
<div class="item">
<img class="item-cover" alt="attēls" src="https://pbs.twimg.com/profile_images/660946436801101824/niM7azZS.jpg" />
<h2 class="item-title">{{Title}}</h2>
<p class="item-description">{{item description}}</p>
</div>
<div class="item">
<img class="item-cover" alt="attēls" src="https://pbs.twimg.com/profile_images/660946436801101824/niM7azZS.jpg" />
<h2 class="item-title">{{Title}}</h2>
<p class="item-description">{{item description}}</p>
</div>
</div>
Hope this helps!