Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override font size in a DIV using CSS

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.

I am grateful for any help.

like image 475
Sascha C Avatar asked Jun 15 '12 19:06

Sascha C


People also ask

How do I change the font size in a div in HTML?

To change font size in HTML, use the CSS font-size property. Set it to the value you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a paragraph, heading, button, or span tag.

How do I increase the size of text in a div?

To change the font size of a div using JavaScript, get reference to the div element, and assign required font size value to the element. style. fontSize property.

How do you override a div?

To override an attribute that a CSS class defines, simply append a new inline style after the DIV's class definition.


1 Answers

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 inherit the font-size from the parent element:

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

JS Fiddle demo.

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

Incidentally, while !important will 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 can be avoided) it should be, since it complicates later debugging of styles, and associated inheritance problems.

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

References:

  • font element, at the W3.org.
  • font element at the MDN.
like image 177
David Thomas Avatar answered Oct 22 '22 03:10

David Thomas