Can you please provide me with the CSS translation function reference?

I have been pondering about the reference position of the CSS translation function.

For instance, if I were to use:

p {
    transform:translate(100px,100px);
}
<div id="container">
  <p> Try! </p>
</div>

In Firefox developer tools, you can see a green and blue "box", where the green one represents the reference position and the blue indicates the actual object's position.

However, the reference point is not at the top-left corner of the window or the object's parent. Even though I am certain that there are no properties such as padding or margin involved.

Answer №1

Remember to use the following syntax:

p {
   transform: translate(100px, 100px);
}

To see a working example, click here:

https://jsfiddle.net/hhhotv8x/

The starting point is at the top left corner (0, 0). The translation is based on this position and will move elements accordingly within their container.

Answer №2

The `translate` transform-function shifts the position of the element on the page, with the first value determining the horizontal offset and the second value determining the vertical offset. Let's look at a group of elements where the 3rd one has been translated by moving it 10px to the left and 5px down:

body{
  color:#fff;
  font-family:sans-serif;
  font-size:0;
  text-align:center;
}
p{
  background:#000;
  display:inline-block;
  font-size:14px;
  margin:0;
  padding:5px;
}
p:nth-of-type(3){
  background:#090;
  transform:translate(10px,5px);
}
<p>Some text</p>
<p>A little more text</p>
<p>This text is transformed</p>
<p>This paragraph isn't</p>
<p>Nor is this last one</p>

Answer №3

Summary: The transformed element is positioned at the top left corner of the parent element, including margin.


This positioning is determined by a transformation matrix:

The transformation matrix defines the mapping from one coordinate system to another based on the values of the transform and transform-origin properties.

In accordance with w3.org guidelines:

The transformation matrix calculation involves:

  • Beginning with the identity matrix
  • Translating by computed X, Y, and Z values of transform-origin
  • Multiplying by each transform function in transform property sequentially
  • Translating by negated computed X, Y, and Z values of transform-origin

In this scenario, using translate means that the transform-origin does not impact the transformed element's position.

This implies that the element is moved from the coordinate system of the <div id="container">, which is the top left corner (0,0) of the bounding box.

<length>

A length value provides a fixed offset from the top left corner of the bounding box for horizontal and vertical positions.

https://i.sstatic.net/8bip6.png

If margin is added to the #container, it will change the position of the <p>.

#container {
    margin: 20px;
}
p {
    transform:translate(100px,100px);
}
<div id="container">
  <p> Try! </p>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

problem with saving session data

I am attempting to access data from another page using session storage. On my initial page, named home.html function go_to_faq(qnum){ window.open('FAQ.html', '_blank'); sessionStorage.setItem('key2', qnum); } <a s ...

Expanding the width of the textfield is not possible in CSS styling

My current search bar design features an input area that does not match my expectations. The input length is restricted and starts from the center rather than the black border I had envisioned. If you'd like to see the code and design in action, chec ...

Top methods for integrating custom divs into your WordPress site using PHP

As someone who is new to wordpress, I have a solid understanding of php, html, css and javascript. I am interested in integrating a custom font resizer into my wordpress site similar to the example shown below: https://i.stack.imgur.com/FwoIJ.jpg What w ...

Excess space in the top margin of the <p> tag causing layout

There seems to be some spacing between the green and blue colored divs in the snippet below, caused by the top margin of the <p> tag extending beyond the <div>. The blue div should ideally start right after the green div. Any suggestions on how ...

Tips for personalizing the CSS styling of input fields based on the id value

I need help finding a way to customize the CSS of form fields (such as input) based on the id value. Let's take a look at an example: HTML: <mat-form-field class="form-field-container"> <input tabindex="0" id="{ ...

What are the steps for editing and preserving a logo with an 8-bit indexed color palette?

After purchasing a RE/MAX franchise, I now need to make edits to the official logo found on remax.com using Adobe Photoshop. However, I have encountered a problem when trying to edit their logo in Photoshop. When I open their PNG logo, the layer appears d ...

Pasting a canvas over a div using CSS

My website features a <div> containing creatively styled rings with a design reminiscent of the android unlock pattern. The div showcases 9 dots that can be connected in various configurations. I'm looking to overlay a canvas on this setup to tr ...

The distance from the edge of my tilted svg icon to the svg artboard

I'm currently working on developing a basic web icon system using SVG sprites. However, I have encountered a spacing issue in my code around one specific icon (between the Smash Magazine icon and the red border). I am trying to figure out how to remov ...

Tips for incorporating an image into the background of a mobile device

My attempt to set an image as a background was successful on desktop, but unfortunately, it doesn't work on my phone. I have specified 'cover' as the background-size, expecting it to cover all the space on mobile as well. Here is the code s ...

Samsung devices exclusively showcase background in responsive design

I am encountering a major problem with my website design, specifically with its compatibility on mobile devices. To ensure its responsiveness, I have tested the design on my iPhone, iPad, and Windows Phone. Unfortunately, since I lack an Android device, I ...

Interactive menu with visual previews

As I work on creating a menu that displays images upon hovering, I've managed to achieve the show/hide functionality. However, there are a few additional features that I'm struggling to implement: When hovering over a submenu item, the main men ...

What's the best way to add a border of 1 pixel solid #ddd to a select box with the help of jQuery?

I'm in need of some assistance, Many websites utilize the jQuery framework to add stylish effects to their selectboxes. Unfortunately, I am limited to using ie7 at my workplace and I want to include a 1px solid #ddd border around my selectbox (which ...

CSS: customize the color of a highlighted row within a table

I am struggling to implement a new feature in my table. Specifically, I want the row to change color (#FFCF8B) when a user selects it. I attempted using #newspaper-b tbody tr.selected td, but it was not successful. #newspaper-b { border-colla ...

Adjust website layout for screens less than 320px in width

Struggling to complete my project and having trouble figuring out how to scale my website for screens smaller than 320px, even though they are rare. I admire the solution used by Apple where the website freezes and scales down while maintaining its origin ...

How can I achieve both horizontal and vertical centering simultaneously?

UPDATE: Seeking advice on aligning a rangeslider and its label vertically within the same row. The goal is to have them centered with respect to each other while ensuring the rangeslider takes up all available space. How can I vertically center a floatin ...

What steps should I take to establish an efficient folder organization system for a website utilizing HTML, CSS, and Javascript with Twitter Bootstrap?

After cloning the latest release of Twitter Bootstrap using Git with the command 'git clone git://github.com/twbs/bootstrap.git', I am in the process of creating an HTML/CSS site utilizing Bootstrap. Upon reviewing this answer on Stack Overflow ( ...

How can I retrieve the offset top of a td element in relation to its parent tr element?

Here is some sample dummy HTML code: <table> <body> <tr> <td id="cell" style="height: 1000px; width: 200px;"></td> </tr> </body> </table> I am looking to attach a click event ...

Tips for eliminating gaps between navigation items (HTML / CSS)

I'm struggling to eliminate the spacing between the li items in my navigation bar. Even after setting margin: 0px for both the item and anchor (link), the gap still persists. How can I get rid of these spaces? /* navigation styles */ nav { backgroun ...

Improper alignment on mobile devices

Looking to enhance the website for an organization? Check out this link: www.notevayaslionel.com The website looks great on desktop, but on mobile devices, there seems to be a problem where the background image appears larger than the screen, causing sid ...

fa icons moving the elements aside

I'm attempting to design a navigation menu with tabs, but I'm facing an issue where the tabs containing "fa icons" are consuming too much space, causing other elements to be shifted to the right or below (if there were more lines). How can I pre ...