How can I vertically center rotated text using Flexbox layout? - html

How can I vertically center rotated text using flexbox layout? I want something that looks like this:
Here's what I have so far:
html, body { height: 100%; }
body { background-color: #efefef; }
body > div {
align-content: center;
background-color: white;
border: 1px solid black;
display: flex;
height: 100%;
width: 25px;
}
body > div > div {
flex: 1;
transform: rotate(-90deg);
}
<div>
<div>
Where did I go?
</div>
</div>

Add white-space: nowrap and center horizontally and vertically using:
align-items: center;
justify-content: center;
(and you don't need the flex: 1!)
Also removed the browser margin and added in box-sizing: border-box to add the finishing touches.
See demo below:
* {
box-sizing: border-box;
}
html,
body {
height: 100%;
margin: 0;
}
body {
background-color: #efefef;
}
body > div {
background-color: white;
border: 1px solid black;
display: flex;
height: 100%;
width: 25px;
align-items: center;
white-space: nowrap;
justify-content: center;
}
body > div > div {
transform: rotate(-90deg);
}
<div>
<div>
Where did I go?
</div>
</div>

You can accomplish this by changing a couple things in your code:
Give your text a white-space: nowrap;.
Give your containing div a justify-content: center;.
In your containing div, change align-content to align-items.
html, body { height: 100%; }
body { background-color: #efefef; }
body > div {
align-items: center;
justify-content: center;
background-color: white;
border: 1px solid black;
display: flex;
height: 100%;
width: 25px;
}
body > div > div {
white-space: nowrap;
transform: rotate(-90deg);
}
<div>
<div>
Where did I go?
</div>
</div>
*Note You can also remove flex: 1; from your inner div, as it is not doing anything.

Related

Flexbox items alignment with same height & item aligment

I have a simple layout with image on Left and Title of blog on right with light grey background for large screen or were width is minimum 800px. for smaller screens it should show image on top and Title below image. It is working fine except two thing
It show extra space under image which is shown as yellow background in this case.
I want Title Item to be same height as Image element with light grey background, in this case which is represented as red.
.flex-container {
display: flex;
align-items: stretch;
flex-direction: row;
border: 1px solid blue;
height: 100%;
}
.flex-container>div {
background-color: yellow;
color: #555;
width: 50%;
height: 100%;
margin: 0px;
text-align: center;
align-self: center;
font-size: 30px;
}
.title-wrapper {
background-color: #f00 !important;
}
.imgx {
width: 100%;
}
#media (max-width: 800px) {
.flex-container {
flex-direction: column;
}
.flex-container>div {
width: 100%;
margin: 0px;
}
.imgx {
width: 100%;
}
}
<div class="flex-container">
<div class="image-wrapper"><img class="imgx" src="https://dummyimage.com/600x400/000/add413&text=IMAGE"></div>
<div class="title-wrapper">This is the title</div>
</div>
To avoid the gap under the image, 2 classical options :
reset vertical-align: to top or bottom
or reset display to block.
To center content inside the second box, make it also a grid or flex box
Possible fix :
.flex-container {
display: flex;
flex-direction: row;
border: 1px solid blue;
height: 100%;
}
.flex-container>div {
background-color: yellow;
color: #555;
width: 50%;
text-align: center;
font-size: 30px;
}
.flex-container .title-wrapper {
background-color: #f00;
display:flex;
align-items:center;
justify-content:center;
margin:0;
}
.imgx {
width: 100%;
display:block;
}
#media (max-width: 800px) {
.flex-container {
display:grid;/* or block*/
}
.flex-container>div ,
.imgx {
width: 100%;
}
}
<div class="flex-container">
<div class="image-wrapper"><img class="imgx" src="https://dummyimage.com/600x400/000/add413&text=IMAGE"></div>
<div class="title-wrapper">This is the title</div>
</div>
To get rid of the space beneath the image, the image needs to be display: block, then to make the title full height and still aligned centre, you need to remove the height and then make the title itself flex and use align and justify on it (see comments in css below):
.flex-container {
display: flex;
align-items: stretch;
flex-direction: row;
border: 1px solid blue;
height: 100%;
}
.flex-container>div {
background-color: yellow;
/* remove height from here */
color: #555;
width: 50%;
margin: 0px;
font-size: 30px;
}
.title-wrapper {
background-color: #f00 !important;
/* add the following to here */
display: flex;
justify-content: center;
align-items: center;
}
.imgx {
width: 100%;
display: block; /* add this */
}
#media (max-width: 800px) {
.flex-container {
flex-direction: column;
}
.flex-container>div {
width: 100%;
margin: 0px;
}
.imgx {
width: 100%;
}
}
<div class="flex-container">
<div class="image-wrapper"><img class="imgx" src="https://dummyimage.com/600x400/000/add413&text=IMAGE"></div>
<div class="title-wrapper">This is the title</div>
</div>
you have to remove height and align-self in .flex-container > div
.flex-container > div {
background-color: yellow;
color: #555;
width: 50%;
margin: 0px;
text-align: center;
font-size: 30px;
}
are you sure to use align-self or did you mean align-items and justify-content?

How to enable scrollbar with non-fixed height in a window-centered div?

I am creating a div which is centered to the window. It's content can grow, and if it grows passed the size of the window, the content div should have it's scrollbar account for the overflow. But instead, the div just grows off the screen and gets clipped. If I set an explicit height on the content, everything works, but since I don't know the explicit height of the environment I cannot do that. What is the correct way to do this?
JSFiddle: https://jsfiddle.net/CodeVirtue/cjhz31xq
Here is the template:
<div class="fullscreen-overlay">
<div class="fullscreen-container">
<div class="window-with-titlebar">
<div class="titlebar">
<div class="titlebar-left">
Left
</div>
<div class="titlebar-right">
Right
</div>
</div>
<div class="content">
1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>11<br>12<br>13<br>14<br>15<br>16<br>17<br>18<br>19<br>20<br>21<br>22<br>23<br>24<br>25<br>26<br>27<br>28<br>29<br>30<br>31<br>32<br>33<br>34<br>35<br>36<br>37<br>38<br>39<br>40
</div>
</div>
</div>
</div>
And all the CSS:
* {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0;
overflow: hidden;
}
.fullscreen-overlay {
background-color: red;
display: flex;
justify-content: center;
align-items: center;
width: 100vw;
height: 100vh;
padding: 12px 12px;
}
.fullscreen-container {
background-color: orange;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
max-width: 100%;
max-height: 100%;
overflow: hidden;
}
.window-with-titlebar {
background-color: yellow;
max-width: 100%;
max-height: 100%;
}
.titlebar {
background-color: green;
display: flex;
align-items: center;
justify-content: space-between;
height: 30px;
}
.titlebar-left {
background-color: darkgreen;
}
.titlebar-right {
background-color: lightgreen;
}
.content {
background-color: blue;
overflow-y: scroll;
max-width: 100%;
max-height: 100%;
}
I believe I was able to achieve what you are looking for by making the parent container use flexbox:
.window-with-titlebar {
background-color: yellow;
max-width: 100%;
max-height: 100%;
display: flex;
flex-direction: column;
}

Aligning divs in relation to other divs

I'm trying to replicate something like this: .
The red div (the container) is centered horizontaly and hasn't a fixed width. The "title" div inside it should set the width of the container by being the widest div inside it, and the "welcome to" and "subtitle" divs are attached to the "title" div and the left/right of the container.
I'm struggling to understand how I can put my "welcome to" div to the left, before having set the "title" div which gives the width of the container.
For the moment, I have this code:
.container {
text-align: center;
}
.welcome_to{
text-align: left;
}
.subtitle{
text-align:right;
}
.title{
}
It centers the title div, but put the other divs at the edges of the screen, and not of the container.
This should give you a start
.container {
border: 3px solid red;
display: flex;
flex-direction: column;
font-size: 2em;
}
.left,
.center,
.right {
border: 3px solid blue;
}
.full-width {
width: 100%;
}
.left {
display: flex;
align-self: flex-start;
}
.center {
display: flex;
align-self: center;
justify-content: center;
}
.right {
display: flex;
align-self: flex-end;
}
<div class="container">
<div class="left">Left</div>
<div class="center full-width">Center and full width</div>
<div class="right">right</div>
</div>
text-align aligns text, not block elements. The best way here is to use flexbox:
/* This is the actual code */
.container {
display: flex;
flex-direction: column;
}
.welcome-to {
align-self: flex-start;
width: 50%;
}
.title {
text-align: center;
}
.subtitle {
align-self: flex-end;
width: 50%;
text-align: right;
}
/* Decoration only */
.container {
border: 3px solid red;
}
.container > * {
box-sizing: border-box;
padding: 1rem;
border: 3px solid blue;
}
<div class="container">
<div class="welcome-to">Welcome to</div>
<div class="title">Title</div>
<div class="subtitle">Subtitle</div>
</div>

Prevent text wrapper to span the entire width of its parent after text overflow to the next line

In responsive design, when I shrink the width, the text overflows to the next line and the text wrapper now takes the entire width of its parent.
Is there a way to prevent that? So it looks like the red container?
I could add padding on the container (and I will, like 1em) but thats still not what I want as it would make the text wrapper always be the same width (after overflow). I would rather have these text wrappers to have width based on its content.
Here is the jsFiddle:
https://jsfiddle.net/derive/n2qk03wt/8/
Here is the testing code:
*, *:before, *:after {
box-sizing: border-box;
-moz-box-sizing: border-box;
}
html,body {
width: 100%;
height: 600px;
margin: 0;
padding: 0;
display: flex;
flex-flow: column;
justify-content: center;
align-items: center;
}
div {
display: flex;
justify-content: center;
align-items: center;
width: 30%;
height: 200px;
margin-bottom: 1em;
}
.red {
background: red;
}
.blue {
width: 20%;
backgroud-color: #064f7f;
}
span {
padding: 1em;
font-size: 25px;
background: white;
text-align: center;
}
<div class="red">
<span>some text here</span>
</div>
<div class="blue">
<span>some text here</span>
</div>
Please find below-attached css code.
*, *:before, *:after {
box-sizing: border-box;
-moz-box-sizing: border-box;
}
html,body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
display: flex;
flex-flow: column;
justify-content: center;
align-items: center;
}
div {
display: flex;
justify-content: center;
align-items: center;
width: 40%;
height: 200px;
margin-bottom: 1em;
}
.red {
background: red;
}
.blue {
width: 30%;
background: blue;
}
span {
padding: 5px;
font-size: 30px;
background: white;
text-align: center;
width: 98%;
}
I hope this helps you
Edited: you need to set margin to span check updated snippet:
*,
*:before,
*:after {
box-sizing: border-box;
-moz-box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
display: flex;
flex-flow: column;
justify-content: center;
align-items: center;
}
div {
display: flex;
justify-content: center;
align-items: center;
width: 30%;
height: 200px;
margin-bottom: 1em;
}
.red {
background: red;
}
.blue {
width: 20%;
background: blue;
}
span {
padding: 5px;
font-size: 25px;
background: white;
text-align: center;
margin: 20px;
}
<div class="red">
<span>some text here</span>
</div>
<div class="blue">
<span>some text here</span>
</div>

How to make centered, dynamic-width flexbox

This markup should produce a 30px high box that is 600px width, centered. But instead it shrinks the box so it is no width (or if there is content, it shrinks to the minimum content width). Wondering how to make this so the centered box is 600px, but is responsive at smaller window sizes.
* {
position: relative;
box-sizing: border-box;
}
html, body {
height: 100%;
overflow: hidden;
}
div {
display: flex;
}
body > div {
align-items: center;
-webkit-box-pack: center;
-webkit-justify-content: center;
justify-content: center;
border: 1px solid orange;
}
body > div > div {
max-width: 600px;
border: 1px solid black;
}
body > div > div > div {
border: 1px solid blue;
background: red;
height: 30px;
}
<div>
<div>
<div></div>
</div>
</div>
Please try this. Give width:100% to body > div > div and body > div > div > div class.
* {
position: relative;
box-sizing: border-box;
}
html, body {
height: 100%;
overflow: hidden;
}
div {
display: flex;
}
body > div {
align-items: center;
-webkit-box-pack: center;
-webkit-justify-content: center;
justify-content: center;
border: 1px solid orange;
}
body > div > div {
max-width: 600px;
border: 1px solid black;
width:100%;
}
body > div > div > div {
border: 1px solid blue;
background: red;
height: 30px;
width:100%;
}
<div>
<div>
<div></div>
</div>
</div>
That is the default behavior of a flex item, being as wide as its content, similar to an inline block.
The reason is its default flex-grow value, which is 0 and tells it to not fill the remaining space.
Add flex-grow: 1 to every level of flex item that should fill its parent.
* {
position: relative;
box-sizing: border-box;
}
html, body {
height: 100%;
overflow: hidden;
}
div {
display: flex;
}
body > div {
align-items: center;
-webkit-box-pack: center;
-webkit-justify-content: center;
justify-content: center;
border: 1px solid orange;
}
body > div > div {
max-width: 600px;
border: 1px solid black;
flex-grow: 1; /* added */
}
body > div > div > div {
border: 1px solid blue;
background: red;
height: 30px;
flex-grow: 1; /* added */
}
<div>
<div>
<div></div>
</div>
</div>
Please allow me to simplify the code to its basic components. You have a div.container that is set as display:flex. Inside that you have a div which you want to adjust according to flex parameters. The inner div is set to grow and shrink according to its container:
flex: 1 1 auto; /* shorthand for flex-grow, flex-shrink, flex-basis */
It is also set to have a max-width:600px and centered in the page with margin-left and margin-right set to auto. I believe this construct meets your requirements.
div.container {
display: flex;
border: 1px solid orange;
}
div.container div {
flex: 1 1 auto;
border: 1px solid blue;
background: red;
height: 30px;
margin: 0 auto 0 auto;
max-width: 600px;
}
<div class="container">
<div></div>
</div>