CSS - Cascading Style Sheet: Three Ways To Insert CSS
CSS - Cascading Style Sheet: Three Ways To Insert CSS
Selector
. Select class
# Select id
Comments
<head>
<style>
h1 {
color: blue;
font-family: verdana;
font-size: 300%;
p{
color: red;
font-family: courier;
font-size: 160%;
</style>
</head>
<head>
</head>
styles.css
body {
background-color: powderblue;
h1 {
color: blue;
p{
color: red;
p{
padding: 30px;
p{
margin: 50px;
}
Content - The content of the box, where text and images appear
Padding - Clears an area around the content. The padding is
transparent
Border - A border that goes around the padding and content
Margin - Clears an area outside the border. The margin is
transparent
320px (width of content area)
+ 20px (left padding + right padding)
+ 10px (left border + right border)
= 350px (total width)
50px (height of content area)
+ 20px (top padding + bottom padding)
+ 10px (top border + bottom border)
= 80px (total height)
elements
Use the CSS padding property for space inside the border
Use the CSS margin property for space outside the border
Sytax
p{
color: red;
text-align: center;
style: <p>).
p{
text-align: center;
color: red;
<p>And me!</p>
p.center {
text-align: center;
color: red;
}
<h1 class="center">Red and center-aligned heading</h1>
style.</p>
<p>And me!</p>
=
h1, h2, p {
text-align: center;
color: red;
}
CSS Comments
/* This is
a multi-line
comment */
CSS Backgrounds
In these chapters, you will learn about the following CSS background
properties:
background-color
body {
background-color: lightblue;
}
div {
background-color: green;
opacity: 0.3;
}
Opacity 透明度
div {
background: rgba(0, 128, 0, 0.3) /* Green background with 30% opacity
*/
}
background-image
body {
background-image: url("bgdesert.jpg");
}
background-repeat
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
}
background-attachment
The background-attachment property specifies whether the background
image should scroll or be fixed
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
background-attachment: fixed;/scroll;
}
background-position
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}
background (shorthand property)
body {
background-color: #ffffff;
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}
body {
background: #ffffff url("img_tree.png") no-repeat right
top;
}
div {
background:white;
color: black;
padding: 15px;
margin: 60px auto 0px;
width: 600px;
font-family: "Raleway";
}
hr {
width: 80%;
border: 1px solid #C72A33;
margin-top: 15px;
}
CSS Border Style
The border-style property specifies what kind of border to display.
The following values are allowed:
dotted - Defines a dotted border
dashed - Defines a dashed border
solid - Defines a solid border
double - Defines a double border
groove - Defines a 3D grooved border. The effect depends on the
border-color value
ridge - Defines a 3D ridged border. The effect depends on the border-color
value
inset - Defines a 3D inset border. The effect depends on the border-color
value
outset - Defines a 3D outset border. The effect depends on the border-color
value
none - Defines no border
hidden - Defines a hidden border
p.dotted {outline-style: dotted;}
p.dashed {outline-style: dashed;}
p.solid {outline-style: solid;}
p.double {outline-style: double;}
p.groove {outline-style: groove;}
p.ridge {outline-style: ridge;}
p.inset {outline-style: inset;}
p.outset {outline-style: outset;}
p.ex1 {
border: 1px solid black;
outline-style: solid;
outline-color: red;
outline-width: thin;
}
p.ex2 {
border: 1px solid black;
outline-style: solid;
outline-color: red;
outline-width: thin;
}
p.ex3 {
border: 2px solid black;
outline-style: outset;
outline-color: grey;
}
p.one {
border-style: solid;
border-width: 5px;
}
p.one {
border-style: solid;
border-color: red;
}
p{
border-top-style: dotted;
border-right-style: solid;
border-bottom-style: dotted;
border-left-style: solid;
}
border-width
border-style (required)
border-color
p{
border: 5px solid red;
}
p{
border: 2px solid red;
border-radius: 5px; 圆角
}
CSS Margin
p{
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}
p{
margin: 25px 50px 75px 100px;
}
CSS Padding
div {
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
}
CSS Color
body {
background-color: lightgrey;
color: blue;
}
h1 {
background-color: black;
color: white;
}
div {
background-color: blue;
color: white;
}
CSS Text
Align
img.a {
vertical-align: baseline;
}
img.b {
vertical-align: text-top;
}
img.c {
vertical-align: text-bottom;
}
img.d {
vertical-align: sub;
}
img.e {
vertical-align: super;
}
Decoration
h1 {
text-decoration-line: overline;
}
h2 {
text-decoration-line: line-through;
}
h3 {
text-decoration-line: underline;
}
p{
text-decoration-line: overline underline;
}
Transformation
p.uppercase {
text-transform: uppercase;
}
p.lowercase {
text-transform: lowercase;
}
p.capitalize {
text-transform: capitalize;
}
Spacing
p{
text-indent: 50px; first-line space
}
h1 {
letter-spacing: 5px; spaces between letters
}
h2 {
letter-spacing: -2px;
}
Shadow
p.oblique {
font-style: oblique;
}
p.normal {
font-weight: normal;
}
p.thick {
font-weight: bold;
}
Size
h1 {
font-size: 40px;
}
h2 {
font-size: 30px;
}
p{
font-size: 14px;
}
Google font
<head>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Sofia">
<style>
body {
font-family: "Sofia", sans-serif;
}
</style>
</head>
CSS Links
/* unvisited link */
a:link {
color: red;
}
/* visited link */
a:visited {
color: green;
}
/* mouse over link */
a:hover {
color: hotpink;
}
/* selected link */
a:active {
color: blue;
}
CSS List
ul.a {
list-style-type: circle;
}
ul.b {
list-style-type: square;
}
ol.c {
list-style-type: upper-roman;
}
ol.d {
list-style-type: lower-alpha;
}
Img
ul {
list-style-image: url('sqpurple.gif');
}
CSS Table
table, th, td {
border: 1px solid;
}
table {
width: 100%;
}
th {
height: 70px;
}
td {
text-align: center;
}
th, td {
padding: 15px;
text-align: left;
}
th, td {
border-bottom: 1px solid #ddd;
}
th {
background-color: #04AA6D;
color: white;
}
CSS Display
Examples of block-level elements:
<div>
<h1> - <h6>
<p>
<form>
<header>
<footer>
<section>