/    Sign up×
Community /Pin to ProfileBookmark

define margin using pixels or percent?

Hi there! This is my question.

Let’s say I have two divs, div1 and div2. To define the top margin of div2 respect div1, I can do it in two ways using css.

top:20px;
margin-top:2%;

What’s the best way to do it?? I am sorry because I know this is a stupid question, but I want to know the main differences between those two ways.

Thanks a lot!

to post a comment
CSS

2 Comments(s)

Copy linkTweet thisAlerts:
@simplypixieNov 13.2012 — It totally depends on if you want the top margin to always be 20px or if you want it to be 20% of the screen or containing div size.

To throw something else into the mix, you could also use ems (based on your 20px, that would be 1.25ems).

So basically do you want your top margin to be fixed or fluid / responsive (either way, use margin-top, not just top as that is for use with position: relative or position: absolute).
Copy linkTweet thisAlerts:
@deepodeNov 13.2012 — Yup as simple as simplypixie said!

I prefer Pixels more....and em is better then %.
×

Success!

Help @hebrerillo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...