Of Web Development and of Web Development and
Of Web Development and of Web Development and
Chapter 3
3 Location of Styles
4 Selectors
WHAT IS CSS?
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
What is CSS?
You be styling soon
CSS SYNTAX
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
CSS Syntax
Rules, properties, values, declarations
syntax
selector { property: value; property2: value2; } rule
declaration block
selector
em { color: red; }
• Each declaration is
declaration block
terminated with a
semicolon. selector
em { color: red; }
syntax
thinking of selectors is selector { property: value; property2: value2; } rule
syntax
defines over a hundred selector { property: value; property2: value2; } rule
selector
em { color: red; }
LOCATION OF STYLES
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
Actually there are three …
Different types of style sheet
SELECTORS
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
Selectors
Things that make your life easier
declaration
declaration block
selector
em { color: red; }
property value
p {
margin: 5px 0 10px 0;
font-weight: bold;
font-family: Arial, Helvetica, sans-serif;
}
<div>
<p class="first">By Susan on <time>October 1, 2012</time></p>
<p>I love Central Park.</p>
</div>
<hr/>
</body>
.first {
font-style: italic;
color: brown;
}
<div>
<p>By Susan on <time>October 1, 2012</time></p>
<p>I love Central Park.</p>
</div>
<hr/>
</body>
#latestComment {
font-style: italic;
color: brown;
}
<div>
#main div p:first-child { <p>By Susan on <time>October 1, 2012</time></p>
color: green; <p>I love Central Park.</p>
} </div>
<hr/>
</div>
<footer>
<ul>
<li><a href="#">Home</a> | </li>
<li><a href="#">Browse</a> | </li>
</ul>
</footer>
</body>
Because
• there are three different types of style sheets
(author-created, user-defined, and the default
browser style sheet),
• author-created stylesheets can define multiple rules
for the same HTML element,
CSS has a system to help the browser determine how
to display elements when different style rules conflict.
<meta> <title> <h1> <h2> <p> <img> <h3> <div> <div> <p>
<time> <time>
<h3>Reviews</h3>
<div>
<p>By Ricardo on <time>September 15, 2012</time></p>
<p>Easy on the HDR buddy.</p>
</div>
<hr/>
<div>
<p>By Susan on <time>October 1, 2012</time></p>
<p>I love Central Park.</p>
</div>
<hr/>
<meta> <title> <h1> <h2> <p> <img> <h3> <div> <div> <p>
<time> <time>
1 div form {
descendant selector overrides color: orange; 0002
}
(elements only)
2 .example {
overrides
class and attribute color: blue; 0010
selectors }
3
#firstExample {
id selector overrides 0100
color: magenta;
}
4
overrides id + div #firstExample {
additional color: grey; 0101
}
selectors
A higher specificity value
overrides lower specificity
values
inline style 5
overrides <div style="color: red;"> 1000
attribute
user-styles.css #example {
1 color: green;
overrides }
2 <head>
overrides <link rel="stylesheet" href="stylesA.css" /> 3
<link rel="stylesheet" href="stylesWW.css" /> overrides
4 <style>
overrides
#example {
color: orange; 5 #example {
overrides color: blue;
color: magenta; }
}
</style>
</head>
<body> 6 overrides
<p id="example" style="color: red;">
sample text
</p>
</body>
Can you guess what will be the color of the sample text ?
Browser’s
default style
settings
user-styles.css #example {
1 color: green;
overrides }
2 <head>
overrides <link rel="stylesheet" href="stylesA.css" /> 3
<link rel="stylesheet" href="stylesWW.css" /> overrides
4 <style>
overrides
#example {
color: orange; 5 #example {
overrides color: blue;
color: magenta; }
}
</style>
</head>
<body> 6 overrides
<p id="example" style="color: red;">
sample text
</p>
</body>
background-attachment Specifies whether the background image scrolls with the document (default) or
remains fixed. Possible values are: fixed, scroll.
background-image Specifies the background image (which is generally a jpeg, gif, or png file) for
the element. Note that the URL is relative to the CSS file and not the HTML.
CSS3 introduced the ability to specify multiple background images.
background-position Specifies where on the element the background image will be placed. Some
possible values include: bottom, center, left, and right. You can also supply a
pixel or percentage numeric position value as well. When supplying a numeric
value, you must supply a horizontal/vertical pair; this value indicates its
distance from the top left corner of the element.
background-repeat Determines whether the background image will be repeated. This is a common
technique for creating a tiled background (it is in fact the default behavior).
Possible values are: repeat, repeat-x, repeat-y, and no-repeat.
background-size New to CSS3, this property lets you modify the size of the background image.
background-image: url(../images/backgrounds/body-background-tile.gif);
background-repeat: repeat;
300px
body {
background: white url(../images/backgrounds/body-background-tile.gif) no-repeat;
background-position: 300px 50px;
}
border-style Specifies the line type of the border. Possible values are: solid,
dotted, dashed, double, groove, ridge, inset, and outset.
border-width The width of the border in a unit (but not percents). A variety of
keywords (thin, medium, etc) are also supported.
border-color: red green orange blue; /* sets all four sides differently */
When using this multiple values shortcut, they are applied in clockwise order starting at the top.
Thus the order is: top right bottom left.
TRBL (Trouble)
top
border-color: top right bottom left;
left right
border-color: red green orange blue;
bottom
p {
border: solid 1pt red;
margin: 30px;
padding: 0;
}
p {
border: solid 1pt red;
margin: 30px;
padding: 30px;
}
50px
div {
2 90px border: dotted 1pt green;
padding: 0;
50px margin: 90px 20px;
}
50px 5
p {
50px border: solid 1pt red;
padding: 0;
3 90px margin: 50px 20px;
}
If overlapping margins did not collapse, then margin space for
would be 180p (90pixels for the bottom margin of the first <div> +
90 pixels for the top margin of the second <div>), while the
margins and for would be 100px.
Since the width and the height refer to the size of the
content area, by default, the total size of an element is
equal to not only its content area, but also to the sum
of its padding, borders, and margins.
div {
...
box-sizing: border-box; True element width = 10 + 200 + 10 = 220 px
}
True element height = 10 + 100 + 10 = 120 px
p {
background-color: silver;
}
} 100px
p {
background-color: silver;
width: 200px;
height: 100px;
}
overflow: hidden;
overflow: scroll;
overflow: auto;
<body>
.parentFixed { <div class="parentFixed">
width:400px; <strong>parent has fixed size</strong>
height:150px; <div class="percent">
background: beige; PERCENT - 50% of width and height
} </div>
50% of parent (= 200px)
.parentRelative { </div>
width:50%; <div class="parentRelative">
height:50%; <strong>parent has relative size</strong>
background: yellow; <div class="percent">
} PERCENT - 50% of width and height
</style> </div>
</div>
50% 50% </body>
50% of parent
50% 50%
TEXT STYLING
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
Text Properties
Two basic types
This cursive
This
Decorative and cursive fonts
fantasy vary from system to system;
rarely used as a result.
3 Location of Styles
4 Selectors