Margin-top failure in Div

Source: Internet
Author: User

Resolution of element Margin-top invalidation in div tag

Element superior tag is Div, has set the width and height and other properties, but, in the element using margin to adjust the time, can not be effective, the following is a good solution, interested friends can refer to the next title.

The problem is very wonderful. The element ancestor tag is a Div, which has set the properties of width and height, but cannot take effect when adjusting the element using margin. Student experience Tip, set the parent element property of the element to

Overflow:hidden; Solutions to Margin-top transfer problems in nested div

In both browsers, there are two nested relationship div, if the outer div's parent element padding value is 0, then the value of the inner div's margin-top or margin-bottom will be "transferred" to the outer div.

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>

<body>
<div style= "width:300px; height:100px "> Upper layer </div>

<div style= "width:300px; Height:300px;overflow:hidden "> <!--parent Layer--
<div style= "margin:50px; width:200px; height:200px "" > sub-layer </div>
</div>

</body>

Reason: The box did not get haslayout caused margin-top invalid

Workaround:

1, in the parent div plus: overflow:hidden;

2, the outer margin of the margin-top is changed into the padding-top inner margin;

3. The parent element produces a margin overlapping edge with a border that is not 0 padding or width is not 0 and style is not a none.

Parent-level div plus: padding-top:1px;

4, let the parent element generate a block formating context, the following properties can be implemented
* Float:left/right
* Position:absolute
* Display:inline-block/table-cell (or other table type)
* Overflow:hidden/auto

Parent-level div plus: position:absolute;

Margin-top failure in Div

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.