Why isn't this absolute positioned div vertically centering? - html

I need multi-line text to be centered vertically within a parent. I need the child to be position: absolute; because it will have other items in there behind it. I cannot figure out how to make the child (a div containing text) vertically centered. (I also tried doing it with display: table-cell but couldn't get that working either)
https://jsfiddle.net/t7q1ffm2/
HTML:
<div class="box">
<div class="text">This has some text in it that's long</div>
</div>
<div class="box">
<div class="text">Short text</div>
</div>
CSS:
.box
{
position: relative;
top: 0em;
left: 0em;
width: 125px;
height: 125px;
-webkit-flex-grow: 1;
-moz-flex-grow: 1;
-ms-flex-grow: 1;
flex-grow: 1;
margin-right: .625em;
margin-bottom: .5em;
text-decoration: none;
overflow: hidden;
display: table;
background-color: red;
}
.box .text
{
position: absolute;
top: 0;
bottom: 0;
left:0;
right:0;
margin: auto;
width:100%;
height: 50%;
font-family: Arial, Helvetica;
font-size: 1em;
font-weight: normal;
line-height: 1.125em;
color: #ffffff;
text-align: center;
z-index: 2;
}

If you want to use position relative/absolute, you have to add transform: translate(-50%, -50%); on absolute element for center align.
.box{
position: relative;
top: 0em;
left: 0em;
width: 125px;
height: 125px;
margin-right: .625em;
margin-bottom: .5em;
text-decoration: none;
overflow: hidden;
background-color: red;
}
.box .text {
position: absolute;
top: 50%;
left:50%;
width: 100%;
font-family: Arial, Helvetica;
font-size: 1em;
font-weight: normal;
color: #ffffff;
text-align: center;
z-index: 2;
transform: translate(-50%, -50%);
}
<div class="box">
<div class="text">This has some text in it that's long</div>
</div>
<div class="box">
<div class="text">Short text</div>
</div>

Using flexbox is your best bet. Here is my fiddle, and here is the CSS I used (I cut out some unnecessary CSS):
.box {
position: relative;
top: 0em;
left: 0em;
width: 125px;
height: 125px;
display: flex;
align-items: center;
margin-right: .625em;
margin-bottom: .5em;
text-decoration: none;
overflow: hidden;
background-color: red;
}
.box .text {
margin: auto;
width:100%;
font-family: Arial, Helvetica;
font-size: 1em;
font-weight: normal;
line-height: 1.125em;
color: #ffffff;
text-align: center;
z-index: 2;
}

CSS attribute table-cell does work:
HTML:
<div class="box">
<div class="text">This has some text in it that's long</div>
</div>
<div class="box">
<div class="text">Short text</div>
</div>
CSS:
.box {
width: 130px;
height: 130px;
display: table;
}
.text {
display: table-cell;
vertical-align: middle;
text-align: center;
}
However, you need to wrap that inside another box.
This fiddle illustrates your exact use case: https://jsfiddle.net/stgermaniac/qdc84bxo/

Related

How to show half of the middle section when scrolled to the top and bottom of the page?

I've written this code for a simple html page in which there are three sections. The top(first) and bottom(third) are full screen sections and the middle section has to be shown with both the top and bottom sections i.e., the upper half of the middle section with the top section and the bottom half with the third section.
I was able to show the first part correctly(when scroll position is at the top) but not the second part(which is when scroll position at the bottom).
Can anybody tell me how can I achieve that?
Full page: https://i.imgur.com/YDV1usM.png
When scroll position is at the top: https://i.imgur.com/Mngkqth.png
When scroll position is at the bottom: https://i.imgur.com/wugOxCY.png
* {
font-family: monospace;
}
h2 {
font-size: 100px;
font-family: monospace;
}
.section-1, .section-3 {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.section-2 h6 {
font-size: 18px;
position: relative;
margin: 80px 0 !important;
}
.section-2 p {
font-size: 20px;
position: relative;
}
.section-2 {
text-align: center;
margin-top: -145px;
}
.section-2 h6:after {
content: '';
position: absolute;
height: 60px;
width: 1px;
background: #000;
left: 50%;
top: 30px;
}
<div class="section-1">
<h2>SECTION 1</h2>
</div>
<div class="section-2">
<h6>Scroll</h6>
<p>Studio Liana Lalush</p>
</div>
<div class="section-3">
<h2>SECTION 3</h2>
</div>
Are you expecting like this:
Note: check out my answer in full screen mode
Demo:
* {
font-family: monospace;
}
h2 {
font-size: 100px;
font-family: monospace;
}
.section-1,
.section-3 {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.section-2 h6 {
font-size: 18px;
position: relative;
margin: 70px 0 !important;
}
.section-2 p {
font-size: 20px;
position: relative;
}
.section-2 {
text-align: center;
position: absolute;
top: 98%;
left: 50%;
transform: translate(-50%, -50%);
}
.section-2 h6:after {
content: '';
position: absolute;
height: 60px;
width: 1px;
background: #000;
left: 50%;
top: 25px;
}
<div class="section-1">
<h2>SECTION 1</h2>
</div>
<div class="section-2">
<h6>Scroll</h6>
<p>Studio Liana Lalush</p>
</div>
<div class="section-3">
<h2>SECTION 3</h2>
</div>
Try with this code. You only need to change in css code.
* {
font-family: monospace;
}
h2 {
font-size: 100px;
font-family: monospace;
}
.section-1 {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.section-3{
height: 90vh;
display: flex;
align-items: center;
justify-content: center;
}
.section-2 h6 {
font-size: 18px;
position: relative;
margin: 80px 0 !important;
}
.section-2 p {
font-size: 20px;
position: relative;
}
.section-2 {
text-align: center;
margin-top: -145px;
}
.section-2 h6:after {
content: '';
position: absolute;
height: 60px;
width: 1px;
background: #000;
left: 50%;
top: 30px;
}

padding-top "bigger" than padding-bottom

I don't understand why in this setup the padding-top is various times bigger than the padding-bottom. Tried tweaking stuff around to find the culprit property but so far nothing. I did notice, that I accidentally left a " after the spans, the issue was gone, but not sure how that relates.
https://jsfiddle.net/3n0yuzs3/1/
body
{
font-family: sans-serif;
}
#window
{
background-color: black;
color: white;
display: flex;
flex-direction: column;
opacity: 1;
left: 50%;
bottom: 0px;
position: fixed;
width: auto;
height: auto;
min-width: 600px;
min-height: auto;
max-width: 80vw;
max-height: 80vh;
transform: translateX(-50%);
outline: 0px;
cursor: default;
z-index: 5000002;
zoom: 1;
}
#container
{
overflow-y: auto;
overflow-x: hidden;
border: none;
outline: 0;
margin: 0;
flex-grow: 1;
}
#content
{
font-size: 22px;
text-align: center;
overflow-wrap: break-word;
padding-top: 1.6em;
padding-bottom: 1.6em;
padding-left: 1.6em;
padding-right: 1.6em;
}
.snack_msg
{
padding-right: 200px;
float:left;
}
.snack_btn
{
color:#5fce49;
text-transform: uppercase;
letter-spacing:3px;
cursor:pointer;
float:right;
}
<div id='window'>
<div id='container'>
<div id='content'>
<span class='snack_msg'>New message arrived</span>
<span class='snack_btn' onclick='open_snack_message()'>open</span>
</div>
</div>
</div>
The issue is with floating elements and not padding. As you can see below, you have equal padding in all the sizes :
And if you check well you will see also that you have a height equal to 0 because you have floating element and since the parent is not floating it will collapse (which means no height). To fix this you need to add oveflow:auto to #content.
body {
font-family: sans-serif;
}
#window {
background-color: black;
color: white;
display: flex;
flex-direction: column;
opacity: 1;
left: 50%;
bottom: 0px;
position: fixed;
width: auto;
height: auto;
min-width: 600px;
min-height: auto;
max-width: 80vw;
max-height: 80vh;
transform: translateX(-50%);
outline: 0px;
cursor: default;
z-index: 5000002;
zoom: 1;
}
#container {
overflow-y: auto;
overflow-x: hidden;
border: none;
outline: 0;
margin: 0;
flex-grow: 1;
}
#content {
font-size: 22px;
text-align: center;
overflow-wrap: break-word;
padding-top: 1.6em;
padding-bottom: 1.6em;
padding-left: 1.6em;
padding-right: 1.6em;
overflow: auto;
}
.snack_msg {
padding-right: 200px;
float: left;
}
.snack_btn {
color: #5fce49;
text-transform: uppercase;
letter-spacing: 3px;
cursor: pointer;
float: right;
}
<div id='window'>
<div id='container'>
<div id='content'>
<span class='snack_msg'>New message arrived</span>
<span class='snack_btn' onclick='open_snack_message()'>open</span>
</div>
</div>
</div>
Here is some usefull questions where you can gather more information and more ways to prevent this behavior:
How do you keep parents of floated elements from collapsing?
Why does 'overflow: auto' clear floats? And why are clear floats needed?

Text inside div wont center

Problem Statement : Center alignment not happening
Relevant code:
#tekst {
font-family: 'Roboto', sans-serif;
font-size: 14px;
color: #EFEFEF;
width: 100%;
height: 30%;
}
#innhold {
width: 100%;
height: 30%;
top: 18%;
left: 0%;
background-color: #7e7e7e;
position: absolute;
border-radius: 5px;
z-index: 2;
}
<div id="innhold">
<div id="tekst"> text </div>
</div>
any help would be appreciated.
You can use text-align:center; to center text.
#tekst {
font-family: 'Roboto', sans-serif;
font-size: 14px;
color: #EFEFEF;
width: 100%;
height: 30%;
text-align: center;
}
#innhold {
width: 100%;
height: 30%;
top: 18%;
left: 0%;
background-color: #7e7e7e;
position: absolute;
border-radius: 5px;
z-index: 2;
}
<div id="innhold">
<div id="tekst"> text </div>
</div>
To fully center your text you can use flexbox:
#tekst {
font-family: 'Roboto', sans-serif;
font-size: 14px;
color: #EFEFEF;
height: 30%;
}
#innhold {
width: 100%;
height: 30%;
top: 18%;
left: 0%;
background-color: #7e7e7e;
position: absolute;
border-radius: 5px;
z-index: 2;
display: flex;
justify-content: center;
align-items: center;
}
<div id="innhold">
<div id="tekst"> text </div>
</div>
You need to add the CSS property text-align:center;.
Hope this helps.
#tekst {
font-family: 'Roboto', sans-serif;
font-size: 14px;
text-align:center;
color: #EFEFEF;
width: 100%;
height: 30%;
}
#innhold {
width: 100%;
height: 30%;
top: 18%;
left: 0%;
background-color: #7e7e7e;
position: absolute;
border-radius: 5px;
z-index: 2;
}
<div id="innhold">
<div id="tekst"> text </div>
</div>

Center a vertical span within an a element

I have a layout that looks sort of like a bookshelf, see my jsfiddle example. The bookshelf is responsive so no fixed widths. The issue I'm faced with is that I cannot center the text within the panels, "horizontally" within the books sides. It's extra tricky since the text is transformed to be displayed vertically. Does anyone have any ideas of how to make this work?
HTML
<div class="panel">
<a href="#first">
<span>first</span>
</a>
</div>
CSS
.panel {
float: left;
height: 100%;
width: 15%;
margin-right: 5%;
}
.panel a {
text-align: right;
background: green;
padding: 20px 10px;
height: 100%;
display: block;
white-space: nowrap;
color: white;
float: left;
width: 100%;
text-decoration:none;
}
.panel a span {
white-space: nowrap;
color: white;
text-transform: lowercase;
-ms-transform: rotate(-90deg);
-webkit-transform: rotate(-90deg);
transform: rotate(-90deg);
transform-origin: top right;
display: block;
width: 300px;
margin-left: -300px;
letter-spacing: 1px;
font-size: 20px;
}
you have 2 easy options, with either display:flex or table :
display:table example:
body,
html,
.panel-wrapper {
height: 100%;
width: 100%;
}
.panel-wrapper {
display: table;
border-collape: collapse;
table-layout: fixed;
background: white;
}
.panel {
display: table-cell;
height: 100%;
width: 20%;/* you have five here*/
}
.panel a {
display: block;
text-align: center;
background: green;
padding: 20px 10px;
box-sizing:border-box;/*includes padding and borders */
height: 100%;
width: 75%;/* each is 15% width */
text-decoration: none;
white-space: nowrap;
}
.panel a:before {
content: '';
height: 100%;
display: inline-block;
vertical-align: middle;/*inline content will vertical-align middle aside it */
}
.panel a span {
display: inline-block;/* triggers transform*/
color: white;
text-transform: lowercase;
transform: rotate(-90deg);
letter-spacing: 1px;
font-size: 20px;
vertical-align: middle;
}
<div class="panel-wrapper">
<div class="panel"><span>first</span>
</div>
<div class="panel"><span>second</span>
</div>
<div class="panel"><span>third</span>
</div>
<div class="panel"><span>fourth</span>
</div>
<div class="panel"><span>fifth</span>
</div>
</div>
http://codepen.io/gc-nomade/pen/JGEbLX
or flex:
body,
html,
.panel-wrapper {
height: 100%;
width: 100%;
margin: 0;
}
.panel {
float: left;
height: 100%;
width: 15%;
margin-right: 5%;
}
.panel a {
text-align: right;
background: green;
padding: 20px 10px;
height: 100%;
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
;
white-space: nowrap;
color: white;
text-decoration: none;
}
.panel a span {
white-space: nowrap;
color: white;
transform: rotate(-90deg);
letter-spacing: 1px;
font-size: 20px;
}
<div class="panel-wrapper">
<div class="panel"><span>first</span>
</div>
<div class="panel"><span>second</span>
</div>
<div class="panel"><span>third</span>
</div>
<div class="panel"><span>fourth</span>
</div>
<div class="panel"><span>fifth</span>
</div>
</div>
http://codepen.io/gc-nomade/pen/obBYyY
Do the following:
.panel a span {
white-space: nowrap;
color: white;
text-transform: lowercase;
-ms-transform: rotate(-90deg);
-webkit-transform: rotate(-90deg);
transform: rotate(-90deg);
transform-origin: top right;
display: block;
width: 300px;
margin-left: -300px;
letter-spacing: 1px;
font-size: 20px;
position:relative;
left: 45%;
}
See my demo here or your jfiddle
You may need a small adjustment at tiny smartphones with a media query, but other than that it works
Add this to the responsive css:
#media (max-width: 560px){
.panel a span {
left: 25%;
}
}
Add these lines to your code :
.panel a{
position:relative;
}
.panel a span {
position:absolute;
left:40%;
/* top:50%; */
}
Here is the fiddle
I f you want center the text vertically use the below property below the text-align: center in .panel a span make the text in corner center of a panel
.panel a span {
text-align: center;
}
and if you want center text in the middle of the panel not in corner
.panel a span {
text-align: center;
margin-left: -260px;
}

How to align center elements in this CodePen?

http://codepen.io/leongaban/pen/ACJta
^ Updated (I can never get SASS to work right in Codepen)
Can't align to the middle my elements above. Using SASS, I want to avoid using position hacks
<main>
<div class="gradient">
<div class="hero">
<img src="http://placehold.it/350x150" alt=""/>
</div>
</div>
<section class="tagline">
<h1>Hi there Lorem Ipsum <em>My name is Bob</em></h1>
<button>Learn More</button>
</section>
</main>
body {
font-family:Arial
}
.hero {
display: table-cell;
//position: relative;
margin: 0 auto;
// top: 42%;
// left: 10%;
text-align: center;
width: auto;
height: 447px;
vertical-align: bottom;
img {
width: 300px;
height: 280px;
text-align: center;
vertical-align: bottom;
border:0;
}
}
.tagline {
position: relative;
margin: 0 auto;
width: 100%;
height: 300px;
text-align: center;
color: blue;
background: gray;
z-index: 2;
h1 {
margin: 0 auto;
padding-top: 5%;
width: 80%;
font-size: 42px;
text-align: center;
}
em {
font-weight: bold;
}
button {
margin-top: 25px;
padding: 10px 20px;
font-family: Arial;
font-size: em(21);
color: white;
border: 0;
background: orange;
}
}
I was able to center your elements above my doing the following:
.hero {
margin: 0 auto;
text-align: center;
width: 50%;
height: 447px;
vertical-align: bottom;
}
It seems it did not want to center because you had specified width: auto