Having difficulty centring text - html

I was wondering if I could have someone's help as I'm having difficulty vertically and horizontally centring the text in the centre of the page.
Thanks for taking the time, have a good day.
html:
<div class="somethingsimple">
</p>"...I place importance on honesty and expressiveness when creating for a society that is heavily influenced by art."</p>
</div>
Css:
.somethingsimple
{
position: absolute;
top: 50%;
text-align:center;
margin-left:auto;
margin-right: auto;
font-family: font-family: 'Open Sans', sans-serif;
font-size: 22px;
}

Just add width:100%; to somethingsimple like this:
.somethingsimple {
/* your current "absolute positioning" style properties */
width:100%;
}
Here's a jsfiddle with above codes: http://jsfiddle.net/6pwth4zv/

you can additionally add this css-
transform: translateY(-50%);
this moves the text up by 50% of its height, so its centered vertically. http://jsfiddle.net/ym1tys16/
AND finally to align it center horizontally you can use -
left: 50%;
transform: translateX(-50%);
For both horizontal as well as vertical centering, use transform like this -
transform: translate(-50%, -50%);
Fiddle Demo
Full snippet -
.somethingsimple {
position: absolute;
top: 50%;
text-align: center;
left: 50%;
font-family: font-family: 'Open Sans', sans-serif;
font-size: 22px;
transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
}
<div class="somethingsimple">
<p>"...I place importance on honesty and expressiveness when creating for a society that is heavily influenced by art."</p>
</div>

Related

Why does "position: relative" only work in Chrome?

I have a table, which should be centered both horizontally and vertically. The horizontal centration works fine (using "margin: 0 auto"), but the vertical centration (using "position: relative; top: 50%; transform: translateY(-50%)") only works in Google Chrome, in Firefox, Edge & MS Explorer it does not work.
I found out that "transform" works like it is supposed to, but "position: relative" does not work in all browsers.
<table border="1" id="content">
<tr>
<td><video id="video" controls></video></td>
</tr>
</table>
#content {
margin: 0 auto;
text-align: center;
font-family: arial, sans-serif;
font-size: 15pt;
position: relative;
top: 50%;
-webkit-transform: -webkit-translateY(-50%);
-ms-transform: -ms-translateY(-50%);
-moz-transform: -moz-translateY(-50%);
-o-transform: -o-translateY(-50%);
transform: translateY(-50%);
z-index: 0;
}
Your issue is that position relative doesn't allow you to use the top, right, etc. positioning. So it's only transforming your Y.
Change it to position: absolute; instead.
#content {
/* Center #content */
margin: 0 auto;
/* Align the text */
text-align: center;
/* Setting the font */
font-family: arial, sans-serif;
font-size: 15pt;
/* Setting the position */
position: absolute;
top: 50%; left: 50%;
/* Transforming #content */
-webkit-transform: -webkit-translate(-50%, -50%);
-ms-transform: -ms-translate(-50%, -50%);
-moz-transform: -moz-translate(-50%, -50%);
-o-transform: -o-translate(-50%, -50%);
transform: translate(-50%, -50%);
z-index: 0;
}
<table border="1" id="content">
<tr>
<td><video id="video" controls></video></td>
</tr>
</table>
#content {
display:flex;
justify-content: center;
align-items: center;
height:100%;
}
html,body {
height:100%;
}
If you want to use flex you can do it that way , thats all...
Look at this;
https://jsfiddle.net/uniak/z9vmnde6/
#content {
font-family: arial, sans-serif;
font-size: 15pt;
z-index: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
#content td {}
<table border="1" id="content">
<tr>
<td><video id="video" controls></video></td>
</tr>
</table>

HTML-CSS overlay covering whole page with centered elements

I want to create a simple full-screen overlay with loader and text in the center. I have some problems with the text. I want the image to be over ABOVE the text. Can you help me with this?
<div id="loadingOverlay" class="loader-overlay">
<div class="loader-content loader-center">
<img src="http://www.mysarkarinaukri.com/images/loadingBar.gif" class="loader-center" alt=""/>
<div class="loader-center loader-text">Loading, please wait...</div>
</div>
</div>
http://jsfiddle.net/bLz7wgvs/2/
[edit]
Sorry for my English. I meant "above", not "under"...
It should look like:
[-------------loader here-------------]
Loader text (plz wait, etc.) in one line, both centered horizontally and vertically
Edit
I didn't know you wanted to let the image appear above the text. I've changed your code a little bit: http://jsfiddle.net/bLz7wgvs/7/
CSS:
.loader-overlay {
-ms-opacity: 0.9;
background: #444;
display: block;
height: 100%;
left: 0;
opacity: 0.9;
position: fixed;
top: 0;
vertical-align: middle;
width: 100%;
z-index: 100000;
}
.loader-content {
margin-left: auto;
margin-top: auto;
width: 50%;
}
.loader-center {
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
left: 50%;
display: block;
position: fixed;
top: 50%;
transform: translate(-50%, -55%);
}
.loader-text {
color: #FFF;
font-size: 18px;
height: 50%;
}
Try this,
.loader-overlay {
-ms-opacity: 0.9;
background: #444;
display: block;
height: 100%;
left: 0;
opacity: 0.9;
position: fixed;
top: 0;
vertical-align: middle;
width: 100%;
z-index: 100000;
}
.loader-content {
height: auto;
margin-left: auto;
margin-top: auto;
width: auto;
}
.loader-center {
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
left: 50%;
position: fixed;
top: 50%;
transform: translate(-50%, -50%);
z-index:1;
}
.loader-text {
color: #FFF;
font-size: 18px;
z-index: 1;
}
Ive added z-index to the image and loader text :)
You just need to add a z-index to the image (and the associated class required to the HTML element):
.loader-img {
z-index: 10;
}
http://jsfiddle.net/bLz7wgvs/4/
Otherwise, you can just replace the order of the elements in the DOM if you have control over this. Siblings will appear above other siblings if they appear after them in the DOM tree As stated in the spec:
Within each stacking context, the following layers are painted in
back-to-front order:
the background and borders of the element forming the stackingcontext.
the child stacking contexts with negative stack levels (most negative first).
the in-flow, non-inline-level, non-positioned descendants.
the non-positioned floats.
the in-flow, inline-level, non-positioned descendants, including inline tables and inline blocks.
the child stacking contexts with stack level 0 and the positioned
descendants with stack level 0.
the child stacking contexts with positive stack levels (least positive first).
So you can just change the markup to:
<div id="loadingOverlay" class="loader-overlay">
<div class="loader-content loader-center">
<div class="loader-center loader-text">Loading, please wait...</div>
<img src="http://www.mysarkarinaukri.com/images/loadingBar.gif" class="loader-center loader-img" alt=""/>
</div>
</div>
http://jsfiddle.net/bLz7wgvs/6/

CSS transform: translate not working on iPad

I've made a simple under construction website which has an image and some text centred in the middle of the page like following:
HTML Code:
<body>
<div id="container">
<span id="wip">Under Construction</span>
<img id="image" src="katte.jpg">
<span id="text">Glæd dig, her åbner katteboxen.dk i foråret 2015. Vi glæder os til at forkæle din kat med en spændende pakke hver måned.</span>
</div>
</body>
CSS Code:
body {
margin: 0;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
font-weight: 700;
text-align: center;
}
#container {
max-width: 1230px;
width: 100%;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
#image {
width: 100%;
}
#text {
font-size: 20px;
padding: 0 15px;
display: block;
}
#wip {
font-size: 40px;
padding: 0 15px;
display: block;
}
Link: http://katteboxen.dk/
Everything is working good except when it comes to iPads. The content is showing like when for example the css rule transform: translate(-50%, -50%); wasn't applied for the container. What are the alternatives for fixing this issue? Any guidance or feedback is more than welcomed.
You might need to try browser specific prefixes for transform property, so:
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
should do the trick.
For reference have a look here
transform property are browser based property set -webkit-transform, -moz-transform, -o-transform .... ans so set it according to your i-pad browser i this it will solve the problem
or just use
margin-left:-50%;
margin-top:-50%;

Align rotated div with another div

I have a main div (the red div in the fiddle) that has a smaller vertical tab on the side (the blue div in the fiddle).
The RED div is standard BUT the Blue div is rotated through 90 degrees (as I need to have vertical text in it). This is where the problems starts.
The red div is vertically positioned at 50% so it is in the middle of the page and locked with scrolling etc.
I want to align the blue div so that the top edge of the blue div is at the same Y position as the top edge of the red div.
I would prefer NOT to use jQuery but can do if required.
Desired output :
Fiddle is here : http://jsfiddle.net/kBKf6/
Here is the code I am using :
<div id="main" style="position: fixed; top: 50%; margin-top: -250px; left:0; height: 500px; width: 450px; background-color:red;">
Main Content Div
</div>
<div id="vertical_div" style="overflow:hidden; position: fixed; left:350px; height:40px; width:200px; margin: auto; background-color:blue; text-align:center; color:white; -webkit-transform: rotate(90deg) translate(-50%, -50%); -moz-transform: rotate(90deg) translate(-50%, -50%); -ms-transform: rotate(90deg) translate(-50%, -50%); -o-transform: rotate(90deg) translate(-50%, -50%); transform: rotate(90deg) translate(-50%, -50%);">
Side Tab
</div>
You don't need JS to align the rotated div. You can define a transform origin in CSS then, it becomes easy to align.
Side note : You can remove the -moz- and -o- vendor prefixes see caniuse
DEMO
HTML :
<div id="main">Main Content Div
<div id="verticaldiv">Side Tab</div>
</div>
CSS :
#main {
position: fixed;
top: 50%;
margin-top: -250px;
left:0;
height: 500px;
width: 450px;
background-color:red;
}
#verticaldiv {
overflow:hidden;
position: absolute;
left:100%;
bottom:100%;
height:40px;
width:200px;
background-color:blue;
text-align:center;
color:white;
-webkit-transform: rotate(90deg);
-ms-transform: rotate(90deg);
transform: rotate(90deg);
-ms-transform-origin:0 100%;
-webkit-transform-origin: 0 100%;
transform-origin: 0 100%;
}
You can also do it without relying on hardcoded sizes that move your div into position, but you need a wrapper around your .verticaldiv
demo:
http://jsfiddle.net/MCr6f/
demo 2:
http://jsfiddle.net/9LtKw/ (to show that different sizes don't matter)
html:
<div class="one">
Hello
<div class="pivot">
<div class="two">
Pretty!
</div>
</div>
</div>
css:
.one {
background: red;
position: relative;
float: left;
/*strange and difficult sizes*/
font-size: 3.237827em;
padding: 10px;
}
.pivot {
position: absolute;
top: 0;
right: 0;
-webkit-transform: rotate(90deg);
-moz-transform: rotate(90deg);
transform: rotate(90deg);
width: 0px;
height: 0px;
}
.two {
background: blue;
color: white;
position: absolute;
left: 0;
bottom: 0;
/*strange and difficult sizes*/
font-size: 12px;
padding: 0.3em;
}

absolute img center position in div

How to center align img with absolute positioning in div or table cell if I dont know width of image?
for example
<div style="position: relative">
<img style="position: absolute" />
</div>
Aligns the element horizontal and vertical to the relative parent.
<div style="position: relative">
<img class="centered" />
</div>
.centered {
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
'top' and 'left' set to 50% will center the top-left-corner of the element. Translating is by -50% for both X and Y sets the element to the exact center of the parent.
For browser support take a look here:
Translate2d - caniuse.com
You can use the negative margin trick, assumed you know the dimensions of the image:
img{
top:50%;
left:50%;
margin-left : -(<imagewidth>/2)px;
margin-height: -(<imageheight>/2)px;
}
when you only target browsers which support the calc() feature you could do:
img{
top: calc(50% - <imagewidth>/2px);
left: calc(50% - <imageheight>/2px);
}
When the browser supports translation you can translate it -50% of the image-dimensions:
img{
top: 50%;
left: 50%;
position: absolute;
transform: translate(-50%, -50%);
}
If none of that is possible, your last resort is altered markup and display:table:
<div>
<div>
<img />
</div>
</div>
div{height:100%;width:100%;
display:table;
text-align: center;
vertical-align: middle;}
div>div{
display:table-cell;
}
see Demo for latest case
img {
display: block;
margin: 0 auto;
}
Check this JSFIDDLE1
or
div {
position: relative;
text-aligm: center;
width:2000px;
}
img {
position: absolute;
}
Check this JSFIDDLE2
Simple way to center an image vertically
if you know the height of div. then you can do image as vertical align.
div{
text-align:center;
height:150px;
line-height: 150px;
}
img{
vertical-align: middle;
}