Lecture-3
Lecture-3
Lecture-3
• CSS describes the appearance and layout of information on a web page (as
opposed to HTML, which describes the content of the page)
• can be embedded in HTML or placed into separate .css file (preferred)
Basic CSS rule syntax
selector { p {
property: value; font-family: sans-serif;
property: value; color: red;
... }
property: value;
}
• a CSS file consists of one or more rules
• a rule's selector specifies HTML element(s) and applies
style properties
• a selector of * selects all elements
Inline styles: the style attribute (BAD!)
<p style="font-family: sans-serif; color: red;">
This is a paragraph</p>
HTML
This is a paragraph
output
• CSS (like HTML) is usually not commented as much as code such as Java
• the // single-line comment style is NOT supported in CSS
• the <!-- ... --> HTML comment style is also NOT supported in CSS
W3C CSS Validator
<p>
<a href="http://jigsaw.w3.org/css-validator/check/referer">
<img src="http://jigsaw.w3.org/css-validator/images/vcss"
alt="Valid CSS!" /></a>
</p> HTML
output
• jigsaw.w3.org/css-validator/
• checks your CSS to make sure it meets the official CSS specifications
• more picky than the web browser, which may render malformed CSS correctly
Grouping styles
p, h1, h2 {
color: green;
}
h2 {
background-color: yellow;
} CSS
This paragraph uses the above style.
This h2 uses the above styles.
output
[TO LUKE SKYWALKER] The alliance... will die. As will your friends. Good, I can feel your anger. I
am unarmed. Take your weapon. Strike me down with all of your hatred and your journey
towards the dark side will be complete. output
• can be left, right, center, or justify (which widens all full lines of the element so that
they occupy its entire width)
Text-decoration
p {
text-decoration: underline;
} CSS
This paragraph uses the style above.
output
• value consists of two tokens, each of which can be top, left, right, bottom,
center, a percentage, or a length value in px, pt, etc.
• value can be negative to shift left/up by a given amount
The visibility property
p.secret {
visibility: hidden;
} CSS
output
property description
visibility sets whether an element should be shown onscreen;
can be visible (default) or hidden
• hidden elements will still take up space onscreen, but will not be shown
• to make it not take up any space, set display to none instead
• can be used to show/hide dynamic HTML content on the page in response to events
The opacity property
body { background-image: url("images/marty-mcfly.jpg");
background-repeat: repeat; }
p { background-color: yellow;}
p.mcfly1 { opacity: 0.75; }
p.mcfly2 { opacity: 0.50; }
p.mcfly3 { opacity: 0.25; } CSS
property description
opacity how not-transparent the element is; value ranges from 1.0 (opaque) to 0.0 (transparent)
box-shadow
box-shadow: h-shadow v-shadow blur; CSS
box-shadow: 10px 10px 5px; CSS
Styles that conflict
p, h1, h2 { color: blue; font-style: italic; }
h2 { color: red; background-color: yellow; }
CSS
This paragraph uses the first style above.
• when two styles set conflicting values for the same property, the latter style
takes precedence
Cascading style sheets
• It's called Cascading Style Sheets because the properties of an
element cascade together in this order:
• browser's default styles (reference)
• external style sheet files (in a <link> tag)
• internal style sheets (in a <style> tag in the page header)
• inline style (the style attribute of an HTML element)
Inheriting styles (explanation)
body { font-family: sans-serif; background-color: yellow; }
p { color: red; background-color: aqua; }
a { text-decoration: underline; }
h2 { font-weight: bold; text-align: center; } CSS
This is a heading
A styled paragraph. Previous slides are available on the website.
• A bulleted list output
• when multiple styles apply to an element, they are inherited
• a more tightly matching rule can override a more general inherited rule
• not all properties are inherited (notice link's color above)