CSS Flexbox: Center container with left-justified elements [duplicate] - html

This question already has answers here:
Flex-box: Align last row to grid
(35 answers)
Targeting flex items on the last or specific row
(10 answers)
Closed 5 years ago.
I've set up a short demo here: codepen.io. I have a flexbox with a number of boxes in it. I've set the minimum width of the container to be 3 boxes, and the maximum to be 4. It works, but I'd like the last element to be left justified, instead of floating in the middle. Any way I can left-justify the elements and simply center the container?
CSS code for the container is here, where the width/height of one box is 100px:
.container {
padding: 1em;
display: flex;
flex-wrap: wrap;
min-width: calc(300px + 6em);
max-width: calc(400px + 8em);
align-items: center;
justify-content: center;
margin: 0 auto;
background: red;
}
Thanks!
Edit: The solution I'm looking for is not as simple as making the last item align to grid; more specifically, I would like the screen width to be dynamic, so the container elements are always horizontally centered and flush with their container.

Use margin-right: auto; on your last item (using :last-child) to add a margin that will push your box to the left:
.container {
padding: 1em;
display: flex;
overflow: auto;
flex-wrap: wrap;
min-width: calc(300px + 6em);
max-width: calc(400px + 8em);
align-items: center;
justify-content: center;
background: red;
}
.box {
width: 100px;
height: 100px;
background: #000;
margin: 1em;
display: block;
}
.box:last-child { margin-right: auto; }
<h1> try resizing window</h1>
<div class="container">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>

I changed the justify-content to "flex-start" using Chrome's "Inspect" function.
That seemed to produce the results that you want.

Related

I can't center div using flexbox [duplicate]

This question already has answers here:
How can I vertically center a div element for all browsers using CSS?
(48 answers)
How can I horizontally center an element?
(133 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 2 days ago.
So I’ve tried centering a div, but only the children get effected.
What am I doing wrong?
Here’s my current code:
<div class="card">
<img src="images/image-equilibrium.jpg" alt="Image">
</div>
And my CSS:
.card {
display: flex;
justify-content: center;
align-items: center;
width: 200px;
height: 200px;
background-color: black;
}
Here’s what’s actually happening:
Use the flexbox on the parent, not on the div you want to center:
.parent {
display: flex;
justify-content: center;
align-items: center;
}
.card {
width: 200px;
height: 200px;
background-color: black;
}
<div class="parent">
<div class="card">
<img src="images/image-equilibrium.jpg" alt="">
</div>
</div>
If you want to keep the HTML structure (not inserting parent), you could just use the horizontal margin auto like this:
.card {
width: 200px;
height: 200px;
background-color: black;
margin: 0 auto;
}
<div class="card">
<img src="images/image-equilibrium.jpg" alt="">
</div>
EDIT: OP wants to center it both vertically and horizontally.
If you want to do that, you need to modify the most top parent as well (the <body> tag) and make its height 100%, like this:
body {
height: 100vh;
margin: 0;
}
.parent {
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.card {
width: 200px;
height: 200px;
background-color: black;
}
<div class="parent">
<div class="card">
<img src="images/image-equilibrium.jpg" alt="">
</div>
</div>
The properties justify-content and align-items were applied for the children of .card. If you want to center the .card you will have to set the display property to flex for the parent of .card. or if the .card has a fixed width which in your case it has, you can also set the margin-x:auto on the .card, by doing so it will be aligned at the center of it's parent container. Hope I was able to make you understand.

Align div at bottom of div container [duplicate]

This question already has answers here:
How can I position my div at the bottom of its container?
(25 answers)
Closed 1 year ago.
I know this question came up many times but none of the suggested solutions is working for me so I opened up a code sandbox and made an example.
What I want
I want to place an item at the bottom of a div that already has some children. In my code sandbox the desired div to place at the bottom has a className of note__actions.
What I tried
Setting the container to position: relative and the item to position: absolute
Using flexbox and setting the container to display: flex and the item to align-self: flex-end
Multiple other things like vertical-align, and making empty items to fill up the space in-between
Div setup
<div className="flexBoxContainer" />
<div className="item">
<div>Header</div>
<div>Title</div>
<div>Paragraph</div>
<div className="bottom__item">Actions>/div> // Only this div should go to the bottom of the "item" div
</div>
</div>
Sandbox
https://codesandbox.io/s/infallible-sound-wf166?file=/src/App.js
Give the container display: flex; justify-content: space-between; - that will pull the content from top to bottom. If you want only the last item on the bottom and the rest on the top, wrap the rest with a div, like
<div className="flexBoxContainer" />
<div className="item">
<div>
<div>Header</div>
<div>Title</div>
<div>Paragraph</div>
</div>
<div className="bottom__item">Actions>/div>
</div>
</div>
You are already using flexbox which is great. Now, use margin-top:auto on note__actions to push the div to the bottom.
.note__actions {
margin-top: auto;
}
You could add a flex: 1; to the flexible content, for instance
.note {
background-color: rgb(255, 255, 211);
border-radius: 15px;
height: 400px;
width: 200px;
padding: 24px;
display: flex;
flex-direction: column;
}
.note-content {
flex: 1;
}
.note-action {
align-self: flex-end;
}
<div class="note">
<div>Peter Holbert - 09. Jul 21 23:28</div>
<div>Note Title</div>
<div class="note-content">Aldus PageMaker including versions of Lorem Ipsum.</div>
<div class="note-action">Action 1</div>
</div>
You can use a grid for that.
Change this in your Style on codesandbox.io:
.note {
background-color: rgb(255, 255, 211);
border-radius: 15px;
width: 200px;
padding: 24px;
display: grid;
/*flex-direction: column;*/
grid-template-rows: 50px 80px 1fr 80px; /* height: 100%; */
/* align-items: flex-end; */
/* position: relative; */
}
.note__actions_bot {
background-color: red;
height: 100px;
display: grid;
justify-content: center;
align-items: center;
}
I recommend this awesome Video to get a nice overview and some hint's how to do, and when to use which one of flex, grid and other solutions:
https://www.youtube.com/watch?v=qm0IfG1GyZU&feature=youtu.be
Add margin-top : auto to the note__actions element

Draw a verticle line between two vertically placed divs - CSS?

I am trying to achieve something like this
Circle with letters starts at starting of first div and second div with a line separater in between. Here the length of line should be calculated based on space in middle of two divs. I tried couple of ways but no luck. Please suggest me an approach to do this.
Update:
I tried #Vadim answer, but in my case separater length should be dynamic, not static, and div's are like this:
<div class="container">
<div class="first-div">
<div class="letter">Q</div>
<div>another div</div>
</div>
<div class="second-div">
<div class="letter">A</div>
<div>another div</div>
</div>
</div>
I would like to draw vertical line between letters. Height of line should be dynamic according to inner divs height.
You can use flexbox to achieve desired layout:
.container {
background-color: #ccc;
padding: 15px;
/* align items in one column and take space defined by content */
/* this is used for centering separator */
display: inline-flex;
flex-direction: column;
}
.letter {
width: 25px;
height: 25px;
background-color: #fff;
border-radius: 50%;
/* styles for text centering */
display: flex;
justify-content: center;
align-items: center;
}
.separator {
width: 5px;
height: 50px;
border-radius: 3px;
background-color: #fff;
margin-top: 5px;
margin-bottom: 5px;
/* center horizontally */
align-self: center;
}
<div class="container">
<div class="letter">Q</div>
<div class="separator"></div>
<div class="letter">A</div>
</div>

Keep one element centered between two elements of different widths in flexbox

I am making a music playback controller, and the container has 3 sections: left, center, and right. However, since the left and right sides have different widths, the center section isn't in the true center of the div, but I need it to be. I am using flexbox's space-between option to layout the items.
#container {
display: flex;
justify-content: space-between;
background-color: lightgrey;
}
#container > div {
height: 100px;
border: 2px dashed red;
/*This is only for looks*/
text-align: center;
padding: 5px;
}
<div id="container">
<div>Left Side</div>
<div>I want this centered</div>
<div>Right Side (Extra text for extra length)</div>
</div>
You can use margins to approximate centering. But in order to get perfect centering with flexbox that's consistent across a variety of viewports, you'll have to slightly modify your HTML somewhat.
You need to turn the direct children of #container into flex containers themselves with a display:inline-flex declaration and give them a flex value of 1 and justify-content: center.
From there, you add your content into child divs. To get alignment on the left and right divs, use margin-right: auto and margin-left: auto, respectively.
#container {
display: flex;
background-color: lightgrey;
}
.flex {
flex: 1;
display: inline-flex;
justify-content: center;
}
.flex > div {
height: 100px;
border: 2px dashed red;
text-align: center;
padding: 5px;
}
.left div {
margin-right: auto;
}
.right div {
margin-left: auto;
}
<div id="container">
<div class="left flex">
<div>Left Side</div>
</div>
<div class="center flex">
<div>I want this centered</div>
</div>
<div class="right flex">
<div>Right Side (Extra text for extra length)</div>
</div>
</div>

Is there any way to align float elements in center

You can see there is an extra space after fifth box. Is there any way to align it in center ?
Please note these boxes can be of any no.
Update:
Here is the jsfiddle link
http://jsfiddle.net/esy2hgLm/
I'm trying to achieve the same using display: flex for dynamic width
You can use some flexbox magic instead of floats:
.wrapper {
display: flex; /* Magic begins */
flex-wrap: wrap; /* Multiline */
justify-content: center; /* Center items */
}
.wrapper {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.box {
width: 100px;
height: 100px;
background: seagreen;
margin: 20px;
float: left;
}
<div class="wrapper">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
Try adding this to your CSS:
.wrapper {
width: 700px;
margin: 0 auto
}
Please note that this requires the wrapper to have a fixed width (which means it won't be ideal if the number of boxes changes).
I'd suggest taking a look at Flexbox for centering elements. Here's a couple of articles that you should read:
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Flexible_boxes
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
http://caniuse.com/#feat=flexbox
Note the lack of support for < IE9
Here's how that would look:
.wrapper {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
You'll have to align the wrapper, not the items. Make sure the wrapper is not wider than the content and add margin: 0 auto; to the wrapper CSS, which should center it in it's parent.
You can use a margin of 0 auto.
Try this:
.wrapper {
width: 600px;
margin: 0 auto;
display: block;
}