FAQ: CSS Display and Positioning - Position: Relative


#1

This community-built FAQ covers the “Position: Relative” exercise from the lesson “CSS Display and Positioning”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn CSS

FAQs on the exercise Position: Relative

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

How is position:relative different in effect from using margin spacing to “move” the element?


#3

By offsetting from the default static position of the div using the top, left etc properties, does that not make the position command superfluous?


#4

top, left etc don’t work on static elements, nothing happens.

Besides relative and static, there also other possible values for position (fixed for example)


#5

lets do an example:

https://jsbin.com/dukidawico/edit?html,css,output

if you uncomment (ctrl + /) the css, you will see that both divs move down

this is because margin is applied first, then the elements are “rendered”, causing both to move down.

if we use move (again, uncomment the css line):

https://jsbin.com/dokarohiye/edit?html,css,output

the elements are “rendered” first, and then the #first element is pushed down, causing overlap, because the second element doesn’t move.


#6

Hello All,
Just want to know in above exercise we have 5 question classes(.question) and we gave them

postion:relative;

and offset

top:40px;

so my question is why this offset top 40px applied to only first question class why it have no effect on other question classes?


#7

Are you sure? Have you tested this hypothesis? Go ahead, and only apply the top: 40px to the first question. (use an id selector or nth, whichever you prefer)


#8

Yes Sir I tested this,
My style.css

.question {
text-align: center;
position: relative;
top:400px;
}

I change 40px to 400px just for a proper change

Here is index.html code

as you see there are 2 div’s with class name question

my output is

image 1

Imgur

image 2

Imgur

as you see in Image 1 there is proper 400px gap between top and question 1 but in Image 2 why there is no 400px gap .As i know if i am giving 400px gap from top to class question than every div who have class question should have gap of 400px from top.


#9

now you still apply the top: 400px still to all questions, not only the first one

anyway, as explained earlier (please checkout the jsbin example as well):

So all the questions move down, but because they all move down an equal amount (40px, 400px or whatever value you give them), the relative distance between the questions stays the same.


#10

Thank you very much sir
It cleared my doubts