Prashant Pandey
Prashant Pandey

Reputation: 4642

Moving a div element left by a few pixels

I am a newbie in front-end development, so please bear with me if the question is too basic. What I am trying to achieve is very similar to the following problem:

How do I move the search bar on SO's website to the left without affecting the other elements? I can't use the margin-left property since that would move the other elements as well. Is there any way to achieve this? Thanks!

Upvotes: 4

Views: 14784

Answers (3)

Krayz
Krayz

Reputation: 79

Depends on your situation: I would say try adding “position: relative;” & “right: 10px” for e.g.

I hope this helps!

Upvotes: 1

HelloWorld
HelloWorld

Reputation: 327

You can do this by using the transform property.
Use transform: translateX(n px/vh/vw/em)

This will not affect other elements.

There is another way to do this:

  • Give the required element a position of relative
  • And use top and left properties to modify the position.

Note: In both cases only the rendering of graphics on the screen is shifted. The element will be present in its original position.

Upvotes: 1

Mike Trinh
Mike Trinh

Reputation: 1303

You can use margin-left negative + margin-right positive;

margin-left: -10px;
margin-right: 10px;

Upvotes: 10

Related Questions