introduction to css - university of babylon › eprints › publication_11_26283_6025.pdf ·...

31
INTRODUCTION TO CSS Mohammad Jawad Kadhim

Upload: others

Post on 07-Jun-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

INTRODUCTION TO CSS

Mohammad Jawad Kadhim

Page 2: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted
Page 3: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted
Page 4: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted
Page 5: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

WHAT IS CSS

Like HTML, CSS is an interpreted language. When a web page request is processed by a web server, the server’s response can include style sheets, which are simply collections of cascading style instructions. The style sheets can be Included in the server’s response In three ways:

External style sheet files.Internal style sheet embedded directly in the web pageInline style sheets

Page 6: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

EXTERNAL STYLE SHEET

You link external style sheets into web pages by using the HTML<link> tag.

<link href="css/main.css“ rel="stylesheet" />

You can also use the CSS import statement directly in your stylesheet to actually link multiple style sheets together:

@import url("secondary.css");

Page 7: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

A single web page can contain multiple style sheet references

<!DOCTYPE html>

<html>

<head>

<title>HTML 5 Sample</title>

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

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

</head>

<body>

<div>Lorem Ipsum</div>

</body>

</html>

Page 8: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

INTERNAL STYLE SHEETS

Internal style sheets are collections of CSS styles that are storedinternally in a single web page. The styles are located inside of theHTML <style> tag, which is generally located in the <head> sectionof the web page.

<head>

<title></title>

<style type="text/css">

body { }

</style>

</head>

Page 9: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

<!DOCTYPE html>

<html>

<head>

<title>HTML 5 Sample</title>

<style type="text/css">

body {

font-family: Arial;

font-size: medium;}

</style>

</head>

<body>

<div>Lorem Ipsum</div>

</body>

</html>

Page 10: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

INLINE STYLE SHEETInline style are CSS styles that are applied directly to an individual HTML element using the element’s style attribute, which is available on most HTML elements.

<!DOCTYPE html>

<html>

<head>

</head>

<body>

<div style=“font-family:Arial;”>Lorem Ipsum</div>

</body>

</html>

Page 11: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

Now lets imagine amazon without CSS

Page 12: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted
Page 13: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

TWO CONTENT LAYOUT WITH TABLE

• First bullet point here

• Second bullet point here

• Third bullet point here

Class Group A Group B

Class 1 82 95

Class 2 76 88

Class 3 84 90

Page 14: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

Cascading

+

Style Sheet

Page 15: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

THE STYLESHEET

• A stylesheet is a set of rules defining how an html element will be “presented” in the browser.

• These rules are targeted to specific elements in the html document.

Page 16: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

THE CASCADE

• The “cascade” part of CSS is a set of rules for resolving conflicts with multiple CSS rules applied to the same elements.

• For example, if there are two rules defining the color or your h1 elements, the rule that comes last in the cascade order will “trump” the other.

Page 17: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

Browser stylesheet

Linked (external) stylesheet

Embedded (internal) stylesheet

Inline (internal) Styles

hig

h im

po

rtan

celo

w im

po

rtan

ce

Page 18: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

INHERITANCE

Most elements will inherit many style properties from their parent elements by default.

HTML

<body>

<div>

<ul>

<li></li>

</ul>

</div>

</body>

Relationship

parent of site

parent of ul and li, child of body

parent of li, child of div and body

child of ul, div, and body

Page 19: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted
Page 20: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

SPECIFICITY

• Shortly after styling your first html elements, you will find yourself wanting more control over where your styles are applied.

• This is where specificity comes in.

• Specificity refers to how specific your selector is in naming an element.

Page 21: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted
Page 22: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

Three terms for describing your styles:

CSS rule

CSS selector

Page 23: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

CSS RULE

selector {property: value;}

Declaration

Every style is defined by a selector and a declaration. The declaration contains at least one property/value pair. Together they are called a CSS Rule.

Page 24: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

CSS RULE

the browser is responsible for parsing the styles and applying them to theappropriate HTML elements in the web page.

if a style is stored in either an external or internal style sheet, the style will bedefined as a CSS rule.

Rules are what the browser uses to determine what styling to apply to whichHTML elements.

Note: inline style do not need to be defined as a rule because they are automatically applied to the element they are included with

Page 25: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

CSS SELECTOR

body {font-family: Arial, Helvetica}

p {color: #666666}

h1 {font-size: 24px}

a {color: blue}

The selector associates css rules with HTML elements.

Page 26: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

Universal Selectors

Style should apply to any element in the web page

* {

font-family: Arial;

}

Type Selector

create a style that applies to a specific type of HTML element

P {

font-family: Arial;

}

Descendant Selectors

create styles that target HTML elements that are descendants of a specific type of element.

div span {

font-family: Arial;

}

Page 27: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

Child Selectors

The Child selector is similar to the Descendant selector except unlike the Descendantselector, which searches the entire descendant hierarchy of an element, the Child selectorrestricts its element search to only those elements that are direct children of the parentelement.

div > span

{

font-family:Arial;

}

Attribute Selectors

Attribute selectors enable you to define a style that is applied to elements based on the existence of element attributes rather than the actual element name.

*[href]

{

font-family:Arial;

}

Page 28: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

Adjacent selector

Select HTML element that are immediately adjacent to another element type

li

{

color: maroon;

}

li + li

{

color: silver;

}

Page 29: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

Class Selectors

Allow you to apply a style to any element with a specific class name

.title

{

font-size: larger;

font-weight: bold

}

This CSS rule would then be applied to any element whose class attribute value matched the rule name

<div class=“title”> abcd</div>

Page 30: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

ID Selectors

Allow you to create style that target elements with specific ID value

#title

{

font-size: larger;

font-weight: bold

}

This CSS rule would then be applied to any element whose ID attribute value matched the rule name

<div id=“title”> abcd</div>

Page 31: INTRODUCTION TO CSS - University of Babylon › eprints › publication_11_26283_6025.pdf · INTRODUCTION TO CSS Mohammad Jawad Kadhim. WHAT IS CSS Like HTML, CSS is an interpreted

Selector Grouping

When creating CSS rules, CSS allows you to group several selectors together into a single rule.

h1, h2, h3

{

Color: blue;

}