Teaching Presentation - Style Attributes
Definition and UsageHTML Styling
Text Color
Cascading Style Sheets (CSS)
History of the Style Attribute
Definition and Usage
- The style attribute specifies an inline style for an element.
- The style attribute will override any style set globally, e.g. styles specified in the <style> tag or in an external style sheet.
w3schools.com
HTML Styling
- Every HTML element has a default style (background color is white and text color is black).
- Changing the default style of an HTML element, can be done with the style attribute.
- This example changes the default background color from white to light grey
<body style = "background-color:lightgrey">
<h1> This is a heading </ h1>
<p> This is a paragraph. </ p>
</ body>
w3schools.com
Text Color:
<! DOCTYPE html>
<html>
<body>
<h2 style= " color:red "> I am Red </ h2>
I am Red
<h2 style="color:blue"> I am Blue </ h2>
I am Blue
</ body>
</ html>
w3schools.com
Cascading Style Sheets (CSS)
- In Cascading Style Sheets (CSS), a key feature is the cascade itself. In the cascade, styles set at different levels take different levels of importance, so a style that’s set in a globally linked style sheet can be overridden by a style for the same class or id that’s included in an embedded style sheet. The style attribute goes a level further, to override styles set in linked or embedded style sheets.
- However, the use of the style attribute is generally considered to be a bad practice, as it causes the presentation to become intrinsically mixed with the content of the document—a practice that’s almost as bad as using the font element to style text. One way in which you might use inline styles is to debug CSS display issues (applying the style at its lowest level in the cascade, and progressively moving higher up the cascade until the problem is isolated). You should, therefore, avoid using inline style attributes in your markup.
- The style attribute includes a series of CSS property and value pairs. Each "property":"value" pair is separated by a semicolon, just as it is when it’s typed into an embedded or linked style sheet (although there should not be a carriage return after the semicolon when the pair is used inside the style attribute.
w3schools.com
History of the Style Attribute
- HTML 4.0 introduced the "style" attribute, whose contents could contain any styling language (through the use of META http-equiv Content-Style-Type), but by default, and in practice, has only contained CSS. XHTML Modularization introduced the Style Attribute Module which also has a "style" attribute whose semantics are the same as that in HTML 4. SVG also has a language neutral "style" attribute, whose language is specified with the contentStyleType attribute which has the default value of "text/css". MathML states that all MathML elements accept the "style" attribute to facilitate compatibility with CSS.
- Not all XML-based document formats have a "style" attribute to permit the user to use CSS to style documents and specific elements in documents, but in case a certain format has a "style" attribute and the attribute accepts CSS as its value, then this specification describes the attribute's syntax and interpretation.