Css Notes
Css Notes
-- This is a comment
-->
<html>
<head>
<style>
/*class selector example in css*/
.colorchange{ background-color:#FFFF33 }
.large{font-size:24px}
</style>
</head>
<body class="colorchange">
<p class="large">hello world
</body>
What is CSS?
CSS Syntax
A CSS rule set consists of a selector and a declaration block:
This is a paragraph.
M2: OSI Model, TCP/IP Model, Networking Devices & Peer-Peer Networking
M3: Introduction to Ethical Hacking & its Phases
M4: IP Spoofing, MAC Spoofing, Anonmizer, TOR & Finding IP Addresses
M5: Information Gathering & Foot printing
M6: Advanced Google Techniques & Google Dorks
M7: Proxy Server & VPN
M8: Scanning Networks, Ping Sweeps, OS Finger Printing & Banner Grabbing
M9: System Security( Linux & Windows System Security)
M10: E-Mail Ethical Hacking, Secure Email, Email Tracking & Email Bombing
M11: Stopping Phishing attacks, Security of Facebook, Yahoo & Gmail Accounts
M12: Virus & worms
M13: Trojan, Backdoor, Spyware, Adware & Botnets
M14: Steganography (Hiding Images, text & data)
M15: Cryptograpghy & Keyloggers
M16: Sniffers, Exe Binders & Crypters
M17: Social Engineering
M18: DOS & DDOS attacks using Backtrack Linux 5
M19: Wireless Security (WEP, WPA & WPA2), Brute Force Attack & Its
preventation
M20: Firewall & Honeypots
M21: IDS & IPS
M22: SQL Injection & Shell Uploading
M23: XSS (Cross Site Scripting)
M24: Directory Traversal Attack, RFI & LFI
M25: Introduction to Android Ethical Security & Mobile Phone Ethical Security
Address
Plot
No
:-
5,
Lalkothi
Apex
Scheme,
Indrapuri,
Near
SMS
Lane,
Stadium,
Behind
APEX-DIVYA
Tonk
MALL,
Road
Telephone
:-
Email
[email protected]@linuxsoft.co.in
CSS Example
A CSS declaration always ends with a semicolon, and declaration groups are surrounded
by curly braces:
p {color:red;text-align:center;}
To make the CSS code more readable, you can put one declaration on each line.
In the following example all <p> elements will be center-aligned, with a red text color:
:-
<html>
<head>
<style>
p{
color: red;
text-align: center;
}
</style>
</head>
<body>
<p>Hello World!</p>
<p>This paragraph is styled with CSS.</p>
</body>
</html>
Output:
Hello World!
This paragraph is styled with CSS.
CSS Comments
Comments are used to explain your code, and may help you when you edit the source
code at a later date. Comments are ignored by browsers.
A CSS comment starts with /* and ends with */. Comments can also span multiple lines:
<html>
<head>
<style>
p{
color: red;
/* This is a single-line comment */
text-align: center;
}
/* This is
a multi-line
comment */
</style>
</head>
<body>
<p>Hello World!</p>
<p>This paragraph is styled with CSS.</p>
<p>CSS comments are not shown in the output.</p>
</body>
</html>
Output:
Hello World!
This paragraph is styled with CSS.
CSS comments are not shown in the output.
CSS Selectors
CSS Selectors
CSS selectors allow you to select and manipulate HTML elements.
CSS selectors are used to "find" (or select) HTML elements based on their id, class, type,
attribute, and more.
Example
p{
text-align: center;
color: red;
}
The id Selector
The id selector uses the id attribute of an HTML element to select a specific element.
An id should be unique within a page, so the id selector is used if you want to select a
single, unique element.
To select an element with a specific id, write a hash character, followed by the id of the
element.
The style rule below will be applied to the HTML element with id="para1":
Example
#para1 {
text-align: center;
color: red;
}
Do NOT start an ID name with a number!
Example
.center {
text-align: center;
color: red;
}
You can also specify that only specific HTML elements should be affected by a class.
In the example below, all <p> elements with class="center" will be center-aligned:
Example
p.center {
text-align: center;
color: red;
}
Do NOT start a class name with a number!
Grouping Selectors
If you have elements with the same style definitions, like this:
h1 {
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;
}
p{
text-align: center;
color: red;
}
Example
h1, h2, p {
text-align: center;
color: red;
}
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: red;
}
</style>
</head>
<body>
Hello World!
This paragraph is not affected by the
style.
text-align: center;
color: red;
aligned.
}
</style>
</head>
<body>
<h1 class="center">This heading will
not be affected</h1>
<p class="center">This paragraph will
be red and center-aligned.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
h1, h2, p {
text-align: center;
color: red;
}
</style>
</head>
<body>
Hello World!
Smaller heading!
This is a paragraph.
<h1>Hello World!</h1>
<h2>Smaller heading!</h2>
<p>This is a paragraph.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1 class="center">Red and centeraligned heading</h1>
<p class="center">Red and centeraligned paragraph.</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
p{
text-align: center;
color: red;
}
</style>
</head>
<body>
Inline style
<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. The style sheet file must be saved with a .css extension. An example of a style
sheet file called "myStyle.css", is shown below:
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
Do not add a space between the property value and the unit (such as
margin-left: 20 px;). The correct way is: margin-left: 20px;
Example
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<!DOCTYPE html>
<html>
This is a heading
<head>
This is a paragraph.
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Inline Styles
An inline style may be used to apply a unique style for a single element.
An inline style loses many of the advantages of a style sheet (by mixing content with
presentation). Use this method sparingly!
To use inline styles, add the style attribute to the relevant tag. The style attribute can
contain any CSS property. The example shows how to change the color and the left
margin of a <h1> element:
Example
<h1 style="color:blue;margin-left:30px;">This is a heading.</h1>
<!DOCTYPE html>
This is a heading.
<html>
This is a paragraph.
<body>
<h1 style="color:blue;margin-left:30px;">This
is a heading.</h1>
<p>This is a paragraph.</p>
</body>
</html>
then, assume that an internal style sheet also has the following property for the <h1>
element:
h1 {
color: orange;
}
If the page with the internal style sheet also links to the external style sheet the
properties for the <h1> element will be:
color: orange;
margin-left: 20px;
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css"
href="mystyle.css">
<style>
h1 {
color: orange;
}
</style>
</head>
<body>
This is a heading
The style of this document is a
combination of an external stylesheet, and
internal style
<h1>This is a heading</h1>
<p>The style of this document is a
combination of an external stylesheet, and
internal style</p>
</body>
</html>
The left margin is inherited from the external style sheet and the color is replaced by the
internal style sheet.
Cascading order
What style will be used when there is more than one style specified for an HTML element?
Generally speaking we can say that all the styles will "cascade" into a new "virtual" style
sheet by the following rules, where number three has the highest priority:
1. Browser default
2. External and internal style sheets (in the head section)
3. Inline style (inside an HTML element)
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css"
href="mystyle.css">
<style>
body {background-color: linen;}
</style>
</head>
<body style="background-color: lightcyan">
<h1>Multiple Styles Will Cascade into
One</h1>
<p>In this example, the background color is
set inline, in an internal stylesheet, and in an
external stylesheet.</p>
<p>Try experimenting by removing styles to
see how the cascading stylesheets work. (try
removing the inline first, then the internal, then
the external)</p>
</body>
</html>
So, an inline style (inside an HTML element) has the highest priority, which means that it
will override a style defined inside the <head> tag, or in an external style sheet, or in a
browser (a default value).
Note: If the link to the external style sheet is placed below the internal
style sheet in HTML <head>, the external style sheet will override the
internal style sheet!
CSS Background
background-color
background-image
background-repeat
background-attachment
background-position
Background Color
The background-color property specifies the background color of an element.
The background color of a page is set like this:
Example
body {
background-color: #b0c4de;
}
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: #b0c4de;
}
</style>
</head>
<body>
<h1>My CSS web page!</h1>
<p>Hello world! This is a
W3Schools.com example.</p>
</body>
</html>
Look at CSS Color Values for a complete list of possible color values.
In the example below, the <h1>, <p>, and <div> elements have different background
colors:
Example
h1 {
background-color: #6495ed;
}
p{
background-color: #e0ffff;
}
div {
background-color: #b0c4de;
}
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
background-color: #6495ed;
}
p{
background-color: #e0ffff;
}
div {
background-color: #b0c4de;
}
</style>
</head>
<body>
</body>
</html>
Background Image
The background-image property specifies an image to use as the background of an
element.
By default, the image is repeated so it covers the entire element.
The background image for a page can be set like this:
Example
body {
background-image: url("paper.gif");
}
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("paper.gif");
}
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Below is an example of a bad combination of text and background image. The text is
almost not readable:
Example
body {
background-image: url("bgdesert.jpg");
}
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image: url("bgdesert.jpg");
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>This text is not easy to read on this
background image.</p>
</body>
</html>
Example
body {
background-image: url("gradient_bg.png");
}
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image:
url("gradient_bg.png");
}
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
If the image is repeated only horizontally (repeat-x), the background will look better:
Example
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image:
url("gradient_bg.png");
background-repeat: repeat-x;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Example
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
}
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image:
url("img_tree.png");
background-repeat: no-repeat;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>W3Schools background image
example.</p>
<p>The background image is only
showing once, but it is disturbing the
reader!</p>
</body>
</html>
In the example above, the background image is shown in the same place as the text. We
want to change the position of the image, so that it does not disturb the text too much.
The position of the image is specified by the background-position property:
Example
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-image:
url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
margin-right: 200px;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>W3Schools background norepeat, set position example.</p>
<p>Now the background image is
only shown once, and positioned
away from the text.</p>
<p>In this example we have also
added a margin on the right side, so
the background image will never
disturb the text.</p>
</body>
</html>
Example
body {
background: #ffffffurl("img_tree.png") no-repeat right top;
}
<!DOCTYPE html>
<html>
<head>
<style>
body {
background:
#ffffffurl("img_tree.png") no-repeat
right top;
margin-right: 200px;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<p>Now the background image is
only shown once, and it is also
positioned away from the text.</p>
<p>In this example we have also
added a margin on the right side, so
that the background image will not
disturb the text.</p>
</body>
</html>
When using the shorthand property the order of the property values is:
background-color
background-image
background-repeat
background-attachment
background-position
It does not matter if one of the property values is missing, as long as the ones that are
present are in this order.
This example uses more advanced CSS. Take a look: Advanced example