Position absolute div in center of screen view - html

I want to place div that has absolute position in center of the screen view (scrolled or not scrolled).
I have this but its places div in mid od the document and not mid of current view.
#main {
width: 140px;
height:100px;
border: 1px solid Black;
text-align: left;
position: absolute;
top: 50%;
left: 50%;
margin-left:-70px;
margin-top:-50px;
}

Use the following CSS:
.centered {
position: fixed;
top: 50%;
left: 50%;
/* bring your own prefixes */
transform: translate(-50%, -50%);
}

Change position:absolute to position: fixed and you should be good to go!
When you say position - absolute, the reference div is the parent div that has a position - relative. However if you say position -fixed, the reference is the browser's window. which is wat you want in your case.
In the case of IE6 i guess you have to use CSS Expression

If you don't want to change your element's position to fixed, here is a solution with keeping your element absolut.
Since CSS's calc() is supported by all browsers now, here a solution using calc().
#main {
width: 140px;
height:100px;
border: 1px solid Black;
text-align: left;
position: absolute;
top: calc(50vh - (/* height */100px / 2));
left: calc(50vw - (/* width */140px / 2));
}

A bit more complex way is to use multiple outer boxes. This method works well with or without hard coded width/height of the middle box (background colors added just to show what each box does):
/* content of this box will be centered horizontally */
.boxH
{
background-color: rgba(0, 127, 255, 0.2);
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
display: flex;
flex-direction: column;
align-items: center;
}
/* content of this box will be centered vertically */
.boxV
{
background-color: rgba(255, 0, 0, 0.2);
height: 100%;
display: flex;
flex-direction: row;
align-items: center;
}
/* content of this box will be centered horizontally and vertically */
.boxM
{
background-color: lightblue;
padding: 3em;
}
<div>
some text in the background
</div>
<div class="boxH">
<div class="boxV">
<div class="boxM">
this div is in the middle
</div>
</div>
</div>
https://jsfiddle.net/vanowm/7cj1775e/
If you want display div in the middle regardless of the scroll position, then change position to fixed

Here is a solution using margin and position: fixed :
#main{
width: 140px;
height:100px;
border: 1px solid black;
/* Centering #main on the screen */
position: fixed;
margin: auto;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
It centers the div by increasing the margin on all sides to fit the whole screen.
EDIT: I found out there is a shorthand for top,right,bottom,left that is inset. It has been implemented in major browsers and you can see the compatibility on other browsers here
So to absolutely center a div on a screen:
#main{
width: 140px;
height:100px;
border: 1px solid black;
/* Centering #main on the screen */
position: fixed;
margin: auto;
inset: 0;
}

I managed to place absolutely positioned text in the center with the following:
position: absolute;
text-align: center;
left: 1%;
right: 1%;
This is a variation of the answer from Kenneth Bregat. It maintains absolute positioning rather than fixed, plus it solves text wrapping issues mentioned in some answers. Don't forget that the parent will need relative positioning.

What about this trick:
position: absolute;
height:200px;
top: 0;
left: 1%;
right: 1%;

margin-left: -half_of_the_div;
left: 50%;
position: fixed;
example on codepen

Related

How to make a <div>'s bottom edge below background image

I have a background image, but I need to place a div that its bottom edge should go below the image. What's the easiest way to do this?
Please see the attached image. The white part is the background image and the blue part is my div over the background.
You can create a relative positioned wrapper and then set absolute positioning with bottom: -10%; or bottom: -20px; for a div over a div with image:
.image-with-block-wrapper {
position: relative;
}
.image {
height: 200px;
border: 1px solid #111;
background: url('https://www.gravatar.com/avatar/f42a832da648291bf80206eda08e3332?s=328&d=identicon&r=PG&f=1');
}
.div-over-bg {
border: 1px solid #111;
position: absolute;
height: 50px;
bottom: -10%;
left: 50%;
transform: translateX(-50%);
background: green;
width: 100px;
margin: 0 auto;
}
<html>
<head></head>
<body>
<div class='image-with-block-wrapper'>
<div class='image'></div>
<div class='div-over-bg'></div>
</div>
</body>
</html>
Edit:
In the case of using percents for bottom it will be related with the wrapper height, but you can use bottom: 0;
and transform: translate(-50%, 15%); in order to set the upper block vertical position as relative to the block itself.
So I've created a container with a background image and placed a div inside.
I've given the .block margin: auto; to center it and added position: relative; so I can move it, because it has position: relative; I can add top: 100px; to move it down from the top by 100px
.container {
background-image: url('https://via.placeholder.com/150');
width: 100%;
background-position: cover;
height: 300px;
position: relative;
}
.container .block {
height: 300px;
background-color: blue;
width: 500px;
position: relative;
margin: auto;
top: 100px;
}
<div class="container">
<div class="block">
</div>
</div>
Extra info by #I_Can_Help
In the case of using percents for bottom it will be related with the wrapper height, but you can use bottom: 0;
and transform: translate(-50%, 15%); in order to set the upper block vertical position as relative to the block itself.

transition property for left does not provide smooth animation [duplicate]

An element is arbitrarily placed on a page, and needs to transition to a fixed position on event (screen scroll in my case, but im using hover in the fiddle)
The original position of the element is centered withing a parent (top: auto and left: auto). On hover, it's supposed to smoothly move to the corner of the screen (left: 0, top: 0) and then comeback. Instead, it just jumps in place, ignoring the transition property.
I realize that none of the browsers support transition to auto, but was hoping to find some work around for this.
fiddle
<div>
<span>test</span>
</div>
div {
border: 1px solid red;
text-align: center;
height: 100px;
margin: 15px;
}
span {
display: inline-block;
width: 50px;
height: 50px;
background: blue;
transition: all 1s;
position: fixed;
left: auto;
top: auto;
}
div:hover span {
left: 0;
top: 0;
}
PS. I'm hoping for a css only fix, but a solution with JQuery would work as well.
You are correct in that modern browsers are unable to transition to 'auto'. You can use CSS to achieve what you're looking for though.
In your example, you'll need to center by changing
top: auto;
left: auto;
to
vertical-align: top;
left: calc(50% - 25px);
Remove the top property from the span and span:hover and replace it with vertical-align.
JSFiddle Example
Why don't you set a specific top and left? you have span{position: fixed} so in this case you always know about your top, right, bottom, left (relative to viewport).
so try with:
div {
border: 1px solid red;
text-align: center;
height: 100px;
margin: 15px;
}
span {
display: inline-block;
width: 50px;
height: 50px;
background: blue;
transition: all 1s;
position: fixed;
left: 50%;
margin-left: -25px; /*or transform: translateX(-50%) if you don't know the width of span*/
top: 16px;
}
div:hover span {
left: 0;
top: 0;
margin-left: 0; /*or transform: none if you don't know the width of span*/
}
<div>
<span>test</span>
</div>
You can change the top left as you which to achieve what you want.
Here a jsfiddle example to play with

Keep element always on screen with "position: sticky"

I have a container with known height (height is sometimes longer then screen).
This container has a vertically and horizontally centered element (via flex).
I was hoping to keep this element always on the screen vertically centered in the block in the visible portion of the container.
What I tried:
position:sticky; top:50% - however this only keeps it centered on the bottom half of container.
position:sticky; bottom:50% - however this only keeps it centered on top half
position:sticky; top: 50%; bottom:50%; - it seems top overrides bottom so this is just like my first try with top:50% only
I tried setting negative values but it didn't work
Here is a demo:
.container {
height: 1200px;
background-color: rgba(0, 0, 0, .7);
color: white;
display: flex;
justify-content: center;
align-items: center;
}
.center-piece {
background-color: green;
position: sticky;
top: 50%;
}
.center-piece2 {
background-color: steelblue;
position: sticky;
bottom: 50%;
}
<div class="container">
<div class="center-piece">
#1
</div>
<div class="center-piece2">
#2
</div>
</div>
Is there anyway to keep it perfectly centered, while "always on screen", in the visible porition of container, both top and bottom?
Here is a screencast of my application: https://www.youtube.com/watch?v=CwYaBgolNHU
The "rawr" will be the controls for the image behind it.
I may have misunderstood your question, but can you use:
position: fixed;
top: 50vh;
?
.container {
height: 1200px;
background-color: rgba(0, 0, 0, .7);
color: white;
display: flex;
justify-content: center;
align-items: center;
}
.center-piece {
background-color: green;
position: fixed;
top: 50vh;
left: 50vw;
transform: translate(-50%,50%);
}
<div class="container">
<div class="center-piece">#1</div>
</div>
NOTE: Compatibility varies across browsers..
According to caniuse.com, position: sticky is supported on current major browsers (Not IE).
jsFiddle
.container {
height: 1200px;
background-color: rgba(0, 0, 0, .7);
color: white;
display: flex;
justify-content: center;
align-items: center;
}
.center-piece {
background-color: green;
position: sticky;
top: 10px; /* 1 */
bottom: 50%;
left: 50%;
transform: translate(-50%,50%); /* 2 */
}
.center-piece2 {
background-color: steelblue;
position: sticky;
bottom: 10px; /* 3 */
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
<div class="container">
<div class="center-piece">#1</div>
<div class="center-piece2">#2</div>
</div>
NOTES:
position: sticky kicks in when element reaches top: 10px of viewport
How centering works with CSS positioning and transform properties
position: sticky kicks in when element reaches bottom: 10px of viewport

Center text inside a DIV [duplicate]

This question already has answers here:
How can I center text (horizontally and vertically) inside a div block?
(27 answers)
Closed 3 years ago.
I have been stuck with this annoying issue that I have...I cannot center a text inside a div.
I managed to get the text to BEGIN at the center, but I want the text in whole to be centered.
Here is my example - any tips and tricks are VERY appreciated.
#box {
position: absolute;
top: 100px;
left: 50%;
height: 100px;
width: 450px;
text-align: center;
margin: 0px 0px 0px -225px;
border: 2px solid black;
}
#TEXT {
position: absolute;
top: 30px;
left: 50%
}
<div id="box">
<p id="TEXT">This text is not centered</p>
</div>
My example: http://jsfiddle.net/y97myrap/
<div style="text-align:center;">
data is here
</div>
in html5
use text-align:center;, this will work if you give width to the concerned div....
Make div 100% wide, then text-align:center; will push everything in center
#box {
position: absolute;
top: 100px;
left: 50%;
height: 100px;
width: 450px;
text-align: center;
margin: 0px 0px 0px -225px;
border: 2px solid black;
}
#TEXT {
position: absolute;
top: 30px;
width:100%;
text-align:center;
<!-- left: 50% -->
}
<div id="box">
<p id="TEXT">This text is not centered</p>
</div>
try to remove position: absolute; from #TEXT
http://jsfiddle.net/y97myrap/4/
Fot centering an element using position absolute, the common way is set:
position:absolute;
left:50%;
margin left:{Minus half of the width of the element you want to center}
So, my suggestion for you case is the following:
http://jsfiddle.net/y97myrap/1/
using an outside container div and then adding text-align to that one,
and if you want to center the height as well, if the parent height is fixed you can just use a line height on the text the same height as the container, like this:
http://jsfiddle.net/y97myrap/5/
You can use display: table tecnique. Add table in container and table-cell in child element:
#box {
position: absolute;
top: 100px;
left: 50%;
height: 100px;
width: 450px;
text-align: center;
margin: 0px 0px 0px -225px;
border: 2px solid black;
display: table;/*Add display table*/
}
#TEXT {
display: table-cell;/*Add display table cell*/
vertical-align: middle;/*add vertical-align middle*/
}
<div id="box">
<p id="TEXT">This text is not centered</p>
</div>
the problem in your fiddle is that you have position:absolute; and a left of 50%...I'm guessing u tried to center it this way, but the problem is the text block will start after 50%. Remove position, give it text-align:center;.
And to align it vertically consider using table cells
Remove the position:absolute and just add auto margings to your #TEXT:
#TEXT {
top: 30px;
left: 50%
margin-left: auto;
margin-right:auto;
}
#TEXT {
position: relative;
top:20%
}
Try it
It's a simple matter of applying a negative top margin of half the images height, and a negative left margin of half the width.
Code:
#box {
position: fixed;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -100px;
}
It will make the position of the object fixed, you can adjust the positions of the div as per your need.
This code solved it:
#TEXT {
position: relative;
display: block;
text-align: center;
width: 100%;
top: 30px;
}

Element relatively position to the right:0 of element

I am trying to achieve a dashed (custom) bored along the left and right of a 1000px fixed width page.
The left one is fine, this works a treat:
#border-left{
position: absolute;
float:left;
top: 0px;
bottom: 0;
width: 5px;
background-image: url('../img/border.gif');
background-repeat:repeat-y;
}
However when I do it over on the right hand side, it wont quite work. I need it to relatively position to the right of the 1000px rather than of the window.
#border-right{
position: relative;
right: 0;
top: 0;
bottom: 0;
margin-top: -90px;
width: 5px;
background-image: url('../img/border.gif');
background-repeat:repeat-y;
}
Parent element:
#container{
width:1000px;
display: block;
margin:0px auto;
text-align:left;
padding-top:90px;
}
That does not work. Can I achieve this? I need it to essentially float: right (but then i cannot make the height 100% of the browser window). Thanks
Demo: http://jsfiddle.net/iambriansreed/sAhmc/
Removed the floats on absolute elements. Added absolute position to parent and centered using left and margin. Removed unneeded margin-top on right border. Replaced border id's with classes.
Borders sit outside the 1000px width.
#container>.border{
position: absolute;
top: 0;
bottom: 0;
width: 5px;
background-image: url('../img/border.gif');
background-repeat:repeat-y;
}
#container>.border.left{
left: -5px;
background-color: red; /* demo */
}
#container>.border.right{
right: -5px;
background-color: blue; /* demo */
}
#container{
position: absolute;
top: 0;
bottom: 0;
width: 100px; /* demo */
left: 50%;
margin-left: -50px; /* half of width */
text-align: left;
padding-top: 90px;
overflow: visible;
background: #eee; /* demo */
}​
I think adding a "position: relative;" rule to the #container element should work for you.