SlideShare a Scribd company logo
1 of 20
Download to read offline
CS142 Lecture Notes - CSS
Cascading Style Sheets
(CSS)
Mendel Rosenblum
1
CS142 Lecture Notes - CSS
Driving problem behind CSS
What font type and size does <h1>Introduction</h1> generate?
Answer: Some default from the browser (HTML tells what browser how)
Early HTML - Override defaults with attributes
<table border="2" bordercolor="black">
Style sheets were added to address this:
Specify style to use rather than browser default
Not have to code styling on every element
2
CS142 Lecture Notes - CSS
Key concept: Separate style from content
Content (what to display) is in HTML files
Formatting information (how to display it) is in separate style sheets (.css files).
Use an element attribute named class to link (e.g. <span class="test">)
Result: define style information once, use in many places
Consider can you make all the text in the app slightly bigger?
Or purple is our new company color.
DRY principle: Don't Repeat Yourself
3
CS142 Lecture Notes - CSS
Style sheet contain one or more CSS Rules
body {
font-family: Tahoma, Arial, sans-serif;
color: black;
background: white;
margin: 8px;
}
4
Selector
Property Value
Declaration
Block
CS142 Lecture Notes - CSS 5
CSS
Selector
CSS HTML
Tag name
h1 {
color: red;
}
<h1>Today’s Specials</h1>
Class attribute
.large {
font-size: 16pt;
}
<p class="large">...
Tag and Class p.large {...} <p class="large">...
Element id
#p20 {
font-weight: bold;
}
<p id="p20">...
CS142 Lecture Notes - CSS
CSS Pseudo Selectors
hover - Apply rule when mouse is over element (e.g. tooltip)
p:hover, a:hover {
background-color: yellow;
}
a:link, a:visited - Apply rule when link has been visited or not visited (link)
a:visited { a:link {
color: green; color: blue;
} }
6
CS142 Lecture Notes - CSS
CSS Properties
Control many style properties of an element:
● Coloring
● Size
● Position
● Visibility
● Many more: (e.g. p: { text-decoration: line-through; })
● Also used in animation
7
CS142 Lecture Notes - CSS
Color - Properties: color & background_color
Must ultimately turn into red, green, and blue intensities between 0 and 255:
● Predefined names: red, blue, green, white, etc. (140 standard names)
● 8-bit hexadecimal numbers for red, green, blue: #ff0000
● 0-255 decimal intensities: rgb(255,255,0)
● Percentage intensities: rgb(80%,80%,100%)
Example: h1: { color: red; }
8
R G B
R G B
R G B
CS142 Lecture Notes - CSS
CSS Box Model
9
Element
Margin
Border
Padding
width
height
Total element width =
width +
left padding +
right padding +
left border +
right border +
left margin +
right margin
Margin & Padding
Transparent
CS142 Lecture Notes - CSS
CSS distance units
10
Absolute
2px pixels
1mm millimeters
2cm centimeters
0.2in inches
3pt printer point 1/72 inch
Relative
2em 2 times the element’s current font size
3rem 3 times the root element’s current font size
CS142 Lecture Notes - CSS
Size Properties - Element, pad, margin, border
width - Override element defaults
height
padding-top
padding-right
padding-bottom
padding-left
margin-top
margin-right
margin-bottom
margin-left
11
border-bottom-color
border-bottom-style
border-bottom-width
border-left-color
border-left-style
border-left-width
border-right-color
border-right-style
border-right-width
etc.
p {
border: 5px solid red;
}
CS142 Lecture Notes - CSS
position property
position: static; (default) - Position in document flow
position: relative; Position relative to default position via
top, right, bottom, and left properties
position: fixed; Position to a fixed location on the screen via
top, right, bottom, and left properties
position: absolute; Position relative to ancestor absolute element via
top, right, bottom, and left properties
Fixed position (0,0) is top left corner
12
CS142 Lecture Notes - CSS
Some more common properties
background-image: image for element's background
background-repeat: should background image be displayed in a repeating
pattern (versus once only)
font, font-family, font-size, font-weight, font-style: font
information for text
text-align, vertical-align: Alignment: center, left, right
cursor - Set the cursor when over element (e.g. help)
13
CS142 Lecture Notes - CSS
Element visibility control properties
display: none; - Element is not displayed and takes no space in layout.
display: inline; - Element is treated as an inline element.
display: block; - Element is treated as a block element.
display: flex; - Element is treated as a flex container.
display: grid; - Element is treated as a grid container.
visibility: hidden; - Element is hidden but space still allocated.
visibility: visible; - Element is normally displayed
14
CS142 Lecture Notes - CSS
Flexbox and Grid layout
● display: flex; (Flexbox)
● display: grid; (Grid) newer layout method
○ Items flex to fill additional space and shrink to fit into smaller spaces.
○ Useful for web app layout:
■ Divide up the available space equally among a bunch of elements
■ Align of different sizes easily
■ Key to handling different window and display sizes
● Flexbox - Layout one dimension (row or column) of elements
● Grid - Layout in two dimensions (rows and columns) of elements
● Covered in discussion section
15
CS142 Lecture Notes - CSS
Some other CSS issues
● Inheritance
○ Some properties (e.g. font-size) are inherited from parent elements
○ Others (border, background) are not inherited.
● Multiple rule matches
○ General idea: most specific rule wins
<span>Text1</span> span.test { color: green }
<span class="test">Text2</span> span { color: red }
16
CS142 Lecture Notes - CSS
Adding Styles to HTML
<head>
<link rel="stylesheet" type="text/css" href="myStyles.css" />
<style type="text/css">
body {
font-family: Tahoma, Arial, sans-serif;
}
</style>
</head>
<body>
<div style="padding:2px; ... ">
</body>
17
Separate style sheet (best way)
Page-specific styles
Element-specific styles
CS142 Lecture Notes - CSS
body {
font-family: Tahoma, Arial, sans-serif;
font-size: 13px;
color: black;
background: white;
margin: 8px;
}
h1 {
font-size: 19px;
margin-top: 0px;
margin-bottom: 5px;
border-bottom: 1px solid black
}
.shaded {
background: #d0d0ff;
}
18
<body>
<h1>First Section Heading</h1>
<p>
Here is the first paragraph, containing
text that really doesn't have any use
or meaning; it just prattles on and on,
with no end whatsoever, no point to
make, really no purpose for existence
at all.
</p>
<div class="shaded">
<h1>Another Section Heading</h1>
<p>
Another paragraph.
</p>
</div>
</body>
CSS: HTML:
CS142 Lecture Notes - CSS
Example Output
19
CS142 Lecture Notes - CSS
CSS in the real world
20
● CSS preprocessors (e.g. less) are commonly used
○ Add variable and functions to help in maintaining large collections of style sheets
○ Apply scoping using the naming conventions
● Composition is a problem
○ It can be really hard to figure out what rule from which stylesheet is messing things up

More Related Content

Similar to a.pdf

Web topic 15 1 basic css layout
Web topic 15 1  basic css layoutWeb topic 15 1  basic css layout
Web topic 15 1 basic css layout
CK Yang
 
waxaada canshuurahaiyosiyaaddaguudeexamarwaaardaydaDAALBADAN.ppt
waxaada canshuurahaiyosiyaaddaguudeexamarwaaardaydaDAALBADAN.pptwaxaada canshuurahaiyosiyaaddaguudeexamarwaaardaydaDAALBADAN.ppt
waxaada canshuurahaiyosiyaaddaguudeexamarwaaardaydaDAALBADAN.ppt
Ismaciil2
 
Web app development_html_css_03
Web app development_html_css_03Web app development_html_css_03
Web app development_html_css_03
Hassen Poreya
 

Similar to a.pdf (20)

html-css
html-csshtml-css
html-css
 
Unit 2-CSS & Bootstrap.ppt
Unit 2-CSS & Bootstrap.pptUnit 2-CSS & Bootstrap.ppt
Unit 2-CSS & Bootstrap.ppt
 
Web topic 15 1 basic css layout
Web topic 15 1  basic css layoutWeb topic 15 1  basic css layout
Web topic 15 1 basic css layout
 
waxaada canshuurahaiyosiyaaddaguudeexamarwaaardaydaDAALBADAN.ppt
waxaada canshuurahaiyosiyaaddaguudeexamarwaaardaydaDAALBADAN.pptwaxaada canshuurahaiyosiyaaddaguudeexamarwaaardaydaDAALBADAN.ppt
waxaada canshuurahaiyosiyaaddaguudeexamarwaaardaydaDAALBADAN.ppt
 
css1.ppt
css1.pptcss1.ppt
css1.ppt
 
CSS
CSSCSS
CSS
 
Cascading Style Sheet
Cascading Style SheetCascading Style Sheet
Cascading Style Sheet
 
WT CSS
WT  CSSWT  CSS
WT CSS
 
Css
CssCss
Css
 
Ict 8 css
Ict 8 cssIct 8 css
Ict 8 css
 
CascadingStyleSheets in ONESHOT By DeathCodeYT .pdf
CascadingStyleSheets  in ONESHOT By DeathCodeYT .pdfCascadingStyleSheets  in ONESHOT By DeathCodeYT .pdf
CascadingStyleSheets in ONESHOT By DeathCodeYT .pdf
 
Web app development_html_css_03
Web app development_html_css_03Web app development_html_css_03
Web app development_html_css_03
 
TUTORIAL DE CSS 2.0
TUTORIAL DE CSS 2.0TUTORIAL DE CSS 2.0
TUTORIAL DE CSS 2.0
 
CSS.pptx
CSS.pptxCSS.pptx
CSS.pptx
 
Webpage style with CSS
Webpage style with CSSWebpage style with CSS
Webpage style with CSS
 
CSS Overview
CSS OverviewCSS Overview
CSS Overview
 
INTRODUCTIONS OF CSS
INTRODUCTIONS OF CSSINTRODUCTIONS OF CSS
INTRODUCTIONS OF CSS
 
David Weliver
David WeliverDavid Weliver
David Weliver
 
Introduction to css by programmerblog.net
Introduction to css by programmerblog.netIntroduction to css by programmerblog.net
Introduction to css by programmerblog.net
 
Css
CssCss
Css
 

More from JP Chicano (15)

cssfasfasfasfas afsasfasfasfas safasfasf
cssfasfasfasfas afsasfasfasfas safasfasfcssfasfasfasfas afsasfasfasfas safasfasf
cssfasfasfasfas afsasfasfasfas safasfasf
 
TRAINING PLAN 1ST Year-1st Sem.dasdasdasdpdf
TRAINING PLAN 1ST Year-1st Sem.dasdasdasdpdfTRAINING PLAN 1ST Year-1st Sem.dasdasdasdpdf
TRAINING PLAN 1ST Year-1st Sem.dasdasdasdpdf
 
TRAINING PLAN 2ND Year-2nd Semgsdgsdg.pdf
TRAINING PLAN 2ND Year-2nd Semgsdgsdg.pdfTRAINING PLAN 2ND Year-2nd Semgsdgsdg.pdf
TRAINING PLAN 2ND Year-2nd Semgsdgsdg.pdf
 
fwfgsdfsagsdag sdgsdagsadf qwf sdafasdaf sdaf
fwfgsdfsagsdag sdgsdagsadf qwf sdafasdaf sdaffwfgsdfsagsdag sdgsdagsadf qwf sdafasdaf sdaf
fwfgsdfsagsdag sdgsdagsadf qwf sdafasdaf sdaf
 
MariaSoccorro-Ramosfagsagsdgsdagdsagsadgsag
MariaSoccorro-RamosfagsagsdgsdagdsagsadgsagMariaSoccorro-Ramosfagsagsdgsdagdsagsadgsag
MariaSoccorro-Ramosfagsagsdgsdagdsagsadgsag
 
1.pdf
1.pdf1.pdf
1.pdf
 
parts of mobo for sharing.pdf
parts of mobo for sharing.pdfparts of mobo for sharing.pdf
parts of mobo for sharing.pdf
 
12786246.ppt
12786246.ppt12786246.ppt
12786246.ppt
 
CSS HTML.pdf
CSS HTML.pdfCSS HTML.pdf
CSS HTML.pdf
 
download.pdf
download.pdfdownload.pdf
download.pdf
 
MIS.ppt
MIS.pptMIS.ppt
MIS.ppt
 
11.ppt
11.ppt11.ppt
11.ppt
 
13665449.ppt
13665449.ppt13665449.ppt
13665449.ppt
 
oopsinvb-191021101327.pdf
oopsinvb-191021101327.pdfoopsinvb-191021101327.pdf
oopsinvb-191021101327.pdf
 
3306565.ppt
3306565.ppt3306565.ppt
3306565.ppt
 

Recently uploaded

Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Riyadh +966572737505 get cytotec
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
amitlee9823
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
amitlee9823
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
AroojKhan71
 
Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter Lessons
JoseMangaJr1
 
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and Milvus
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
 
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
Call Girls in Sarai Kale Khan Delhi 💯 Call Us 🔝9205541914 🔝( Delhi) Escorts S...
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
Mg Road Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Banga...
 
Invezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signalsInvezz.com - Grow your wealth with trading signals
Invezz.com - Grow your wealth with trading signals
 
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al BarshaAl Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
Al Barsha Escorts $#$ O565212860 $#$ Escort Service In Al Barsha
 
Capstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics ProgramCapstone Project on IBM Data Analytics Program
Capstone Project on IBM Data Analytics Program
 
Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter Lessons
 
Week-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interactionWeek-01-2.ppt BBB human Computer interaction
Week-01-2.ppt BBB human Computer interaction
 
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
 
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
 
Mature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptxMature dropshipping via API with DroFx.pptx
Mature dropshipping via API with DroFx.pptx
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Research
 
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 nightCheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
Cheap Rate Call girls Sarita Vihar Delhi 9205541914 shot 1500 night
 

a.pdf

  • 1. CS142 Lecture Notes - CSS Cascading Style Sheets (CSS) Mendel Rosenblum 1
  • 2. CS142 Lecture Notes - CSS Driving problem behind CSS What font type and size does <h1>Introduction</h1> generate? Answer: Some default from the browser (HTML tells what browser how) Early HTML - Override defaults with attributes <table border="2" bordercolor="black"> Style sheets were added to address this: Specify style to use rather than browser default Not have to code styling on every element 2
  • 3. CS142 Lecture Notes - CSS Key concept: Separate style from content Content (what to display) is in HTML files Formatting information (how to display it) is in separate style sheets (.css files). Use an element attribute named class to link (e.g. <span class="test">) Result: define style information once, use in many places Consider can you make all the text in the app slightly bigger? Or purple is our new company color. DRY principle: Don't Repeat Yourself 3
  • 4. CS142 Lecture Notes - CSS Style sheet contain one or more CSS Rules body { font-family: Tahoma, Arial, sans-serif; color: black; background: white; margin: 8px; } 4 Selector Property Value Declaration Block
  • 5. CS142 Lecture Notes - CSS 5 CSS Selector CSS HTML Tag name h1 { color: red; } <h1>Today’s Specials</h1> Class attribute .large { font-size: 16pt; } <p class="large">... Tag and Class p.large {...} <p class="large">... Element id #p20 { font-weight: bold; } <p id="p20">...
  • 6. CS142 Lecture Notes - CSS CSS Pseudo Selectors hover - Apply rule when mouse is over element (e.g. tooltip) p:hover, a:hover { background-color: yellow; } a:link, a:visited - Apply rule when link has been visited or not visited (link) a:visited { a:link { color: green; color: blue; } } 6
  • 7. CS142 Lecture Notes - CSS CSS Properties Control many style properties of an element: ● Coloring ● Size ● Position ● Visibility ● Many more: (e.g. p: { text-decoration: line-through; }) ● Also used in animation 7
  • 8. CS142 Lecture Notes - CSS Color - Properties: color & background_color Must ultimately turn into red, green, and blue intensities between 0 and 255: ● Predefined names: red, blue, green, white, etc. (140 standard names) ● 8-bit hexadecimal numbers for red, green, blue: #ff0000 ● 0-255 decimal intensities: rgb(255,255,0) ● Percentage intensities: rgb(80%,80%,100%) Example: h1: { color: red; } 8 R G B R G B R G B
  • 9. CS142 Lecture Notes - CSS CSS Box Model 9 Element Margin Border Padding width height Total element width = width + left padding + right padding + left border + right border + left margin + right margin Margin & Padding Transparent
  • 10. CS142 Lecture Notes - CSS CSS distance units 10 Absolute 2px pixels 1mm millimeters 2cm centimeters 0.2in inches 3pt printer point 1/72 inch Relative 2em 2 times the element’s current font size 3rem 3 times the root element’s current font size
  • 11. CS142 Lecture Notes - CSS Size Properties - Element, pad, margin, border width - Override element defaults height padding-top padding-right padding-bottom padding-left margin-top margin-right margin-bottom margin-left 11 border-bottom-color border-bottom-style border-bottom-width border-left-color border-left-style border-left-width border-right-color border-right-style border-right-width etc. p { border: 5px solid red; }
  • 12. CS142 Lecture Notes - CSS position property position: static; (default) - Position in document flow position: relative; Position relative to default position via top, right, bottom, and left properties position: fixed; Position to a fixed location on the screen via top, right, bottom, and left properties position: absolute; Position relative to ancestor absolute element via top, right, bottom, and left properties Fixed position (0,0) is top left corner 12
  • 13. CS142 Lecture Notes - CSS Some more common properties background-image: image for element's background background-repeat: should background image be displayed in a repeating pattern (versus once only) font, font-family, font-size, font-weight, font-style: font information for text text-align, vertical-align: Alignment: center, left, right cursor - Set the cursor when over element (e.g. help) 13
  • 14. CS142 Lecture Notes - CSS Element visibility control properties display: none; - Element is not displayed and takes no space in layout. display: inline; - Element is treated as an inline element. display: block; - Element is treated as a block element. display: flex; - Element is treated as a flex container. display: grid; - Element is treated as a grid container. visibility: hidden; - Element is hidden but space still allocated. visibility: visible; - Element is normally displayed 14
  • 15. CS142 Lecture Notes - CSS Flexbox and Grid layout ● display: flex; (Flexbox) ● display: grid; (Grid) newer layout method ○ Items flex to fill additional space and shrink to fit into smaller spaces. ○ Useful for web app layout: ■ Divide up the available space equally among a bunch of elements ■ Align of different sizes easily ■ Key to handling different window and display sizes ● Flexbox - Layout one dimension (row or column) of elements ● Grid - Layout in two dimensions (rows and columns) of elements ● Covered in discussion section 15
  • 16. CS142 Lecture Notes - CSS Some other CSS issues ● Inheritance ○ Some properties (e.g. font-size) are inherited from parent elements ○ Others (border, background) are not inherited. ● Multiple rule matches ○ General idea: most specific rule wins <span>Text1</span> span.test { color: green } <span class="test">Text2</span> span { color: red } 16
  • 17. CS142 Lecture Notes - CSS Adding Styles to HTML <head> <link rel="stylesheet" type="text/css" href="myStyles.css" /> <style type="text/css"> body { font-family: Tahoma, Arial, sans-serif; } </style> </head> <body> <div style="padding:2px; ... "> </body> 17 Separate style sheet (best way) Page-specific styles Element-specific styles
  • 18. CS142 Lecture Notes - CSS body { font-family: Tahoma, Arial, sans-serif; font-size: 13px; color: black; background: white; margin: 8px; } h1 { font-size: 19px; margin-top: 0px; margin-bottom: 5px; border-bottom: 1px solid black } .shaded { background: #d0d0ff; } 18 <body> <h1>First Section Heading</h1> <p> Here is the first paragraph, containing text that really doesn't have any use or meaning; it just prattles on and on, with no end whatsoever, no point to make, really no purpose for existence at all. </p> <div class="shaded"> <h1>Another Section Heading</h1> <p> Another paragraph. </p> </div> </body> CSS: HTML:
  • 19. CS142 Lecture Notes - CSS Example Output 19
  • 20. CS142 Lecture Notes - CSS CSS in the real world 20 ● CSS preprocessors (e.g. less) are commonly used ○ Add variable and functions to help in maintaining large collections of style sheets ○ Apply scoping using the naming conventions ● Composition is a problem ○ It can be really hard to figure out what rule from which stylesheet is messing things up