1 of 121

Cascading Style Sheets

2 of 121

Introduction

  • CSS stands for Cascading Style Sheets
  • Style sheet language used to describe the presentation of

a document written in HTML or XML

  • how HTML elements are to be displayed on screen, paper,

or in other media

  • Colors, fonts, alignment, borders, backgrounds, spacing, margins, etc…
  • It can control the layout of multiple web pages all at once
  • Advantage
    • Reusability
    • Separate the content and presentation

3 of 121

What is CSS?

  • A simple mechanism for controlling the style of a Web document without compromising its structure.

  • It allows you to separate the visual design elements (layout, fonts, colors, margins, and so on) from the contents of a Web page.

  • Allows for faster downloads, streamlined site maintenance, and global control of design attributes across multiple pages.

3

4 of 121

CSS vs. just HTML

  • What can we do with CSS that we can’t do with HTML?
      • Control of backgrounds.
      • Set font size to the exact height you want.
      • Highlight words, entire paragraphs, headings or even individual letters with background colors.
      • Overlap words and make logo-type headers without making images.
      • Precise positioning.
      • Linked style sheets to control the look of a whole website from one single location.
      • and more….

4

5 of 121

How to write CSS?

  • CSS rule-set consists of a selector and a declaration block

  • Selector
    • HTML element tags �(examples: p, h2, body, img, table)
    • class and ID names
  • Property (examples: color, font-size)
  • Value (examples: red, 14pt)

5

6 of 121

Basic Structure of a Style

  • Each definition contains:
    • A property
    • A colon
    • A value
    • A semicolon to separate two or more values
    • Can include one or more values

  • h1 {font-size:12pt; color:red}

7 of 121

HTML & CSS Code

  • To set red color as the background color of a webpage

HTML

<body bgcolor="#FF0000">

CSS

body {background-color: #FF0000;}

8 of 121

Example:

p {

color: red;

text-align: center;

}

body {�    background-color: lightblue;� }

9 of 121

Applying CSS / Types

  • Three ways u can apply CSS to a HTML Document
  • Inline
  • Internal / Embedded
  • External
  • Inline - Using the style attribute in HTML elements
  • Embedded (Internal) - Using a <style> element in the <head> section
  • External - Using an external CSS file

10 of 121

Types - Inline Style sheet

  • Styles are placed inside tags
  • Specific to a single instance of an html tag on a page.
  • Must be used instead of <font> tags to specify font size, color, and typeface and to define margins, etc
  • Use to override an external or embedded style specification
  • used to apply a unique style for a single element

10

11 of 121

Types – Internal / Embedded CSS

  • Applicable to an entire document
  • Styles are defined within the <style> </style> tag
  • placed in the header of the html file <head> and </head>
  • Styles are written in the same page
  • used if one single page has a unique style

11

<html>

<head>

<style>

p {

color: red;

text-align: center;

}

</style>

</head>

<body> <p>hai </p> <p> hello></p> </body></html>

12 of 121

Types – External CSS

  • Styles are saved in a separate file, with the extension .css
  • This single style sheet can be used to define the look of multiple pages
  • Call the .css file from header of html file

  • Rel – relationship between the current doc and

other document

12

13 of 121

Types – External CSS - Example

13

<html>

<head>

<link href="mystyle.css“ rel="stylesheet" type="text/css" >

</head>

<body>

<h1>This is a heading</h1>

<p>This is a paragraph.</p>

</body>

</html>

mystyle.css

body 

{�    background-color: lightblue;�}�h1 {�    color: navy;�    margin-left: 20px;�}

14 of 121

Types – Inline / Embedded / External

Inline CSS

Embedded CSS

15 of 121

Types – Inline / Embedded / External

.CSS file

External CSS

16 of 121

Applying a single style sheet to multiple documents

17 of 121

Advantages of External Style Sheet

  • Saves Time
  • can change the look of an entire website by changing just one file

18 of 121

CSS Selectors

  • CSS selectors are used to "find" (or select) HTML elements based on their element name, id, class, attribute, and more
  • Types
    • Simple selectors (select elements based on name, id, class)
      • Element name selector
      • Element id selector
      • Element class selector
      • Universal selector
      • Grouping Selectors
    • Pseudo-class selectors (select elements based on a certain state)
      • :hover

19 of 121

CSS Simple Selectors

20 of 121

CSS Selectors - Example

21 of 121

CSS Class Selectors - Example

22 of 121

CSS Universal Selector - Example

23 of 121

CSS pseudo classes

  • A pseudo-class is used to define a special state of an element
  • Example
    • Style an element when a user mouses over it
    • Style visited and unvisited links differently
    • Style an element when it gets focus
  • Pseudo-class names are not case-sensitive
  • Types:
    • :hover
    • :visited
    • :active
    • :link
  • To be effective:
    • a:hover MUST come after a:link and a:visited in the CSS definition
    • a:active MUST come after a:hover in the CSS

24 of 121

CSS pseudo classes - example

25 of 121

CSS other pseudoclasses

26 of 121

CSS other pseudoclasses

27 of 121

CSS other pseudoclasses

28 of 121

CSS other pseudo classes - Example

29 of 121

CSS Inheritance: - which style prevails when several are present?

  • Inline (local) overrides internal & external styles

  • Internal style sheet overrides external styles

30 of 121

Cascading Priority

  • The way styles will be used when there is more than one style specified for an HTML element:�
  • Browser default
  • External Style Sheet (Linked) (in an external .css file)
  • Internal Style Sheet (embedded) (inside the <head> tag)
  • Inline Style (Local) (inside HTML element)

  • An inline style (inside an HTML element) has the highest priority, which means that it will override every style declared inside the <head> tag, in an external style sheet, and in the browser (default value).

31 of 121

CSS PROPERTIES

 

 

32 of 121

CSS Properties

  • Properties
    • Color
    • Background
    • Border
    • Font
    • Margin …….

33 of 121

Color Property

  • Color - set the font color

Color: value;

  • Examples
    • color:blue;
    • color: “#44ffaa”;
    • color:rgb(0,255,0);

34 of 121

Color Property

  • value of color can be given as below:
    • a valid color name - like "red"
    • an RGB value - like "rgb(255, 0, 0)"
    • a HEX value - like "#ff0000

  • Note:
  • hexadecimal values between 00 and FF 
  •  Each parameter rgb(red, green, blue) defines the intensity of the color between 0 and 255..

35 of 121

Color Property - Example

<h2 style=color: “green">

text displayed in green

</h2>

36 of 121

Background property

  • The background property is a shorthand property for:
    • background-color
    • background-image
    • background-position
    • background-size
    • background-repeat
    • background-origin
    • background-clip
    • background-attachment

https://www.w3schools.com/cssref/tryit.asp?filename=trycss_background-position

37 of 121

Background property

38 of 121

Background property

  • background-color
    • background-color: red;

  • background-image
    •  background-image: url(“image.jpg");
    • By default: placed at the top-left corner of an element, and repeated both vertically and horizontally.

39 of 121

Background property

  • background-position
    • sets the starting position of a background image
    • background-position: value;

40 of 121

Background property

  • background-size: auto;

  • background-repeat
  • background-repeat: repeat-y;

background-size

background-repeat

41 of 121

Background repeat examples:

41

42 of 121

Background property

  • background-origin
    • background-origin: content-box;

  • background-clip
    • background-clip: padding-box;
    • defines how far the background (color or image) should extend within an element.

43 of 121

Background property

  • background-attachment
    • background-attachment: fixed;
    • sets whether a background image scrolls with the rest of the page, or is fixed

44 of 121

Background-shorthand Property

  • It does not matter if one of the property values is

missing, as long as the other ones are in this order

45 of 121

Background – Example1

46 of 121

Background – Example2

47 of 121

CSS – Element Dimensions

  • In addition to positioning elements, CSS rules can specify the actual dimensions of each page element.
  • Overflow: Scroll, auto, hidden, visible

48 of 121

Width and height

  • height and width properties are used to set the height and width of an element

  • <div> above occurs when the browser window is smaller than the width of the element (500px)

  • browser then adds a horizontal scrollbar to the page

49 of 121

CSS - Height and width Example

50 of 121

CSS - Height and width Example

51 of 121

NOTE- width and height

  • problem with the <div> in the above example

  • when the browser window is smaller than the width of the element (500px).

  • The browser then adds a horizontal scrollbar to the page.

52 of 121

CSS Box Model

  • All HTML elements can be considered as boxes
  • CSS box model is essentially a box that wraps around every HTML element.
  • It consists of
    • 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

53 of 121

BOX MODEL

54 of 121

BOX MODEL - Example

55 of 121

BOX MODEL - Example

56 of 121

BORDER PROPERTY

Shorthand property

border: 2px solid pink

border-left::2px solid pink

border-right:2px solid pink

border-bottom:2px solid pink

border-top:2px solid pink

border-width:2px

border-style:solid

border-color:pink

border-left

border-left-width

border-left-color

border-left-style

border-right

border-right-width

border-right-color

border-right-style

border-bottom:

border-bottom-width

border-bottom-color

border-bottom-style

border-top

border-top-width

border-top-color

border-top-style

border: border-width border-style border-color|initial|inherit;

57 of 121

border

Sets all the border properties in one declaration

border-bottom

Sets all the bottom border properties in one declaration

border-bottom-color

Sets the color of the bottom border

border-bottom-style

Sets the style of the bottom border

border-bottom-width

Sets the width of the bottom border

border-color

Sets the color of the four borders

border-left

Sets all the left border properties in one declaration

border-left-color

Sets the color of the left border

border-left-style

Sets the style of the left border

border-left-width

Sets the width of the left border

border-radius

Sets all the four border-*-radius properties for rounded corners

border-right

Sets all the right border properties in one declaration

border-right-color

Sets the color of the right border

border-right-style

Sets the style of the right border

border-right-width

Sets the width of the right border

border-style

Sets the style of the four borders

border-top

Sets all the top border properties in one declaration

border-top-color

Sets the color of the top border

border-top-style

Sets the style of the top border

border-top-width

Sets the width of the top border

border-width

Sets the width of the four borders

58 of 121

CSS Border Style

59 of 121

CSS Border Style

60 of 121

CSS Border Width

<style>

p.one { border-style: solid; border-width: 5px; }

p.two { border-style: solid; border-width: medium; }

p.three { border-style: dotted; border-width: 2px; }

p.four { border-style: dotted; border-width: thick; }

p.five { border-style: double; border-width: 15px; }

p.six { border-style: double; border-width: thick; }

</style>

<p class="one">Some text.</p>

<p class="two">Some text.</p>

<p class="three">Some text.</p>

<p class="four">Some text.</p>

<p class="five">Some text.</p>

<p class="six">Some text.</p>

61 of 121

CSS Border-color

p.one {�  border-style: solid;�  border-color: red;�}��p.two {�  border-style: solid;�  border-color: green;�}��p.three {�  border-style: dotted;�  border-color: blue;�}

62 of 121

Style, color, width, top-style, left-style - Shorthands

63 of 121

MARGIN PROPERTY

64 of 121

CSS- Box Margin - Example

65 of 121

PADDING PROPERTY

Padding properties are used to generate space around contents[inside border]

Shorthand property

padding: 2px ;

padding-left::2px ;

padding-right:2px ;

padding-bottom:2px;

padding-top:2px ;

66 of 121

CSS- Box-Padding Example

67 of 121

max-height

Sets the maximum height of an element

max-width

Sets the maximum width of an element

min-height

Sets the minimum height of an element

min-width

Sets the minimum width of an element

  • will improve the browser's handling of small windows

MAX-width, height, Min - width, height properties

68 of 121

Min-height and max-height Example

69 of 121

CSS-OUTLINE Property

Outline is a line that is drawn around elements, OUTSIDE the border.

70 of 121

CSS-OUTLINE Property

outline-color

Specifies the color of the outline

outline-style

Specifies the style of the outline

outline-width

Specifies the width of outline

Shorthand Property

Outline

width color style

p {

border: 10px solid red;

outline:40px green solid ;

padding:20px;

margin:100px }

71 of 121

CSS Text Property

  • color:rgb(10,0,255)
  • text-align :center|left|right|justify
  • text-transform: capitalize| lowercase|uppercase|none
  • text-decoration: none|underline|overline|line-through
  • text-indent: length[px ,em]or %
  • line-height: normal|length
  • letter-spacing: normal|length(px)
  • word-spacing: normal|length(px)
  • direction: ltr|rtl
  • text-shadow : h-shadow v-shadow blur-radius color|none

https://www.w3schools.com/cssref/tryit.asp?filename=trycss_text-indent

72 of 121

CSS Text

  • p.one {
  • color:red;
  • background-color: lightgray;
  • text-align: center;
  • text-decoration: overline;
  • text-decoration: line-through;
  • text-decoration: underline;
  • text-transform: uppercase;
  • text-transform: lowercase;
  • text-transform: capitalize;
  • line-height: 0.8;
  • word-spacing: 10px;
  • word-spacing: -5px;
  • text-shadow: 2px 2px 5px red; }

73 of 121

CSS Text - Example

<html>

<head>

<style>

h1{

text-shadow:5px 5px 10px red;

}

</style>

</head>

<body>

<h1>Text-shadow effect</h1>

</body>

</html>

74 of 121

LIST properties

  • CSS list properties allows to
    • Set different list item markers for ordered lists
    • Set different list item markers for unordered lists
    • Set an image as the list item marker
    • Add background colors to lists and list items
    • Shorthand Property:

list-style: list-style-type list-style-position list-style-image

75 of 121

List Properties

  • list-style-image: none|url
  • list-style-position: inside|outside
  • List-style:disc|circle|square|none|lower-alpha|upper-alpha|lower-roman|upper-roman

Outside: the bullet points will be outside the list item. The start of each line of a list item will be aligned vertically. 

Inside: the bullet points will be 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

76 of 121

List Properties- Example

77 of 121

�CSS Font Properties

78 of 121

CSS Font Properties

  • font-family: Times New Roman;
  • font-style: italic; // normal, oblique
  • font-size: 30px;
  • font: 20px Arial, sans-serif;
  • font: italic bold 12px/30px Georgia, serif;

79 of 121

CSS Font Properties - Example

80 of 121

CSS – Positioning Elements

  • Position Property
    • Static
      • not affected by the top, bottom, left, and right properties
    • Relative
      • positioned relative to its normal position
    • Fixed
      • always stays in the same place even if the page is scrolled
    • Absolute
      • positioned relative to the nearest positioned ancestor
      • Places itself relative to its parent
    • Sticky
      • is positioned based on the user's scroll position
      • It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed)
  • Z-index – for overlapping elements
    • Elements with higher Z index will be shown at the first layer, others in the back layer

81 of 121

CSS – Relative Positioning

82 of 121

CSS – Absolute Positioning

83 of 121

CSS – Positioning Elements

84 of 121

CSS-Float Property

  • used for positioning and formatting content
  • e.g. let an image float left/right to the text in a container.
    • left - The element floats to the left of its container
    • right - The element floats to the right of its container
    • none - The element does not float

85 of 121

CSS-Float Property- Example

86 of 121

Float- Right & Left

87 of 121

<Div> & <Span> tag

  • HTML Block (DIV) and Inline (SPAN) Elements
  • <DIV>
    • defines a division or a section in an HTML document
    • used as a container for HTML elements - which is then styled with CSS or manipulated with JavaScript
    • easily styled by using the class or id attribute
  • <Span>
    • an inline container used to mark up a part of a text, or a part of a document
    • easily styled by CSS or manipulated with JavaScript using the class or id attribute
    • much like the <div> element, but <div> is a block-level element and <span> is an inline element.

88 of 121

<Div> tag

89 of 121

<span> tag

90 of 121

HTML Block and Inline Elements

Block Elements

Inline Elements

91 of 121

<DIV> tag - Example

.

.

Clear:both – do not allow floating elements on the left or the right side of a specified <p> element

92 of 121

<DIV> tag - Example

93 of 121

Page Structured Elements

  • <header> - Defines a header for a document or a section
  • <nav> - Defines a set of navigation links
  • <section> - Defines a section in a document
  • <article> - Defines an independent, self-contained content
  • <aside> - Defines content aside from the content (like a sidebar)
  • <footer> - Defines a footer for a document or a section
  • <details> - Defines additional details that the user can open and close on demand
  • <summary> - Defines a heading for the <details> element

94 of 121

Page Structured Elements

95 of 121

Page Structured Elements

96 of 121

Overflow

  • Overflow property specifies whether to clip the content or to add scrollbars when the content of an element is too big to fit in the specified area.
  • The overflow property has the following values:
    • visible - Default. The overflow is not clipped. The content renders outside the element's box
    • hidden - The overflow is clipped, and the rest of the content will be invisible
    • scroll - The overflow is clipped, and a scrollbar is added to see the rest of the content
    • auto - Similar to scroll, but it adds scrollbars only when necessary

97 of 121

Display Property

  • Display property specifies the display behavior (the type of rendering box) of an element.
  • display: value;
  • inline
    • Displays an element as an inline element (like <span>). Any height and width properties will have no effect
  • block
    • Displays an element as a block element (like <p>). It starts on a new line, and takes up the whole width
  • inline-block
    • Displays an element as an inline-level block container. The element itself is formatted as an inline element, but you can apply height and width values

98 of 121

CSS- NAV Tag

  • Navigation Bar = List of Links
  • using the <ul> and <li> elements makes perfect sense
  • Types
    • Vertical
    • Horizontal

99 of 121

CSS-Menu Bar-Vertical

    • Menu bars are created using navigation bar
    • A navigation bar is basically a list of links

Step 1:

Create the needed menu as list of links

100 of 121

CSS-Menu Bar-Vertical

    • Step 2:

Remove Bullets and Margins, padding from list

    • list-style-type: none; - Removes the bullets.
    • Set margin: 0; and padding: 0; to remove browser default settings

101 of 121

CSS-Menu Bar-Vertical

    • Step 3: Vertical navigation bar
      • To build a vertical navigation bar style the <a> elements inside the list
      • display: block;

Makes the whole link area clickable

It allows us to specify the width, padding, margin,

height etc

102 of 121

CSS-Menu Bar-Vertical

Step 4: Vertical navigation bar

      • To create navigation bar with background color

103 of 121

CSS-Menu Bar-Vertical

    • Step 5: Vertical navigation bar

hover: To changes the background color when user places curser over menu

104 of 121

CSS-Menu Bar-Horizontal

float: left; - use float to get block elements to slide next to each other

Since we use float:left, the contents may take next row. To mention how it should appear, we use overflow property

105 of 121

CSS- Dropdown menu

    • Div class is better suited to design drop down menu
    • To create a horizontal menu with dropdown, specify div class for menu with links
    • For Dropdown menu create a separate div class and add necessary style into that.

106 of 121

CSS- Dropdown menu

Step 1:

    • Create a div class with links
    • Place button for drop down menu

107 of 121

CSS- Dropdown menu

    • Step 2:
      • Specify the design for navbar class and link in nav bar

108 of 121

CSS- Dropdown menu

    • Step 3:
      • Change the button design and make that as normal menu

109 of 121

CSS- Dropdown menu

    • Step 4:
      • Make the dropdown links invisible for user when page loads

110 of 121

CSS- Dropdown menu

    • Step 5:
    • On hover the dropdown content should appear
    • Specify the design for dropdown content

111 of 121

CSS- Dropdown menu

    • Step 6:
      • Provide design to show dropdown content below the drop down menu
      • On hover color of drop down content may change

112 of 121

SOME MORE TAGS

113 of 121

<Meta> tag

  • It defines metadata about an HTML document
  • Metadata is data (information) about data

inside the <head> element

  • used to specify character set, page description, keywords, author of the document, and viewport settings
  • Metadata is used by browsers (how to display content or reload page), search engines (keywords), and other web services

114 of 121

<Meta> tag - Attributes

115 of 121

  • Define keywords for search engines:
    • <meta name="keywords" content="HTML, CSS, JavaScript">

  • Define a description of your web page:
    • <meta name="description" content="Free Web tutorials for HTML and CSS">

  • Define the author of a page:
    • <meta name="author" content="John Doe">

116 of 121

  • Refresh document every 30 seconds:
    • <meta http-equiv="refresh" content="30">

  • Setting the viewport to make your website look good on all devices:
    • <meta name="viewport" content="width=device-width, initial-scale=1.0">

117 of 121

Without the viewport meta tag

118 of 121

Example

<head>

<meta charset="UTF-8">

<meta name="description" content="Free Web tutorials">

<meta name="keywords" content="HTML,CSS,XML, JavaScript">

<meta name="author" content="John">

<meta name="viewport" content= "width=device-width, initial-scale=1.0">

<meta http-equiv="refresh" content="30">

</head>

119 of 121

CSS Units

  • px- are relative to the viewing device. For low-dpi devices, 1px is one device pixel (dot) of the display.
  • For printers and high resolution screens 1px implies multiple device pixels.

  • em, rem – mostly used
  • Viewport = the browser window size
  • If the viewport is 50cm wide, 1vw = 0.5cm

Absolute Lengths

Relative Lengths

120 of 121

Refer CSS Properties

121 of 121

Thank You