Unit 2
Unit 2
Unit 2
What is CSS?
• 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
� CSS SYNTAX:
Input Output
mystyle.css
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
CSS Colors
CSS Color property is used to set the color of HTML elements. This property is used to set font
color, background color, etc. The color of an element can be defined in the following ways:
1. Built-In Color
2. RGB Format
3. RGBA Format
4. Hexadecimal Notation
5. HSL
6. HSLA
1. Built-In Color: These are a set of predefined colors which are used by its name. For
example: red, blue, green etc.
Syntax:
h1 { color: color-name; }
2. RGB Format: The RGB(Red, Green, Blue) format is used to define the color of an HTML element by specifying the R, G, B values range
between 0 to 255. For example: RGB value of Red color is (255, 0, 0), Green color is (0, 255, 0), Blue color is (0, 0, 255) etc.
Syntax:
h1 { color: rgb(R, G, B); }
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <title>CSS color property</title>
5. <style>
6. h1 {
7. color: rgb(0, 153, 0);
8. text-align: center;
9. }
10. </style>
11. </head>
12. <body>
13. <h1>
14. GL BAJAJ INSTITUTE OF TECH. AND MANAGMNT.
15. </h1>
16. </body>
17. </html>
3. RGBA Format: The RGBA format is similar to the RGB, but the difference is RGBA contains A
(Alpha) which specifies the transparency of elements. The value of alpha lies between 0.0 to 1.0 where
0.0. represents fully transparent and 1.0 represents not transparent.
4. Hexadecimal Notation: The hexadecimal notation begins with # symbol followed by 6 characters
each ranging from 0 to F. For example: Red #FF0000, Green #00FF00, Blue #0000FF etc.
Syntax:
h1 { color:#(0-F)(0-F)(0-F)(0-F)(0-F)(0-F); }
5. HSL: HSL stands for Hue, Saturation, and Lightness respectively. This format uses the cylindrical coordinate system.
• Hue: Hue is the degree of the color wheel. Its value lies between 0 to 360 where 0 represents red, 120 represents green and 240 represents blue color.
• Saturation: It takes a percentage value, where 100% represents completely saturated, while 0% represents completely unsaturated (gray).
• Lightness: It takes percentage value, where 100% represents white, while 0% represents black.
6. HSLA:
The HSLA color property is similar to HSL property, but the difference is HSLA contains A (Alpha)
which specifies the transparency of elements. The value of alpha lies between 0.0 to 1.0 where 0.0.
represents fully transparent and 1.0 represents not transparent.
Border Color: It is used to set the border color of an element. Border-style is used to set the
border type.
Syntax:
h1 { border-style:solid/dashed/dotted
border-color:color_name; }
� List of Colors: Following is the list of a few CSS colors.
Black 000000 0, 0, 0
� Opacity / Transparency
CSS Border Style
� CSS Fonts
In CSS there are five generic font families:
1. Serif fonts have a small stroke at the edges of each letter. They create a sense of formality and elegance.
2. Sans-serif fonts have clean lines (no small strokes attached). They create a modern and minimalistic look.
3. Monospace fonts - here all the letters have the same fixed width. They create a mechanical look.
Sans-serif Arial
Verdana
Helvetica
Fantasy Copperplate
Papyrus
� CSS Box Model
� 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: content, padding, borders and margins. 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
� When setting the style for several link states, there are some order rules:
• a:hover MUST come after a:link and a:visited
• a:active MUST come after a:hover
ADVANCE CSS
CSS Navigation Bar
� Navigation Bar = List of Links
� A navigation bar needs standard HTML as a base.
� A navigation bar is basically a list of links, so using
the <ul> and <li> elements makes perfect sense:
Horizontal Navigation Bar
� There are two ways to create a horizontal navigation bar. Using inline or floating list items.
Output:
Floating List Items
� Another way of creating a horizontal navigation bar is to float the <li>
elements, and specify a layout for the navigation links:
CSS Dropdowns
CSS Website Layout
� A website is often divided into headers, menus, content and a footer:
Header
� A header is usually located at the top of the website (or right below a top
navigation menu). It often contains a logo or the website name:
Content
The layout in this section, often depends on the target users. The most common layout is one (or combining them) of the
following:
[attribute~=value] [title~="flower"] Selects all elements with a title attribute that contains a
space-separated list of words, one of which is "flower"
[attribute|=value] [lang|="en"] Selects all elements with a lang attribute value starting
with "en"
[attribute^=value] a[href^="https"] Selects all <a> elements with a href attribute value
starting with "https"
[attribute$=value] a[href$=".pdf"] Selects all <a> elements with a href attribute value
ending with ".pdf"
[attribute*=value] a[href*=“GLBAJAJ"] Selects all <a> elements with a href attribute value
containing the substring “GLBAJAJ"
.
position: static;
HTML elements are positioned static by default.
Static positioned elements are not affected by the top, bottom, left, and right
properties.
An element with position: static; is not positioned in any special way; it is
always positioned according to the normal flow of the page:
This <div> element has position: static;
position: relative;
An element with position: relative; is positioned relative to its normal position.
Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be
adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by
the element.
position: fixed;
An element with position: fixed; is positioned relative to the viewport, which means it always stays in
the same place even if the page is scrolled. The top, right, bottom, and left properties are used to
position the element.
A fixed element does not leave a gap in the page where it would normally have been located.
position: absolute;
An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of
positioned relative to the viewport, like fixed).
However; if an absolute positioned element has no positioned ancestors, it uses the document body,
and moves along with page scrolling.