Change 3D perspective while scrolling - fixed vanishing point - css

I'm trying to built up a small webpage, which is based on 3d boxes.
This page will be scrollable, and i want the vanishing point to stay fixed in the middle, so when I scroll the 3d boxes should change their look dynamically. The only result I was able to get is this: http://deesr.com/3dscroll/
In this Version the vanishing point stays at the starting point, and when i scroll the boxes stay the same.
EDIT: JS did the job. I used the OnScroll event to check the scroll position and re-setting the Perspective-Origin. Let me know if there's a better solution!

I know it is to late for the OP, but for all others coming across this question:
I was able to solve it by using a <div> inside the body that is emulating the body's scrollbars.
<html>
<head>
<style>
body {
padding: 0;
margin: 0;
}
body > * {
overflow: auto;
width: 100%;
height: 100%;
position: absolute;
perspective: 1000px;
}
</style>
</head>
<body>
<div>
3d objects
</div>
</body>
</html>
Full example:
<html>
<head>
<style>
body {
padding: 0;
margin: 0;
}
body > * {
overflow: auto;
width: 100%;
height: 100%;
position: absolute;
perspective: 1000px;
}
div#container {
width: 200%;
transform-style: preserve-3d;
}
#t,
#b,
#l,
#r {
margin-left: 45%;
margin-right: 45%;
width: 100px;
height: 100px;
background-color: yellow;
border: 10px solid black;
border-radius: 10px;
transform-style: preserve-3d;
}
#t {
transform: rotateX(270deg);
}
#b {
transform: rotateX(90deg);
}
#l {
position: relative;
top: 180px;
left: -60px;
transform: rotateY(270deg);
}
#r {
position: relative;
top: -180px;
left: 60px;
transform: rotateY(90deg);
}
</style>
</head>
<body>
<div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<div id="container">
<div id="l">
<h1>left</h1>
</div>
<div id="t">
<h1>top</h1>
</div>
<div id="b">
<h1>bottom</h1>
</div>
<div id="r">
<h1>right</h1>
</div>
</div>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</div>
</body>
</html>

My solution was that I reset the vanishing-point every time the user scrolls.
$(window).scroll(function() {
var scrollLocation = $(document).scrollTop();
var vanishingPoint = scrollLocation + window.innerHeight / 2;
$("#wrapper").css('-webkit-perspective-origin', ' 50% ' + vanishingPoint + 'px');
})

Related

Make an image look like it is placed over 2 <div> boxes with CSS

Hello I need to position an image as in the example. Theoretically it looks like it is positioned over 2 seperate boxes with different background colors, that is the goal, but practically it is not possible, at least for me. How to solve the problem?
Usually you'd do this with flex and vertical alignment, but since you want specifically the image to be between boxes i'd say absolute is the way to go here
.card {
display: block;
margin-left: 80px; /* image width + 20px */
}
.header, .image-container {
display: block;
margin: 0;
}
.header h1 {
margin: 0;
}
.image-container {
height: 1px;
position: relative;
}
.image-container .image {
display; inlnie-block;
width: 60px;
height: 60px;
border-radius: 50%;
background: purple;
position: absolute;
top: -50%;
left: -10px;
transform: translateY(-50%) translateX(-100%);
}
<div class="card">
<div class="header">
<h1>Header</h1>
</div>
<div class="image-container">
<div class="image"></div>
</div>
<div class="header">
<h1>Header 2</h1>
</div>
</div>
The simplest solution will be using a combination of an of z-index and position:absolute.
*A small suggestion if you may encounter the problem: you must use z-index with specifying the position (position: static will not work)
img {
width: 100px;
height: 100px;
z-index: 99;
position: absolute;
}
div {
background-color: black;
z-index: 1;
width: 200px;
height: 100px;
position: absolute;
top: 10px;
left: 5px;
}
<img src='https://upload.wikimedia.org/wikipedia/en/thumb/8/80/Wikipedia-logo-v2.svg/1200px-Wikipedia-logo-v2.svg.png'>
<div></div>

How to get the div block to be centered in a modal window

I am a NOOB with css and am trying to construct a modal that will hold a video. I want the div that will hold the video to be placed in the center of the screen.
However, I'm having some trouble positioning it. I must be misunderstanding how this positioning works. It seems incorrect to have to use bottom: 1700px.
I want the user to be able to move the box up and down by scrolling in case their screen is too small.
How can center this div in the screen correctly?
HTML:
<!-- language: lang-html -->
<div id="videoModal" class="modalContainer">
<div class="videoOverlay"></div>
<div class="videoContainer">
</div>
</div>
CSS:
/*-------------------------*/
/* ----- Video Modal ----- */
/*-------------------------*/
<!-- language: lang-css -->
.modalContainer {
display: none;
}
.modalContainer.open {
display: inherit;
}
.videoOverlay {
background-color: black;
opacity: .8;
position:fixed;
top:0px;
bottom:0px;
left:0px;
right:0px;
z-index:100;
}
.videoContainer {
background-color: white;
width: 500px;
height: 500px;
z-index: 200;
position: absolute;
bottom: 1700px;
left: 25%;
}
JS:
<!-- language: lang-js -->
$('#openVideoModal').click(function(){
$('.modalContainer').addClass('open').removeClass('closed');
});
If you want to position .videoContainer at the center of the screen, you can do the following:
.videoContainer {
background-color: white;
width: 500px;
height: 500px;
z-index: 200;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
Try this on your videoContainer:
.videoContainer {
background-color: white;
width: 500px;
height: 500px;
z-index: 200;
position: absolute;
top: 50%;
margin-top: 250px;
left: 25%;
}
Basically, you set your block at 50% from the top of your page with a negative margin of half your height value.
Same for horizontal values.

Fixed element in transform translate container not working

I have a wrapper box that I want to animate with transform translate but if I do this I can't use fixed element inside.
example :
<div class="wrapper">
<div class="box-content">
<div class="fixed-element">
</div>
</div>
</div>
<style type="text/css">
.wrapper {
transform: translateX(50px);
background: pink;
}
.box-content {
height: 1000px;
background: green;
}
.fixed-element{
position: fixed;
top: 0;
left: 0;
width: 50px;
height: 50px;
background: blue;
}
</style>
https://jsfiddle.net/aobv5azy/
I don't want use javascript, and I want use transform translate. Animate with "left" is not good for performances.

Overlapping images in css/html5

I am trying to put together a portfolio web site with four images that will be used as buttons and a center logo. I want four images to serve as links in the corners of the screen (each taking up roughly half the page) with a center logo overlapping them.
I've put together the CSS for the site, and each time I've tried to preview the site, I get the four images in the corner of the screen, with my center logo not overlapping.
here is the code that I'm using for the images:
div.upperLEFT {
position: relative;
float: left;
left: 0;
top: 0px;
right: auto;
bottom: auto;
width: 50%;
height: 50%;
max-width: 50%;
max-height: 50%;
z-index: 10;
}
div.upperRIGHT {
position: relative;
float: right;
left: auto;
top: 0px;
bottom: auto;
right: 0;
width: 50%;
height: 50%;
max-width: 50%;
max-height: 50%;
z-index: 10;
}
div.lowerLEFT {
position: relative;
margin-left: 0;
margin-top: auto;
margin-bottom: 0;
margin-right: auto;
width: 50%;
height: 50%;
max-width: 50%;
max-height: 50%;
z-index: 10;
}
div.lowerRIGHT {
position: relative;
float: right;
margin-left: auto;
margin-right: 0;
margin-top: auto;
margin-bottom: 0;
width: 50%;
height: 50%;
max-width: 50%;
max-height: 50%;
z-index: 10;
}
div.centerLOGO {
position: relative;
overflow: visible;
margin-left: auto;
margin-right: auto;
margin-top:auto;
margin-bottom: auto;
width: auto;
height: auto;
max-width: 50%;
max-height: 50%;
z-index: 20;
}
here is my html:
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<title> CJ Wormely's Portfolio </title>
<link href = "cjwstyle.css" rel = "stylesheet" type "text/css">
</head>
<body>
<h1> CJ Wormely Portfolio </h1>
<div class = "upperLEFT"> <img src = "aboutmetile.svg" /> </div>
<div class = "upperRIGHT"> <img src = "IDTile.svg"/> </div>
<div class = "centerLOGO"> <img src = "siteLogo.svg"/> </div>
<div class = "lowerLEFT"> <img src = "eMailTile.svg"/> </div>
<div class = "lowerRIGHT"> <img src = "artTile.svg"/> </div>
</body>
</html>
There are several ways to center your logo on the screen.
Now, as the other poster said, your code is "all over the place". I'm thinking you were experimenting and noticed this combination was close to working, and so stuck with it (we've all been guilty of that at some point). But using the right tool for the right job is important (to avoid unpredictable/hilarious results), so might I suggest this combination instead...
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CJ Wormely's Portfolio</title>
<link href="cjwstyle.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>CJ Wormely Portfolio</h1>
<div class="corner upperLEFT"><img src="aboutmetile.svg"></div>
<div class="corner upperRIGHT"><img src="IDTile.svg"></div>
<div class="corner lowerLEFT"><img src="eMailTile.svg"></div>
<div class="corner lowerRIGHT"><img src="artTile.svg"></div>
<div class="centerLOGO"><img src="siteLogo.svg"></div>
</body>
</html>
CSS
.corner {
position: absolute;
width: 50%;
height: 50%;
}
.upperLEFT {
left: 0;
top: 0;
}
.upperRIGHT {
right: 0;
top: 0;
}
.lowerLEFT {
left: 0;
bottom: 0;
}
.lowerRIGHT {
right: 0;
bottom: 0;
}
.centerLOGO {
position: absolute;
left: 50%;
top: 50%;
}
.centerLOGO img {
position: relative;
top: -15px; /* your logo height divided in half */
left: -15px; /* your logo width divided in half */
}
There are definitely several different methods of vertically centering an element. Given the provided info, however, this method should work easily for you.
To see how this is expected to work, please see this example:
http://jsfiddle.net/D2Mz5/2/
Edit: To use z-index properly in your effect, use z-index: 999999;. Your images are spaced like that, undesirably as your .png because you need to define a wrapper.
Below is what I would do. I would nest in a few divs Note: // This isn't the full solution, I hope to point you in the right layout direction before writing this full page for you. You will have to define a few more properties to the CSS even to the ones I started - this is the logic I would suggest and should get you there pretty quick.
#mainwrap {
width: 100%;
min-height: ;
}
#coollogo {
z-index: 999999;
width: ;
height: ;
position: absolute;
margin-top: ;
margin: 0 auto;
}
#top {
width: 100%;
height: ;
}
.upperLeft {
min-width: ;
width: 50%;
float: left;
}
.upperRight {
min-width: ;
width: 50%;
float: right;
}
#bottom {
margin-top: 200px; // define
width: 100%;
height: ;
}
/// etc, etc
The Mark-Up:
<div id="mainwrap"><!-- main wrapper -->
<div id="coollogo"></div><!-- the cool logo -->
<div id="top"><!-- top half -->
<div class="upperLeft"></div>
<div class="upperRight"></div>
</div>
<div id="bottom"><!-- bottom half -->
<div class="bottomLeft"></div>
<div class="bottomRight"></div>
</div>
</div><!--// end main wrap -->
I would also minimize div.upperLEFT { to just .upperLEFT { as there is no need to type div each time. If you continue to run into coding errors, consult a Validator for more information http://validator.w3.org/

How can I center an absolutely positioned element in a div?

I want to place a div (with position:absolute;) element in the center of the window. But I'm having problems doing so, because the width is unknown.
I tried the following CSS code, but it needs to be adjusted because the width is responsive.
.center {
left: 50%;
bottom: 5px;
}
How can I achieve this?
This works for me:
#content {
position: absolute;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
width: 100px; /* Need a specific value to work */
}
<body>
<div>
<div id="content">
I'm the content
</div>
</div>
</body>
<body>
<div style="position: absolute; left: 50%;">
<div style="position: relative; left: -50%; border: dotted red 1px;">
I am some centered shrink-to-fit content! <br />
tum te tum
</div>
</div>
</body>
Responsive Solution
Here is a good solution for responsive design or unknown dimensions in general if you don't need to support IE8 and lower.
.centered-axis-x {
position: absolute;
left: 50%;
transform: translate(-50%, 0);
}
.outer {
position: relative; /* or absolute */
/* unnecessary styling properties */
margin: 5%;
width: 80%;
height: 500px;
border: 1px solid red;
}
.inner {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
/* unnecessary styling properties */
max-width: 50%;
text-align: center;
border: 1px solid blue;
}
<div class="outer">
<div class="inner">I'm always centered<br/>doesn't matter how much text, height or width i have.<br/>The dimensions or my parent are irrelevant as well</div>
</div>
Here is a JS Fiddle
The clue is, that left: 50% is relative to the parent while the translate transform is relative to the elements width/height.
This way you have a perfectly centered element, with a flexible width on both child and parent. Bonus: this works even if the child is bigger than the parent.
You can also center it vertically with this (and again, width and height of parent and child can be totally flexible (and/or unknown)):
.centered-axis-xy {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
Keep in mind that you might need transform vendor prefixed as well. For example -webkit-transform: translate(-50%,-50%);
<div style='position:absolute; left:50%; top:50%; transform: translate(-50%, -50%)'>
This text is centered.
</div>
This will center all the objects inside div with position type static or relative.
I just wanted to add if someone wants to do it with a single div tag then here is the way out:
Taking width as 900px.
#styleName {
position: absolute;
left: 50%;
width: 900px;
margin-left: -450px;
}
In this case one should know the width beforehand.
Responsive solution
Assuming the element in the div, is another div...
This solution works fine:
<div class="container">
<div class="center"></div>
</div>
The container can be any size (must be position relative):
.container {
position: relative; /* Important */
width: 200px; /* Any width */
height: 200px; /* Any height */
background: red;
}
The element (div) can also be any size (must be smaller than the container):
.center {
position: absolute; /* Important */
top: 50%; /* Position Y halfway in */
left: 50%; /* Position X halfway in */
transform: translate(-50%,-50%); /* Move it halfway back(x,y) */
width: 100px; /* Any width */
height: 100px; /* Any height */
background: blue;
}
The result will look like this. Run the code snippet:
.container {
position: relative;
width: 200px;
height: 200px;
background: red;
}
.center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100px;
height: 100px;
background: blue;
}
<div class="container">
<div class="center"></div>
</div>
I found it very helpful.
Absolute Centre
HTML:
<div class="parent">
<div class="child">
<!-- content -->
</div>
</div>
CSS:
.parent {
position: relative;
}
.child {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
Demo:
http://jsbin.com/rexuk/2/
It was tested in Google Chrome, Firefox, and Internet Explorer 8.
This works for vertical and horizontal:
#myContent{
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
And if you want make an element center of the parent, set the position of the parent relative:
#parentElement{
position: relative
}
For vertical center align, set the height to your element. Thanks to Raul.
If you want make an element center of the parent, set the position of the parent to relative
If you need to center horizontally and vertically too:
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
Searching for a solution, I got the previous answers and could make content centered with Matthias Weiler's answer, but using text-align:
#content{
position: absolute;
left: 0;
right: 0;
text-align: center;
}
It worked with Google Chrome and Firefox.
I understand this question already has a few answers, but I've never found a solution that would work in almost all classes that also makes sense and is elegant, so here's my take after tweaking a bunch:
.container {
position: relative;
}
.container .cat-link {
position: absolute;
left: 50%;
top: 50%;
transform: translate3d(-50%,-50%,0);
z-index: 100;
text-transform: uppercase; /* Forces CSS to treat this as text, not a texture, so no more blurry bugs */
background-color: white;
}
.color-block {
height: 250px;
width: 100%;
background-color: green;
}
<div class="container">
<a class="cat-link" href="">Category</a>
<div class="color-block"></div>
</div>
It is saying give me a top: 50% and a left: 50%, then transform (create space) on both the X/Y axis to the -50% value, in a sense "create a mirror space".
As such, this creates an equal space on all the four points of a div, which is always a box (has four sides).
This will:
Work without having to know the parent's height / width.
Work on responsive.
Work on either X or Y axis. Or both, as in my example.
I can't come up with a situation where it doesn't work.
Flexbox can be used to center an absolute positioned div.
display: flex;
align-items: center;
justify-content: center;
.relative {
width: 275px;
height: 200px;
background: royalblue;
color: white;
margin: auto;
position: relative;
}
.absolute-block {
position: absolute;
height: 36px;
background: orange;
padding: 0px 10px;
bottom: -5%;
border: 1px solid black;
}
.center-text {
display: flex;
justify-content: center;
align-items: center;
box-shadow: 1px 2px 10px 2px rgba(0, 0, 0, 0.3);
}
<div class="relative center-text">
Relative Block
<div class="absolute-block center-text">Absolute Block</div>
</div>
This is a mix of other answers, which worked for us:
.el {
position: absolute;
top: 50%;
margin: auto;
transform: translate(-50%, -50%);
}
This works on any random unknown width of the absolute positioned element you want to have in the centre of your container element:
Demo
<div class="container">
<div class="box">
<img src="https://picsum.photos/200/300/?random" alt="">
</div>
</div>
.container {
position: relative;
width: 100%;
}
.box {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
display: flex;
justify-content: center;
align-items: center;
}
It's possible to center an element that has aspect-ratio:1 with position absolute by using calc()
In the following example I'm using a circle because it's easier to explain and understand, but the same concept can be applied to any shape with aspect-ratio:1 meaning that the width and height are equal. (about aspect-ratio)
:root{
--diameter: 80px;
}
div{
position:absolute;
top: calc(50% - var(--diameter)/2);
right:calc(50% - var(--diameter)/2);
aspect-ratio:1;
width:var(--diameter);
border-radius:100%;
background:blue;
}
<div/>
Explanation
As far as I know, this is impossible to achieve for an unknown width.
You could - if that works in your scenario - absolutely position an invisible element with 100% width and height, and have the element centered in there using margin: auto and possibly vertical-align. Otherwise, you'll need JavaScript to do that.
I'd like to add on to bobince's answer:
<body>
<div style="position: absolute; left: 50%;">
<div style="position: relative; left: -50%; border: dotted red 1px;">
I am some centered shrink-to-fit content! <br />
tum te tum
</div>
</div>
</body>
Improved: /// This makes the horizontal scrollbar not appear with large elements in the centered div.
<body>
<div style="width:100%; position: absolute; overflow:hidden;">
<div style="position:fixed; left: 50%;">
<div style="position: relative; left: -50%; border: dotted red 1px;">
I am some centered shrink-to-fit content! <br />
tum te tum
</div>
</div>
</div>
</body>
Just wrap your content with a new div and use display flex and then use align-items: center; and justify-content: center; take a look...
<div class="firstPageContainer">
<div class="firstPageContainer__center"></div>
</div>
.firstPageContainer{
display: flex;
width: 1000px;
height: 1000px;
justify-content: center;
align-items: center;
background-color: #FF8527;
}
.firstPageContainer__center{
position:absolute;
width: 50px;
height: 50px;
background-color: #3A4147;
}
Sass/Compass version of a previous responsive solution:
#content {
position: absolute;
left: 50%;
top: 50%;
#include vendor(transform, translate(-50%, -50%));
}
This worked for me:
<div class="container><p>My text</p></div>
.container{
position: absolute;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
}
My preferred centering method:
position: absolute;
margin: auto;
width: x%
absolute block element positioning
margin auto
same left/right, top/bottom
A JSFiddle is here.
Here's a useful jQuery plugin to do this. I found it here. I don't think it's possible purely with CSS.
/**
* #author: Suissa
* #name: Absolute Center
* #date: 2007-10-09
*/
jQuery.fn.center = function() {
return this.each(function(){
var el = $(this);
var h = el.height();
var w = el.width();
var w_box = $(window).width();
var h_box = $(window).height();
var w_total = (w_box - w)/2; //400
var h_total = (h_box - h)/2;
var css = {"position": 'absolute', "left": w_total + "px", "top":
h_total + "px"};
el.css(css)
});
};
#container
{
position: relative;
width: 100%;
float: left
}
#container .item
{
width: 50%;
position: absolute;
margin: auto;
left: 0;
right: 0;
}
HTML:
<div id='parent'>
<div id='child'></div>
</div>
CSS:
#parent {
display: table;
}
#child {
display: table-cell;
vertical-align: middle;
}
I know I already provided an answer, and my previous answer, along with others given, work just fine. But I have used this in the past and it works better on certain browsers and in certain situations. So I thought I'd give this answer as well. I did not "Edit" my previous answer and add it because I feel this is an entirely separate answer and the two I have provided are not related.
The accepted solution of this question didn't work for my case...
I'm doing a caption for some images and I solved it using this:
top: 0;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
display: flex;
align-items: center;
figure {
position: relative;
width: 325px;
display: block
}
figcaption{
position: absolute;
background: #FFF;
width: 120px;
padding: 20px;
-webkit-box-shadow: 0 0 30px grey;
box-shadow: 0 0 30px grey;
border-radius: 3px;
display: block;
top: 0;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
display: flex;
align-items: center;
}
<figure>
<img src="https://picsum.photos/325/600">
<figcaption>
But as much
</figcaption>
</figure>
HTML
<div id='parent'>
<div id='centered-child'></div>
</div>
CSS
#parent {
position: relative;
}
#centered-child {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto auto;
}
http://jsfiddle.net/f51rptfy/
This solution works if the element has width and height
.wrapper {
width: 300px;
height: 200px;
background-color: tomato;
position: relative;
}
.content {
width: 100px;
height: 100px;
background-color: deepskyblue;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
<div class="wrapper">
<div class="content"></div>
</div>
.center {
position: absolute
left: 50%;
bottom: 5px;
}
.center:before {
content: '';
display: inline-block;
margin-left: -50%;
}
This is a trick I figured out for getting a DIV to float exactly in the center of a page. It is really ugly of course, but it works in all browsers.
Dots and Dashes
<div style="border: 5 dashed red;position:fixed;top:0;bottom:0;left:0;right:0;padding:5">
<table style="position:fixed;" width="100%" height="100%">
<tr>
<td style="width:50%"></td>
<td style="text-align:center">
<div style="width:200;border: 5 dashed green;padding:10">
Perfectly Centered Content
</div>
</td>
<td style="width:50%"></td>
</tr>
</table>
</div>
Cleaner
Wow, those five years just flew by, didn't they?
<div style="position:fixed;top:0px;bottom:0px;left:0px;right:0px;padding:5px">
<table style="position:fixed" width="100%" height="100%">
<tr>
<td style="width:50%"></td>
<td style="text-align:center">
<div style="padding:10px">
<img src="Happy.PM.png">
<h2>Stays in the Middle</h2>
</div>
</td>
<td style="width:50%"></td>
</tr>
</table>
</div>
HTML:
<div class="wrapper">
<div class="inner">
content
</div>
</div>
CSS:
.wrapper {
position: relative;
width: 200px;
height: 200px;
background: #ddd;
}
.inner {
position: absolute;
top: 0; bottom: 0;
left: 0; right: 0;
margin: auto;
width: 100px;
height: 100px;
background: #ccc;
}
This and more examples here.

Resources