I have a flex-div with 7 elements in it.
<div class="d-flex flex-wrap justify-content-around">
<div style="width: 350px">
[...]
</div>
[6 more identical divs]
</div>
On large screens, there are 3 divs in each row, on medium screens 2 and on small screens only 1. The last element is always centered but I want it to be aligned right. I tried margin-left: auto on the last element but due to justify-content-around there should be space on the right (first 2 rows on large screens, first 3 rows on medium screens). Unfortunately, margin-left: auto pushes the last element all the way to the right without the space from justify-content-around. How can I position the last element exactly as if it was an element of the right column?
.d-flex {
display: flex
}
.flex-wrap {
flex-wrap: wrap
}
.justify-content-around {
justify-content: space-around
}
.card {
border: 1px solid rgba(0, 0, 0, .125)
}
.ml-auto {
margin-left: auto
}
<h3>
Without margin-left: auto
</h3>
<div class="d-flex flex-wrap justify-content-around">
<div class="card" style="width: 200px">
1
</div>
<div class="card" style="width: 200px">
2
</div>
<div class="card" style="width: 200px">
3
</div>
<div class="card" style="width: 200px">
4
</div>
<div class="card" style="width: 200px">
5
</div>
<div class="card" style="width: 200px">
6
</div>
<div class="card" style="width: 200px">
7 is centered
</div>
</div>
<h3>
With margin-left: auto
</h3>
<div class="d-flex flex-wrap justify-content-around">
<div class="card" style="width: 200px">
1
</div>
<div class="card" style="width: 200px">
2
</div>
<div class="card" style="width: 200px">
3
</div>
<div class="card" style="width: 200px">
4
</div>
<div class="card" style="width: 200px">
5
</div>
<div class="card" style="width: 200px">
6
</div>
<div class="card ml-auto" style="width: 200px">
7 is too far to the right
</div>
</div>
It goes from
1 2
3 4
5 6
7 8
For 7 to come under 6 is tricky, but lets say thats not what you meant, and just want to align the 7 to left side.
Let's start removing all these style=width, If you ever want to edit the width of div you would need to edit every single div, dumping it into the CSS is much more efficient.
<div class="card">
4
</div>
This is the adjusted CSS
.flex-wrap {
flex-wrap: wrap;
}
.justify-content-around {
justify-content: space-between;
display: flex;
}
.card {
border: 1px solid rgba(0, 0, 0, 0.125);
width: 200px;
}
If you want it aligned on the right then you can use JS but its difficult, since you use justify it might conflict.
The following solution works, if the element and the parent width are constant, i. e. you know, when there are 1 or 2 or 3 columns:
.d-flex {
display: flex
}
.flex-wrap {
flex-wrap: wrap
}
.justify-content-around {
justify-content: space-around
}
.card {
width: 200px
}
.ml-auto {
margin-left: auto
}
.d-none {
display: none
}
.border {
border: 1px solid rgba(0, 0, 0, .125);
}
#media (min-width:500px) {
.d-lg-flex {
display: flex
}
}
#media (min-width:700px) {
.d-xl-flex {
display: flex
}
}
<h3>
Centered
</h3>
<div class="d-flex flex-wrap justify-content-around">
<div class="card border">
1
</div>
<div class="card border">
2
</div>
<div class="card border">
3
</div>
<div class="card border">
4
</div>
<div class="card border">
5
</div>
<div class="card border">
6
</div>
<div class="card border">
7 is centered
</div>
</div>
<h3>
With margin-left: auto
</h3>
<div class="d-flex flex-wrap justify-content-around">
<div class="card border">
1
</div>
<div class="card border">
2
</div>
<div class="card border">
3
</div>
<div class="card border">
4
</div>
<div class="card border">
5
</div>
<div class="card border">
6
</div>
<div class="card border ml-auto">
7 is too far to the right
</div>
</div>
<h3>
Correct position
</h3>
<div class="d-flex flex-wrap justify-content-around">
<div class="card border">
1
</div>
<div class="card border">
2
</div>
<div class="card border">
3
</div>
<div class="card border">
4
</div>
<div class="card border">
5
</div>
<div class="card border">
6
</div>
<div class="card d-none d-lg-flex"></div>
<div class="card d-none d-xl-flex"></div>
<div class="card border">
7 is now at correct position
</div>
</div>
Instead of min-width:500px and min-width:700px in the media queries, use the bootstrap ones: min-width:992px and min-width:1200px. I just needed the smaller values for the snippet. Or simply use <div class="data-box d-none d-lg-flex"></div> and <div class="data-box d-none d-xl-flex"></div> for the placeholder-divs.
While other answers here are better, You should be tricky and smart,
you can create another element ,say div 8,now
Change
<div class="card" style="width: 200px">8</div> to
<div class="space" style="width: 200px"></div>
Place this div before div 7
What have we done?
We have removed all content inside that div(removed 8 from it)
We have changed class card to space, this removes the border property we apply in css(We can even remove the class instead of changing)
.card {
border: 1px solid rgba(0, 0, 0, .125);/*removed to our element 8*/
}
Your code
Note: I have added just 1 line of code,in html
.d-flex {
display: flex
}
.flex-wrap {
flex-wrap: wrap
}
.justify-content-around {
justify-content: space-around
}
.card {
border: 1px solid rgba(0, 0, 0, .125)
}
<h3>
Without margin-left: auto
</h3>
<div class="d-flex flex-wrap justify-content-around">
<div class="card" style="width: 200px">
1
</div>
<div class="card" style="width: 200px">
2
</div>
<div class="card" style="width: 200px">
3
</div>
<div class="card" style="width: 200px">
4
</div>
<div class="card" style="width: 200px">
5
</div>
<div class="card" style="width: 200px">
6
</div>
<div class="space" style="width: 200px"></div><!--I HAVE ADDED ONLY THIS LINE-->
<div class="card" style="width: 200px">
7
</div>
</div>
As of your original question,
"but due to justify-content-around",
You get problem when using margin-left: auto because of, justify-content- around.
The solution is to use justify-content-space-between
Snippet:
.d-flex {
display: flex
}
.flex-wrap {
flex-wrap: wrap
}
.justify-content-around {
justify-content: space-between;
margin: auto;
}
.card {
border: 1px solid rgba(0, 0, 0, .125)
}
.card:last-child{
margin-left: auto
}
<div class="d-flex flex-wrap justify-content-around">
<div class="card" style="width: 200px">
1
</div>
<div class="card" style="width: 200px">
2
</div>
<div class="card" style="width: 200px">
3
</div>
<div class="card" style="width: 200px">
4
</div>
<div class="card" style="width: 200px">
5
</div>
<div class="card" style="width: 200px">
6
</div>
<div class="card" style="width: 200px">
7
</div>
</div>
Note: If you need space-around also,then use margin or fixed width,or even another parent div to make it center!
I'm trying to create three vertical dots and align the bottom left of my container with css and bootstrap 5. exactly like the pic below:
my HTML and CSS:
.dot {
border-radius: 50%;
height: 12px;
width: 12px;
background: linear-gradient(90deg, #76b3fe, #8680e4);
}
.selected-dot {
height: 20px;
width: 20px;
background: #97cdfe;
}
<section class="slider-bg-1 d-flex align-items-end">
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-5 d-flex flex-column justify-content-center pt-4 pt-lg-0 order-2 order-lg-1">
<h1 class="head-font-size">Choose a powerful design for your Start-up</h1>
<h6 claas="caption-text-color">Get your freebie template now</h6>
<div class="d-lg-flex">
<div class="mt-5"><button class="my-btn" type="submit">Discover</button></div>
</div>
</div>
<div class="col-lg-5 col-md-12 col-sm-12 order-1 order-lg-2 d-flex justify-content-center">
<img src="Assets/home-slider/slider-m-1.png" class="img-fluid img-slider" alt="">
</div>
</div>
</div>
<span class="dot selected-dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</section>
this is my output
how can i align the vertically and move them to bottom left of container? because when i use div for wrapping they disappear
1st: wrap the dots in a container. I used a section with the class: .dot-wrapper.
2nd: give the wrapper a width: min-content;. With that, it will only be as wide as the seclected dot.
3rd: to cenetr the dots, give them a margin left and right of auto;
.dot {
border-radius: 50%;
height: 12px;
width: 12px;
background: linear-gradient(90deg, #76b3fe, #8680e4);
margin: 5px auto;
}
.selected-dot {
height: 20px;
width: 20px;
background: #97cdfe;
}
.dot-wrapper {
width: min-content;
}
<section class="slider-bg-1 d-flex align-items-end">
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-5 d-flex flex-column justify-content-center pt-4 pt-lg-0 order-2 order-lg-1">
<h1 class="head-font-size">Choose a powerful design for your Start-up</h1>
<h6 claas="caption-text-color">Get your freebie template now</h6>
<div class="d-lg-flex">
<div class="mt-5"><button class="my-btn" type="submit">Discover</button></div>
</div>
</div>
<div class="col-lg-5 col-md-12 col-sm-12 order-1 order-lg-2 d-flex justify-content-center">
<img src="Assets/home-slider/slider-m-1.png" class="img-fluid img-slider" alt="">
</div>
</div>
</div>
<section class="dot-wrapper">
<div class="dot selected-dot"></div>
<div class="dot"></div>
<div class="dot"></div>
</section>
</section>
it's quite easy, you can wrap the dots with a div container like this
<!--Column Dots -->
<div class="dot-container">
<span class="dot selected-dot"></span>
<span class="dot"></span>
<span class="dot"></span>
</div>
and style the container as below
.dot-container{
width: 15px;
display: flex;
flex-direction:column;
align-items:center;
}
and for better styling. give dot class some amrgin like margin: 3px 0;
here is a codpen : https://codepen.io/shammlo/pen/QWKggNX?editors=1100
I am doing a simple page with a bootstrap grid and I am having some problems with placing the middle row occupying the full height in the middle.
These are the 3 rows: page
I want to place the row with the logo in the middle, but I am having problems with it. I placed height: 100%;, height: auto;. I even placed the container height to 100% and auto and nothing works!
I think there is a concept about bootstrap heigh that I am missing, because its the heigh that I have problems
html:
<div class="container-fluid">
<div class="row">
<div class="col-md-1 col-2">
<img id="menuIcon" (click)="toggleRightSidenav()" onmouseover="this.src='../../../assets/Images/generic/menuIcon_hover.png'" src="/assets/Images/generic/menuIcon.png" onmouseout="this.src='/assets/Images/generic/menuIcon.png'">
</div>
<div class="col-md-10 col-8">
</div>
<div class="col-md-1 col-2">
<img id="helpIcon" routerLink="/guide" onmouseover="this.src='../../../assets/Images/home/helpIconHover.png'" src="/assets/Images/home/helpIcon.png" onmouseout="this.src='/assets/Images/home/helpIcon.png'">
</div>
</div>
<div class="row row align-items-center mh-100" >
<div class= "col-md-12">
<img id="logo" src="/assets/Images/home/Logo.png">
</div>
</div>
<div class="row align-items-center justify-content-around fixed-bottom">
<div class="col-md-2 col-sm-2">
<img id="addButton" routerLink="/addPlayer" onmouseover="this.src='../../../assets/Images/generic/addIcon_hover.png'" src="../../../assets/Images/generic/addIcon.png" onmouseout="this.src='/assets/Images/generic/addIcon.png'" />
</div>
<div class="col-md-2 col-sm-2">
<p>Players Waiting: {{playerWaiting}} </p>
</div>
<div class="col-md-2 col-sm-2">
<p>Listed Players: {{playersListed}}</p>
</div>
<div class="col-md-2 col-sm-2">
<img id="searchButton" routerLink="/search" onmouseover="this.src='../../../assets/Images/generic/searchIcon_hover.png'" src="../../../assets/Images/generic/searchIcon.png" onmouseout="this.src='/assets/Images/generic/searchIcon.png'" />
</div>
</div>
</div>
css:
div{
border: yellow 1px solid;
}
#menuIcon{
width: 100%;
height: auto;
}
#helpIcon{
width: 100%;
height: auto;
}
#addButton{
width: 100%;
height: auto;
max-width: 127px;
}
#searchButton{
width: 100%;
height: auto;
max-width: 127px;
}
https://www.codeply.com/p/Xd0xi5hFNc
Couple of things.
To be able to fill several rows, you'd need a height for the container div.
Current HTML
<div class="container-fluid">
Update as
<div class="container-fluid d-flex vh-100 flex-column">
Now you can easily make your middle div fill the remaining gap by adding a fill-height class.
Current HTML
<div class="row row align-items-center mh-100" >
Update as
<div class="row row align-items-center fill-height">
Add this to your css
.fill-height {
flex: 1;
}
.fixed-bottom {
position: sticky;
}
jsFiddle
For screens, I put padding at 0 for container-fluid but the screen still does not fill the screen that is less than or equal to width 1024.
.container-fluid {
background-color: #e2e5f4;
height: 600px;
margin-bottom: 200px;
padding: 0;
}
What works:
at 1024- width: 101%;
at 768- width: 112%;
at 320- width: 112%;
HTML code:
<div class="container-fluid p-0">
<div class="row border bg-danger p-0">
<div class="col-md-7 p-0">
<div class="MededX">
MEDedX
</div>
<div class="capture_tittle">
liunhliunh
<br> lunliu luinu liuni
<br> nllniun Nilo ahh
</div>
<div class="subCapture_tittle">
LOerm Swaminarayan swmainarayan swaminarayan swaminarayan
</div>
</div>
<div class="col-md-5 p-0 ">
<div>
<img class= "hex" src="hex.png">
</div>
<div>
<img class= "medIpad" src="medipad.png">
</div>
<div>
<img class= "plantsLeft" src="plantsLeft.png">
</div>
</div>
</div>
</div>
What's the problem? How do I fix it?
Use a custom wrapper and set its width to 100%.
.container-full {
margin: 0 auto;
width: 100%;
}
or a fast solution
style="width:100%"
I know this may be easy for most of you but I'm stuck with this issue.
I need to implement this design:
Layout Design
... and for now I've got this Current layout.The general structure is a row with col-3 and col-9, this col-9 has two rows, one for name and job title, and the other one for statistics in the page (col-3 for each of them). I need them to fill the height of his parent, but height property doesn't work. Which could be a clean solution? Thanks a lot.
Here is the structure, it's pretty simple tho.
<div class="row profile-header">
<div class="col-md-3 user-image text-center">
<img ...>
<span>..</span>
</div>
<div class="col-md-9">
<div class="row">
<div class="col-md-12">
<h2 class="text-white">...</h2>
<h4 class="text-muted">...</h4>
</div>
</div>
<div class="row text-center">
<div class="col-md-3 stat">
<h3>...</h3>
<small>...</small>
</div>
...
</div>
</div>
Use position: relative on the parent and then position:absolute; bottom: 0; on the children.
Nice explanation there.
Flexbox would be my recommendation although CSS tables might be an option too.
Codepen Demo
Snippet Demo (view Fullscreen)
.user-image {
background: pink;
}
.user-image img {
display: block;
margin: auto;
}
.profile-header {
display: flex;
}
.right {
background: #c0ffee;
display: flex;
flex-direction: column;
}
.stats {
margin-top: auto;
}
.stat {
background: lightgrey;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="row profile-header">
<div class="col-md-3 user-image text-center">
<img src="http://www.fillmurray.com/300/200" alt="" />
<span>User Ranking</span>
</div>
<div class="col-md-9 right">
<div class="row">
<div class="col-md-12">
<h2 class="text-white">User Name</h2>
<h4 class="text-muted">reference</h4>
</div>
</div>
<div class="row text-center stats">
<div class="col-md-3 stat">
<h3>Some Stat</h3>
<small>Some Stat</small>
</div>
<div class="col-md-3 stat">
<h3>Some Stat</h3>
<small>Some Stat</small>
</div>
<div class="col-md-3 stat">
<h3>Some Stat</h3>
<small>Some Stat</small>
</div>
<div class="col-md-3 stat">
<h3>Some Stat</h3>
<small>Some Stat</small>
</div>
</div>
</div>