homer.j.simpson Posted January 15, 2010 Share Posted January 15, 2010 Why is div element moved when I change margin-top? I have this div element that has some texts in it. I wanted to change the top margin so that those texts are shown at the desire position. The problem is that the whole div element move when I set it. Is there any way to adjust the margin without moving the whole element? Link to comment Share on other sites More sharing options...
Synook Posted January 15, 2010 Share Posted January 15, 2010 Maybe you want to change the padding? Link to comment Share on other sites More sharing options...
Err Posted January 15, 2010 Share Posted January 15, 2010 Try padding-top: 10px or line-height: 10px. Change around the values to whatever works for you. Link to comment Share on other sites More sharing options...
homer.j.simpson Posted January 15, 2010 Author Share Posted January 15, 2010 Padding does work. I'm just curious why margin setting doesn't act like it supposed to be. I mean it suppose to adjust the distance from the one edge of parent element to child one; not the distance between same level elements. Now, it's like a padding and move the parent element to set distance. Link to comment Share on other sites More sharing options...
ShadowMage Posted January 15, 2010 Share Posted January 15, 2010 Padding does work. I'm just curious why margin setting doesn't act like it supposed to be. I mean it suppose to adjust the distance from the one edge of parent element to child one; not the distance between same level elements. Now, it's like a padding and move the parent element to set distance.Actually, it is supposed to move the same level elements. Padding is what is used to move child elements and text. Here's a link that explains the box model that CSS uses to lay out a page:http://w3schools.com/css/css_boxmodel.asp Link to comment Share on other sites More sharing options...
Ingolme Posted January 15, 2010 Share Posted January 15, 2010 Margin is supposed to determine the distance of a box from the edges of its container. Margin always affects the position of the box. Link to comment Share on other sites More sharing options...
homer.j.simpson Posted January 15, 2010 Author Share Posted January 15, 2010 Margin is supposed to determine the distance of a box from the edges of its container. Margin always affects the position of the box.Ya that's what I meant. Instead of moving just the child elements, it moves the whole container. Anyway, I found the root cause; it was height attributes that I set on both the parent and the child. When I eliminate the height attribute of the chlid, the margin works as it should. Link to comment Share on other sites More sharing options...
ShadowMage Posted January 15, 2010 Share Posted January 15, 2010 Margin is supposed to determine the distance of a box from the edges of its container. Margin always affects the position of the box.But it also defines the distance between the box and other elements around it not just it's parent container. Besides I thought padding is supposed to define the distance between the edges of a parent and its children. Link to comment Share on other sites More sharing options...
homer.j.simpson Posted January 15, 2010 Author Share Posted January 15, 2010 Thant's an interesting read. I always thought margin attribute is like that of word documents - defines distance from the container's edge to the contents. Anyway, like I said in earlier post, don't know why, but margin does the trick when I remove the high attribute of the child. Link to comment Share on other sites More sharing options...
Synook Posted January 16, 2010 Share Posted January 16, 2010 +-----------------------------------+ | contents contents | | contents contents |<-- margin --> | <-- padding --> +---------------+ | | | child child | | | +---------------+ | +-----------------------------------+ Link to comment Share on other sites More sharing options...
homer.j.simpson Posted January 16, 2010 Author Share Posted January 16, 2010 Thanks for the replies guys.cheers Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.