Html 使用 CSS 覆盖 DIV 中的字体大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11056902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:03:47  来源:igfitidea点击:

Override font size in a DIV using CSS

csshtmloverridingfont-size

提问by Sascha C

In a DIV I place some text/html code which get loaded from a database. This text sometimes contains font size definitions (ex: font size="3"). is there a way to override this font size in this specific DIV using CSS.

在 DIV 中,我放置了一些从数据库加载的文本/html 代码。此文本有时包含字体大小定义(例如:font size="3")。有没有办法在这个特定的 DIV 中使用 CSS 覆盖这个字体大小。

I am grateful for any help.

我很感激任何帮助。

回答by David says reinstate Monica

Assuming mark-up similar to the following:

假设标记类似于以下内容:

<div>
    <font size="1">Some text at 'size="1"'</font> and natively-sized text, with more at <font size="26">'size="26".'</font>
</div>?

Then you can explicitly instruct CSS to inheritthe font-sizefrom the parent element:

然后你可以明确地通知CSS来inheritfont-size从父元素:

div {
    font-size: 1.5em;
}
div font {
    font-size: inherit;
}

JS Fiddle demo.

JS小提琴演示

Please note, of course, that fontis deprecated and should, therefore, not be used (as support for the element can stop without notice, and/or implementations change without warning).

请注意,当然,这font已被弃用,因此不应使用(因为对元素的支持可能会在没有通知的情况下停止,和/或在没有警告的情况下更改实现)。

Incidentally, while !importantwill force a declaration to override the usual cascade of styles, it's taking a sledgehammer to crack a nut; and, if it can be avoided (and in this case, it seems, it canbe avoided) it should be, since it complicates later debugging of styles, and associated inheritance problems.

顺便说一句,虽然!important会强制声明覆盖通常的级联样式,但要破解坚果需要大锤;并且,如果能够避免它(在这种情况下,看来,它可以被避免的),它应该是,因为它后来变得复杂调试的风格,以及相关的继承问题。

Further, this is treating the symptom of your problem; the problem you're really facing is the presence of the fonttags in your content/database. This should be corrected, by removing the elements, and replacing them with appropriately-styled elements, such as em, spanand so forth...

此外,这是在治疗您的问题的症状;您真正面临的问题是font您的内容/数据库中存在标签。这应该通过删除元素并用适当样式的元素替换它们来纠正,例如emspan等等......

References:

参考:

回答by Zuul

Using the CSS !importantnotation you are telling the browser to overwrite any font-size defined inside your div:

使用CSS !important符号,您告诉浏览器覆盖在您的div: 中定义的任何字体大小:

From the above link it reads:

从上面的链接它是这样写的:

However, for balance, an "!important" declaration (the delimiter token "!" and keyword "important" follow the declaration) takes precedence over a normal declaration.

但是,为了平衡,“!important”声明(分隔符“!”和关键字“important”跟在声明之后)优先于普通声明。

Example

例子

See this working FiddleExample!

看到这个工作小提琴示例!

.htmlContents * {font-size:10px!important;}

<div class="htmlContents">my database html content</div>

回答by yujust

How about stripping the "font" tags from the text before inserting into the div? Then just style the div with a font size.

在插入 div 之前从文本中去除“字体”标签怎么样?然后只需使用字体大小设置 div 的样式。

回答by Viele

One idea: give these text tags an id or class, then use JavaScript to find these elements and change the style on them.

一个想法:给这些文本标签一个 id 或 class,然后使用 JavaScript 找到这些元素并更改它们的样式。