ITIY3 Introduction to Web Publishing University of Tampere, COMS 1 CSS - Cascading Style Sheets As a W3C standard, CSS provides a powerful mechanism for defining the presentation of elements in web pages. With CSS style rules, you can instruct the web browser to render page content. With conditional rules, layout may even adapt to different environments and software solutions. Features like the viewport’s size may be quite different in desktop and mobile environments. Mobile web browsers have user interfaces and interactive features that differ from the typical desktop schemes. All these differences can be taken into consideration when designing web pages. With CSS as a separate presentation layer, you can alter the layout of a document and its elements without changing the structural layer created with HTML markup. There may be no need to alter the hierarchical and semantical structure of the document to adapt styling solutions, so presentation and structure may be kept separate. One of the great advantages of CSS lies in the possibility to centrally control the styles of multiple documents in a web site. The same set of style rules may apply to all documents or a selected set of documents within a site. At the document level, you can add styles to selected element types, specific parts of the document (element groups), and even to single elements if needed. https://www.w3.org/standards/webdesign/htmlcss#whatcss https://www.w3.org/Style/CSS/ https://www.w3.org/standards/techs/css#w3c_all Online example demonstrating CSS: An example site intended as a demonstration of what can be accomplished through CSS-based design. The same HTML-document is redesigned using only CSS rules and design specific graphics. Read the description on the page, check the designs and inspect HTML and CSS code examples from the pages: http://www.csszengarden.com/
16
Embed
CSS - Cascading Style Sheets...ITIY3 Introduction to Web Publishing University of Tampere, COMS 1 CSS - Cascading Style Sheets As a W3C standard, CSS provides a powerful mechanism
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
ITIY3 Introduction to Web Publishing
University of Tampere, COMS 1
CSS - Cascading Style Sheets
As a W3C standard, CSS provides a powerful mechanism for defining the presentation of elements in
web pages. With CSS style rules, you can instruct the web browser to render page content. With
conditional rules, layout may even adapt to different environments and software solutions. Features
like the viewport’s size may be quite different in desktop and mobile environments. Mobile web
browsers have user interfaces and interactive features that differ from the typical desktop schemes.
All these differences can be taken into consideration when designing web pages.
With CSS as a separate presentation layer, you can alter the layout of a document and its elements
without changing the structural layer created with HTML markup. There may be no need to alter the
hierarchical and semantical structure of the document to adapt styling solutions, so presentation and
structure may be kept separate.
One of the great advantages of CSS lies in the possibility to centrally control the styles of multiple
documents in a web site. The same set of style rules may apply to all documents or a selected set of
documents within a site. At the document level, you can add styles to selected element types, specific
parts of the document (element groups), and even to single elements if needed.
Using the Inspect element tool in Chrome to check style rules and specificity Developer tools in Chrome is perfect for inspecting style rules added to elements. The tool also shows
rule origin, specificity, inheritance, the whole cascade of style rules (which rules are inherited, which
are overridden, etc.).
When an HTML document is opened in Chrome, open the tool with the Inspect command
(SHFT+CTRL+I or from the context menu “Inspect)” to show how the browser sees the document
(Elements tab, for HTML), and element styles (Styles tab, for CSS). See below (CTRL + SHFT + I , and
undock the panel ) :
You can inspect CSS style rules applied to a selected element, the style rules are listed in the Styles
tab. The rules are organized in specificity order, increasing weight from bottom to top. Overruled
styles show up strike-through, meaning that the style is redefined or replaced by a heavier rule. Web
browser default styles (user agent stylesheet) show with gray background. There is also a reference
(on the right) where the rule is originated - user agent, linked, embedded styles, element.styles (style
attribute).
A simple example, the BODY element is selected (elements tab) for inspection. You can click on an
element in the elements tab to see all the styles affecting it in the Styles tab. Body styles here (see
below) include the default user agent styles and added text color as an embedded style in the
document head (style element):
You can also select any element from the browser’s viewport with the arrow tool (image below).
Select the tool (blue) and click on an element in the browser window. The style tab will show the
selected element’s styles:
ITIY3 Introduction to Web Publishing
University of Tampere, COMS 11
Style inheritance example in the Developer Tool:
Here, the em element is selected from the
document. You can see the elements of the
document in the “Elements” tab, click the triangles
to open the nested elements. The bottom left
indicates the element hierarchy for the selected
element. Here: html > body > p > em
The style tab shows styles applied for the selected element both browser styles (user agent
stylesheet, mid gray) and author styles, here added in the style element.
ITIY3 Introduction to Web Publishing
University of Tampere, COMS 12
The style tab shows all style rules that affect the presented styles of the selected element. On the top,
the declaration (color: green) of the em type selector, which override inherited rules below.
Browser default styles (user agent stylesheet) apply here, the text in em elements is renderd with
italic style (cursive text). It is also visible that besides the em element, there are other elements that
get the same style (i, cite, var, address, dfn) by default.
The em element also inherits text styles (here, text color) from its parents. The em element is in a
paragraph (p) and the paragraph is a child of the body element. The inherited styles for text colors are
overridden by the more specific em selector, element hierarchy for the em element is higher.
Both styles inherited from the p and body elements are stroke through (these are overridden):
Some overridden relative values may still affect the final styles of the element!
The inspector also displays the origin of the style rule. On the right after each selector, there is a
reference to the origin of the rule. In the image above, “selector-1.html:8” and “selector-1.html:7”
suggest that the rules are document specific (selector-1.html is the example HTML file name). The
ITIY3 Introduction to Web Publishing
University of Tampere, COMS 13
digits 8 and 7 are references to the line numbers the rules are added to the document (see below,
view-source CTRL+U).
If there are styles originated from an external stylesheet, the there is a reference to that stylesheet.
Here below some additional rules are added from a separate stylesheet – “example.css”.
<head>
<meta charset="utf-8">
<title>Page Title</title>
<link rel="stylesheet" href="example.css">
<style>
body { color: red; }
p { color: blue; }
em { color: green; }
</style>
The file contains the following rule:
em { font-weight: bold; }
ITIY3 Introduction to Web Publishing
University of Tampere, COMS 14
em { font-weight: bold; } is on line 1 of the linked CSS file “example.css”.
em { color: green; } is displayed above the linked style, since it is defined in the style element within
the document. Document specific styles must be defined after the linked styles to allow document
specific changes when needed. If the linked style sheet would have overlapping styles with document
specific styles (em { font-weight: bold; color: purple; } in example.css), the document specific styles
win (color: green):
ITIY3 Introduction to Web Publishing
University of Tampere, COMS 15
Inspecting more complex element styles from the examples (css-hierarchy.html):
The HTML code of the selected element in the image above: