This question already has answers here:
Align inline-block DIVs to top of container element
(5 answers)
Why is this inline-block element pushed downward?
(8 answers)
Closed 1 year ago.
I would like to align the boxes vertically. It works fine if the words in the box is all in one line. However if the words in one of the box goes to 2 lines or more the box will not align properly.
.col {
display: inline-block;
width: 31%;
padding: 2px 5px;
}
.box {
height: 80px;
margin: 0 auto;
display: flex;
background-color: #f4f4f4;
justify-content: center;
align-items: center;
font-size: 20px;
text-decoration: none;
font-weight: normal;
}
<div class="col">
<div class="box">Box</div>
</div>
<div class="col">
<div class="box">This is a box</div>
</div>
<div class="col">
<div class="box">Please see inside this box for some contents to read</div>
</div>
you can use flex box to easily avoid this issue.
put all cols in a flex wrapper.
<div class="wrapper">
<div class ="col">
<div class="box">Box</div>
</div>
<div class ="col">
<div class="box">This is a box</div>
</div>
<div class ="col">
<div class="box">Please see inside this box for some contents to read</div>
</div>
</div>
.wrapper{
display: flex;
}
Its because the <div class="box"> overflow with content.
you could adjust class .box height and add some padding to align them right properly.
.box {
height: 80px;
margin: 0 auto;
display: flex;
background-color: #f4f4f4;
justify-content: center;
align-items: center;
font-size: 20px;
text-decoration: none;
font-weight: normal;
height: 100%;
padding: 28px;
}
Heres an example https://codepen.io/mcfaith9/pen/vYmJvLK
Add a flex parent .row which will contain your col cell columns
/*QuickReset*/ * {margin:0; box-sizing:border-box;}
.row {
display: flex;
}
.col {
display: flex;
flex-direction: column;
}
.cell-4 {
flex: 1 1 33.333%;
}
.box {
padding: 10px;
border: 1px solid #aaa;
background-color: #f4f4f4;
min-height: 80px;
}
<div class="row">
<div class="col cell-4">
<div class="box">Box</div>
</div>
<div class="col cell-4">
<div class="box">This is a box</div>
</div>
<div class="col cell-4">
<div class="box">Please see inside this box for some contents to read</div>
</div>
</div>
I have a flex wrapper with 3 columns, in third column there is rotated text, how can I place the text to the bottom right position of the column?
What I want:
What I have:
.wrapper {
border: 1px solid black;
padding-top: 50px;
display: flex;
justify-content: space-between;
}
.column-2 {
transform-origin: left bottom;
transform: rotate(-90deg);
}
<div class="wrapper">
<div class="column-1">Column 1</div>
<div class="column-2">Column 2</div>
</div>
writing-mode can also be used to rotate text :
The writing-mode CSS property sets whether lines of text are laid out horizontally or vertically, as well as the direction in which blocks progress. When set for an entire document, it should be set on the root element (html element for HTML documents).
possible examples:
.wrapper {
border: 1px solid black;
display: flex;
justify-content: space-between;
align-items: center;
}
.column-2 {
writing-mode: vertical-lr;
border: solid;
margin: 2em;
}
/* also */
.wrapper.bis {
align-items: end;
}
.bis .column-2 {
margin: 0;
}
<div class="wrapper">
<div class="column-1">Column 1</div>
<div class="column-2">Column 2</div>
</div>
<hr>
<div class="wrapper bis">
<div class="column-1">Column 1</div>
<div class="column-2">Column 2</div>
</div>
An older similar answer : How do I center a transformed and rotated div? (when prefix was to be used ) applied here without prefix would do :
.wrapper {
border: 1px solid black;
display: flex;
justify-content: space-between;
align-items: center;
}
.column-2 {
writing-mode: vertical-lr;
transform:scale(-1);
border: solid;
margin: 2em;
}
/* also */
.wrapper.bis {
align-items: end;
}
.bis .column-2 {
margin: 0;
}
<div class="wrapper">
<div class="column-1">Column 1</div>
<div class="column-2">Column 2</div>
</div>
<hr>
<div class="wrapper bis">
<div class="column-1">Column 1</div>
<div class="column-2">Column 2</div>
</div>
If you want to stick to transform, here is an older answer of mine that allows to stretch the container according to the string length : How to display vertical text in table headers with auto height / without text overflow?
text-orientation can be usefull too :
The text-orientation CSS property sets the orientation of the text characters in a line. It only affects text in vertical mode (when writing-mode is not horizontal-tb). It is useful for controlling the display of languages that use vertical script, and also for making vertical table headers.
You can do a display: flex on the columns div, and adjust them as you need. I've made an example on codepen
<div class="parent">
<div class="child first">Column 1</div>
<div class="child second">Column 2</div>
<div class="child third"><p>Column 3</p></div>
</div>
And then the CSS:
.parent {
display: flex;
}
.child {
width: 20%;
display: flex;
height: 100px;
border: 2px solid black;
}
.first {
font-size: 40px;
}
.second {
align-items: flex-end;
justify-content: center;
}
.third p {
transform: rotate(-90deg);
}
You can do something like this
.inner {
position: absolute;
top: 50%;
left: 50%;
}
.rotate {
transform: translateX(-50%) translateY(-50%) rotate(-90deg);
}
Take a look at transform-origin
.wrapper {
font-family: Arial;
display: flex;
flex-direction: row;
}
.wrapper div {
padding: 0 20px;
}
.wrapper h3 {
font-size: 0.4em;
transform: rotate(-90deg);
transform-origin: 35px 16px;
}
<div class="wrapper">
<div>
<h1>Column 1</h1>
</div>
<div>
<h2>Column 2</h2>
</div>
<div>
<h3>Column 3</h3>
</div>
</div>
i want to create a flexbox layout title, detail, and other content inside one div and a div with content close next to this div and should be placed in the center of the main box (named container).
What i have tried to do?
I created a div named container and placed title and other details inside it. In doing so, close div is also inside the div named container. It should be outside the container div and in middle of it.
I want to create a layout like in picture below,
Could someone help me solving this? link to code
https://codepen.io/anon/pen/BbaKwy
.box_wrapper {
width: calc(100% - 450px);
border: 1px solid green;
margin: 0 auto;
}
.container {
display: flex;
justify-content: space-between;
border: 1px solid blue;
padding: 10px;
}
<div class="box_wrapper">
<div class="container">
<div class="content">
<div>title</div>
<div>detail</div>
<div>
<div>ticket number</div>
<div>
<h2>Debug</h2>
someresponse
<div/>
</div>
</div>
</div>
<div>close</div></div></div>
Thanks.
You can achieve the desired result by moving the close div and adding a couple more styles to box_wrapper
.box_wrapper {
display: flex;
flex-direction: row;
align-items: center;
border: none;
margin: 0 auto;
}
.container {
display: flex;
justify-content: space-between;
width: calc(100% - 450px);
border: 1px solid blue;
padding: 10px;
}
<div class="box_wrapper">
<div class="container">
<div class="content">
<div>title</div>
<div>detail</div>
<div>
<div>ticket number</div>
<div>
<h2>Debug</h2>
someresponse
<div/>
</div>
</div>
</div>
</div>
</div>
<div>close</div>
adding the flex-direction:row to .box_wrapper is what aligns it to the right, and setting align-items:center is what positions it in the middle vertically.
EDIT:
If you want to achieve this while keeping the close div within the box_wrapper class, you can do so as follows:
.box_wrapper {
display: flex;
flex-direction: row;
border: none;
margin: 0 auto;
width: calc(100% - 450px);
}
.container {
display: flex;
justify-content: space-between;
align-items: center;
flex-grow: 1;
padding: 10px;
}
.content {
border: 1px solid blue;
flex-grow: 1;
}
<div class="box_wrapper">
<div class="container">
<div class="content">
<div>title</div>
<div>detail</div>
<div>
<div>ticket number</div>
<div>
<h2>Debug</h2>
someresponse
<div/>
</div>
</div>
</div>
</div>
<div>close</div>
</div>
I would like to have three separate vertical columns, is there a way I can change my code to make the columns vertical instead of horizontal (like they are now).
.cols {
font-weight: bold;
min-height: 50%;
min-width: 90%;
background: #000000;
margin-bottom: 15px;
display: table;
table-layout: fixed;
}
.cols div {
position: relative;
background: #232323;
}
.col {
display: table-cell;
}
<div class="cols">
<div class="col">Column 1</div>
<div class="col">Column 2</div>
<div class="col">Column 3</div>
</div>
Currently I have three horizontal boxes stretching across an outside container, I would like the three boxes to be evenly set out in vertical columns, if that makes sense.
If I understand what you mean, this can be done using flex:
.cols {
min-height: 50%;
min-width: 90%;
background: #000000;
margin-bottom: 15px;
display: -webkit-flex;
display: flex;
-webkit-flex-direction: column;
flex-direction: column;
}
.cols div {
background: #232323;
color: #fff;
}
<div class="cols">
<div class="col">Column 1</div>
<div class="col">Column 2</div>
<div class="col">Column 3</div>
</div>
Is there a more flexbox-ish way to right-align "Contact" than to use position: absolute?
.main {
display: flex;
}
.a,
.b,
.c {
background: #efefef;
border: 1px solid #999;
}
.b {
flex: 1;
text-align: center;
}
.c {
position: absolute;
right: 0;
}
<h2>With title</h2>
<div class="main">
<div class="a">Home</div>
<div class="b">Some title centered</div>
<div class="c">Contact</div>
</div>
<h2>Without title</h2>
<div class="main">
<div class="a">Home</div>
<!--<div class="b">Some title centered</div>-->
<div class="c">Contact</div>
</div>
http://jsfiddle.net/vqDK9/
A more flex approach would be to use an auto left margin (flex items treat auto margins a bit differently than when used in a block formatting context).
.c {
margin-left: auto;
}
Updated fiddle:
.main { display: flex; }
.a, .b, .c { background: #efefef; border: 1px solid #999; }
.b { flex: 1; text-align: center; }
.c {margin-left: auto;}
<h2>With title</h2>
<div class="main">
<div class="a">Home</div>
<div class="b">Some title centered</div>
<div class="c">Contact</div>
</div>
<h2>Without title</h2>
<div class="main">
<div class="a">Home</div>
<!--<div class="b">Some title centered</div>-->
<div class="c">Contact</div>
</div>
<h1>Problem</h1>
<p>Is there a more flexbox-ish way to right align "Contact" than to use position absolute?</p>
Here you go. Set justify-content: space-between on the flex container.
.main {
display: flex;
justify-content: space-between;
}
.a, .b, .c { background: #efefef; border: 1px solid #999; }
.b { text-align: center; }
<h2>With title</h2>
<div class="main">
<div class="a">Home</div>
<div class="b">Some title centered</div>
<div class="c">Contact</div>
</div>
<h2>Without title</h2>
<div class="main">
<div class="a">Home</div>
<!-- <div class="b">Some title centered</div> -->
<div class="c">Contact</div>
</div>
You can also use a filler to fill the remaining space.
<div class="main">
<div class="a">Home</div>
<div class="b">Some title centered</div>
<div class="filler"></div>
<div class="c">Contact</div>
</div>
.filler{
flex-grow: 1;
}
I have updated the solution with 3 different versions. This because of the discussion of the validity of using an additional filler element. If you run the code snipped you see that all solutions do different things. For instance setting the filler class on item b will make this item fill the remaining space. This has the benefit that there is no 'dead' space that is not clickable.
<div class="mainfiller">
<div class="a">Home</div>
<div class="b">Some title centered</div>
<div class="filler"></div>
<div class="c">Contact</div>
</div>
<div class="mainfiller">
<div class="a">Home</div>
<div class="filler b">Some title centered</div>
<div class="c">Contact</div>
</div>
<div class="main">
<div class="a">Home</div>
<div class="b">Some title centered</div>
<div class="c">Contact</div>
</div>
<style>
.main { display: flex; justify-content: space-between; }
.mainfiller{display: flex;}
.filler{flex-grow:1; text-align:center}
.a, .b, .c { background: yellow; border: 1px solid #999; }
</style>
Or you could just use justify-content: flex-end
.main { display: flex; }
.c { justify-content: flex-end; }
If you want to use flexbox for this, you should be able to, by doing this (display: flex on the container, flex: 1 on the items, and text-align: right on .c):
.main { display: flex; }
.a, .b, .c {
background: #efefef;
border: 1px solid #999;
flex: 1;
}
.b { text-align: center; }
.c { text-align: right; }
...or alternatively (even simpler), if the items don't need to meet, you can use justify-content: space-between on the container and remove the text-align rules completely:
.main { display: flex; justify-content: space-between; }
.a, .b, .c { background: #efefef; border: 1px solid #999; }
Here's a demo on Codepen to allow you to quickly try the above.
As easy as
.main {
display: flex;
flex-direction:row-reverse;
}
margin-left: auto works well. But clean flex box solution would be space-between in the main class. Space between works well if there is two or more elements. I have added a solution for single element as well.
.main { display: flex; justify-content: space-between; }
.a, .b, .c { background: #efefef; border: 1px solid #999; padding: 0.25rem; margin: 0.25rem;}
.b { flex: 1; text-align: center; }
.c-wrapper {
display: flex;
flex: 1;
justify-content: flex-end;
}
.c-wrapper2 {
display: flex;
flex: 1;
flex-flow: row-reverse;
}
<div class="main">
<div class="a">Home</div>
<div class="b">Some title centered</div>
<div class="c">Contact</div>
</div>
<div class="main">
<div class="a">Home</div>
<div class="c">Contact</div>
</div>
<div class="main">
<div class="c-wrapper">
<a class="c" href="#">Contact</a>
<a class="c" href="#">Contact2</a>
</div>
</div>
<div class="main">
<div class="c-wrapper2">
<span class="c">Contact</span>
<span class="c">Contact2</span>
</div>
</div>
Add the following CSS class to your stylesheet:
.my-spacer {
flex: 1 1 auto;
}
Place an empty element between the element on the left and the element you wish to right-align:
<span class="my-spacer"></span>
If you need one item to be left aligned (like a header) but then multiple items right aligned (like 3 images), then you would do something like this:
h1 {
flex-basis: 100%; // forces this element to take up any remaining space
}
img {
margin: 0 5px; // small margin between images
height: 50px; // image width will be in relation to height, in case images are large - optional if images are already the proper size
}
Here's what that will look like (only relavent CSS was included in snippet above)
'justify-content: flex-end' worked within price box container.
.price-box {
justify-content: flex-end;
}
For those using Angular and Flex-Layout, use the following on the flex-item container:
<div fxLayout="row" fxLayoutAlign="flex-end">
See fxLayoutAlign docs here and the full fxLayout docs here.
I find that adding 'justify-content: flex-end' to the flex container solves the problem while 'justify-content: space-between' doesnt do anything.
Example code based on answer by TetraDev
Images on right:
* {
outline: .4px dashed red;
}
.main {
display: flex;
flex-direction: row;
align-items: center;
}
h1 {
flex-basis: 100%;
}
img {
margin: 0 5px;
height: 30px;
}
<div class="main">
<h1>Secure Payment</h1>
<img src="https://i.stack.imgur.com/i65gn.png">
<img src="https://i.stack.imgur.com/i65gn.png">
</div>
Images on left:
* {
outline: .4px dashed red;
}
.main {
display: flex;
flex-direction: row;
align-items: center;
}
h1 {
flex-basis: 100%;
text-align: right;
}
img {
margin: 0 5px;
height: 30px;
}
<div class="main">
<img src="https://i.stack.imgur.com/i65gn.png">
<img src="https://i.stack.imgur.com/i65gn.png">
<h1>Secure Payment</h1>
</div>