0% found this document useful (0 votes)
2 views

Unit- II CSS.pptx

CSS (Cascading Style Sheets) is used to control the display of HTML elements across various media, allowing for efficient styling of multiple web pages. It includes syntax for defining styles, various selectors, and methods for inserting styles such as external, internal, and inline styles. Additionally, CSS provides properties for font styling, colors, text alignment, and list formatting, among others.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit- II CSS.pptx

CSS (Cascading Style Sheets) is used to control the display of HTML elements across various media, allowing for efficient styling of multiple web pages. It includes syntax for defining styles, various selectors, and methods for inserting styles such as external, internal, and inline styles. Additionally, CSS provides properties for font styling, colors, text alignment, and list formatting, among others.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

CSS

Definition
• CSS stands for Cascading Style Sheets
• CSS describes how HTML elements are to be displayed on screen,
paper, or in other media
• CSS saves a lot of work. It can control the layout of multiple web
pages all at once
• External stylesheets are stored in CSS files
Syntax for CSS
A CSS rule-set consists of a selector and a declaration block:

The selector points to the HTML element you want to style.


The declaration block contains one or more declarations separated by semicolons.
Each declaration includes a CSS property name and a value, separated by a colon.
A CSS declaration always ends with a semicolon, and declaration blocks are surrounded by
curly braces.
p {
Example color: red;
In this example all <p> elements will be text-align: center;
center-aligned, with a red text color: }
CSS Selectors
• CSS selectors are used to "find" (or select) HTML elements based on their element name, id, class,
attribute, and more.
The element Selector
• The element selector selects elements based on the element name.
Example
• You can select all <p> elements on a page like this (here, all <p> elements will be center-aligned,
with a red text color):
p{
text-align: center;
color: red;
}
Three Ways to Insert CSS
There are three ways of inserting a style sheet:
• External style sheet
• Internal style sheet
• Inline style
External Style Sheet
With an external style sheet, you can change the look of an entire website by changing just one file!
Each page must include a reference to the external style sheet file inside the <link> element.
Example
External styles are defined within the <link> element, inside the <head> section of an HTML page:

<head>
<link rel="stylesheet" type="text/css" href="my
style.css">
</head>
An external style sheet can be written in any text editor. The file should not contain any html tags.
The style sheet file must be saved with a .css extension.
Here is how the "mystyle.css" file looks like:
“mystyle.css“
body {
background-color: lightblue;
}

h1 {
color: navy;
margin-left: 20px;
}
Internal Style Sheet
An internal style sheet may be used if one single page has a unique style.
Example
Internal styles are defined within the <style> element, inside the <head> section of an HTML page:
<head>
<style>
body {
background-color: linen;
}
h1 {
color: maroon;
margin-left: 40px;
}
</style>
</head>
Inline Styles
• An inline style may be used to apply a unique style for a single element.
• To use inline styles, add the style attribute to the relevant element. The style attribute can contain
any CSS property.
Example
Inline styles are defined within the "style" attribute of the relevant element:
<h1 style="color:blue;margin-left:30px;">This is a heading</h1>
CSS - Font
CSS Font Families
In CSS, there are two types of font family names:
generic family - a group of font families with a similar look (like "Serif" or "Monospace")
font family - a specific font family (like "Times New Roman" or "Arial")

Generic family Font family Description


Serif Times New Roman Serif fonts have small lines at the ends
Georgia on some characters
Sans-serif Arial "Sans" means without - these fonts do
Verdana not have the lines at the ends of
characters
Monospace Courier New All monospace characters have the
Lucida Console same width
Font Family
The font family of a text is set with the font-family property.
The font-family property should hold several font names as a "fallback" system. If the browser does not
support the first font, it tries the next font, and so on.
Start with the font you want, and end with a generic family, to let the browser pick a similar font in the
generic family, if no other fonts are available.
More than one font family is specified in a comma-separated list:
p{
font-family: "Times New Roman", Times, serif;
}
Font Style
The font-style property is mostly used to specify italic text.

This property has three values:

normal - The text is shown normally


italic - The text is shown in italics
oblique - The text is "leaning" (oblique is very similar to italic, but less supported)
Example: Font Style
p.normal {
font-style: normal;
}
p.italic {
font-style: italic;
}
p.oblique {
font-style: oblique;
}
Font Size
The font-size property sets the size of the text.
Being able to manage the text size is important in web design. However, you should not use font size
adjustments to make paragraphs look like headings, or headings look like paragraphs.
Always use the proper HTML tags, like <h1> - <h6> for headings and <p> for paragraphs.
The font-size value can be an absolute, or relative size.
Absolute size:
Sets the text to a specified size
Does not allow a user to change the text size in all browsers (bad for accessibility reasons)
Absolute size is useful when the physical size of the output is known
Relative size:
Sets the size relative to surrounding elements
Allows a user to change the text size in browsers
Set Font Size With Pixels
• Setting the text size with pixels gives you full control over the text
size:
h1 {
font-size: 40px;
}

h2 {
font-size: 30px;
}

p{
font-size: 14px;
}
Set Font Size With Em
To allow users to resize the text (in the browser menu), many developers use em instead of pixels.
The em size unit is recommended by the W3C.
1em is equal to the current font size. The default text size in browsers is 16px. So, the default size of 1em is
16px.
The size can be calculated from pixels to em using this formula: pixels/16=em
h1 {
font-size: 2.5em; /* 40px/16=2.5em */
}

h2 {
font-size: 1.875em; /* 30px/16=1.875em */
}

p{
font-size: 0.875em; /* 14px/16=0.875em */
}
Font Weight
The font-weight property specifies the weight of a font:
Example
p.normal {
font-weight: normal;
}
p.thick {
font-weight: bold;
}
Font Variant
The font-variant property specifies whether or not a text should be displayed in a small-caps font.
In a small-caps font, all lowercase letters are converted to uppercase letters. However, the converted uppercase letters appears in a
smaller font size than the original uppercase letters in the text.
Example
p.normal {
font-variant: normal;
}
p.small {
font-variant: small-caps; }
Font-Colors
Background Color:
You can set the background color for HTML elements:
Example
<h1 style="background-color:DodgerBlue;">Hello World</h1>
<p style="background-color:Tomato;">Lorem ipsum...</p>
Text Color
Example:
<h1 style="color:Tomato;">Hello World</h1>
<p style="color:DodgerBlue;">Lorem ipsum...</p>
<p style="color:MediumSeaGreen;">Ut wisi enim...</p>
Border Color
Example
<h1 style="border:2px solid Tomato;">Hello World</h1>
<h1 style="border:2px solid DodgerBlue;">Hello World</h1>
<h1 style="border:2px solid Violet;">Hello World</h1>
Font-Colors
Color Values:
Example
<h1 style="background-color:rgb(255, 99, 71);">...</h1>
<h1 style="background-color:#ff6347;">...</h1>
<h1 style="background-color:hsl(9, 100%, 64%);">...</h1>

<h1 style="background-color:rgba(255, 99, 71, 0.5);">...</h1>


<h1 style="background-color:hsla(9, 100%, 64%, 0.5);">...</h1>
RGB Value
In HTML, a color can be specified as an RGB value, using this formula:

rgb(red, green, blue)


Each parameter (red, green, and blue) defines the intensity of the color between 0 and 255.
For example, rgb(255, 0, 0) is displayed as red, because red is set to its highest value (255) and the others are set to 0.
To display the color black, all color parameters must be set to 0, like this: rgb(0, 0, 0).
To display the color white, all color parameters must be set to 255, like this: rgb(255, 255, 255).
Experiment by mixing the RGB values below:
HEX Value
In HTML, a color can be specified using a hexadecimal value in the form:
#rrggbb
Where rr (red), gg (green) and bb (blue) are hexadecimal values between 00 and ff (same as decimal 0-255).
For example, #ff0000 is displayed as red, because red is set to its highest value (ff) and the others are set to the
lowest value (00).
HSL Value
In HTML, a color can be specified using hue,
saturation, and lightness (HSL) in the form:
hsl(hue, saturation, lightness)
Saturation
Hue is a degree on the color wheel from 0 to 360. 0 is
red, 120 is green, and 240 is blue. Saturation can be described as the intensity of a color.
Saturation is a percentage value, 0% means a shade of 100% is pure color, no shades of gray
gray, and 100% is the full color. 50% is 50% gray, but you can still see the color.
0% is completely gray, you can no longer see the color.
Lightness is also a percentage, 0% is black, 50% is
neither light or dark, 100% is white
Lightness
The lightness of a color can be described as how much light you want to give the color, where 0%
means no light (black), 50% means 50% light (neither dark nor light) 100% means full lightness (white).
RGBA Value
RGBA color values are an extension of RGB color values with an alpha channel - which specifies the opacity
for a color.
An RGBA color value is specified with:
rgba(red, green, blue, alpha)
The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (not transparent at all):
HSLA Value
HSLA color values are an extension of HSL color values with an alpha channel - which specifies the opacity
for a color.
An HSLA color value is specified with:
hsla(hue, saturation, lightness, alpha)
The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (not transparent at all):
CSS Text
Text Color
The color property is used to set the color of the text. The color is specified by:
a color name - like "red"
a HEX value - like "#ff0000"
an RGB value - like "rgb(255,0,0)“
The default text color for a page is defined in the body selector.
Example
body {
color: blue;
}

h1 {
color: green;
}
Text Alignment
The text-align property is used to set the horizontal alignment of a text.
A text can be left or right aligned, centered, or justified.
The following example shows center aligned, and left and right aligned text (left alignment is default if text
direction is left-to-right, and right alignment is default if text direction is right-to-left):
Example:
h1 {
When the text-align property is set to "justify", each line is
text-align: center; stretched so that every line has equal width, and the left and
} right margins are straight (like in magazines and newspapers):

Example
h2 {
text-align: left; div {
text-align: justify;
} }

h3 {
text-align: right;
}
Text Decoration Text Transformation
The text-decoration property is used to set or remove decorations
The text-transform property is used to specify uppercase and
from text. lowercase letters in a text.
The value text-decoration: none; is often used to remove underlines It can be used to turn everything into uppercase or lowercase
from links: letters, or capitalize the first letter of each word:
Example Example
a{ p.uppercase {
text-decoration: none; text-transform: uppercase;
} }
The other text-decoration values are used to decorate text:
p.lowercase {
Example
text-transform: lowercase;
h1 { }
text-decoration: overline;
} p.capitalize {
text-transform: capitalize;
h2 { }
text-decoration: line-through;
}

h3 {
text-decoration: underline;
}
Text Indentation Line Height
The text-indent property is used to specify the indentation of The line-height property is used to specify the space between lines:
the first line of a text: Example
p.small {
Example line-height: 0.8;
p { text-indent: 50px;} }
Letter Spacing
p.big {
The letter-spacing property is used to specify the space line-height: 1.8;
between the characters in a text. }
The following example demonstrates how to increase or Text Direction
decrease the space between characters: The direction property is used to change the text direction of an element:
p{
Example
direction: rtl;}
h1 { letter-spacing: 3px;} Word Spacing
h2 { letter-spacing: -3px;} The word-spacing property is used to specify the space between the words
in a text.
Text Shadow The following example demonstrates how to increase or decrease the
The text-shadow property adds shadow to text. space between words:
Example
The following example specifies the position of the horizontal h1 {
shadow (3px), the position of the vertical shadow (2px) and
word-spacing: 10px;
the color of the shadow (red):
}
h1 { h2 {
text-shadow: 3px 2px red;} word-spacing: -5px;
}
CSS Lists
HTML Lists and CSS List Properties
Example

In HTML, there are two main types of lists: ul.a {


unordered lists (<ul>) - the list items are marked with bullets list-style-type: circle;
ordered lists (<ol>) - the list items are marked with numbers }
or letters
The CSS list properties allow you to: ul.b {
Set different list item markers for ordered lists list-style-type: square;
Set different list item markers for unordered lists }
Set an image as the list item marker
ol.c {
Add background colors to lists and list items list-style-type: upper-roman;
Different List Item Markers }
The list-style-type property specifies the type of list item
marker. ol.d {
list-style-type: lower-alpha;
The following example shows some of the available list item }
markers:
An Image as The List Item Marker
The list-style-image property specifies an image as the list item
Example
marker: ul.a { list-style-position: outside;}
Example ul.b { list-style-position: inside;}
ul {
list-style-image: url('sqpurple.gif'); Remove Default Settings
} The list-style-type:none property can also be
Position The List Item Markers used to remove the markers/bullets. Note that
The list-style-position property specifies the position of the list-item the list also has default margin and padding. To
markers (bullet points). remove this, add margin:0 and padding:0 to <ul>
"list-style-position: outside;" means that the bullet points will be or <ol>:
outside the list item. The start of each line of a list item will be
aligned vertically. This is default: Example
Coffee - A brewed drink prepared from roasted coffee beans...
ul {
Tea
list-style-type: none;
Coca-cola margin: 0;
"list-style-position: inside;" means that the bullet points will be padding: 0;
inside the list item. As it is part of the list item, it will be part of the
text and push the text at the start: }
Coffee - A brewed drink prepared from roasted coffee beans...
Tea
Coca-cola
List - Shorthand property Example
ol {
The list-style property is a shorthand property. It is used to set all
background: #ff9999;
the list properties in one declaration:
padding: 20px;
Example }
ul { list-style: square inside url("sqpurple.gif");}
ul {
When using the shorthand property, the order of the property values background: #3399ff;
are: padding: 20px;
list-style-type (if a list-style-image is specified, the value of this }
property will be displayed if the image for some reason cannot be
displayed) ol li {
background: #ffe5e5;
list-style-position (specifies whether the list-item markers should padding: 5px;
appear inside or outside the content flow) margin-left: 35px;
list-style-image (specifies an image as the list item marker) }

Styling List With Colors ul li {


We can also style lists with colors, to make them look a little more background: #cce5ff;
interesting. margin: 5px;
}
Anything added to the <ol> or <ul> tag, affects the entire list, while
properties added to the <li> tag will affect the individual list items:
The CSS Box Model
All HTML elements can be considered as boxes. In CSS, the term "box model" is used when talking
about design and layout.
The CSS box model is essentially a box that wraps around every HTML element. It consists of: margins,
borders, padding, and the actual content. The image below illustrates the box model:
Explanation of the different parts:

Content - The content of the box, where


text and images appear
Padding - Clears an area around the
content. The padding is transparent
Border - A border that goes around the
padding and content
Margin - Clears an area outside the
border. The margin is transparent
The box model allows us to add a border
Example div {
around elements, and to define space
width: 300px;
between elements.
border: 15px solid green;
padding: 50px;
margin: 20px;
}
Width and Height of an Element
In order to set the width and height of an element correctly in all browsers, you need to know how the box model works.
Example
This <div> element will have a total width of 350px:
div {
width: 320px;
padding: 10px;
border: 5px solid gray;
margin: 0; }
Here is the calculation:
320px (width)
+ 20px (left + right padding)
+ 10px (left + right border)
+ 0px (left + right margin)
= 350px
The total width of an element should be calculated like this:
Total element width = width + left padding + right padding + left border + right border + left margin + right margin
The total height of an element should be calculated like this:
Total element height = height + top padding + bottom padding + top border + bottom border + top margin + bottom
margin

You might also like