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>
Related
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>
This question already has answers here:
How does flex-wrap work with align-self, align-items and align-content?
(2 answers)
Closed 2 years ago.
so I am trying to use a flexbox to space evenly objects inside of it. The problem is that the flexbox adds space to the bottom of element but not at the top.
Here is the code of the section, the divs inside of it are the elements that I would like to space evenly inside it.
#Items {
width: 100%;
height: 800px;
display: flex;
flex-flow: row wrap;
justify-content: space-around;
}
#Items a {
text-decoration: none;
}
/* Classes */
.Box {
width: 320px;
height: 100px;
background-color: #f2f2f2;
}
.Box h3 {
font-family: 'Roboto', serif;
font-size: 22px;
color: #111;
}
<section id="Items">
<div class="Box">
<h3>Case</h3>
</div>
<div class="Box">
<h3>Case</h3>
</div>
<div class="Box">
<h3>Case</h3>
</div>
<div class="Box">
<h3>Case</h3>
</div>
<div class="Box">
<h3>Case</h3>
</div>
<div class="Box">
<h3>Case</h3>
</div>
</section>
The divs are being indeed spaced evenly inside the section with the flexbox EXCEPT that there is no space between the top of the first div in the flexbox and the top of the flexbox, but there is space between the bottom of the last div box and the bottom of the flexbox. Is there a clean way to fix this? I don't want to add padding or margin to my fist div box because I'd like to turn the div box into an anchor(or link) later on so users can click it and navigate to other parts of the website.
Thanks!
To vertical align the items, so there is an even margin above and below, use
align-items: center;
#Items {
width: 100%;
height: 800px;
display: flex;
flex-flow: row wrap;
justify-content: space-around;
border: 1px solid black;
align-items: center;
}
#Items a {
text-decoration: none;
}
/* Classes */
.Box {
width: 320px;
height: 100px;
background-color: #f2f2f2;
}
.Box h3 {
font-family: 'Roboto', serif;
font-size: 22px;
color: #111;
}
<section id="Items">
<div class="Box">
<h3>Case</h3>
</div>
<div class="Box">
<h3>Case</h3>
</div>
<div class="Box">
<h3>Case</h3>
</div>
<div class="Box">
<h3>Case</h3>
</div>
<div class="Box">
<h3>Case</h3>
</div>
<div class="Box">
<h3>Case</h3>
</div>
</section>
Your flex container is in row direction.
That means that justify-content: space-around will pad the horizontal edges.
If you want the padding on the vertical edges, add align-content: space-around.
#Items {
height: 800px;
display: flex;
flex-flow: row wrap;
justify-content: space-around;
align-content: space-around; /* new */
}
#Items a {
text-decoration: none;
}
/* Classes */
.Box {
width: 320px;
height: 100px;
background-color: #f2f2f2;
}
.Box h3 {
font-family: 'Roboto', serif;
font-size: 22px;
color: #111;
}
<section id="Items">
<div class="Box">
<h3>Case</h3>
</div>
<div class="Box">
<h3>Case</h3>
</div>
<div class="Box">
<h3>Case</h3>
</div>
<div class="Box">
<h3>Case</h3>
</div>
<div class="Box">
<h3>Case</h3>
</div>
<div class="Box">
<h3>Case</h3>
</div>
</section>
I think you need of center ("h3") vertically that's inside the divs inside the section!
if I got that right then you need to add the following to ".Box" , that will align them vertically display: flex; align-items: center;, if you also need to align them horizontally then add "justify-content: center;"
.Box {
width: 320px;
height: 100px;
background-color: #f2f2f2;
display: flex;
align-items: center;
}
This question already has answers here:
Why does the outer <div> here not completely surround the inner <div>?
(2 answers)
Closed 2 years ago.
I have a scrollable area inside which there are a div and a collection of smaller divs.
The yellow container takes the width of the visible viewport of the scrollable area.
How do I make it "wrap" the whole set of pink rectangles automatically like how it happens in a regular non-overflow div?
https://codepen.io/sergeibasharov/pen/mdEdKWO
<div class="scroll-area">
<div class="header">
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell">3</div>
<div class="cell">4</div>
<div class="cell">5</div>
<div class="cell">6</div>
<div class="cell">7</div>
<div class="cell">8</div>
</div>
</div>
.scroll-area{
background: gray;
width: 500px;
height: 400px;
overflow: auto
}
.header{
background: yellow;
display: flex;
padding: 20px;
}
.cell{
width: 100px;
min-width: 100px;
padding: 8px 0;
margin: 2px;
text-align: center;
background: pink;
}
To wrap children of an element with display: flex, use flex-wrap: wrap.
.scroll-area{
background: gray;
width: 500px;
height: 400px;
overflow: auto
}
.header{
background: yellow;
display: flex;
padding: 20px;
flex-wrap: wrap;
}
.cell{
width: 100px;
min-width: 100px;
padding: 8px 0;
margin: 2px;
text-align: center;
background: pink;
}
<div class="scroll-area">
<div class="header">
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell">3</div>
<div class="cell">4</div>
<div class="cell">5</div>
<div class="cell">6</div>
<div class="cell">7</div>
<div class="cell">8</div>
</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>
This question already has answers here:
How to disable equal height columns in Flexbox?
(4 answers)
Closed 6 years ago.
I would like to adapt the parent div height to children height in CSS only.
In my code, both columns have same height.
I would like column 2 to have a smaller height as it contains less divs to show.
Let me show you on an example:
#mainPane {
display: flex;
background-color: blue;
}
.column {
width: 250px;
background-color: gray;
margin: 5px 15px 5px 15px;
display: inline-block;
}
.news {
background-color: white;
}
<section id="mainPane">
<div class="column">
<div class="colHeader">Col1</div>
<div class="news">Line1</div>
<div class="news">Line2</div>
<div class="colFooter">Footer</div>
</div>
<div class="column">
<div class="colHeader">Col2</div>
<div class="colFooter">Footer</div>
</div>
</section>
Add align-items:flex-start; to #mainPane:
#mainPane{
display: flex;
align-items: flex-start;
background-color: blue;
}
.column {
width: 250px;
background-color: gray;
margin: 5px 15px 5px 15px;
}
.news{
background-color: white;
}
<section id="mainPane">
<div class="column">
<div class="colHeader">
Col1
</div>
<div class="news">
Line1
</div>
<div class="news">
Line2
</div>
<div class="colFooter">
Footer
</div>
</div>
<div class="column">
<div class="colHeader">
Col2
</div>
<div class="colFooter">
Footer
</div>
</div>
</section>