Cascading Style Sheet (CSS) : The Type Selectors
Cascading Style Sheet (CSS) : The Type Selectors
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.
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.
You can apply more than one class selectors to given element. Consider the following example:
<p class="center bold">This para will be styled by the classes center and bold.</p>
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.
#black { color: #000000; }
CSS Inclusion
Embedded CSS - The <style> Element (Internal CSS)
You can put your CSS rules into an HTML document using the <style> element. This tag is placed inside
<head>...</head> tags. Rules defined using this syntax will be applied to all the elements available in
the document.
<!DOCTYPE html>
<html>
<head>
<style type = "text/css" media = "all">
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>
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.
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.....*/.
<!DOCTYPE html>
<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>
</body>
</html>
CSS Colors
CSS uses color values to specify a color. Typically, these are used to set a color either for the foreground of an
element (i.e., its text) or else for the background of the element. They can also be used to affect the color of
borders and other decorative effects.
CSS - Background
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.
CSS Fonts
The font-family property is used to change the face of a font.
The font-style property is used to make a font italic or oblique.
The font-variant property is used to create a small-caps effect.
The font-weight property is used to increase or decrease how bold or light a font appears.
The font-size property is used to increase or decrease the size of a font.
The font property is used as shorthand to specify a number of other font properties.
CSS - Text
The color property is used to set the color of a text.
The direction property is used to set the text direction.
The letter-spacing property is used to add or subtract space between the letters that make up a word.
The word-spacing property is used to add or subtract space between the words of a sentence.
The text-indent property is used to indent the text of a paragraph.
The text-align property is used to align the text of a document.
The text-decoration property is used to underline, overline, and strikethrough text.
The text-transform property is used to capitalize text or convert text to uppercase or lowercase
letters.
The white-space property is used to control the flow and formatting of text.
The text-shadow property is used to set the text shadow around a text.
<p style="letter-spacing:5px;">
CSS images
1. The border property is used to set the width of an image border.
<img style="border:0px;" src="/css/images/logo.png" />
<img style="border:3px dashed red;" src="/css/images/logo.png" />
<style type="text/css">
a:link {color: #000000}
a:visited {color: #006600}
a:hover {color: #FFCC00}
a:active {color: #FF00CC}
</style>
CSS-Tables
The border-collapse specifies whether the browser should control the appearance of the adjacent
borders that touch each other or whether each cell should maintain its style.
<html>
<head>
<style type="text/css">
table.one {border-collapse:collapse;}
table.two {border-collapse:separate;}
td.a {
border-style:dotted;
border-width:3px;
border-color:#000000;
padding: 10px;
}
td.b {
border-style:solid;
border-width:3px;
border-color:#333333;
padding:10px;
}
</style>
</head>
<body>
<table class="one">
<caption>Collapse Border Example</caption>
<tr><td class="a"> Cell A Collapse Example</td></tr>
<tr><td class="b"> Cell B Collapse Example</td></tr>
</table>
<br />
<table class="two">
<caption>Separate Border Example</caption>
<tr><td class="a"> Cell A Separate Example</td></tr>
<tr><td class="b"> Cell B Separate Example</td></tr>
</table>
</body>
</html>
The border-spacing specifies the width that should appear between table cells.
<html>
<head>
<style type="text/css">
table.one {
border-collapse:separate;
width:400px;
border-spacing:10px;
}
table.two {
border-collapse:separate;
width:400px;
border-spacing:10px 50px;
}
</style>
</head>
<body>
<table class="one" border="1">
<caption>Separate Border Example with border-
spacing</caption>
<tr><td> Cell A Collapse Example</td></tr>
<tr><td> Cell B Collapse Example</td></tr>
</table>
<br />
</body>
</html>
The caption-side captions are presented in the <caption> element. By default, these are rendered
above the table in the document. You use the caption-side property to control the placement of the
table caption.
<html>
<head>
<style type="text/css">
caption.top {caption-side:top}
caption.bottom {caption-side:bottom}
caption.left {caption-side:left}
caption.right {caption-side:right}
</style>
</head>
<body>
<table style="width:400px; border:1px solid black;">
<caption class="top">
This caption will appear at the top
</caption>
<tr><td > Cell A</td></tr>
<tr><td > Cell B</td></tr>
</table>
<br />
CSS Borders
The border-color specifies the color of a border.
<style type="text/css">
p.example1{
border:1px solid;
border-bottom-color:#009900; /* Green */
border-top-color:#FF0000; /* Red */
border-left-color:#330000; /* Black */
border-right-color:#0000CC; /* Blue */
}
p.example2{
border:1px solid;
border-color:#009900; /* Green */
}
</style>
The border-style specifies whether a border should be solid, dashed line, double line, or one of the
other possible values.
The border-style Property
o none: No border. (Equivalent of border-width:0;)
o solid: Border is a single solid line.
o dotted: Border is a series of dots.
o dashed: Border is a series of short lines.
o double: Border is two solid lines.
o groove: Border looks as though it is carved into the page.
o ridge: Border looks the opposite of groove.
o inset: Border makes the box look like it is embedded in the page.
o outset: Border makes the box look like it is coming out of the canvas.
o hidden: Same as none, except in terms of border-conflict resolution for table elements.
CSS 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.
CSS Padding
The padding-bottom specifies the bottom padding of an element.
The padding-top specifies the top padding of an element.
The padding-left specifies the left padding of an element.
The padding-right specifies the right padding of an element.
The padding serves as shorthand for the preceding properties.