html - 'em' css 单元如何用于定位元素?

标签 html css

下面是嵌套了div的html代码和一个 p作为最内层的元素,

<!DOCTYPE html>
<html>
    <head>
        <title>How em unit works?</title>
        <link href="style.css" rel="stylesheet" type="text/css" media="all">
    </head>
    <body>
        <div>
            <div>
                <p> Some text</p>           
            </div>
        </div>
    </body>
</html>

如果样式为,

body{
    font-size: 10px;  /* Default font size is 16 css pixels for body tag*/
}

body > div{
    font-size: 20px;

}

body > div > div{
    font-size: 50px;
}

p{
    font-size: 0.5em;   /*   25 css pixels  */
    width: 6em;         /* 6X50 css pixels  */
    height: 6em;        /* 6X50 css pixels  */  
    padding-right: 3em; /* 3X50 css pixels  */  
    margin-left: 3em;   /* 3X50 css pixels  */  
    background-color: red;
}

然后,段落文本,取 font-size作为0.5 x 50px (最近 parent 的 0.5 倍 font-size )。

我的问题,

body{
    font-size: 10px;  /* Default font size is 16 css pixels for body tag*/
}

body > div{
    font-size: 20px;
    position: relative;

}

body > div > div{
    font-size: 50px;
}

p{
    font-size: 0.5em;   
    width: 6em;         
    height: 6em;        
    padding-right: 3em; 
    margin-left: 3em;   
    background-color: red;
    position: absolute;
}

如果p得到 position: absolute和最外层 div成为position: relative ,那么,em 是如何实现的? p 的单元行为更改元素?

最佳答案

How does the em unit behavior change, if p is position: absolute?

根本没有变化。

父元素还是父元素。

关于html - 'em' css 单元如何用于定位元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39399123/

相关文章:

jquery - 创建全屏图片库的简单方法是什么

html - Material Design Lite 响应式 CSS 不适用于移动设备/平板电脑或 Chrome DevTools 模拟器

php - 我的 friend 系统为每个用户更改 session

html - 为响应主题调整页面大小时无法使 Genesis 页脚小部件堆叠

javascript - xcode 中的 HTML5 和 javascript

javascript - 按钮大小应该固定,即使其中的文本增加

css - 很难在包含背景图像的网页上(水平)居中文本

JavaScript;具有相同来源的选项卡/窗口之间的通信

javascript - 表格内容根据下拉选项而变化

css - CSS 中奇怪的反斜杠和行为