Pseudo 3d effect with CSS transforms? - css

I have a div containing small div's that each have a background-image of a colored ball:
<div class="container">
<div class="ball" style="left: 100px; top: 50px;">
<div class="ball" style="left: 120px; top: 100px;">
<div class="ball" style="left: 140px; top: 150px;">
<div class="ball" style="left: 160px; top: 200px;">
<div class="ball" style="left: 180px; top: 250px;">
</div>
Now I would like to create some sort of pseudo 3D effect, where the balls that have a smaller value of 'top' are smaller , and balls that are more near the bottom appear larger.
I've read some bits about CSS3 transforms and perspective, but I have no idea how to do it.
Is there any way to adjust the width and height of div's , based on its top-property ?

You can create a variable to store top value in css like this style="--top: 10px" and use it later like this var(--top).
Update
You need to use JavaScript to set the --top property using setProperty method and you can get the value of --top using getPropertyValue method. See the example for dynamically changing --top onclick event which changes the --top value and width and height are adjusted automatically.
Code Snippet
balls = document.getElementsByClassName('ball');
Array.prototype.forEach.call(balls, function(ball){
ball.onclick = function() {
ball.style.setProperty('--top', parseInt(ball.style.getPropertyValue('--top')) * 1.2 + 'px')
}
})
.ball {
background-image:url(https://upload.wikimedia.org/wikipedia/commons/e/ec/Soccer_ball.svg);
width: calc(var(--top) * 1.2);
height: calc(var(--top) * 1.2);
background-size: cover;
position: absolute;
}
<div class="container">
<div class="ball" style="left: 100px; --top: 50px;"></div>
<div class="ball" style="left: 120px; --top: 100px;"></div>
<div class="ball" style="left: 140px; --top: 150px;"></div>
<div class="ball" style="left: 160px; --top: 200px;"></div>
<div class="ball" style="left: 180px; --top: 250px;"></div>
</div>

You can set a real 3d transform on the containing element:
.container {
width: 400px;
height: 300px;
position: relative;
perspective: 300px;
}
.inner {
width: 100%;
height: 100%;
position: relative;
transform: rotateX(60deg);
transform-style: preserve-3d;
border: solid 2px green;
}
.ball {
width: 30px;
height: 30px;
border-radius: 50%;
background-color: lightblue;
position: absolute;
transform: rotateX(-60deg);
transform-style: preserve-3d;
}
<div class="container">
<div class="inner">
<div class="ball" style="left: 100px; top: 50px;"></div>
<div class="ball" style="left: 120px; top: 100px;"></div>
<div class="ball" style="left: 140px; top: 150px;"></div>
<div class="ball" style="left: 160px; top: 200px;"></div>
<div class="ball" style="left: 180px; top: 250px;"></div>
</div>
</div>

Related

Change the image size on hover without changing the parent div's size

I have a question. I need to change the image size on hover without changing the parent div's size. Also, I have some tags inside the same div with the image. When I add hover on image and give some pxs to width and height, the tags move with the image.
img {
width: 200px;
height: 200px;
}
img:hover {
width: 250px;
height: 250px;
overflow: hidden;
}
<div class="product-items">
<div class="desc">
<img src="https://cdn.vox-cdn.com/thumbor/v97OD-MBgNjw8p5crApucVs9RB8=/0x0:2050x1367/1800x1800/filters:focal(1025x684:1026x685)/cdn.vox-cdn.com/uploads/chorus_asset/file/22022572/bfarsace_201106_4269_012.0.jpg" alt="">
<div class="cost">
<p class="your_product">Ваш товар</p>
<p>$89.00</p>
</div>
</div>
</div>
You don't need to alter your HTML - just scale the img on hover rather than alter its width and height. Scaling will not affect the positioning of other elements as the scaled element keeps its original size as far as they are concerned.
img {
width: 200px;
height: 200px;
}
img:hover {
/*width: 250px;
height: 250px;*/
transform: scale(1.25);/* added */
overflow: hidden;
}
<div class="product-items">
<div class="desc">
<img src="https://cdn.vox-cdn.com/thumbor/v97OD-MBgNjw8p5crApucVs9RB8=/0x0:2050x1367/1800x1800/filters:focal(1025x684:1026x685)/cdn.vox-cdn.com/uploads/chorus_asset/file/22022572/bfarsace_201106_4269_012.0.jpg" alt="">
<div class="cost">
<p class="your_product">Ваш товар</p>
<p>$89.00</p>
</div>
</div>
</div>
you can do this by using the transform property
.product-items{
width: 200px;
height: 200px;
}
.img-div{
height: 100px;
width: 100%;
overflow: hidden;
}
img {
width: 100%;
height: 100%;
transition: 0.4s;
}
div:hover img {
transform: scale(1.2,1.2);
}
<div class="product-items">
<div class="desc">
<div class="img-div">
<img
src="https://cdn.vox-cdn.com/thumbor/v97OD-MBgNjw8p5crApucVs9RB8=/0x0:2050x1367/1800x1800/filters:focal(1025x684:1026x685)/cdn.vox-cdn.com/uploads/chorus_asset/file/22022572/bfarsace_201106_4269_012.0.jpg"
alt=""
/>
</div>
<div class="cost">
<p class="your_product">Ваш товар</p>
<p>$89.00</p>
</div>
</div>
</div>
You can try add container on image and set position:relative and for img position:absolute
.img-container {
width: 200px;
height: 200px;
position: relative;
}
img {
width: 100%;
height: 100%;
position: absolute;
}
img:hover {
width: 250px;
height: 250px;
overflow: hidden;
}
<div class="product-items">
<div class="desc">
<div class="img-container">
<img src="https://cdn.vox-cdn.com/thumbor/v97OD-MBgNjw8p5crApucVs9RB8=/0x0:2050x1367/1800x1800/filters:focal(1025x684:1026x685)/cdn.vox-cdn.com/uploads/chorus_asset/file/22022572/bfarsace_201106_4269_012.0.jpg" alt="">
</div>
<div class="cost">
<p class="your_product">Ваш товар</p>
<p>$89.00</p>
</div>
</div>
</div>

How to add new div after over anoter div in CSS?

Hi i am using this code.....
But when i add new div .myContainer after it is comes up. If i use clear both it not working.
#container {
width: 100px;
height: 100px;
position: relative;
}
#navi,
#infoi {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
}
#infoi {
z-index: 10;
}
<div id="container">
<div id="navi">a</div>
<div id="infoi">
<img src="https://appharbor.com/assets/images/stackoverflow-logo.png" height="20" width="32" />b
</div>
</div>
<div class="myContainer">
ABCDEFGHICJLMNOP
</div>

How to bottom align and center an responsive image in a column

I have this layout (which is a header):
<div class="container-fluid">
<div class="row">
<div class="col-sm-2" style="background-color: aqua; height: 160px;">
<img src="logo.png" class="img-responsive logo">
</div>
<div class="col-sm-8" style="background-color: blueviolet; height: 160px;"></div>
<div class="col-sm-2" style="background-color: aqua; height: 160px;"></div>
</div>
</div>
what i would like to achieve is:
Align the logo to the bottom and the center
Let the image be responsible (set width to 80% of the column)
I did this:
.logo {
position: absolute;
width: 80%;
left: 10%;
right: 10%;
bottom: 0;
}
but it somehow dosen't work as you can see here:
https://jsfiddle.net/9kauhbhs/2/
Use left: 50% and a margin left that is negative half of the image width.
e.g.
.logo {
position: absolute;
width: 80%;
left: 50%;
bottom: 0;
margin-left: calc(-80% / 2);
}
Fiddle
You can try this..
https://jsfiddle.net/9kauhbhs/7/
.container{
position:absolute;
bottom:0px;
height:auto;
width:100%;
text-align:center;
}
.logo {
position: relative;
width: auto;
height:auto;
margin:0 auto;
max-height:100%;
vertical-align:bottom;
}
<div class="col-sm-2 text-center" style="background-color: aqua; height: 160px;">
<div class="container">
<img src="http://www.w3schools.com/html/pic_mountain.jpg" class="logo">
</div>
</div>

Positioning with Top: -px

I'm overlapping a div (div2 over div1) using top: -50px; to push it up. Now this leaves 50px below it before the next div (div3). How can I "clear" that and make the div (div3) fall in exactly below the div (div2) I positioned -50px?
<div id="div1" style="width: 1000px; height: 90px; background: red;"></div>
<div id="div2" style="position: relative; top:-50px; width: 1000px; height: 90px; background: blue;"></div>
<!--50 wasted pixels here-->
<div id="div3" style="width: 1000px; background: green; height:90px;"></div>
Add margin-top: -50px to your #div3:
<div id="div3" style="width: 1000px; background: green; height:90px; margin-top: -50px;"></div>
Fiddle: http://jsfiddle.net/N7z6e/

CSS3 opacity animation does not work with 'overflow:hidden'

I have A Scenrio where I need to do a Fade-In Animation on a DIV, which wasn't working as desired.
After much experiment, I found out that one of the div's has got "overflow:hidden" in the css class applied to it. If I comment the "overflow:hidden" part, the animation seems to work perfectly.
Though it fixed my problem, However, the question lingers in my mind, whether 'overflow:hidden' doesn not work with opacity animation.
For your perusal, here's the code.
My Browser Chrome 15.0.XXX.X
My OS Windows XP
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<style type="text/css">
#MainContainer {
opacity: 1;
position: absolute;
height: 500px;
top: 10px;
width: 600px;
left: 10px;
-webkit-animation-timing-function: linear;
-webkit-animation-fill-mode: forwards;
}
.focussedItem {
position: absolute;
left: 300px;
top: 200px;
width: 450px;
height: 230px;
margin: 0px 0px;
opacity: 1;
}
.innerDiv {
position: relative;
width: 450px;
height: 150px;
left: 10px;
top: 40px;
overflow: hidden; /* This is where the Problem is */
}
.optionItem {
position: absolute;
vertical-align: middle;
text-align: left;
font-size: 35px;
width: 450px;
height: 50px;
left: 25px;
}
#
-webkit-keyframes fadeIn {
0% {opacity: 0;}
100%{opacity:1;}
}
</style>
<script type="text/javascript">
document.onkeydown = KeyCheck;
function KeyCheck(e) {
console.log(e.keyCode);
document.getElementById("MainContainer").style.webkitAnimationDuration = "2000ms";
document.getElementById("MainContainer").style.webkitAnimationName = "fadeIn"
}
</script>
</head>
<body>
<div>press space to test</div>
<div id="MainContainer" class="MainContainer">
<div id="SubContainer" class="focussedItem"
style="height: 290px; top: 250px;">
<div id="OptionRing" class="innerDiv"
style="height: 190px; top: 50px;">
<div class="optionItem" style="top: -40px;">OPTION 1</div>
<div class="optionItem" style="top: 10px;">OPTION 2</div>
<div class="optionItem" style="top: 60px;">OPTION 3</div>
<div class="optionItem" style="top: 110px;">OPTION 4</div>
<div class="optionItem" style="top: 160px;">OPTION 5</div>
<div class="optionItem" style="top: 210px;">OPTION 6</div>
</div>
</div>
</div>
</body>
</html>
#
-webkit-keyframes fadeIn {
change to :
#-webkit-keyframes fadeIn {
# need in the same line
http://jsfiddle.net/wX8DW/
Overflow: hidden does not affect the result

Resources