简体   繁体   English

在同一浏览器上,样式值相同,元素相同但计算值不同

[英]Same style values, same element but different computed values on same browser

I am working on a chrome extension which returns the font-size property of any element. 我正在开发一个chrome扩展程序,该扩展程序返回任何元素的font-size属性。 I am loading the ajax response data into the extension document and computing their properties. 我正在将ajax响应数据加载到扩展文档中并计算其属性。 Now something strange happening and I'm not being able to find out the reason. 现在发生了一些奇怪的事情,我无法找出原因。

I have a heading tag on the webpage I'm analysing upon. 我正在分析的网页上有一个标题标签。 Style tab (inspect element) on the webpage says its font-size is 2em and computed value is 32 pixels. 网页上的“样式”标签(检查元素)表示其字体大小为2em,计算值为32像素。

Now, when i load the same page in my chrome extension, Style tab (inspect element) on my extension says its font-size is 2em but shows its computed style to be 24px. 现在,当我在chrome扩展程序中加载同一页面时,扩展程序上的“样式”标签(检查元素)表示其字体大小为2em,但其计算出的样式为24px。 To clarify, I'm attaching the images of styles and computed syles of both webpage and chrome extension. 为了澄清,我附上了网页和chrome扩展程序的样式和计算样式的图像。

Case 1: Style (Webpage) 情况1:样式(网页) 看最右边,font-size:2em

Computed Style (Webpage) 计算样式(网页) 计算的字体大小= 32px

Case 2: Style (After Loading In Chrome Extension) 情况2:样式(在Chrome Extension中加载后) 样式:font-size = 2em(类似于网页上的内容)

Computed Style (After Loading In Chrome Extension) 计算样式(在Chrome Extension中加载后) 这是我的问题。计算的字体大小= 24px

I just want to know why is this happening? 我只想知道为什么会这样? Same styles (2em) but browser window showing computed size as 32 and extension window (on same browser) showing 24px. 样式相同(2em),但浏览器窗口显示的计算尺寸为32,扩展窗口(在同一浏览器上)显示24px。

The problem lies in that em is a relative unit in CSS, which means that an element whose font-size is in ems will base the font-size on its parent's font-size. 问题在于em是CSS中的相对单位,这意味着其font-size在ems中的元素会将font-size基于其父级的font-size。 In your second example, it seems that the body has had its font-size changed to 75%, which you need to change to 100%. 在第二个示例中,似乎body的字体大小已更改为75%,您需要将其更改为100%。 The behind-the-scenes calculation that is occuring here is that html 's default font-size is 16px, 75% of that is 12px, and twice that ( 2em ) is 24px- the computed font size. 这里发生的幕后计算是html的默认字体大小为16px,其中75%为12px,两倍( 2em )为24px,即计算出的字体大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM