InfoHeap
Tech tutorials, tips, tools and more
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

CSS transform values

  • rotate
  • scale
  • skew
  • translate
 
  • Home
  • > Tutorials
  • > CSS
  • > CSS properties
  • > CSS transform

CSS transform – translateX and translateY

on Mar 8, 2016

Using CSS3 property transform, elements can be translated, rotated, scaled and skewed in two or three dimensional space. This tutorial will covered translating an element in X and Y direction using CSS transform (2D) translateX and translateY.

css-transform-translate

CSS Syntax:

transform:translate(10px, 20px);
transform:translateX(10px);
transform:translateY(20px);

CSS transform – translateX and translateY example

Here box1 will be translated by 10px in X (right) and Y (downward) direction. Note that the translation does not affect the placement on other element in html. Just that the translated element gets shifted.

<style type="text/css">
.outer {
  width: 100px; height:100px; background-color:lightgray;
}
.box1 {
  width: 50px; height:50px; background-color :green;
  transform: translate(10px, 10px); 
}
.box2 {
  width: 50px; height:50px; background-color :lightblue;
}
</style>

<div class="outer">
  <div class="box1"></div>
  <div class="box2"></div>
</div>
refresh done
try it online

CSS transform – translateY using percentage

Here translation is done using percentage of element size. Note that this can very useful to shift an element by percentage of its size when size is unknown.

<style type="text/css">
.outer {
  width: 100px; height:100px; background-color:lightgray;
}
.box1 {
  width: 50px; height:50px; background-color :green;
 transform: translate(50%, 50%); 
}
</style>

<div class="outer">
  <div class="box1"></div>
</div>
refresh done
try it online

Specification and Browser compatibility

SpecificationStatusCategories
CSS3 2D TransformsW3C Working DraftCSS3
Desktop
ChromeFirefoxIEEdgeSafariOpera
Yes 36+ Yes 16+ Yes 10+ Yes 12+ Yes 9+ Yes 12.1+
Mobile
Android ChromeAndroid FirefoxiOS SafariIE MobileOpera Mobile
Yes 47+ Yes 44+ Yes 9.0-9.2+ Yes 10+ Yes 11+
source: caniuse.com

Suggested posts:

  1. Scale an element using css transform
  2. Rotate an element using css transform
  3. CSS transform – skew (skewX and skewY)
  4. CSS text-transform – capitalize, uppercase and lowercase text
  5. CSS resize – make block element resizable by user
  6. CSS – align div in center vertically
  7. CSS animation-direction
  8. CSS color values
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged CSS, CSS properties, CSS transform, Tutorials

Follow InfoHeap

facebook
twitter
googleplus
  • Browse site
  • Article Topics
  • Article archives
  • Recent Articles
  • Contact Us
  • Omoney
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries

Copyright © 2023 InfoHeap.

Powered by WordPress