Align one container in center with display flex [duplicate] - css

This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 3 years ago.
.container {
display: flex;
}
.container>div {
border:2px solid red;
padding:15px;
}
<div class="container">
<div class="one">one</div>
<div class="two">two</div>
</div>
I want to align the <div class="two">two</div> in the middle of the page. The first div should still on the left side. How to align the block two in the center?

Can achieve this with the mix of position absolute and flex
.container {
display: flex;
justify-content: center;
position: relative;
}
.container>div {
border:2px solid red;
padding:15px;
}
.one-absolute {
position: absolute;
left: 0;
}
<div class="container">
<div class="one one-absolute">one</div>
<div class="two">two</div>
</div>

You can use css grid to do that
.container{
display: grid;
grid-template-columns: auto auto auto;
}
.container>div {
border:2px solid red;
padding:15px;
width:50px;
}
<div class="container">
<div class="item">one</div>
<div class="item">two</div>
</div>

Thanks for the good question,
we can use two flex properties along with container class.
justify-content:center; and align-items:center;
.container {
display: flex;
justify-content:center;
align-items:center;
background:pink;
min-height:200px;
}
.container>div {
border:2px solid red;
padding:15px;
}
<div class="container">
<div class="one">one</div>
<div class="two">two</div>
</div>
Note: I additionally add min-height:400px; and background:pink; for demonstration. You can remove both of those.

Flex may not be the right solution for this scenario.
Instead CSS positioning could be used to align one div at the center.
But if you still want flex, add a third empty div and hide it.
<div class="container">
<div class="one">one</div>
<div class="two">two</div>
<div class="three">three</div>
</div>
.
.container {
display: flex;
}
.container>div {
border:2px solid red;
padding:15px;
flex: 1;
}
.container>div.three {
opacity: 0;
}

Related

align-self is not located at the end of the container and it has space available to do so

I do not know what I'm doing wrong. I want the word "content" to be at the end of the container using flexbox.
|1| content|
thank you.
this is my live code:
https://jsfiddle.net/0tunwopb/1/
<div class="container">
<div class="container_flex">
<div>
1
</div>
<div class="container_content">
content
</div>
</div>
</div>
.container{
width:200px;
height:200px;
border:1px solid blue;
}
.container_flex{
display:flex;
flex-direction:row;
}
.container_flex div{
border:1px solid red;
}
.container_content{
flex-grow:1;
align-self:flex-end;
}
thanks
The .container_content element is already flush up against the right-hand edge of .container_flex.
If you want the text content to align to the right, you're looking for text-align: right:
.container {
width: 200px;
height: 200px;
border: 1px solid blue;
}
.container_flex {
display: flex;
flex-direction: row;
}
.container_flex div {
border: 1px solid red;
}
.container_content {
flex-grow: 1;
align-self: flex-end;
text-align: right;
}
<div class="container">
<div class="container_flex">
<div>
1
</div>
<div class="container_content">
content
</div>
</div>
</div>

A centered column without fixed width

I would like to create a page with a centred column, in which the individual items do not have a fixed width (see image).
I've tried using flexbox, but that seems to want to fill out the blocks over the entire box. I've also tried to use the normal margin: 0 auto approach, but that doesn't work if you don't have a fixed width.
Is there a way to do this? I prefer pure CSS of course.
Here's an example using a flexbox. No width settings anywhere.
.container {
display: flex;
flex-direction: column;
align-items: center;
}
.element {
background-color: lightblue;
border: thin solid blue;
}
span:not(first-child) {
margin-left: 1rem;
border: thin solid red;
}
<div class="container">
<div class="element">This is text</div>
<div class="element">This is also some text</div>
<div class="element">
<span>And even in different</span>
<span>boxes on the same row</span>
</div>
</div>
Here is an example, hope it will help ^^ :
.parent {
display:flex;
flex-direction:column;
justify-content:center;
align-items:center;
}
.child {
margin-bottom:20px;
min-width:200px;
min-height:80px;
background-color:#555;
}
.child-fluid {
min-width:100%;
display:flex;
padding:5px;
}
.content {
min-height:80px;
background-color:#AAA;
flex:1;
margin:5px;
}
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<div class="parent">
<div class="child"></div>
<div class="child"></div>
<div class="child" style="min-width: 350px;"></div>
<div class="child child-fluid">
<div class="content"></div>
<div class="content"></div>
</div>
<div class="child"></div>
</div>
</body>
</html>
with flex box you have to have a width set on the parent. It doesn't have to be a fixed width though you could do something like
.parent{
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 100%;
}
that might work.

Expand div when divs nested inside overflow

I am trying to figure out how to expand a div when the children contain divs that overflow (to show overlapping images);
div.container {
position:relative
}
div.column {
display: inline-block; // in my case I want to avoid wrapping
width: 180px;
}
div.item-contains-long-image {
display: block;
height: 25px;
overflow: visible;
position: relative;
}
I would like the container to expand vertically to contain the images overflowing from the inner div. I could add padding to the bottom of the div equivalent to the image height, but am looking for a better way.
#Teobis i took your answer as base for a flex example, hope you dont mind :)
div.container { /* this has been rewritten */
display: flex;
flex-direction: row;
}
div.column {
border:1px solid blue; /*just to show the size*/
display: inline-block; /* in my case I want to avoid wrapping */
width: 180px;
vertical-align:top;
}
div.item-contains-long-image {
display: inline-block;
height: 25px;
/* overflow: visible; no needed, default property*/
position: relative;
}
<div class="container">
<div class="column">
<div class="item-contains-long-image">
<img src="http://via.placeholder.com/180x270">
</div>
</div>
<div class="column">
<div class="item-contains-long-image">
<img src="http://via.placeholder.com/180x310">
</div>
</div>
<div class="column">
<div class="item-contains-long-image">
<img src="http://via.placeholder.com/180x110">
</div>
</div>
</div>
Is this structure what you are looking for??
First of all, you need white-space:nowrap; on the parent of display: inline-block;
or you could use flexbox;
div.container {
position:relative;
border:1px solid red; /*just to show the size*/
white-space:nowrap; /*Necesary for no wrap on .column*/
min-height:150px; /* minimum height */
}
div.column {
border:1px solid blue; /*just to show the size*/
display: inline-block; /* in my case I want to avoid wrapping */
width: 180px;
vertical-align:top;
}
div.item-contains-long-image {
display: inline-block;
height: 25px;
/* overflow: visible; no needed, default property*/
position: relative;
}
<div class="container">
<div class="column">
<div class="item-contains-long-image">
<img src="http://via.placeholder.com/180x270">
</div>
</div>
<div class="column">
<div class="item-contains-long-image">
<img src="http://via.placeholder.com/180x310">
</div>
</div>
<div class="column">
<div class="item-contains-long-image">
<img src="http://via.placeholder.com/180x110">
</div>
</div>
</div>
Hope this helps

How to make one element fill to the bottom of its container in Bootstrap 3?

I have a container element which has two elements:
A which is col-md-8
B which is col-md-4
C which is col-md-4
Now A is longer so it extends the container element.
I would like C to fill in the rest of the space such that height of A = height of B + height of C (including the margins).
How would I do this in bootstrap?
Try using CSS Flexbox. It will be to tidy to achieve this using bootstrap classes.
Have a look at the snippet below:
.section {
display: flex;
font-size: 30px;
color: #fff;
}
.sec-item {
flex: 1;
}
.left-section {
background: blue;
height: 100vh;
flex: 8;
}
.right-section {
display: flex;
flex-direction: column;
flex: 4;
}
.right-section .top {
background: red;
flex: 1;
}
.right-section .bottom {
background: green;
flex: 1;
}
<div class="section">
<div class="sec-item left-section">A</div>
<div class="sec-item right-section">
<div class="top">B</div>
<div class="bottom">C</div>
</div>
</div>
Hope this helps!
Try something like this. It uses Flex-property
*{
box-sizing:border-box;
}
.row{
display:flex;
flex-direction:row;
}
#leftDiv{
height:400px;
border:2px solid black;
}
#rightDiv{
border:2px solid black;
display:flex;
flex-direction:column;
}
.redOne{
background-color:red;
height:100px;
flex-shrink:0;
}
.greenOne{
background-color:green;
flex-shrink:0;
flex-grow:1;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-6" id="leftDiv">
</div>
<div class="col-md-6 col-sm-6 col-xs-6" id="rightDiv">
<div class="redOne"></div>
<div class="greenOne"></div>
</div>
</div>

set div width to content without inline-block and keep divs under each other center aligned

I want some divs to get their width from their content. Display:inline-block does this, but I also want the divs to be under each other, not next to each other as floated.
Using float:left instead of inline-block does this, but I want the divs to be center aligned, not left aligned. How can I do this?
on the parent div put white-space: pre-line;
on the child divs add clear : both
#wrapper{ text-align: center; white-space: pre-line; }
#div1, #div2{
display: inline-block;
clear: both;
border: 1px solid grey;
margin: 3px auto 3px auto;
width: auto;
}
<div id="wrapper">
<div id="div1" class="clearfix">some content here that is bigger</div>
<div id="div2" class="clearfix">some content here</div>
</div>
http://jsfiddle.net/judsonmusic/8HCWp/
Working jsFiddle Demo
Consider the following markup:
<div class="container">
<div class="text">apple</div>
<div class="text">banana</div>
<div class="text">kiwi</div>
<div class="text">orange</div>
</div>
Because you want to align your elements, you must use inline, then we will break
them with :after:
.container {
text-align: center;
}
.text {
background: yellow;
display: inline;
}
.text:after {
content: '';
display: block;
}
As mentioned in this thread, there's also a flex solution to this problem:
#container > p {
border-bottom: 2px solid black;
}
#container {
display: flex;
flex-flow: column;
align-items: flex-start;
}
<div id="container">
<p>A text</p>
<p>A text</p>
<p>A longer text</p>
</div>
html is
<div>
abc <div style="margin:4px auto;width:100px;">div1</div><br/>
abc <div style="margin:4px auto;width:100px;">div1</div><br/>
abc <div style="margin:4px auto;width:100px;">div1</div><br/>
abc <div style="margin:4px auto;width:100px;">div1</div><br/>
</div>
and style sheet is
div{
border:1px solid;
padding:10px;
display:inline-block;
}
check demo at http://jsfiddle.net/xupHN/

Resources