CSS1 1
CSS1 1
TH {color: red;}.
⚫The Selector indicates the element to which
the rule is applied.
⚫The Declaration determines the property values
of a selector.
Understanding Style Rules
⚫The Property specifies a characteristic, such as
color, font-family, position, and is followed by a
colon (:).
⚫The Value expresses specification of a property,
such as red for color, arial for font family, 12 pt
for font-size, and is followed by a semicolon (;).
Property Valu
P {color: red;} e
⚫ CSS declarations always ends with a semicolon, and
declaration groups are surrounded by curly brackets:
⚫ p {color:red;text-align:center;}To make the CSS more readable,
you can put one declaration on each line, like this:
⚫ Example
⚫p
{
color:red;
text-align:center;
}
CSS Comments
⚫ A CSS comment begins with "/*", and ends with "*/", like this:
⚫ /*This is a comment*/
{p
text-align:center;
/*This is another
comment*/
color:black;
font-family:arial;
}
⚫ The id and class Selectors
⚫The id Selector
⚫ The id selector is used to specify a style for a single, unique element.
⚫ The id selector uses the id attribute of the HTML element, and is defined with a
"#".
⚫ The style rule below will be applied to the element with id="para1":
⚫ Example
⚫ #para1
{
text-align:center;
color:red;
}
The class Selector
⚫ The class selector is used to specify a style for a group of elements. Unlike the id
selector, the class selector is most often used on several elements.
⚫ This allows you to set a particular style for any HTML elements with the same
class.
⚫ The class selector uses the HTML class attribute, and is defined with a "."
⚫ In the example below, all HTML elements with class="center" will be center-
aligned:
⚫ Example
⚫ .center
⚫{
⚫ text-align:center;
⚫}
An external style sheet is ideal when the style is applied to many pages. With an external
style sheet, you can change the look of an entire Web site by changing one file. Each page
must link to the style sheet using the <link> tag. The <link> tag goes inside the head
section:
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css" />
</head>
An external style sheet can be written in any text editor. The file should not contain any
html tags. Your style sheet should be saved with a .css extension. An example of a style
sheet file is shown below:
hr {color:red;}
p {margin-left:20px;}
body {background-image:url("images/back40.gif");}
Internal Style Sheet
<h2>CSS Buttons</h2>
<button>Default Button</button>
<a href="#" class="button">Link Button</a>
<button class="button">Button</button>
<input type="button" class="button" value="Input Button">
</body>
</html>
<!DOCTYPE html>
<html>
<head> CSS Box Sizing
<style>
.div1 {
width: 300px;
height: 100px;
border: 1px solid blue;
}
.div2 {
width: 300px;
height: 100px;
padding: 50px;
border: 1px solid red;
}
</style>
</head>
<body>
<h1>Without box-sizing</h1>
</body>
CSS Align Elements
<!DOCTYPE html>
<html>
<head>
<style>
.center {
margin: auto;
width: 60%;
border: 3px solid #73AD21;
padding: 10px;
}
</style>
</head>
<body>
<div class="center">
<p>Hello World!</p>
</div>
</body>
</html>
CSS Navigation Bars
CSS Navigation Bars
<!DOCTYPE html>
<html>
<head>
<style>
CSS Navigation Bars(Vertical)
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 60px;
}
li a {
display: block;
background-color: #dddddd;
}
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<p>A background color is added to the links to show the link
area.</p>
<p>Notice that the whole link area is clickable, not just the
text.</p>
</body>
</html>
<head>
<style>
ul {
CSS Navigation Bars(Horizontal)
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
li {
float: left;
}
li a {
display: block;
padding: 8px;
background-color: #dddddd;
}
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<p><b>Note:</b> If a !DOCTYPE is not specified, floating items can
produce unexpected results.</p>
<p>A background color is added to the links to show the link area. The
whole link area is clickable, not just the text.</p>
<p><b>Note:</b> overflow:hidden is added to the ul element to prevent
li elements from going outside of the list.</p>
</body>
<head>
<style>
.dropdown {
position: relative;
display: inline-block;
CSS Hoverable Dropdown
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
padding: 12px 16px;
z-index: 1;
}
.dropdown:hover .dropdown-content {
display: block;
}
</style>
</head>
<body>
<h2>Hoverable Dropdown</h2>
<p>Move the mouse over the text below to open the dropdown content.</p>
<div class="dropdown">
<span>Mouse over me</span>
<div class="dropdown-content">
<p>Hello World!</p>
</div>
</div>
</body>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
CSS Dropdown Menu
padding: 0;
overflow: hidden;
}
li {
float: left;
}
li a {
display: block;
padding: 8px;
background-color: #dddddd;
}
</style>
</head>
<body>
<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>
<p><b>Note:</b> If a !DOCTYPE is not specified, floating items can
produce unexpected results.</p>
<p>A background color is added to the links to show the link area. The
whole link area is clickable, not just the text.</p>
<p><b>Note:</b> overflow:hidden is added to the ul element to prevent
li elements from going outside of the list.</p>
</body>
<head>
<style>
.dropdown {
position: relative;
CSS Dropdown Image
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown:hover .dropdown-content {
display: block;
}
.desc {
padding: 15px;
text-align: center;
}
</style>
</head>
<body>
<h2>Dropdown Image</h2>
<p>Move the mouse over the image below to open the dropdown content.</p>
<div class="dropdown">
<img src="img_5terre.jpg" alt="Cinque Terre" width="100" height="50">
<div class="dropdown-content">
<img src="img_5terre.jpg" alt="Cinque Terre" width="300" height="200">
<div class="desc">Beautiful Cinque Terre</div>
</div>
</div>
</body>
CSS Counters
<head>
<style>
body {
counter-reset: section;
}
h2::before {
counter-increment: section;
content: "Section " counter(section) ": ";
}
</style>
</head>
<body>
<h2>HTML Tutorial</h2>
<h2>CSS Tutorial</h2>
<h2>JavaScript Tutorial</h2>
<h2>Python Tutorial</h2>
<h2>SQL Tutorial</h2>
</body>
CSS Opacity
<!DOCTYPE html>
<html>
<head>
<style>
img:hover {
opacity: 0.5;
}
</style>
</head>
<body>
<h1>Image Transparency</h1>
<p>The opacity property is often used together with the :hover
selector to change the opacity on mouse-over:</p>
<img src="img_forest.jpg" alt="Forest" width="170"
height="100">
<img src="img_mountains.jpg" alt="Mountains" width="170"
height="100">
<img src="img_5terre.jpg" alt="Italy" width="170" height="100">
</body>
</html>
CSS Positioning
CSS Positioning
CSS Positioning
CSS Positioning
CSS Task(Image Gallery)
CSS Task(Positioning)
CSS Task(Image Sprites)
CSS Task(Website Layout)