What is CSS?
CSS stands for Cascading Style Sheets
CSS is used to describe the look and formatting of a document written in markup language. It provides an additional feature to HTML. It is generally used with HTML to change the style of web pages and user interfaces. It can also be used with any kind of XML documents including plain XML, SVG and XUL. CSS is used along with HTML and JavaScript in most websites to create user interfaces for web applications and user interfaces for many mobile applications.CSS is easy to learn and understand but it provides powerful control over the presentation of an HTML document. Most commonly, CSS is combined with the markup languages HTML or XHTML.
- Styles define how to display HTML elements
- Styles are normally stored in Style Sheets
- Styles were added to HTML 4.0 to solve a problem
- External Style Sheets can save you a lot of work
- External Style Sheets are stored in CSS files
- Multiple style definitions will cascade into one
CSS Example
body
{
background-color:white;
}
h1 { color:seagreen; text-align:left; }
p { font-family:"Arial"; font-size:15px; }
h1 { color:seagreen; text-align:left; }
p { font-family:"Arial"; font-size:15px; }
Styles Solve a Common Problem
HTML tags were originally designed to define the content of a document. They were
supposed to say "This is a header", "This is a paragraph", "This is a table", by using tags
like <h1 >
, <p >
, <table >
, and so on. The layout of the document was supposed to be taken
care of by the browser, without using any formatting tags.
As the two major browsers - Netscape and Internet Explorer - continued to add new
HTML tags and attributes (like the
<font <
tag and the color attribute) to the original
HTML specification, it became more and more difficult to create Web sites where the
content of HTML documents was clearly separated from the document's presentation
layout.
Multiple Styles Will Cascade Into One
Style sheets allow style information to be specified in many ways. Styles can be specified
inside a single HTML element, inside the <head> element of an HTML page, or in an
external CSS file. Even multiple external style sheets can be referenced inside a single
HTML document.
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 four has the highest priority:
1. Browser default
2. External style sheet
3. Internal style sheet (inside the <head> tag)
4. Inline style (inside an HTML element)
So, an inline style (inside an HTML element) has the highest priority, which means that it
will override a style declared inside the <head> tag, in an external style sheet, or in a
browser (a default value).
Grouping
You can group selectors. Separate each selector with a comma. In the example below we
have grouped all the header elements. All header elements will be displayed in blue text
color. You can grouping any tag of multiple like div, p, apply any element.in this example you can see only header.
h1,h2,h3,h4,h5,h6
{
color: blue;
}
Say that you would like to have two types of paragraphs in your document: one rightaligned paragraph, and one center-aligned paragraph. Here is how you can do it with styles:
p.right {
text-align: right
}
p.center {
text-align: center
}
You have to use the class attribute in your HTML document:
Note: To apply more than one class per given element, the syntax is:
Multiple Styles Will Cascade Into One
Style sheets allow style information to be specified in many ways. Styles can be specified
inside a single HTML element, inside the <head> element of an HTML page, or in an
external CSS file. Even multiple external style sheets can be referenced inside a single
HTML document.
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 four has the highest priority:
1. Browser default
2. External style sheet
3. Internal style sheet (inside the <head> tag)
4. Inline style (inside an HTML element)
So, an inline style (inside an HTML element) has the highest priority, which means that it
will override a style declared inside the <head> tag, in an external style sheet, or in a
browser (a default value).
Grouping
You can group selectors. Separate each selector with a comma. In the example below we
have grouped all the header elements. All header elements will be displayed in blue text
color. You can grouping any tag of multiple like div, p, apply any element.in this example you can see only header.
h1,h2,h3,h4,h5,h6
{
color: blue;
}
The class Selector
With the class selector you can define different styles for the same type of HTML element.Say that you would like to have two types of paragraphs in your document: one rightaligned paragraph, and one center-aligned paragraph. Here is how you can do it with styles:
p.right {
text-align: right
}
p.center {
text-align: center
}
You have to use the class attribute in your HTML document:
<p class="right">
This paragraph will be right-aligned.
</p>
<p class="center">
This paragraph will be center-aligned.
</p>
This paragraph will be right-aligned.
</p>
<p class="center">
This paragraph will be center-aligned.
</p>
Note: To apply more than one class per given element, the syntax is:
<p class="center bold">
This is a paragraph.
</p>
This is a paragraph.
</p>
The paragraph above will be styled by the class "center" AND the class "bold". You can also omit the tag name in the selector to define a style that will be used by all HTML elements that have a certain class. In the example below, all HTML elements with class="center" will be center-aligned:
.center {
text-align: center
}
In the code below both the h1 element and the p element have class="center". This means that both elements will follow the rules in the ".center" selector:
<h1 class="center">
This heading will be center-aligned
</h1>
<p class="center">
This paragraph will also be center-aligned.
</p>
Note-Do NOT start a class name with a number! It will not work in Mozilla/Firefox.
Add Styles to Elements with Particular Attributes
You can also apply styles to HTML elements with particular attributes.The style rule below will match all input elements that have a type attribute with a value
of "text":
input[type="text"] {
background-color: blue
}
The id Selector
You can also define styles for HTML elements with the id selector. The id selector isdefined as a #.
The style rule below will match the element that has an id attribute with a value of
"green":
#green {color: green}
The style rule below will match the p element that has an id with a value of "para1":
p#para1
{
text-align: center;
color: gray;
}
Note : Do NOT start an ID name with a number! It will not work in Mozilla/Firefox.
CSS Comments
Comments are used to explain your code, and may help you when you edit the sourcecode at a later date. A comment will be ignored by browsers. A CSS comment begins with
"/*", and ends with "*/", like this:
/* This is a commented line */
p
{
text-align: center;
/* This is another comment line margin:3px*/
color: blue;
font-family: arial;
}
In thi Commented line and assign margin:3px css style ignored by browser because this line is commented
0 comments :
Post a Comment