Module 3 CSS
Module 3 CSS
screen
tv
projection
handheld Specifies the device the document will be displayed on. Default value
media
print is all. This is optional attribute.
braille
aural
all
Types of selectors
We can divide CSS selectors into five categories:
• Simple selectors (select elements based on name, id, class)
• Combinator selectors (select elements based on a specific
relationship between them)
• Pseudo-class selectors (select elements based on a certain state)
• Pseudo-elements selectors (select and style a part of an element)
• Attribute selectors (select elements based on an attribute or
attribute value)
Basic CSS selectors
The CSS element Selector
The element selector selects HTML elements based on the element name
p {
text-align: center;
color: red;
}
The CSS id Selector
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element is unique within a page, so the id selector is used to select one unique
element!
To select an element with a specific id, write a hash (#) character, followed by the id of the
element
#para1 {
text-align: center;
color: red;
}
Universal Selector
The CSS rule for universal selector renders the content of every element in our document
*{
color: #000000;
}
The Class Selector
The class selector selects HTML elements with a specific class attribute.
All the elements having that class will be formatted according to the defined rule
To select elements with a specific class, write a period (.) character, followed by the class name.
.center {
text-align: center;
color: red;
}
You can also specify that only specific HTML elements should be affected by a class
E.g. In this example style rule will be applied to only <p> elements with class="center"
p.center {
text-align: center;
color: red;
}
Grouping Selector
The CSS rule is applied to a group of elements
h1, h2, p {
text-align: center;
color: red;
}
Attribute Selector
Attribute selectors apply styles to HTML elements with particular attributes.
E.g. The style rule below will match all the input elements having a type attribute with a value of text
input[type = "text"] {
color: #000000;
}
:only-child p:only-child Selects every <p> element that is the only child of its parent
:optional input:optional Selects <input> elements with no "required" attribute
:out-of-range input:out-of-range Selects <input> elements with a value outside a specified range
:read-only input:read-only Selects <input> elements with a "readonly" attribute specified
:target #news:target Selects the current active #news element (clicked on a URL containing that anchor
name)