栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

为什么具有位置的元素:与未定位的同级一起固定移动?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

为什么具有位置的元素:与未定位的同级一起固定移动?

使用

position: fixed
,您的
header
元素将从文档流中删除。

第一个流入元素是

main
,它
margin-top: 90px
在您的代码中。

此元素的父元素是

body
,通常具有默认值
margin:8px

由于CSS 保证金崩溃,该

body
元素的
margin-top: 8px
被折叠与
main
元素
margin-top: 90px

结果,这两个现在具有相同边距的元素下移了90px。

html {    background-color: green;    height: 100%; }body {    background-color: pink;    height: 100%;}header {   position: fixed;   border: 1px solid red;}main {  margin-top: 90px;  background-color:yellow;}<header>Project Header</header><main  id="layout-mainContent">    <div  id="first-row">somecontent</div></main>

固定标头移动的原因如下:

  • 尽管元素的包含块
    position: fixed
    是视口…
  • 的CSS偏移性(
    top
    bottom
    left
    right
    )具有的初始值
    auto
    ,其保持在那里它通常将是如果它是在文档流动的元件。
  • 换句话说,当您将元素设置为
    position: absolute
    position: fixed
    (的另一种形式
    position: absolute
    )时,您正在指定所需的定位类型… 但是您并未将其定位在任何地方。
  • 直到您定义元素实际定位的偏移量。
  • 要将标题移到视口的顶部,请使用

    top: 0

    html {

    background-color: green;height: 100%;

    }

    body {

    background-color: pink;height: 100%;

    }

    header {

    position: fixed;

    border: 1px solid red;

    top: 0px; / NEW /

    }

    main {

    margin-top: 90px;

    background-color:yellow;

    }

    Project Header

    <div  id="first-row">somecontent</div>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/635169.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号