Jump to content

Div Element Moved


homer.j.simpson

Recommended Posts

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

Try padding-top: 10px or line-height: 10px. Change around the values to whatever works for you.

Link to comment
Share on other sites

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

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

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

			   +-----------------------------------+			   |				 contents contents |			   |				 contents contents |<-- margin --> | <-- padding --> +---------------+ |			   |				 | child child   | |			   |				 +---------------+ |			   +-----------------------------------+

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...