CSS Grade10 RefNotes
CSS Grade10 RefNotes
Reference Notes
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/XML 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.
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
cache. 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.
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.
stud1.dtd
<!ELEMENT students (firstname,lastname,email)>
<!ELEMENT firstname (#PCDATA)>
<!ELEMENT lastname (#PCDATA)>
<!ELEMENT email (#PCDATA)>
stud2.css
students
{
background-color: pink;
}
firstname,lastname,email
{
font-
size:35pts;
font-
style:italic;
display:block;
color: blue;
margin-left: 50px; }
Type of style sheet (Internal, External and Inline)
Each successive style sheet type adopts all the attributes from the previous style sheet type and
overrides any similar attribute. This allows designers to use a linked style sheet to control an
entire site, an embedded style sheet to control a single page and an inline style sheet to control a
single tag element.
The different types of style sheets are follow types of CSS
Example:
<!DOCTYPE html>
<html>
<head>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Linked style sheets exists as separate files that are linked to a page with <LINK> tag.
They have the css extension and are referenced with an URL.
Inside the css file, the style attributes are contained within opening and closing <STYLE> tags.
<LINK> tag within the <HEAD> tags. Attributes within the <LINK> tag include: the REL
attribute, set to stylesheet; an HREF attribute, whose value is the URL to the css file; and a TYPE
attribute, which is set to the MIME(Multipurpose Internet Mail Extensions) type, text/css. These
are the only attributes that are needed.
An external style sheet is used to define the style for many HTML pages.
With an external style sheet, you can change the look of an entire web site, by changing one file
Syntax of LINK tag used in HTML file:
<LINK REL=stylesheet HREF=”file.css” TYPE=”text/css”>
Example:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
Here is how the "styles.css" looks:
body {
background-color: lightblue;
}
h1 {
color: blue;
text-align: center;
}
p{
font-family: verdana;
font-size: 20px;
}
<!DOCTYPE html>
<html>
<head>
<title>HTML Inline CSS</title>
</head>
<body>
<p style = "color:red;">This is red</p>
<p style = "font-size:20px;">This is thick</p>
<p style = "color:green;">This is green</p>
<p style = "color:green;font-size:20px;">This is thick and green</p>
</body>
</html>
CSS Units
CSS has several different units for expressing a length. Many CSS properties take "length"
values, such as width, margin, padding, font-size, border-width, etc. Length is a number
followed by a length unit, such as 10px, 2em, etc.
A whitespace cannot appear between the number and the unit. However, if the value is 0, the unit
can be omitted. For some CSS properties, negative lengths are allowed.
There are two types of length units: relative and absolute.
Relative Lengths
Relative length units specify a length relative to another length property. Relative length units
scales better between different rendering mediums.
Unit Description
1. Em- Relative to the font-size of the element (2em means 2 times the size of the
current font)
2. ex - Relative to the x-height of the current font (rarely used)
3. ch - Relative to width of the "0" (zero)
4. rem - Relative to font-size of the root element
5. vw - Relative to 1% of the width of the viewport*
6. vh - Relative to 1% of the height of the viewport*
7. vmin - Relative to 1% of viewport's* smaller dimension
8. vmax - Relative to 1% of viewport's* larger dimension 9. %
Example:
p{
font-size: 16px;
line-height: 2em;
}
Absolute Lengths
The absolute length units are fixed and a length expressed in any of these will appear as
exactly that size.
Absolute length units are not recommended for use on screen, because screen sizes vary
so much. However, they can be used if the output medium is known, such as for print
layout.
Unit Description
1. cm - centimeters
2. mm - millimeters
3. in - inches (1in = 96px = 2.54cm)
4. px * - pixels (1px = 1/96th of 1in)
5. pt - points (1pt = 1/72 of 1in)
6. pc - picas (1pc = 12 pt)
Example:-
h2 {font-size: 10mm;}
h2 {font-size: 1cm;}
h1 {font-size: 0.5in;}
h2 {font-size: 30px;}
h2 {font-size: 25pt;}
h2 {font-size: 3pc;}
CSS Classes
It is possible to give an HTML element multiple looks with CSS
For Example, sometimes you want the font to be large and white , while other times you
prefer the font to be small and black.
CSS allows you to do the same thing by using of classes.
To create a class in CSS is simple. You need to add an extension to the typical CSS code and
make sure you specify this extension in our HTML.
CSS Coding:
p.one{color: blue}
p.two{color:
green}
HTML Coding:
<HTML>
<HEAD>
<LINK REL="stylesheet" HREF="class.css" TYPE="text/css">
</HEAD>
<BODY>
<P> THIS IS NORMAL PARAGRAPH </P>
<P class="one"> THIS IS PARAGRAPH WITH BLUE COLOR </P>
<P class="two"> THIS IS PARAGRAPH WITH GREEN COLOR </P>
</BODY>
</HTML>
In the above example the same <P> tag can display three different styles of text which is
only possible because of the class. The are useful for improving the functionality of the
HTML tags further.
ID Attributes:
The ID attribute is nothing but the properties which are assigned to any entity. The #id
selector allows you to target an element by referencing the id HTML attribute. ID attributes are
prefixed with an Hash (#), more commonly known as a “pound sign”.
Example:
HTML code:
<header id=”site-header”> </header>
CSS Code:
#header { /* this is the ID selector */
Background: green;}
ID attribute values should be unique i.e HTML does not allows two or more
identical ids and will produce unpredictable results. If there are two of the same, CSS will
still match and style both. Javascript however, when querying for an ID, will find the first
and stop.
High specifing and uniqueness mean using #id is a CSS. Avoiding the #id
selector in CSS is considered a best practice: it is preferable to use a class in nearly
every case.
ID attributes have several uses outside of CSS:
Providing unique hooks for Javascript.
Element with id attributes can be targeted by anchor tags, by setting the href
attribute to the id value, prefixed by a # symbol. Clicking that anchor link will
refocus the current page on the element with the matching id.
For truly unique elements in your HTML, such as form elements, IDs could be
useful for things like linking labels and inputs.
Point to remember
A valid #ID cannot start with a number.
A valid # ID must be at least one character long.
A large part of the Unicode are valid characters in an #ID
#ID attributes and selectors are case-sensitive.
#ID attributes must exactly match across HTML, CSS and Javascript.
CSS style properties are used for formatting characteristics of XML contents. Some of the
properties are self-explanatory and does not need any explanation regarding their usage.
CSS defines multiple style properties that are used to control fonts, color, alignment and
margins.
Following are the most commonly used style properties supported in CSS:
Font Properties:
The font properties are used to set the various parameters associated with the font
in which name of the font and its family, font-size, font-style and font-weight are used.
Font-family :property is used change the face of a font.
Font-style: property is used to set style of a font ; normal, italic, underline.
Font-size: property specifies the size of the font with one of the unit.
Font-weight: property set the weight of the font; bold, bolder or lighter.
Example:
<html>
<head>
<link rel=”stylesheet” href=file1.css” type=”text/css”>
</head>
<body>
<p style="font-family:georgia;"> This is 1 paragraph </p>
</body>
</html>
CSS Coding:
P{
font-family : georgia;
font-style : italic
font-weight : bold
font-size :20px }
Text Propertuies:
Color Property
The color property use for to change the color of text. It can accept the prefixed color or
RGB values.
Example:
P{ color: blue}
h1{color : rgb(255,0,255)}
Background Properties
to set backgrounds of various HTML elements. You can set the following background
properties of an element −
The background-color property is used to set the background color of an
element.
The background-image property is used to set the background image of an
element.
The background-repeat property is used to control the repetition of an
image in the background.
The background-position property is used to control the position of an
image in the background.
The background-attachment property is used to control the scrolling of an
image in the background.
The background property is used as a shorthand to specify a number of
other background properties.
Example:
background-color:yellow
background-color: #cccccc;
body {
background-image: “css.jpg";
background-repeat: repeat;
background-position:100px;
background-position:100px 200px;
background-attachment: fixed;
}
Display Property
The display property specifies the display behavior (the type of rendering box) of an
element. Common values for display ;inline, block, flex, grid etc
Example:
body {
display: inline;}
display: block;
<html>
<head>
</head>
<body>
<p style="width:400px; height:100px; border:1px solid red; padding:5px; margin:10px;"> This
paragraph is 400pixels wide and 100 pixels high
</p>
</body>
</html>
Margin Property
The margin property defines the space around an HTML element We
have the following properties to set an element margin.
The margin specifies a shorthand property for setting the margin properties in one
declaration.
The margin-bottom specifies the bottom margin of an element.
The margin-top specifies the top margin of an element.
The margin-left specifies the left margin of an element.
The margin-right specifies the right margin of an element.
Example:
P{ margin-top:3.0em;
margin- right:3.0em;
margin- bottom:5.0em;
margin- left:5.0em}