Cascading Style Sheets
Cascading Style Sheets
• Add a reference to the external .cssfile right after <title> tag in the <head> section of HTML sheet:
• <link rel="stylesheet" type="text/css" href="style.css" />
Style Specification Formats in CSS
• In CSS, the style specification format typically involves selectors, properties, and
values.
• A selector is used to target HTML elements to which you want to apply the styles.
You can use selectors to target specific elements such as class or ID, or you can
target multiple elements at once.
• A property is a style attribute that you want to apply to the targeted element. There
are many properties in CSS, such as color, font-size, background-color, and border.
• A value is the specific setting you want to apply to the property. For example, if you
want to set the color of text to red, the property would be "color" and the value
would be "red".
How to target an element and assigned value to
property.
• * Target all paragraphs with the class "intro" */
p.intro {
/* Set the font size to 16 pixels */
font-size: 16px;
/* Set the line height to 1.5 */
line-height: 1.5;
/* Set the text color to dark gray */
color: #333;
/* Set the background color to light gray */
background-color: #f2f2f2;
}
CSS Selector
• CSS selectors are used to select the content you want to style. Selectors are
the part of CSS rule set.
• CSS selectors select HTML elements according to its id, class, type,
attribute etc.
• There are several different types of selectors in CSS.
1.CSS Element Selector
2.CSS Id Selector
3.CSS Class Selector
4.CSS Universal Selector
5.CSS Group Selector
CSS Element Selector
The element selector selects the HTML element by name.
• CSS Id Selector
(1)The id selector selects the id attribute of an HTML element to select a specific element.
(2) An id is always unique within the page so it is chosen to select a single, unique element.
(3)It is written with the hash character (#), followed by the id of the element.
CSS Class Selector
The class selector selects HTML elements with a specific class attribute. It is used with a period character . (full stop symbol)
followed by the class name.
CSS Universal Selector
The universal selector is used as a wildcard character. It selects all the elements
on the pages.
CSS Group Selector
The grouping selector is used to select all the elements with the same style
definitions.
Grouping selector is used to minimize the code. Commas are used to separate
each selector in grouping.
Property Value Forms
Fonts
Lists
Alignment of text
Margins
Colors
Backgrounds
Borders
Property Values (values of properties)
The property value can appear in many forms.
• Keywords – large, medium, small, …
• Number values – integers, decimal numbers etc.
• Length - numbers, maybe with decimal points followed by two character abbreviation of a unit name.
• Unit-
px - pixels
in - inches
cm - centimeters
mm - millimeters
pt - points
pc - picas (12 points)
em – value of the current font size in pixels
ex - height of the letter ‘x’
Note-No space is allowed between the number and the unit specification e.g., 1.5 in is illegal!
Eg: 10px, 24pt etc.
• Percentage - just a number followed immediately by a percent sign. Eg: 70%
• URL values: url(protocol://server/pathname)
Font-Properties
The font-family property specifies the font for an element.
If a font name has more than one word, it should be quoted. ‘Times New Roman‘
Font-Size
• Sets the size of fonts.There are two categories of font-size values, absolute and relative.
• In the case of absolute category the size value could be given as length value in points, picas or pixels or
keywords from the list xx-small, x-small, small, medium, large and x-large.Eg: font-size: 10pt
• The relative size values are smaller and larger, which adjust the font size relative to the font size of the
parent element.Eg: font-size:1.2em
• This sets the font size to 1.2 times the font size of the parent element.
CONT!!
• Font Weight:
The font-weight property sets how thick and thin characters in text should be displayed.Example: font-
weight: normal
• Font-style:
Most commonly used to specify italic. Eg: font-style: italic/ Bold/Normal
• Font-Size-adjust:
The font-size-adjust property gives you better control of the font size .
Example :font-size-adjust: number|none|initial|inherit;
List properties
• list-style-type can applied to both ordered and unordered list.
• The list-style property is a shorthand for the following properties:
list-style-type
list-style-position
list-style-image.
.
Syntax:
(1)list-style-image: url('donald.gif’)
</body>
</html>