¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita+ 1
How to change an element's position using x and y axis in css?
Hey I want to change an element's position using x and y axis. How do I change it in css? Please explain me.
12 Respuestas
+ 3
transform: translate()
https://developer.mozilla.org/en-US/docs/Web/CSS/translate
+ 4
if you want something similar to scale(x ,y) or scaleX(x) or scaleY(y), you can try:
transform: translate(5px, 5px);
or translateX, translateY for axis specific positioning.
+ 4
Arjun Dawande , yes Bob_Li and Lisa you're right, translate is used for this:
transform: translateX(50px);
+ 3
position: relative;
left: 50px;
top: 75px;
There are also many other ways, this is the most direct how you can specify the position, relative to the container (parent) element.
https://sololearn.com/compiler-playground/WEAkRVCqgU7T/?ref=app
+ 3
This IS the way. There is no universal center of reference, the point of x=0 and y=0 is always the top left corner of the parent element.
If you want to draw in a fixed coordinate system, then try canvas. But you will need to learn some javascript to use it.
Keep in mind, that a website can be opened in many types of devices - pc, phone, tablet with different resolutions, and even the user can resize the browser window. So responsive web design rarely uses pixel coordinates these days.
+ 2
Thank you Bob_Li
+ 2
Thank Kalab Girma for your answer but I already got the answer which I wanted from Lisa and Bob_Li . Still Thank you for your answer 😊
+ 1
Hey Tibor Santa I did it but isn't there any way to directly change the values of x and y axis?
Like there is in the scale for example scaleX().
+ 1
Thank you Lisa I wanted this only.
+ 1
Arjun Dawande scaleX() does not set the position of the object, but changes the size of its width, for example, transform: scaleX(2); will double the width of the object.
+ 1
I know it Solo . I was just giving an example that could we change an element's x and y axis like we can change its scale using scaleX() and scaleY()
+ 1
In CSS, you typically use the "top" and "left" properties to change an element's position using the y-axis and x-axis respectively. Let's go through a brief explanation of how you can change an element's position using the x and y-axis in CSS.
Changing an Element's Position Using the X and Y Axis:
Using the top Property (Y-Axis):
- The top property is used to specify the top position of a positioned element.
- It positions the element relative to the top edge of the containing element or a positioned ancestor.
Using the left Property (X-Axis):
- The left property is used to specify the left position of a positioned element.
- It positions the element relative to the left edge of the containing element or a positioned ancestor.