Lesson HTML Css
Lesson HTML Css
<div> as a container
It is a good idea to always include width and height attributes. If
height and width are not set, the page might flicker while the <div>
video loads. <h2>London</h2>
<p>London is the capital city of
The text between the <video> and </video> tags will only be England.</p>
displayed in browsers that do not support the <video>
element.
<p>London has over 9 million
inhabitants.</p>
<video width="320" height="240" controls> </div>
<source src="movie.mp4" type="video/mp4"> Center align a <div> element
<source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag. <style>
</video> div {
width:300px;
HTML <video> Autoplay
margin:auto;
<video width="320" height="240" autoplay>
}
<source src="movie.mp4" type="video/mp4"> </style>
<source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
Multiple <div> elements
</video> <div style="background-color:#FFF4A3;">
The HTML <audio> Element
<h2>London</h2>
<p>London is the capital city of
<audio controls> England.</p>
<source src="horse.ogg" type="audio/ogg <p>London has over 9 million
">
inhabitants.</p>
<source src="horse.mp3" type="audio/mpe
</div>
g">
Your browser does not support the audio
element. <div style="background-color:#FFC0C7;">
</audio> <h2>Oslo</h2>
The controls attribute adds audio controls, like <p>Oslo is the capital city of
play, pause, and volume. Norway.</p>
<p>Oslo has over 700,000
inhabitants.</p>
The <source> element allows you to specify
alternative audio files which the browser may </div>
choose from. The browser will use the first
recognized format. <div style="background-color:#D9EEE1;">
<h2>Rome</h2>
The text between the <audio> and </audio> tags <p>Rome is the capital city of
will only be displayed in browsers that do not Italy.</p>
support the <audio> element. <p>Rome has over 4 million
inhabitants.</p>
HTML <audio> Autoplay </div>
<style>
This <h1> element will be styled according to The CSS element Selector
the #myHeader style definition in the head section:
<!DOCTYPE html> The element selector selects HTML elements based on
<html> the element name.
<head> p {
<style> text-align: center;
#myHeader { color: red;
}
background-color: lightblue;
color: black; The CSS id Selector
padding: 40px;
text-align: center; The id selector uses the id attribute of an HTML
} element to select a specific element.
</style>
</head> The id of an element is unique within a page, so the id
<body> selector is used to select one unique element!
<h1 id="myHeader">My Header</h1> To select an element with a specific id, write a hash (#)
character, followed by the id of the element.
#para1 {
</body>
text-align: center;
</html> color: red;
Note: The id name is case sensitive! }
Note: The id name must contain at least one character,
The CSS class Selector
cannot start with a number, and must not contain
whitespaces (spaces, tabs, etc.). The class selector selects HTML elements with a
Difference Between Class and ID specific class attribute.
A class name can be used by multiple HTML elements,
while an id name must only be used by one HTML To select elements with a specific class, write a period
(.) character, followed by the class name.
element within the page .center {
We can divide CSS selectors into five categories: <link rel="stylesheet" href="mystyle.css">
Simple selectors (select elements based on Note: Do not add a space between the property value
name, id, class) (20) and the unit (px):
Combinator selectors (select elements based Incorrect (space): margin-left: 20 px;
on a specific relationship between them) Correct (no space): margin-left: 20px;
Pseudo-class selectors (select elements based
on a certain state) Internal CSS- Internal styles are defined within
Pseudo-elements selectors (select and style a the <style> element, inside the <head>
part of an element) section of an HTML page:
Attribute selectors (select elements based on
an attribute or attribute value)
<!DOCTYPE html> center center
<html> left bottom
<head> 50% 50% (same as center center)
<style>
body { CSS background-attachment
background-color: linen; The background-attachment property specifies
} whether the background image should scroll or be
fixed (will not scroll with the rest of the page):
h1 { Specify that the background image should be fixed:
color: maroon;
body {
margin-left: 40px;
background-image: url("img_tree.png");
}
</style> background-repeat: no-repeat;
</head> background-position: right top;
background-attachment: fixed;
}
Inline CSS-An inline style may be used to apply
a unique style for a single element. Specify that the background image should scroll with
the rest of the page:
body {
<h1 style="color:blue;text-
background-image: url("img_tree.png");
align:center;">This is a heading</h1>
background-repeat: no-repeat;
<p style="color:red;">This is a
paragraph.</p> background-position: right top;
background-attachment: scroll;
CSS Backgrounds }
body {
background-color: lightblue; CSS Borders
} p.dotted {border-style: dotted;}
Other Elements p.dashed {border-style: dashed;}
h1 { p.solid {border-style: solid;}
background-color: green; p.double {border-style: double;}
} p.groove {border-style: groove;}
p.ridge {border-style: ridge;}
div { p.inset {border-style: inset;}
background-color: lightblue; p.outset {border-style: outset;}
} p.none {border-style: none;}
p.hidden {border-style: hidden;}
p { p.mix {border-style: dotted dashed solid
background-color: yellow; double;}
}
CSS Border Width
Opacity / Transparency p.one {
The opacity property specifies the border-style: solid;
opacity/transparency of an element. It can take a value border-width: 5px;
from 0.0 - 1.0. The lower value, the more transparent: }
div {
background-color: green; p.two {
opacity: 0.3; border-style: solid;
} border-width: medium;
body { }
background-image: url("paper.gif");
} p.three {
p { border-style: dotted;
background-image: url("paper.gif"); border-width: 2px;
}
}
p.four {
CSS background-repeat
border-style: dotted;
If the image above is repeated only horizontally
border-width: thick;
(background-repeat: repeat-x;), the background }
will look better:
body { CSS Border Color
background-image: url("gradient_bg.png"); The border-color property is used to set the color of
background-repeat: repeat-x; the four borders.
}
p.one {
CSS background-repeat: no-repeat border-style: solid;
body { border-color: red;
background-image: url("img_tree.png"); }
background-repeat: no-repeat;
} p.two {
border-style: solid;
CSS background-position border-color: green;
body { }
background-image: url("img_tree.png");
p.three {
background-repeat: no-repeat;
border-style: dotted;
background-position: right top; border-color: blue;
} }
left top
right top CSS Border - Shorthand Property
border-width The auto Value
border-style (required)
border-color
You can set the margin property to auto to horizontally
p { center the element within its container.
border: 5px solid red;
} The element will then take up the specified width, and
the remaining space will be split equally between the
Left Border left and right mar
p {
border-left: 6px solid red; div {
} width: 300px;
margin: auto;
Bottom Border border: 1px solid red;
}
p {
border-bottom: 6px solid red; CSS Padding
}
Padding is used to create space around an element's
CSS Margins content, inside of any defined borders.
Margins are used to create space around elements, Padding - Individual Sides
outside of any defined borders.
CSS has properties for specifying the padding for each
margin-top side of an element:
margin-right
margin-bottom padding-top
margin-left padding-right
padding-bottom
All the margin properties can have the following padding-left
values:
All the padding properties can have the following
auto - the browser calculates the margin values:
length - specifies a margin in px, pt, cm, etc.
% - specifies a margin in % of the width of the length - specifies a padding in px, pt, cm, etc.
containing element % - specifies a padding in % of the width of the
inherit - specifies that the margin should be containing element
inherited from the parent element inherit - specifies that the padding should be
p { inherited from the parent element
margin-top: 100px;
margin-bottom: 100px;
Note: Negative values are not allowed.
margin-right: 150px;
margin-left: 80px; Padding - Shorthand Property
}
Margin - Shorthand Property To shorten the code, it is possible to specify all the
padding properties in one property.
margin-top
margin-right
padding-top
margin-bottom
padding-right
margin-left
padding-bottom
p { padding-left
margin: 25px 50px 75px 100px;
} If the padding property has four values:
If the margin property has three values: padding: 25px 50px 75px 100px;
o top padding is 25px
o right padding is 50px
margin: 25px 50px 75px;
o top margin is 25px o bottom padding is 75px
o right and left margins are 50px o left padding is 100px
o bottom margin is 75px
If the padding property has two values:
If the margin property has two values:
padding: 25px 50px;
o top and bottom paddings are 25px
margin: 25px 50px;
o top and bottom margins are 25px o right and left paddings are 50px
o right and left margins are 50px
If the padding property has one value:
Use the margin shorthand property with one value:
padding: 25px;
p {
o all four paddings are 25px
margin: 25px;
} Padding and Element Width
The CSS width property specifies the width of the
element's content area. The content area is the portion a:active {
inside the padding, border, and margin of an element background-color: hotpink;
(the box model). }
input[type=text]:focus {
background-color: lightblue;
}
input[type=text] { Header
background-color: white;
background-image: url('searchicon.png');
A header is usually located at the top of the website (or navigation or to specify information relevant to the
right below a top navigation menu). It often contains a main content. Change the widths as you like, only
logo or the website name: remember that it should add up to 100% in total:
.header { .column {
background-color: #F1F1F1; float: left;
text-align: center; }
padding: 20px;
}
/* Left and right column */
Navigation Bar .column.side {
width: 25%;
}
A navigation bar contains a list of links to help visitors
navigating through your website:
/* Middle column */
.column.middle {
/* The navbar container */
width: 50%;
.topnav {
overflow: hidden; }
background-color: #333;
} /* Responsive layout - makes the three
columns stack on top of each other
/* Navbar links */ instead of next to each other */
.topnav a { @media screen and (max-width: 600px) {
float: left; .column.side, .column.middle {
display: block; width: 100%;
color: #f2f2f2;
}
text-align: center;
padding: 14px 16px; }
text-decoration: none;
} Footer
/* Links - change color on hover */ The footer is placed at the bottom of your page. It
.topnav a:hover { often contains information like copyright and
background-color: #ddd; contact info:
color: black;
}
.footer {
Content background-color: #F1F1F1;
text-align: center;
The layout in this section, often depends on the target padding: 10px;
users. The most common layout is one (or combining }
them) of the following:
Unequal Columns