FAQ: CSS Display and Positioning - Position: Relative

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!

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

9 Likes

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

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)

lets do an example:

JS Bin - Collaborative JavaScript Debugging

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):

JS Bin - Collaborative JavaScript Debugging

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

13 Likes

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?

15 Likes

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)

2 Likes

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

https://imgur.com/a/ep9MfzL

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.

1 Like

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.

13 Likes

Thank you very much sir
It cleared my doubts

1 Like

I added the position - top 40px in the class “question”, There is only changing top of the first question box but why another questions box`s top are not changed? They are also included to class “questions”.

1 Like

they are, i explained it here:

i even included a simplified jsbin example to play with.

1 Like

Hi,
Thanks for the responses but still not quite getting this. I played about with your jsbin. This is the start position, before we add relative positioning:

(can only add 1 image per post right now so will reply again with next step…)

Part 2:
Then added relative positioning. In this example, I see what I would expect - each relatively positioned element has the additional space above it, relative to the last (static) element:

Really struggling to work out the difference between the lab case and this case.
All clarifications gratefully received!
Thanks

debugging from screenshots is very difficult, couldn’t you post links to the bins?

Hi stetim94,
sorry, first time I’ve ever seen jsbin so not sure what you mean - could you clarify what links you mean.
Thanks

Believe this is what you’re after but let me know if not - Thx

well, in the lab (the codecademy exercise) all questions are positioned relative. which means you have a few relative elements after each other, while in your jsbin example you have relative and static element alternately.

Ah, this is because the answer class element is a sub-element of question class element so we position relative to the previous question as opposed to answer, as I have tried to simulate? (apologies if terminology not correct - early days of learning…)

yes, seems you understand it correctly