`
- Format only part of warning text by adding `
`
- Selector
- "." indicates class name
- "#" indicates ID name
- ":" indicates "pseudo class" selector
CSS Rules
- Rule = Selector + Declaration block
- Selector: tag, class, ID, *, …
- Declaration block:
- Enclosed inside
{ ... }
- List of “property: value;” pairs
Adding CSS Rules to Page
- CSS can be specified either
- directly inside
<style> ... </style>
- in a separate file via
<link rel="stylesheet" href="example.css">
- To format a particular part, add
<div>
or <span>
tags if needed
More CSS selectors (1)
[src] { /* has attribute named "src" */
color: red;
}
[target="_blank"] { /* has attribute "target" with value "_blank" */
color: blue;
}
div, p { /* multiple selectors separated by commas */
background-color: grey;
}
div p { /* p is a descendent of div */
background-color: yellow;
}
div > p { /* p is a direct child of div */
background-color: green;
}
More CSS selectors (2)
p.class1.class2 { /* p belonging to both class1 and class2 */
background-color: yellow;
}
div + p { /* p is an immediately adjacent sibling of div */
background-color: blue;
}
div ~ p { /* p is any sibling of div */
background-color: red;
}
:hover { /* : "pseudo class" selector */
color: #0000ff;
}
::first-letter { /* :: "pseudo element" selector */
font-size: 2em;
}
Inheritance
- CSS can be specified in three places:
- Browser default
- User preference
- Web page
- If not set in any of the three places, an element inherits its parent’s CSS properties
Cascading Rule
- Cascading rule dictates which CSS rule wins in case of conflict
- Specificity: more “specific” rule wins!
- Source order
- if equal specificity, later rule wins
- web page > user preference > browser default
HTML Validator
- Online HTML validators exist to check the standard compliance of a document
- Check your HTML using an HTML validator
Retrieving Static Content
- Early Web was mainly designed to retrieve static content (HTML and images) from servers
Static Web Site
- Q: What should a Web site do to serve static contents for a request?
- A:
- Retrieve the corresponding file
- Return it as a response
- Can be set up with
- HTTP server (say, Apache) + filesystem
- URL path to file mapping needed
- Example:
DocumentRoot /var/www/html/
(Apache)
- Q: How can we specify a particular Web resource that we wants to retrieve?
URL (Uniform Resource Locator)
- Unique ID of any object on the Web
- Format
protocol://hostname/path?query#fragment_id
- Fragment identifier: String behind # in URL
- Points to the HTML element with the given id attribute
- Example: http://a.com/a.html#g3
id
attribute value must be unique in a page
- Query: “
name=value
” pairs
What We Learned
- HTML element
- HTML tag, HTML attribute
- HTML tags are for structure
- Basic CSS rules for styling
- Static Web site
- URL standard