Vertical align inline-flex elements - css

I am trying to vertical-align 3 different inline-flex elements in a same div, but the last one doesn't fit as expected.
.container {
height: 100px;
width: 100%;
background-color: blue;
}
.container .left, .container .right {
height: 100%;
width: 30%;
}
.container .middle {
height: 100%;
width: 40%;
}
.container .left, .container .middle {
display: -webkit-inline-flex;
-webkit-justify-content: center;
-webkit-align-items: center;
display: -moz-inline-flex;
-moz-justify-content: center;
-moz-align-items: center;
}
.container .right {
display: -webkit-inline-flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-webkit-flex-direction: column;
-webkit-box-pack: center;
-webkit-flex-pack: center;
-webkit-justify-content: center;
-webkit-flex-align: center;
-webkit-align-items: center;
display: -moz-inline-flex;
-moz-justify-content: center;
-moz-align-items: center;
-moz-flex-direction: column;
}
.container .right ul {
padding: 0;
margin: 0;
}
<div class="container">
<div class="left">
<span>2014/01/21</span>
</div><div class="middle">
<button>Hello</button>
<button>Bye</button>
</div><div class="right">
<ul>
<li>Chocolate</li>
<li>Caramel</li>
<li>Watermelon</li>
</ul>
</div>
</div>

Just add vertical-align:top to .container .right :
.container .right {
display: -webkit-inline-flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-webkit-flex-direction: column;
-webkit-box-pack: center;
-webkit-flex-pack: center;
-webkit-justify-content: center;
-webkit-flex-align: center;
-webkit-align-items: center;
vertical-align: top;
}
JSFiddle

Related

Flexbox - I align items with flexbox but they are no center

So I am breaking my head over this. I have set global styles and I amtrying to align a logo and a navigation list one beneath the other. When I use flexbox to align it they are not actually in center. However, when I remove the global style for padding completely it aligns fine.
What seems to be the problem.
P.S. I used sass staright away.
* {
padding: 0;
margin: 0;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
html {
font-size: 5px;
}
body {
font-family: "Nunito", sans-serif;
}
li {
list-style: none;
}
a {
width: 100%;
text-decoration: none;
color: #033860;
}
.main-head {
width: 100%;
min-height: 20vh;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: horizontal;
-webkit-box-direction: normal;
-ms-flex-direction: row;
flex-direction: row;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: stretch;
-ms-flex-align: stretch;
align-items: stretch;
-ms-flex-wrap: nowrap;
flex-wrap: nowrap;
background-color: #05b2dc;
}
.main-head .container {
width: 80%;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-pack: space-evenly;
-ms-flex-pack: space-evenly;
justify-content: space-evenly;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-ms-flex-wrap: nowrap;
flex-wrap: nowrap;
}
.main-head .logo {
fill: white;
width: 10%;
}
.main-head .main-nav {
width: 100%;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: horizontal;
-webkit-box-direction: normal;
-ms-flex-direction: row;
flex-direction: row;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-ms-flex-wrap: nowrap;
flex-wrap: nowrap;
}
.main-head .main-nav .nav-list {
padding: initial;
width: 80%;
text-align: center;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: horizontal;
-webkit-box-direction: normal;
-ms-flex-direction: row;
flex-direction: row;
-webkit-box-pack: space-evenly;
-ms-flex-pack: space-evenly;
justify-content: space-evenly;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
-ms-flex-wrap: nowrap;
flex-wrap: nowrap;
}
.main-head .main-nav .nav-list .nav-item {
font-size: 3rem;
}
<header class="main-head">
<div class="container">
<svg
class="logo"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 299.92 81.73"
>
<g id="Layer_2" data-name="Layer 2">
<g id="Layer_1-2" data-name="Layer 1">
<path
d="M79,70.79a6.86,6.86,0,0,1-2.19,5.26,7.67,7.67,0,0,1-5.42,2,9.62,9.62,0,0,1-4-.81L7.28,51.7A12.07,12.07,0,0,1,2,47.42a11.86,11.86,0,0,1,0-13,10.87,10.87,0,0,1,5.33-4.2l60-25.72a11.18,11.18,0,0,1,4-.81,7.67,7.67,0,0,1,5.42,2A6.86,6.86,0,0,1,79,10.94a6.13,6.13,0,0,1-4.06,6L17.8,40.86l57.09,24A6.13,6.13,0,0,1,79,70.79Z"
/>
<path
d="M149.32,1.82c1.62,1.4,2.42,3.51,2.42,6.31s-.7,5.07-2.1,6.47-3.93,2.32-7.6,2.75l-4.85.49q-9.56,1-14,6.47c-3,3.66-4.45,8.24-4.45,13.75V72a9,9,0,0,1-2.91,7.2,10.78,10.78,0,0,1-7.28,2.5,10.42,10.42,0,0,1-7.2-2.5c-1.9-1.67-2.83-4.07-2.83-7.2V9.42a8.61,8.61,0,0,1,2.83-6.95,10.38,10.38,0,0,1,7-2.43,9.75,9.75,0,0,1,6.79,2.35,8.61,8.61,0,0,1,2.59,6.71v6.47a24.07,24.07,0,0,1,9.14-11A27.93,27.93,0,0,1,140.42.2L142.68,0A8.77,8.77,0,0,1,149.32,1.82Z"
/>
<path
d="M211.27,1.82c1.62,1.4,2.42,3.51,2.42,6.31s-.7,5.07-2.09,6.47-3.94,2.32-7.61,2.75l-4.85.49q-9.55,1-14,6.47T180.7,38.06V72a9,9,0,0,1-2.91,7.2,10.78,10.78,0,0,1-7.28,2.5,10.43,10.43,0,0,1-7.2-2.5c-1.9-1.67-2.83-4.07-2.83-7.2V9.42a8.61,8.61,0,0,1,2.83-6.95,10.4,10.4,0,0,1,7-2.43,9.73,9.73,0,0,1,6.79,2.35,8.58,8.58,0,0,1,2.6,6.71v6.47a24,24,0,0,1,9.13-11A28,28,0,0,1,202.37.2L204.63,0A8.75,8.75,0,0,1,211.27,1.82Z"
/>
<path
d="M298,34.39a11.86,11.86,0,0,1,0,13,12,12,0,0,1-5.33,4.28l-60,25.56a9.65,9.65,0,0,1-4,.81,7.67,7.67,0,0,1-5.42-2A6.83,6.83,0,0,1,221,70.79a6.11,6.11,0,0,1,4-6l57.11-24L225,16.92a6.11,6.11,0,0,1-4-6,6.83,6.83,0,0,1,2.19-5.26,7.67,7.67,0,0,1,5.42-2,11.26,11.26,0,0,1,4,.81l60,25.72A10.82,10.82,0,0,1,298,34.39Z"
/>
</g>
</g>
</svg>
<nav class="main-nav">
<ul class="nav-list">
<li class="nav-item">
about
</li>
<li class="nav-item">
portfolio
</li>
<li class="nav-item">contact</li>
</ul>
</nav>
</div>
</header>

Align in the center when the div is displayed flexbox doesn't work in IE11

Align in the center when the div is displayed flexbox doesn't work in IE11. It is ok in Chrome/Firefox;
.wrapper {
align-items: center;
-ms-flex-align: center;
-ms-flexbox; display
display: flex;
-ms-flex-wrap: nowrap;
flex-wrap: nowrap;
width: 100%;
height: 8.125rem;
border: 1px solid blue;
}
.header {
align-items: flex-start;
-ms-flex-align: start;
-ms-flexbox; display
display: flex;
margin: 0.5rem auto 0 auto;
border: 1px solid red;
}
.container {
flex-grow: 1;
flex-direction: row;
-ms-flex-align: center;
-ms-flex-direction: row;
max-width: 38.25rem;
}
.bimage {
margin: 0 1.5rem 0 0;
}
.nav {
-ms-flexbox; display
display: flex;
flex-flow: column nowrap;
-ms-flex-flow: column nowrap;
flex-grow: 1;
-ms-flex-positive: 1;
}
.navbar {
align-items: flex-start;
-ms-flex-align: start;
color: #fff;
display: flex;
justify-content: space-between;
-ms-flex-pack: justify;
text-transform: uppercase;
}
.navbar items > * {
color: inherit;
margin-right: 0.5rem;
}
.navbar items> *:last-child {
margin-right: 0;
}
.search {
-ms-flexbox; display
display: flex;
align-items: flex-start;
-ms-flex-align: start;
margin: 1.5rem 0 1.5rem 0;
}
<div class="wrapper">
<div class="header container">
<div class="bimage">
<img src="https://via.placeholder.com/50">
</div>
<div class="nav">
<div class="navbar">
<div class="items">
Alpha
Beta
Gama
Teta
</div>
<div class="items">
Right Alpha
Right Beta
</div>
</div>
<div class="search">
<form>
<input class="searchinput" placeholder="Search" name="q" >
<button type="submit" value="Search">
</form>
</div>
</div>
</div>
I made some changes in CSS file. Please check the answer,
.wrapper {
align-items: center;
text-align: center;
-ms-flex: display;
display: flex;
-ms-flex-wrap: nowrap;
flex-wrap: nowrap;
width: 100%;
height: 8.125rem;
border: 1px solid blue;
}
.header {
align-items: flex-start;
text-align: start;
-ms-flex: display;
display: flex;
margin: 0.5rem auto 0 auto;
border: 1px solid red;
}
.container {
flex-grow: 1;
flex-direction: row;
text-align: center;
-ms-flex-direction: row;
max-width: 38.25rem;
}
.bimage {
margin: 0 1.5rem 0 0;
}
.nav {
-ms-flex: display;
display: flex;
flex-flow: column nowrap;
-ms-flex-flow: column nowrap;
flex-grow: 1;
}
.navbar {
align-items: flex-start;
text-align: start;
color: #fff;
display: flex;
justify-content: space-between;
text-transform: uppercase;
}
.navbar items>* {
color: inherit;
margin-right: 0.5rem;
}
.navbar items>*:last-child {
margin-right: 0;
}
.search {
-ms-flex: display;
display: flex;
align-items: flex-start;
text-align: start;
margin: 1.5rem 0 1.5rem 0;
}
<div class="wrapper">
<div class="header container">
<div class="bimage">
<img src="https://via.placeholder.com/50">
</div>
<div class="nav">
<div class="navbar">
<div class="items">
Alpha
Beta
Gama
Teta
</div>
<div class="items">
Right Alpha
Right Beta
</div>
</div>
<div class="search">
<form>
<input class="searchinput" placeholder="Search" name="q">
<button type="submit" value="Search">
</form>
</div>
</div>
</div>

How do I "vertically center" two flex items?

Here is the link to my project.
I want both of them to be "centered" so to speak. But yes, I know they can both take up 1 position. So I think if there is a way to draw a (invisible) horizontal line that is vertically centered. Then, I can just place one flex item right on top of this line, and the other right below it.
But I think there is a better way to do this that can be used to centered more than 2 flex items.
body{
background:#4A6556;
}
body>div>hello,hey{
background: #CCC;
border-radius: 7px;
margin: 5px;
padding: 20px;
}
body>div{
display: flex;
flex: row wrap;
flex-direction: column;
}
body>div>hey{
order:2;
font-family: 'Lato', sans-serif;
color:#212121;
}
body>div>hello{
order:1;
font-family: 'Kumar One', cursive;
color: #938653;
}
hello,
hey{
text-align: center;
}
<link href="https://fonts.googleapis.com/css?family=Kumar+One" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Kumar+One|Lato" rel="stylesheet">
<body>
<div>
<hello>Welcome!</hello>
<hey>This is my portfolio page.</hey>
</div>
</body>
It's not entirely clear from your question if you want the two items displayed side-by-side or one-on-top-of-the-other.
A. side by side
.va-twins {
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.va-twins > * {
flex-grow: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
body {
margin: 0; padding: 0; // SO reset, you don't need this.
}
<div class="va-twins">
<hello>Welcome!</hello>
<hey>This is my portfolio page.</hey>
</div>
If you need it, here's the fully prefixed code (for close to full browser compatibility 97.38% at the time of this posting)
.va-twins {
min-height: 100vh;
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-webkit-justify-content: center;
-moz-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-webkit-align-items: center;
-moz-box-align: center;
-ms-flex-align: center;
align-items: center;
}
.va-twins > * {
-webkit-box-flex: 1;
-webkit-flex-grow: 1;
-moz-box-flex: 1;
-ms-flex-positive: 1;
flex-grow: 1;
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-webkit-flex-direction: column;
-moz-box-orient: vertical;
-moz-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-pack: center;
-webkit-justify-content: center;
-moz-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-webkit-align-items: center;
-moz-box-align: center;
-ms-flex-align: center;
align-items: center;
}
<div class="va-twins">
<hello>Welcome!</hello>
<hey>This is my portfolio page.</hey>
</div>
B. one on top of the other
.va-twins {
min-height: 100vh;
display: flex;
flex-direction: column;
justify-content: center;
align-items: stretch;
}
.va-twins > * {
flex-grow: 1;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
body {
margin: 0; padding: 0; // SO reset, you don't need this.
}
<div class="va-twins">
<hello>Welcome!</hello>
<hey>This is my portfolio page.</hey>
</div>
Fully prefixed:
.va-twins {
min-height: 100vh;
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-webkit-flex-direction: column;
-moz-box-orient: vertical;
-moz-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-pack: center;
-webkit-justify-content: center;
-moz-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: stretch;
-webkit-align-items: stretch;
-moz-box-align: stretch;
-ms-flex-align: stretch;
align-items: stretch;
}
.va-twins > * {
-webkit-box-flex: 1;
-webkit-flex-grow: 1;
-moz-box-flex: 1;
-ms-flex-positive: 1;
flex-grow: 1;
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-webkit-flex-direction: column;
-moz-box-orient: vertical;
-moz-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-pack: center;
-webkit-justify-content: center;
-moz-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-webkit-align-items: center;
-moz-box-align: center;
-ms-flex-align: center;
align-items: center;
}
<div class="va-twins">
<hello>Welcome!</hello>
<hey>This is my portfolio page.</hey>
</div>
You could use #media queries to switch between A. and B. on different screen sizes.

Flexbox, center several elements vertically (as one)

I have a container with several elements inside. I want to center these elements vertically, as a group. Can this be done with flexbox and if so, how?
Here is a demo:
.container {
margin: 0 auto;
width: 700px;
height: 600px;
background: #ebebeb;
flex-direction: column;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-ms-flex-align: center;
-webkit-align-items: center;
-webkit-box-align: center;
align-items: center;
}
.btn {
display: inline-block;
padding: 5px 10px;
padding: 0.3125rem 0.625rem;
background: #129c87;
color: #fff;
border: 1px solid #999;
border-radius: 2px;
font-weight: 600;
text-align: center;
outline: none;
border: none;
cursor: pointer;
font-family: Arial;
}
<div class="container">
<h1>Center me and my paragraphs plz</h1>
<p>First paragraph is always first</p>
<p>Then comes the second</p>
<a class="btn" href="#">Read more</a>
</div>
You just need justify-content:center;
fiddle: https://jsfiddle.net/w64ks4x7/
.container {
margin: 0 auto;
width: 700px;
height: 600px;
background: #ebebeb;
flex-direction: column;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-ms-flex-align: center;
-webkit-align-items: center;
-webkit-box-align: center;
align-items: center;
justify-content:center;
}

make flex item 90% wide

This is a simple question. I must be just missing the answer. I have a flex container with one flex item in it. I want the flex item to be 90% wide and centered vertically and horizontally. I just can't get it to be 90% wide. I am puzzled. Here's my code:
<div class="" id="popupContainer">
<div class="flex-item-popup" id="popup">
</div>
</div>
#popupContainer {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
display:flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-content: flex-start;
align-items: center;
}
#popup {
width: 90%;
height: 90%;
flex-grow: 1;
flex-shrink: 0;
flex-basis: 200px;
justify-content: center;
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;
-webkit-flex-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
background-color: black;
justify-content: center; /* align horizontal */
align-items: center; /* align vertical */
}
just delete these two and it will work proprely :
flex-grow: 1; flex-basis: 200px;
Live Demo

Resources