Can't remove the margin | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Can't remove the margin

My div is positioned a little right from the left surface, why's this happening and how to avoid this? https://code.sololearn.com/WmIXDsEI5J0u/?ref=app

13th Feb 2022, 3:30 AM
Rishi
Rishi - avatar
19 Answers
+ 9
Maybe it's because of body margin, set margin: 0; in CSS for body selector.
13th Feb 2022, 3:38 AM
Ipang
+ 8
Rishi What I usually will do is add the following if I am trying to line everything from the top and left side.. I added this to your #box and it fit in the corner exactly. Not sure if that was what you were seeking. margin-left:-7px; margin-top:-7px; I also just did this and it moved the box all the way to the left as well body{ margin-left:.37px; padding:0px; }
13th Feb 2022, 4:04 AM
BroFar
BroFar - avatar
+ 7
Thank you Ipang and BroFar 😊
13th Feb 2022, 5:04 AM
Rishi
Rishi - avatar
+ 6
#box{ width:400px; height:50px; background-color:black; z-index: 1; } body{ padding:0px; margin:0px; }
13th Feb 2022, 7:18 AM
Madara Uchiha
Madara Uchiha - avatar
+ 5
Ipang yeah it works! I didn't think of it cuz i thought the margin is outside the area, so it must be the padding that's causing this. Can you explain how margin is affecting this?
13th Feb 2022, 3:44 AM
Rishi
Rishi - avatar
+ 4
You're right, margin is the outer distance between an element's border and its parent border. Imagine we have two envelopes, put big envelop on a table, then stack the smaller one on top of the bigger one. The part of bigger envelope that's uncovered is margin in context of the smaller envelop. (Edit) In relation to the code, body is the big envelop, and #box is the smaller envelop.
13th Feb 2022, 3:53 AM
Ipang
+ 3
Yeah Krishna Singh and Israel Promise it works! Tnx :)
13th Feb 2022, 8:50 AM
Rishi
Rishi - avatar
+ 3
Set margin: 0px; in body section inside css file
13th Feb 2022, 6:06 PM
Aakash Kumar
Aakash Kumar - avatar
+ 2
I just added "margin:0px" to your code and I think it removed it... So add margin:0 after the padding
13th Feb 2022, 8:18 AM
Israel Promise
+ 2
you can reset the margin at the top of your css: *{ margin: 0; padding: 0; }
13th Feb 2022, 7:42 PM
Jose
Jose - avatar
+ 2
Aakash Kumar Jose sobhan jaefari works! Thank you😊
14th Feb 2022, 2:25 AM
Rishi
Rishi - avatar
+ 2
Try changing the background margin and padding property vaalues
14th Feb 2022, 8:55 AM
Usman Abdulrazak
Usman Abdulrazak - avatar
+ 2
Just change the #box margin to auto and apply box-sizing : border-box; in body That's easy...
14th Feb 2022, 12:52 PM
Anshul Goswami
Anshul Goswami - avatar
+ 2
Richard Kamski it's okay, thank you for trying to help😊 I already got a lot of answers so I probably know this already.
15th Feb 2022, 2:19 AM
Rishi
Rishi - avatar
+ 1
Because your div have width 400 px Try this: *{margin:0; Padding:0;} #box{width:100%;}
13th Feb 2022, 9:20 PM
sobhan jaefari
sobhan jaefari - avatar
+ 1
Hey, I saw your post, this is the way to do it right https://code.sololearn.com/WsRGcOtT1TSO/?ref=app ask any questions!
14th Feb 2022, 4:54 PM
Richard Kamski
+ 1
Richard Kamski the code you shared is not loading. I think maybe the there's an error in the link
15th Feb 2022, 2:16 AM
Rishi
Rishi - avatar
+ 1
I accidentally deleted the code from my code section I didn't know that would also delete the code from my comment😂
15th Feb 2022, 2:18 AM
Richard Kamski
+ 1
I can redo it if you still want some help
15th Feb 2022, 2:18 AM
Richard Kamski