- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Selectors
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Border Block
- CSS - Border Inline
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursor
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS - Inline Block
- CSS - Dropdowns
- CSS - Visibility
- CSS - Overflow
- CSS - Clearfix
- CSS - Float
- CSS - Arrows
- CSS - Resize
- CSS - Quotes
- CSS - Order
- CSS - Position
- CSS - Hyphens
- CSS - Hover
- CSS - Display
- CSS - Focus
- CSS - Zoom
- CSS - Translate
- CSS - Height
- CSS - Hyphenate Character
- CSS - Width
- CSS - Opacity
- CSS - Z-Index
- CSS - Bottom
- CSS - Navbar
- CSS - Overlay
- CSS - Forms
- CSS - Align
- CSS - Icons
- CSS - Image Gallery
- CSS - Comments
- CSS - Loaders
- CSS - Attr Selectors
- CSS - Combinators
- CSS - Root
- CSS - Box Model
- CSS - Counters
- CSS - Clip
- CSS - Writing Mode
- CSS - Unicode-bidi
- CSS - min-content
- CSS - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - Pointer Events
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- CSS Advanced
- CSS - Grid
- CSS - Grid Layout
- CSS - Flexbox
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Paged Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS - Image Sprites
- CSS - Important
- CSS - Data Types
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS - Rounded Corner
- CSS - Border Images
- CSS - Multi Background
- CSS - Color
- CSS - Gradients
- CSS - Box Shadow
- CSS - Box Decoration Break
- CSS - Caret Color
- CSS - Text Shadow
- CSS - Text
- CSS - 2d transform
- CSS - 3d transform
- CSS - Transition
- CSS - Animation
- CSS - Multi columns
- CSS - Box Sizing
- CSS - Tooltips
- CSS - Buttons
- CSS - Pagination
- CSS - Variables
- CSS - Media Queries
- CSS - Functions
- CSS - Math Functions
- CSS - Masking
- CSS - Shapes
- CSS - Style Images
- CSS - Specificity
- CSS - Custom Properties
- CSS Responsive
- CSS RWD - Introduction
- CSS RWD - Viewport
- CSS RWD - Grid View
- CSS RWD - Media Queries
- CSS RWD - Images
- CSS RWD - Videos
- CSS RWD - Frameworks
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
CSS Function - translateZ()
The translateZ() function in CSS is used to translate, or move, an element along the Z-axis in 3D space, i.e., either closer and farther from the viewer. It's one of the transformation functions in CSS that allows you to modify the position and appearance of elements on a web page. The result is a <transform-function> datatype.
Possible values
The function translateZ() can take only one parameter.
tz: Its a <length> value that specifies how far inward or outward the element or elements move. Represents the z-component of the translating vector [0, 0, tz]. A positive value moves the element towards the viewer, and a negative value farther away.
Syntax
transform: translateZ(tz);
The perspective() function defines the virtual distance between the plane of your computer screen and the HTML element you're applying translateZ to.
There are some unexpected behaviors shown by the use of perspective() and translateZ() functions.
When the value provided to translateZ() is more than or equal to the value of perspective(), the HTML element to which it is applied, disappears.
Any value, other than zero value (0px, 0, 0em) is allowed for perspective(), as it results in translateZ() effect getting ignored.
CSS translateZ() - Translation on hover
Following is an example of translateZ() function used with pseudo-class :hover, on a button. The size and position of the button changes on hover:
<html> <head> <style> div { height: 200px; width: 300px; border: 5px solid black; background-color: beige; margin: 50px; display: inline-block; } button { transform: perspective(150px) translateZ(20px); transition: transform 100ms linear; width: 80px; height: 30px; background-color: blue; color: white; margin: 30px; } button:hover { transform: perspective(150px) translateZ(80px); } </style> </head> <body> <div> <h2>translateZ() example</h2> <button>click</button> </div> </body> </html>
CSS translateZ() - Translation with perspective()
Following is an example of translateZ() function along with perspective(). The div element where the function is applied, the position is based on the value passed to translateZ() function.
<html> <head> <style> div { height: 110px; width: 110px; border: 2px solid black; background-color: aquamarine; margin: 15px; } .translate-z-length { transform: perspective(200px) translateZ(50px); background-color: tomato; } </style> </head> <body> <div>No translateZ() applied</div> <div class="translate-z-length">translateZ() applied</div> </body> </html>
Change the value of perspective() and translateZ() to see the effect.
The perspective() function is responsible for positioning the viewer relative to the plane, i.e to the surface of the screen (z=0). So, in the example above, value 0f 200px means, the user is 200 pixels in front of the image.
The value of translateZ(), 50px, means the element is moved 50px outward from the screen, toward the user. The element looks larger when viewed on a 2D display.
The order in which functions are written is important, as the composition of transforms is not commutative. The perspective() function should be placed before the translateZ() function.