css初始容纳块

发布于 2022-09-13 00:32:51 字数 6748 浏览 31 评论 0

css初始容纳块的含义是什么????
看css权威指南时的时候看到容纳块的定义
对于浮动元素,容纳块是最近的祖辈块元素。
以下都是对于非根元素
对于绝对定位(absolute)的元素,容纳块为最近的非static定位的父级块元素。具体规则如下:

  1. 若祖辈元素是块级元素,那么容纳块是那个元素内边距的边界,即由边界定义的区域。
  2. 如果祖辈元素是行内元素,容纳块是祖辈元素的内容边界。
  3. 如果没有祖辈元素,元素容纳块就是初始的容纳块。

看书中的例子试着打了一下

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>float11</title>
    <style>
        html{
            background: cornflowerblue;
        }
        body{
          
        }
        p:nth-of-type(2){
            position:relative;
            /* background: brown; */
        }
        p:nth-of-type(1){
            background: cyan;
        }
        b{
            position: absolute;
            border: 1px solid green;
            right: 10px;
            bottom: 0;

        }
    </style>
</head>
<body>
    <p> So we browsed the shops,buying here and there, but browsing at least every other store.
        The street vendors were less abundant,but much more persistent,which was sort of funny.Kat was fun to 
        which,too,as she haggled with various sellers. I don't think we paid more that two-thirds the original asking
        price on anything!
        <b>float</b>
        All of our buying was done in shops on the outskirts of the maiket area. The main section os the market
        was actually sort of a letdown, being more expensive,more touristy,and less friendly, in  a way. About 
        this time I started to wear down,so we caught a taxi back to the New Otani.
        So we browsed the shops,buying here and there, but browsing at least every other store.
        The street vendors were less abundant,but much more persistent,which was sort of funny.Kat was fun to 
        which,too,as she haggled with various sellers. I don't think we paid more that two-thirds the original asking
        price on anything!
        All of our buying was done in shops on the outskirts of the maiket area. The main section os the market
        was actually sort of a letdown, being more expensive,more touristy,and less friendly, in  a way. About 
        this time I started to wear down,so we caught a taxi back to the New Otani.
        So we browsed the shops,buying here and there, but browsing at least every other store.
        The street vendors were less abundant,but much more persistent,which was sort of funny.Kat was fun to 
        which,too,as she haggled with various sellers. I don't think we paid more that two-thirds the original asking
        price on anything!
        All of our buying was done in shops on the outskirts of the maiket area. The main section os the market
        was actually sort of a letdown, being more expensive,more touristy,and less friendly, in  a way. About 
        this time I started to wear down,so we caught a taxi back to the New Otani.
        So we browsed the shops,buying here and there, but browsing at least every other store.
        The street vendors were less abundant,but much more persistent,which was sort of funny.Kat was fun to 
        which,too,as she haggled with various sellers. I don't think we paid more that two-thirds the original asking
        price on anything!
        All of our buying was done in shops on the outskirts of the maiket area. The main section os the market
        was actually sort of a letdown, being more expensive,more touristy,and less friendly, in  a way. About 
        this time I started to wear down,so we caught a taxi back to the New Otani.
    </p>
    <p>
        So we browsed the shops,buying here and there, but browsing at least every other store.
        The street vendors were less abundant,but much more persistent,which was sort of funny.Kat was fun to 
        which,too,as she haggled with various sellers. I don't think we paid more that two-thirds the original asking
        price on anything!
        <b>float</b>
        All of our buying was done in shops on the outskirts of the maiket area. The main section os the market
        was actually sort of a letdown, being more expensive,more touristy,and less friendly, in  a way. About 
        this time I started to wear down,so we caught a taxi back to the New Otani.
        So we browsed the shops,buying here and there, but browsing at least every other store.
        The street vendors were less abundant,but much more persistent,which was sort of funny.Kat was fun to 
        which,too,as she haggled with various sellers. I don't think we paid more that two-thirds the original asking
        price on anything!
        All of our buying was done in shops on the outskirts of the maiket area. The main section os the market
        was actually sort of a letdown, being more expensive,more touristy,and less friendly, in  a way. About 
        this time I started to wear down,so we caught a taxi back to the New Otani.
        So we browsed the shops,buying here and there, but browsing at least every other store.
        The street vendors were less abundant,but much more persistent,which was sort of funny.Kat was fun to 
        which,too,as she haggled with various sellers. I don't think we paid more that two-thirds the original asking
        price on anything!
        All of our buying was done in shops on the outskirts of the maiket area. The main section os the market
        was actually sort of a letdown, being more expensive,more touristy,and less friendly, in  a way. About 
        this time I started to wear down,so we caught a taxi back to the New Otani.
        So we browsed the shops,buying here and there, but browsing at least every other store.
        The street vendors were less abundant,but much more persistent,which was sort of funny.Kat was fun to 
        which,too,as she haggled with various sellers. I don't think we paid more that two-thirds the original asking
        price on anything!
        All of our buying was done in shops on the outskirts of the maiket area. The main section os the market
        was actually sort of a letdown, being more expensive,more touristy,and less friendly, in  a way. About 
        this time I started to wear down,so we caught a taxi back to the New Otani.
    </p>
</body>
</html>

代码解释:有两个p标签,每个p标签分别包含一个绝对定位的b标签,第一个p标签默认定位即定位方式为static,第二个p标签relative定位。绝对定位的b标签设置right:10px;bottom:0;
效果展示:


问题:第一个b标签bottom为零出现在了第二个p标签的中间偏下方的位置,第一个绝对定位的b标签是相对于谁定位的? 即祖辈元素都是static的绝对定位的元素的容纳块是谁,我看了一下也不是p元素也不是body和html就有点疑惑????求指点!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝咒 2022-09-20 00:32:51

初始包含块:根元素(<html>)所在的包含块。在 Continuous Media,其尺寸就是 viewport的尺寸。

如果要为一个元素定位和设置尺寸,往往需要考虑该元素的包含块,如何确定包含块?

在你的例子中,第一个<p>内的<b> 的情况:position属性是absolute,但找不到有任何父级元素位置属性是absoluterelative或者fixed,所以它的包含块应该是初始包含块。

If the element has 'position: absolute', the containing block is established by the nearest ancestor with a 'position' of 'absolute', 'relative' or 'fixed', in the following way:

  1. In the case that the ancestor is an inline element, the containing block is the bounding box around the padding boxes of the first and the last inline boxes generated for that element. In CSS 2.2, if the inline element is split across multiple lines, the containing block is undefined.
  2. Otherwise, the containing block is formed by the padding edge of the ancestor.

If there is no such ancestor, the containing block is the initial containing block.

所以可以看到,当改变浏览器视口大小时,第一个<p>内的<b> 位置总是相对于浏览器视口保持不动。

沐歌 2022-09-20 00:32:51

第一个元素b标签祖先元素中没有非static定位的父级块元素,那么它基于整个窗口进行定位,根据它定位配置的话它应该出现这个窗口的右下角,你可以改变窗口大小试试看下效果

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文