Basics of CSS For Web
Basics of CSS For Web
The <!DOCTYPE> declaration must be the very first thing in your HTML document,
before the <html> tag.
In HTML 4.01, the <!DOCTYPE> declaration refers to a DTD, because HTML 4.01
was based on SGML. The DTD specifies the rules for the markup language, so that
the browsers render the content correctly.
HTML5 is not based on SGML, and therefore does not require a reference to a DTD.
Tip: Always add the <!DOCTYPE> declaration to your HTML documents, so that the
browser knows what type of document to expect.
• http://www.w3schools.com/tags/tag_doctype.asp
2) http://www.go4expert.com/articles/html4-vs-html5-comparison-
t30141/
CSS is used to control the style of a web document in a simple and easy
way.
CSS is a simple design language intended to simplify the process of making web
pages presentable. CSS handles the look and feel part of a web page.
Using CSS, you can control the color of the text, the style of fonts, the spacing
between paragraphs, how columns are sized and laid out, what background
images or colors are used, layout designs, variations in display for different
devices and screen sizes as well as a variety of other effects.
Advantages of CSS:
CSS saves time − You can write CSS once and then reuse same sheet in multiple
HTML pages. You can define a style for each HTML element and apply it to as many
Web pages as you want.
Pages load faster − If you are using CSS, you do not need to write HTML tag
attributes every time. Just write one CSS rule of a tag and apply it to all the
occurrences of that tag. So less code means faster download times.
Easy maintenance − To make a global change, simply change the style, and all
elements in all the web pages will be updated automatically.
Superior styles to HTML − CSS has a much wider array of attributes than HTML, so
you can give a far better look to your HTML page in comparison to HTML attributes.
Multiple Device Compatibility − Style sheets allow content to be optimized for more
than one type of device. By using the same HTML document, different versions of a
website can be presented for handheld devices such as PDAs and cell phones or for
printing.
Global web standards − Now HTML attributes are being deprecated and it is being
recommended to use CSS. So its a good idea to start using CSS in all the HTML pages
to make them compatible to future browsers.
Offline Browsing − CSS can store web applications locally with the help of an offline
catche.Using of this, we can view offline websites.The cache also ensures faster loading
and better overall performance of the website.
Platform Independence − The Script offer consistent platform independence and can
support latest browsers as well.
CSS-Syntax
A CSS comprises of style rules that are interpreted by the browser and then
applied to the corresponding elements in your document. A style rule is
made of three parts −
Selector − A selector is an HTML tag at which a style will be applied. This could
be any tag like <h1> or <table> etc.
Property - A property is a type of attribute of HTML tag. Put simply, all the
HTML attributes are converted into CSS properties. They could be
color, border etc.
Value - Values are assigned to properties. For example, color property can have
value either red or #F1F1F1 etc.
Here table is a selector and border is a property and given value 1px solid
#C00 is the value of that property.
Ways of defining selectors
h1{
color: #36CFFF;
* { color: #000000; }
This rule renders the content of every element in our document in black.
ul em { color: #000000; }
This rule renders the content in black for every element with class attribute
set to black in our document.
You can apply more than one class selectors to given element.
</p>
5) The ID Selectors
You can define style rules based on the id attribute of the elements. All the
elements having that id will be formatted according to the defined rule.
This rule renders the content in black for every element with id attribute set
to black in our document.
This rule renders the content in black for only <h1> elements
with id attribute set to black.
The true power of id selectors is when they are used as the foundation for
descendant selectors, For example:
In this example all level 2 headings will be displayed in black color when
those headings will lie within tags having id attribute set to black.
The advantage to this method is that the <input type = "submit" />
element is unaffected, and the color applied only to the desired text fields.
p[lang="fr"] - Selects all paragraph elements whose lang attribute has a value
of exactly "fr".
h1 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
Here all the property and value pairs are separated by a semi colon (;).
You can keep them in a single line or multiple lines. For better readability
we keep them into separate lines.
For a while, don't bother about the properties mentioned in the above
block. These properties will be explained in the coming chapters and you
can find complete detail about properties in CSS References.
Grouping Selectors
You can apply a style to many selectors if you like. Just separate the
selectors with a comma, as given in the following example −
h1, h2, h3 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
This define style rule will be applicable to h1, h2 and h3 element as well.
The order of the list is irrelevant. All the elements in the selector will have
the corresponding declarations applied to them.
You can combine the various class selectors together as shown below −
position: absolute;
left: 510px;
width: 200px;}
CSS-Inclusion
There are four ways to associate styles with your HTML document.
Embedded CSS
Inline CSS
External CSS
Imported CSS
Most commonly used methods are inline CSS and External CSS.
<!DOCTYPE html>
<html>
<head>
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>
Attributes
Attributes associated with <style> elements are −
Attribute Value Description
Media Screen
tty
tv
projection
Specifies the device the document will be displayed on.
handheld
Default value is all. This is an optional attribute.
print
braille
aural
all
Attributes
Attribute Value Description
Example
Following is the example of inline CSS based on the above syntax −
</body>
</html>
<head>
<link type = "text/css" href = "..." media = "..." />
</head>
Attributes
Attributes associated with <style> elements are −
Href URL Specifies the style sheet file having Style rules. This
attribute is a required.
Media screen
tty
tv
projection
Specifies the device the document will be displayed on.
handheld
Default value is all. This is optional attribute.
print
braille
aural
all
Consider a simple style sheet file with a name mystyle.css having the following
rules −
h1, h2, h3 {
color: #36C;
font-weight: normal;
letter-spacing: .4em;
margin-bottom: 1em;
text-transform: lowercase;
}
Now you can include this file mystyle.css in any HTML document as follows
−
<head>
<link type = "text/css" href = "mystyle.css" media = " all" />
</head>
4) Imported CSS - @import Rule
@import is used to import an external stylesheet in a manner similar to the
<link> element. Here is the generic syntax of @import rule.
<head>
<@import "URL";
</head>
Here URL is the URL of the style sheet file having style rules.
Example
Following is the example showing you how to import a style sheet file into
HTML document −
<head>
@import "mystyle.css";
</head>
Any rule defined in <style>...</style> tags will override rules defined in any
external style sheet file.
Any rule defined in external style sheet file takes lowest priority, and rules
defined in this file will be applied only when above two rules are not applicable.
<style type="text/css">
<!--
body, td {
color: blue;
}
-->
</style>
CSS Comments
Many times, you may need to put additional comments in your style sheet
blocks. So, it is very easy to comment any part in style sheet. You can
simple put your comments inside /*.....this is a comment in style
sheet.....*/.
You can use /* ....*/ to comment multi-line blocks in similar way you do in
C and C++ programming languages.
Example
p { color: red; /* This is a comment */
text-align: center;}
CSS - Background
You can set the following background properties of an element −
The background-color property is used to set the background color of an element.
<p style = "background-color: yellow;">
<style>
body {
background-image: url("/css/images/css.jpg");
background-color: #cccccc;
background-repeat: repeat;
background-attachment:scroll;
</style>
CSS-Font
You can set following font properties of an element −
The font-family property is used to change the face of a font.
<p style="font-family:georgia,garamond,serif;">
This text is rendered in either georgia, garamond, or the default serif font depending
on which font you have at your system.</p>
<p style="font-style:italic;">
<body>
</body>
<body>
</body>
<body>
</p>
</body>
<p style="direction:rtl;"> This text will be renedered from right to left </p>
The word-spacing property is used to add or subtract space between the words
of a sentence.
<p style="text-indent:1cm;"> This text will have first line indented by 1cm and this
line will remain at its actual position this is done by CSS text-indent property.</p>
<p style="text-transform:capitalize;">
The white-space property is used to control the flow and formatting of text.
Possible values are normal, pre, nowrap.
<p style="white-space:pre;">
The text-shadow property is used to set the text shadow around a text.
<p style="text-shadow:4px 4px 8px blue;"> If your browser supports the CSS text-shadow
property, this text will have a blue shadow. </p>