CSS perspective Property
CSS perspective Property
CSS perspective
property determines the distance between the z=0 plane and the user in order to give a 3D-positioned element some perspective.
Example
<!DOCTYPE html>
<html lang="en-US">
<head>
<style>
div{
width: 100px;
height: 100px;
border: 1px solid #000;
font-size: 17px;
}
#outerdiv{
background: #f69d3c;
margin: 20px 70px;
perspective: 100px;
}
#innerdiv{
background: #0066ff;
transform: rotate3d(1, 1, 1, 45deg);
-webkit-transform: rotate3d(1, 1, 1, 45deg);
}
</style>
</head>
<body>
<h1>CSS perspective Property</h1>
<div id="outerDiv">outerDiv
<div id="innerDiv">innerDiv</div>
</div>
</body>
</html>
Syntax
Using CSS
element{
perspective: 300px;
}
Using Javascript
object.style.perspective="300px";
Animatable
Yes, perspective property is animatable. CSS Animatable Properties Reference.
Default Value
Default value for CSS perspective property is none.
Property Value
The following table provides a list of values for CSS perspective
property.
Value | Explanation |
---|
none | Specifies that the perspective is not set. |
Length | Allows you to define the size of the perspective in any CSS length unit. |
Using JavaScript
In the following example, we will demonstrate how to change the CSS perspective
property of an element using JavaScript.
Example
<!DOCTYPE html>
<html lang="en-US">
<head>
<style>
div{
width: 100px;
height: 100px;
border: 1px solid #000;
font-size: 17px;
}
#outerdiv{
background: #f69d3c;
margin: 50px 70px;
perspective: 100px;
-webkit-perspective: 100px;
}
#innerdiv{
background: #0066ff;
transform: rotate3d(1, 1, 1, 45deg);
-webkit-transform: rotate3d(1, 1, 1, 45deg);
}
</style>
</head>
<body>
<h1>CSS perspective Property</h1>
<div id="outerDiv">outerDiv
<div id="innerDiv">innerDiv</div>
</div>
<button onclick="myFunction()">Click Me</button>
<script>
var x = document.getElementById("outerDiv");
function myFunction(){
x.style.perspective = "400px";
x.style.WebkitPerspective = "400px";
}
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 98.7% of CSS Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in CSS.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.
Share this Page
Meet the Author