Introduction To Cascading Stylesheet Part II
Introduction To Cascading Stylesheet Part II
Part II
Foreground Color
The foreground of an element consists of its text and border (if one is
specified)
Default: transparent
Background Color
A background color fills the canvas behind the element that
includes the content area, and any padding (extra space) added
around the content, extending behind the border out to its outer edge
Default: border-box
opacity
Values: number (0 to 1)
Default: 1
Make the background of the entire page a light green (R:210, G:220,
B:157, or #d2dc9d)
Subsequent-sibling
selector E1 ~ E2 {property: value;}
By default, browsers typically display linked text as blue and links that
have been clicked as purple
User Action Pseudo-Classes
:focus Applies when the element is selected and ready for
input
When the mouse is placed over links, make the text a brighter purple
(#c700f2) and add a white background color (#fff)
Use these same style rules for when the links are in focus
This selector applies style rule for the following properties to the
first line of the specified element
This selector applies style rule for the following properties to the
first letter of the specified element
p::first-line {
letter-spacing: 9px;
}
p::first-letter {
font-size: 300%;
color: orange;
}
First Line and First Letter Example
Pseudo Element Selectors
Generated Content with ::before and ::after
p.warning::before { p.warning::after {
content: url(exclamation.png); content: " Thank you.";
margin-right: 6px; color: red;
} }
Pseudo Element Selectors
The following example shows how to inserts an image by using the
url() function before the paragraph and “Thank you.” at the end of
the paragraph
element[attribute]
element[attribute="exact value"]
img[title="first grade"] {border: 3px solid;}
Attribute Selectors
Partial Attribute Value Selector (indicated with a bar, ~) selects
elements by partial attribute value
element[attribute~="value"]
element[attribute|="value"]
background-image
background-repeat
background-position
background-attachment
background-clip
background-size
background
Background Image
Most decorative images are added to pages and elements as
backgrounds with CSS
Background Image
background-image
Default: none
Background Image
body {
background-image: url(star.png);
}
blockquote {
background-image: url(dot.png);
padding: 2em;
border: 4px dashed;
}
Exercise
Add background image to your page
Background Repeat
background-repeat
Default: repeat
body {
Background Repeat
background-image: url(star.png);
background-repeat: repeat-y;
}
body {
background-image: url(star.png);
background-repeat: no-repeat;
}
body {
background-image: url(star.png);
background-repeat: repeat-x;
}
Background Repeat
space and round
Exercise
Experiment background-repeat property
Example
header {
margin-top: 0;
padding: 3em 1em 2em 1em;
text-align: center;
background-color: rgba(255, 255, 255, 0.5);
background-image: url(images/purpledot.png);
background-repeat: repeat-x;
}
Background Position
background-position
Default: padding-box
Background Position Origin
Background Attachment
background-attachment
Default: scroll
The Box Model
The Box Model
Every element in a document generates a box to which properties such
as width, height, padding, borders, and margins can be applied
Default: auto
height
Default: auto
Specifying Box Dimension
You can specify the width and height only for block-level elements and
non-text inline elements such as images
Specifying Box Dimension
There are two ways to specify the size of an element
p {
background: #f2f5d5;
width: 500px;
height: 150px;
padding: 20px;
border: 5px solid gray;
margin: 20px;
}
Exercise
Using Chrome DevTool calculate the visible and total width and height
values of the element
Specifying Box Dimension
Using the Border Box
p {
background: #f2f5d5;
width: 500px;
height: 150px;
padding: 20px;
border: 5px solid gray;
margin: 20px;
box-sizing: border-box;
}
Exercise
Using Chrome DevTool calculate the visible and total width and height
values of the element
Handling Overflow
When an element is sized too small for its contents, you can specify
what to do with the content that doesn’t fit by using the overflow
property
overflow
Default: visible
Padding
Padding is the space between the content area and the border
padding
Default: 0
Padding
Padding is the space between the content area and the border
Default: 0
Padding
blockquote {
padding-top: 2em;
padding-right: 4em;
padding-bottom: 2em;
padding-left: 4em;
background-color: #d098d4;
}
Padding
The Shorthand padding Property
padding: top right bottom left; // clockwise direction
blockquote {
padding: 2em 4em 2em 4em;
background-color: #d098d4;
}
The Shorthand padding Property
If the left and right padding are the same, you can shorten it by
supplying only three values
blockquote {
padding: 2em 4em 2em;
background-color: #d098d4;
}
The Shorthand padding Property
If the top and bottem padding are the same and right and left padding
are the same, you can shorten it by supplying only two values
blockquote {
padding: 2em 4em;
background-color: #d098d4;
}
The Shorthand padding Property
If all sides padding are the same, you can shorten it by supplying only
one values
padding: top/bottom/right/left;
blockquote {
padding: 2em;
background-color: #d098d4;
}
Exercise
Create the page shown
<header>
<main>
<section>
<aside>
<footer>
Exercise
Set the box-sizing model to
border-box for all the elements in
the document
html {
box-sizing: border-box;
}
* {
box-sizing: inherit;
}
Exercise
Add 1em of padding on all sides of
main element
main {
padding: 1em;
}
Exercise
Add 1em of padding on all sides of
aside element except the left side
and add 45 pixels of padding on
the left side
Borders
A border is a line drawn around the content area and its (optional)
padding
border-style
Default: none
Borders
A border is a line drawn around the content area and its (optional)
padding
Default: none
Borders
If you do not specify a width, the default medium width will be used
If there is no color specified, the border uses the foreground color of the
element (same as the text)
Borders Example
div#silly {
border-top-style: solid;
border-right-style: dashed;
border-bottom-style: double;
border-left-style: dotted;
width: 300px;
height: 100px;
}
div#silly {
border-style: solid dashed double dotted;
}
Borders Example
Border Width (Thickness)
border-width
Default: medium
Border Width (Thickness)
border-top-width, border-right-width,border-bottom-width,
border-left-width
Default: medium
div#silly {
Border Width Example
border-top-width: thin;
border-right-width: medium;
border-bottom-width: thick;
border-left-width: 12px;
border-style: solid;
width: 300px;
height: 100px;
}
div#silly {
border-width: thin medium thick 12px;
border-style: solid;
Border Width Example
Border Color
border-color
border
The values for border and the side-specific border properties may
include style, width, and color values in any order
Default: 0
Rounded Corners with border-radius
border-top-left-radius, border-top-right-radius, border-
bottom-right-radius, border-bottom-left-radius
Default: 0
Rounded Corners with border-radius
Elliptical corners
You can make a corner elliptical by specifying two values:
padding: 1em;
border: double 4px #eaddc4;
Exercise
Add rounded corners to the main and aside elements
border-radius: 25px;
Exercise
Adds a 1-pixel solid border on the top of the headline
Add another border that adds a 3-pixel solid border on the left
text-decoration: none;
border-bottom: 1px dotted;
Margins
A margin is an optional amount of space that you can add on the
outside of the border
Margins keep elements from bumping into one another or the edge of
the browser window or viewport
Default: auto
Margins
margin-top, margin-right, margin-bottom, margin-left
Default: auto
Margins
p#A {
p#B {
margin: 4em;
border: 2px solid red; margin-top: 2em;
background: #e2f3f5; margin-right: 250px;
}
margin-bottom: 1em;
margin-left: 4em;
body {
border: 2px solid red;
margin: 0 20%;
background: #e2f3f5;
border: 3px solid red;
}
background-color: #e2f3f5;
}
Margins
Collapsing margins
The top and bottom margins of neighboring elements collapse
The only time top and bottom margins don’t collapse is for floated or
absolutely positioned elements
When you apply left and right margins to inline text elements,
margin space will be held clear before and after the text in the flow of
the element, even if that element breaks over several lines
body {
margin: 0;
}
nav ul {
margin: 0;
}
Exercise
Add a margin-top to h1
h1 {
margin-top: 1.5em;
}
Exercise
Give the main element a 2.5% margin on all sides
For the aside element put 1em on the top, 2.5% on the right side, 0
on the bottom, and 10% margin on the left side
Assigning Display Types
The display property defines the type of element box an element
generates in the layout
Default: inline
Box Drop Shadow
box-shadow
Default: none
Box Drop Shadow
box-shadow: 6px 6px gray; /* A */
box-shadow: 6px 6px 5px gray; /* B */
box-shadow: 6px 6px 5px 10px gray; /* C */