Bmce P780012
Bmce P780012
Advanced CSS
2
3
Terms
• border-box • class selector
• box model • <div>
• box-sizing • divisions
• cascade • id selectors
• cascading style sheets • position
(CSS) • pseudo-class
• class attribute • z-index property
4
Cascade
• Cascade is a sorting order that determines which
properties will modify a given element based on
importance, specificity, and source order.
Follows three steps to decide which properties to
assign to an element.
5
Cascade
• Through the cascade process:
A weight is assigned to each rule
Weight decides which rule takes precedence when
more than one applies.
The rules defined later in a document tree have
higher precedence than those defined earlier.
6
Class Selectors
• CSS technologies provide a mechanism for defining
a style once and using it across multiple elements
using class selectors.
id selectors may be defined and used in a document.
Class Selectors
• Within the style attribute, formatting information
is provided in “property:value” pairs.
Property-value pairs are separated from one
another by a semi-colon (;) symbol.
When two elements are to be formatted in the
same fashion, the style attributes need to be
duplicated for each element.
10
Class Selectors
• Using class selectors resolves this issue. A class
selector is a tool that creates styles with “names”
(or identifiers).
Once a class selector has been created, it can be
used across multiple elements.
11
Class Selectors
• Figure 2 can be reviewed to see an external style
sheet with a class selector. The class selector is
coded with full stop (“.”) in front of the selector.
This style can be applied to any element.
It also can be used across <p> and <li> elements.
Figure 2
Compare the style specification with that for the <h2> element. Element names are specified
without a full stop (“.”), and class selectors are specified with a period/dot “.” at the beginning.
13
Class Selectors
• Class selectors can be specific to a single element.
The red_text class selector contains an element
name, p, specified before the full stop (“.”).
This means that only <p> elements may make use of
the red_text style.
This is shown in the HTML code. Both the <p> element
and the <li> element make use of the red_text style
via the class attribute.
As shown by the browser rendering of the document,
the style was applied only to the <p> element
14
Class Selectors
• Creating class selectors and class selectors
combined with element specifications are both
valid.
It is better to write simple CSS and create styles
with only class selectors, instead of combining class
selector with element names.
It will help to keep the size of the CSS file smaller.
15
Class Selectors
• Class names must be self-documenting.
For example, a class that sets text color to red and
bolds it should be named so the class name
indicates its purpose, such as “red_bold.”
16
iD Selectors
• Elements may use an attribute called “id,” and
styles may be associated with an id, much like
styles can be associated with a class.
The difference is that id selectors are selectors
that begin with a “#” symbol instead of the “.”
symbol used by the class selector.
17
iD Selectors
• The “id” selector is used by an element by
specifying the id attribute instead of the class
attribute.
An id uses the id attribute of an HTML element to
select a specific element.
18
Pseudo-Class
Pseudo-Class
Figure 8 Pseudo-Class
Here are hover styles for two
different classes of elements. The
format for setting up a selector
along with a pseudo-class is shown
highlighted in yellow. Note the
use of the “.” separating the
element name and the name of
the selector, followed by the “:”
symbol between the name of the
class and the pseudo-class. The
specific class to be used across an
element is specified using the
class attribute. These statements
are shown highlighted in yellow.
28
Centering Elements
Centering Elements
Figure 10
In HTML5, the
text-align property
is set to “center”
to align text within
an element.
36
Centering Elements:
Figure 11 Complicated Example
In this example <nav>,
<section>, and an
<aside> elements are
specified.
•The <nav> element is
to be left aligned
•The <section>
element is to be
centered
•The <aside> element
is to be right aligned.
37
Centering Elements:
Complicated Example
• In figure 11, the CSS file shows that the <nav> and
<section> elements are given a width property
and floated left.
The <aside> element is floated to the right.
NOTE: All elements are provided with a padded
property to ensure that element content does not
“stick” to the border.]
38
Centering
Centering Elements
Centering Elements
Centering Elements
Figure 13
• In figure 13: yellow highlight
The style sets the border property, which
is used to depict the edges of the
<body>element and is specified to verify
that centering is occurring correctly.
The width property states that this
selector takes up 80% of its available
space.
42
Centering Elements
Figure 13
• In figure 13: yellow highlight
The margin property is set to “auto.”
When the margin property is set to
“auto,” it centers the <body> element
within its container, which is the
document.
Then the width property states that the
<body> element must take up 80 percent
of the webpage. The text-align property
ensures that the contents of <body>
element are centered.
43
Centering Elements
Figure 13
• In figure 13: blue highlight
One change has been made to the <nav>
and <aside> selectors. The <nav>
element is floated to the left of the
<body> element.
The <nav> element inherits the text-align
property of the <body> element, and this
text inside this element is centered.
44
Centering Elements
Figure 13
• In figure 13: blue highlight
Setting the text-align property to “left”
overrides the property setting of the
parent <body> element and ensures that
test inside the <nav> element is left
aligned.
The text-align property of the <aside>
element is also changed to “left.” The
<aside> element is floated to the right,
but to ensure that the text inside is not
centered, the text-align property is
changed.
45
Centering Elements
Figure 13
• In figure 13: green highlight
It contains styles for the <section>
element. It is no longer floated, and its
float property setting is commented out.
Instead, its display property is set to
“inline-block.” This ensures that the
element does not start on a new line,
which is the default for block elements,
such as the <section> element.
46
Centering Elements
Figure 13
• In figure 13: green highlight
Since there is no float property
specified, it uses the property settings
specified for its parent <body> element.
It uses the text-align property of the
<body> element to place itself inside its
container, and the <section> element
now appears to be centered inside the
<body> element.
47
Centering Elements
Figure 14
Box Model
Embedded Style
Embedded Style
Position-Related Properties
Position-Related Properties
Position-Related Properties
Position-Related Properties
Position-Related Properties
Position-Related Properties
Position-Related Properties
Position-Related Properties
Position-Related Properties
Box-Sizing Property
Box-Sizing Property
Box-Sizing Property
Figure 22
• This is a simplified version of an
earlier visual where centering
elements within a page were
demonstrated.
To keep the code simple, the
padding and margin properties
have been removed.
70
Box-Sizing Property
Figure 22
• The code highlighted in yellow
sets the width property of each
of the three elements maintained
in the <body> element.
• The width has been specified as a
percentage value, and the sum of
the three width properties is 100
percent.
71
Box-Sizing Property
Box-Sizing Property
Box-Sizing Property
Box-Sizing Property
Box-Sizing Property
Box-Sizing Property
De-bugging CSS
De-bugging CSS
Review
• What do iD selectors begin with?
• What is a set of rules used by the browser to
determine the amount of space taken up by an
element on the page?
• What does box-sizing allow the developer to do?