Margin-top caused by Div nesting does not work (the Sub-Div sets margin-top, leading to gaps between the parent Div and other elements)

Source: Internet
Author: User

Problem:Solution to the margin-top transfer problem in the nested Div (the Sub-Div sets margin-top, leading to gaps between the parent Div and other elements)

In this browser, there are two nested relationship diV. If the padding value of the parent element of the outer div is 0, the value of the margin-top or margin-bottom of the inner div is "transferred" to the outer Div.

 1   <!  Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"  >  2   <  Html  Xmlns  = "Http://www.w3.org/1999/xhtml" >  3   <  Head  >  4   <  Meta  HTTP-equiv  = "Content-Type"  Content  = "Text/html; charsets = UTF-8"   />  5   <  Title  > Untitled document</  Title  >  6   </  Head  >  7   8   <  Body  >  9   <  Div  Style  = "Background-color: # ff0000; width: 300px; Height: 100px" > Upper Layer </  Div  >  10   11   <  Div  Style  = "Background-color: #009900; width: 300px; Height: 300px; overflow: hidden"  >   <! --  Parent Layer  -->  12        < Div  Style  = "Margin: 50px; Background-color: #000000; width: 200px; Height: 200px"  "  > Child Layer </  Div  >  13   </  Div  >  14   15   </  Body >  16   </  Html  >

Cause:Box not obtainedHaslayoutInvalid margin-top

 

Solution:

1. Add overflow: hidden to the parent Div (I recommend this)

2. Change the margin of margin-top to the padding of padding-top;

3. The parent element has a padding with overlapping margins or a border with a width of not 0 and a style of not none.

Parent layer Div plus: padding-top: 1px;

4. Let the parent element generate a block formating context. The following attributes can be implemented:
* Float: left/right
* Position: absolute
* Display: inline-block/table-cell (or other table types)
* Overflow: hidden/auto

  Parent layer Div plus: Position: absolute;

Original article address:Http://blog.sina.com.cn/s/blog_6bec36f9010110w9.html

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.