CSS Notes

What is CSS?
-------------------
1. CSS stands for Cascading Style Sheets
2. Styles define how to display HTML elements
3. Styles were added to HTML 4.0 to solve a problem
4. External Style Sheets can save a lot of work
5. External Style Sheets are stored in CSS files

CSS Syntax:
-----------------
h1 { color:blue; font-size:12px;}

h1 - is selector
color:blue; - is a declaration
color - is a property
blue - is a value
 font-size:12px; - is declaration
font-size - is a property
12px - is a value.

Example 1
----------------
<html>
<head>
<title>This is my First CSS Program</title>
<style>
body
{
background-color:#d0e4fe;
}
h1
{
color:orange;
text-align:center;
}
p
{
font-family:"Times New Roman";
font-size:20px;
}
</style>
</head>
<body>
<h1>Welcome To Aptech..</h1>
<p>Aptech Computer EducationAptech Computer EducationAptech Computer EducationAptech Computer EducationAptech Computer EducationAptech Computer EducationAptech Computer Education</p>

</body>
</html>


The id and class Selectors
=====================
In addition to setting a style for a HTML element, CSS allows you to specify your own selectors called "id" and "class".

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 "#".

Syntax:
----------
#para1
{
text-align:center;
color:red;
}

Example:
----------------
<html>
<head>
<style>
#para1{text-align:center;color:red;}
#para2{text-align:center;color:blue;}
</style>
</head>

<body>
<p id="para1">Hello World!</p>
<p id="para2">This paragraph is not affected by the style.</p>
</body>
</html>


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 many HTML elements with the same class.  The class selector uses the HTML class attribute, and is defined with a "."

Syntax:
--------------
.center {text-align:center;}

Example:
---------------
<head>
<style>
p.center{text-align:center;}
</style>
</head>

<body>
<h1 class="center">This heading will not be affected</h1>
<p class="center">This paragraph will be center-aligned.</p>
<p class="center">This is a new ParagraphThis is a new ParagraphThis is a new ParagraphThis is a new ParagraphThis is a new ParagraphThis is a new ParagraphThis is a new ParagraphThis is a new Paragraph</p>
</body>
</html>



Three Ways to Insert CSS
=====================
There are three ways of inserting a style sheet:

1. External style sheet
2. Internal style sheet
3. Inline style

External Style Sheet:
--------------------------
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.

Syntax:
------------
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>


Example:
--------------------
mystyle.css
----------------
hr {color:red;}
p {margin-left:20px;}
body {background-image:url("images/3.jpg");}

external.html
---------------------
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
Welcome To Aptech..
<hr>
<p>Happy Independence Day..Happy Independence Day..Happy Independence Day..Happy Independence Day..Happy Independence Day..Happy Independence Day..Happy Independence Day..Happy Independence Day..Happy Independence Day..Happy Independence Day..Happy Independence Day..Happy Independence Day..</p>
</body>
</html>


Internal Style Sheet
=================
An internal style sheet should be used when a single document has a unique style. You define internal styles in the head section of an HTML page, by using the <style> tag, like this.

Example:
--------------
<html>
<head>
<title>Internal Styles</title>
<style>
hr {color:blue;}
p {margin-left:20px;}
body {background-image:url("images/5.jpg");}
</style>
</head>
<body>
Aptech Computer Education
<hr>
<p>Welcome To Aptech..Welcome To Aptech..Welcome To Aptech..Welcome To Aptech..Welcome To Aptech..Welcome To Aptech..Welcome To Aptech..Welcome To Aptech..Welcome To Aptech..Welcome To Aptech..Welcome To Aptech..Welcome To Aptech..Welcome To Aptech..Welcome To Aptech..Welcome To Aptech..</p>
</body>
</html>

Inline Styles
===============
An inline style loses many of the advantages of style sheets by mixing content with presentation.

To use inline styles you use the style attribute in 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 paragraph.

Syntax:
----------
<p style="color:green;margin-left:20px">This is a paragraph.</p>

Example:
----------------------
<html>
<head>
<title>Inline Styles</title>
</head>
<body>
<p style="color:sienna;margin-left:20px">This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph. This is a paragraph.  </p>

<p style="color:red;margin-left:30px">Aptech Computer Education.  Aptech Computer Education.  Aptech Computer Education.  Aptech Computer Education.  Aptech Computer Education.  Aptech Computer Education.  Aptech Computer Education.  Aptech Computer Education.  Aptech Computer Education.  Aptech Computer Education.
</p>
</body>
</html>



Background Effects
================
1. background-color
2. background-image
3. background-repeat
4. background-attachment
5. background-position

 background-color
------------------------------

<head>
<style>
body
{
background-color:#b0c4de;
}
</style>
</head>


---------------------------------------------------------
<head>
<style>
h1
{
background-color:#6495ed;
}
p
{
background-color:#e0ffff;
}
div
{
background-color:#b0c4de;
}
</style>
</head>


 background-image
------------------------------------
<head>
<style>
body {background-image:url('paper.gif');}
</style>
</head>

background-repeat
------------------------------------
<html>
<head>
<style>
body
{
background-image:url('gradient2.png');
background-repeat:repeat-y; or background-repeat:repeat-x;
x - is horizontal
y - is vertical
}
</style>
</head>

<body>
<h1>Hello World!</h1>
</body>

</html>


Background Image should not Repeat
-----------------------------------------------
<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>

 background-position
--------------------------------
<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 no-repeat, 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>


4. background-attachment
------------------------------------
<html>
<head>
<style>
body
{
background-image:url('smiley.gif');
background-repeat:no-repeat;
background-attachment:fixed;
}
</style>
</head>

<body>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
<p>The background-image is fixed. Try to scroll down the page.</p>
</body>

</html>

No comments:

Post a Comment

PHP Notes

The Characteristics of PHP:- ----------------------------------- 1.PHP is a high level programming language. 2.It is a server-side scrip...