L10 - CSS 2
L10 - CSS 2
COS216
AVINASH SINGH
DEPARTMENT OF COMPUTER SCIENCE
UNIVERSITY OF PRETORIA
CSS – OVERVIEW
• Styling rules
• Details on how to style elements
• Selectors
• Details on which elements to style
CSS – SELECTORS
• CSS selectors are also used in JavaScript and JQuery to find elements in the DOM
and to manipulate them (eg: add, remove, style, resize, hide, show, etc)
CSS – SELECTORS
selector
{
/* rules here */
}
CSS – INLINE SELECTORS
<html>
<body>
<table><tr><td>Dummy</td></tr></table>
</body>
</html>
body table
{ {
width: 100%; border: 2px solid lime;
height: 100%; height: 500px;
padding: 20px; margin: 20px;
} }
CSS – ID SELECTORS
<html>
<body>
<div id="mydiv"></div>
</body>
</html>
#mydiv
{
border: 2px solid red;
border-radius: 5px;
width: 200px;
height: 200px;
}
CSS – CLASS SELECTORS
<html>
<body>
<div class="greenbox"></div>
<div class="greenbox"></div>
</body>
</html>
.greenbox
{
border: 10px dotted green;
width: 200px;
height: 200px;
}
CSS – ALL SELECTORS
*
{
padding: 0px;
margin: 0px;
}
CSS – MAIN SELECTOR SUMMARY
• Apply a CSS rule set to different groups at the same time (comma separated)
/*
Applies rule to all:
elements with a myclass class
elements with a myid ID
link (a) elements
*/
.myclass, #myid, a
{
width: 200px;
}
CSS – COMBINATORY SELECTORS
• Apply a CSS rule set to elements with one or more attributes (not separated)
• For example, select elements that have 2 classes instead of one
• Apply a CSS rule set to an element inside another element (space separated)
• Inside means any descendant element down the line
• Apply a CSS rule to elements that immediately follow another one (+ separated)
• Apply a CSS rule to elements that immediately precede another one (~ separated)
w3schools.com
w3schools.com
CSS – PSEUDO-ELEMENT SELECTOR
w3schools.com
CSS – PSEUDO-CLASS SELECTOR
button
{
border-width: 1px;
}
button:hover
{
border-width: 3px;
}
CSS – PSEUDO-CLASS SELECTOR
/* If an input is focused */
input:focus
{
color: grey;
}
CSS – PSEUDO-CLASS SELECTOR
D: elements, pseudo-elements
CSS – SPECIFICITY HIERARCHY
• Example:
• A,B,C,D = 0,1,3,1 = 0131 = 131 weight (looses to 1000)
• A,B,C,D = 1,0,0,0 = 1000 = 1000 weight (wins over 131)
CSS – SPECIFICITY HIERARCHY
ul#nav li.active a
• A: inline styles = none [0]
• B: IDs = #nav [1]
• C: classes, pseudo-classes, attributes = .active [1]
• D: elements, pseudo-elements = ul li a [3]
#footer *:not(nav) li
• A: inline styles = none [0]
• B: IDs = #footer [1]
• C: classes, pseudo-classes, attributes = none [0] (not is not counted, only what is
inside it, * is also not counted, since it means everything)
• D: elements, pseudo-elements = nav li [2]