HTML and CSS positioning in the personal share

Locate

  • static - the default value (almost do not understand you can)

absolute - absolute positioning, does not reserve space for elements from the document flow:

  1. If the parent element the current element is the <body> element, then -> is positioned relative to the current page
  2. If the parent of the current element is not the <body> element, then the parent element does not turn on Location -> it is the relative orientation of the page for
  3. If the parent is not the current element <body> element, then opening positioned parent element -> is the parent element is positioned with respect to
    <style>
        body {
            margin: 0;/* 去掉浏览器默认的外边距8px */
        }
        div {
            width: 200px;
            height: 200px;
            background-color: cyan;
            /*
                开启绝对定位
                1.当前元素脱离文档流
                2.如果不设置位置的偏移量的话,位置不会有任何变化的
             */
            position: absolute;
            /*
                设置定位的偏移量:
                 * 水平方向正值 - 向右移动
                 * 水平方向负值 - 向左移动
                 * 垂直方向正值 - 向下移动
                 * 垂直方向负值 - 向上移动
            */
            top: 100px;
            left: 100px;
        }

    </style>
</head>
<body>
<div></div>
</body>

fixed - fixed positioning, does not reserve space for the element:

  1. It is positioned relative to the window element to a position, from document flow
    <style>
        body {
            margin: 0;
            height: 1000px;
        }
        #d1 {
            width: 200px;
            height: 200px;
            background-color: #83c44e;
            /* 开启固定定位 - 相对于浏览器窗口的定位 */
            position: fixed;

            left: 100px;
            top: 100px;
        }
        #d2 {
            width: 200px;
            height: 200px;
            background-color: cyan;

            position: absolute;

            left: 500px;
            top: 100px;
        }
    </style>
</head>
<body>
<div id="d1"></div>
<div id="d2"></div>
</body>

relative - relative positioning, without departing from the document flow

  1. Relative positioning is positioned relative to the original position of the element itself
    <style>
        body{
            margin: 0;
        }
        #d1{
            width: 200px;
            height: 200px;
            background-color: cyan;
            /* 外边距 */
            margin: 100px;
        }
        #d2{
            width: 100px;
            height: 100px;
            background-color: #ffac13;
            /* 外边距 */
            margin-left: 100px;
            /* 相对定位是相对于自身元素原来的位置的定位 */
            position: relative;
            left: 100px;
        }
    </style>
</head>
<body>
<div id="d1">
    <div id="d2"></div>
</div>
</body>

FIG positioned parse:
image description


Stacking

  • z-index attribute values ​​above a large, small below, the z-index is the attribute value is smaller large coverage
 <style>
        body{
            margin: 0;
        }
        div{
            width: 200px;
            height: 200px;
        }
        #d1{

            background-color: cyan;
            /* 开启绝对定位 */
            position: absolute;
            /* 开启定位后设置偏移量 */
            left: 150px;
            top: 150px;
            z-index: 2323;
        }
        #d2{
            background-color: #ffac13;

            /* 开启相对定位 */
            position: relative;
            /* 开启定位后设置偏移量 */
            left: 50px;
            top:50px;
            z-index:1;
        }
        /*
            必须是当前元素开启定位的情况下,z-index属性才会有效
                当多个属性设置z-index属性时 - 值大的会覆盖值小的
         */
    </style>
</head>
<body>
<div id="d1"></div>
<div id="d2"></div>
</body>

Stack analysis chart:
image description


Inherited from the stack

inherit

  • Inheritance is the parent element attributes, child elements use can not be used directly to set
  • Note: CSS property that is not used can inherit property
  • CSS style attributes:
  1. Inheritable property - the style of the specified element, while the role of its descendant elements
  2. Non-inherited property - only role in the specified element

Note: There is a succession inherit value - that is meant to inherit ancestral property of the element, on behalf of property value inheritance parent element used when the property of the child element is set inherit the value.

    <style>
        /*
            CSS的样式属性:
             1. 可继承的属性 - 指定元素的样式,同时作用其后代元素
             2. 不可继承的属性 - 只能作用在指定的元素
         */
        body{
            /*
                页面中的默认的字体大小为 16px
                页面中的默认的颜色为黑色
             */
            font-size: 148px;
            color: cyan;
        }
        p{
            /*
                inherit值 - 是继承于祖先元素属性的意思
                当前样式属性的值是继承于祖先元素
             */
            font-size: inherit;
        }
    </style>
</head>
<body>
<p>一花一世界,一叶一孤城</p>
</body>

Inherited from the stacked analysis chartimage description

Laminated

  • Is the priority selector, when set to some selectors! Important priority when the maximum level selector
  • Note:! Set to some selectors important when disrupt the original selector priority rule could affect the entire page layout, so can not to do, know there is this thing on the line

Guess you like

Origin www.cnblogs.com/jlfw/p/11851799.html