SlideShare a Scribd company logo
Lecture 2: CSS II SFDV2001 Web Development
The Box Model In CSS  every  element in your HTML file is contained within a rectangular box. There are four main components to this box: Content Margin Padding Border Additionally there a couple of background components:  background colour background image
Image source:  http://www.hicksdesign.co.uk/boxmodel/
The Box Model Margins are always transparent. Padding is the space between the content and  the border. Background colour and/or image extend throughout the box model to the border. If a style of border used has gaps in it (e.g. dashed), the background shows through those gaps. The background of the parent element shows through the margin area of the box.
Width The width set for an element is the width of the  content . Padding, border and margin are additional to this width. h1{ width: 300px; padding: 5px; border: solid 10px #ff69b4; margin: 10px; } Total width on page for this element = 350 pixels Unless your IE: IE implements the box model incorrectly.
 
IE 5 (Mac) Safari Firefox
Source:  http://en.wikipedia.org/wiki/Image:Box-model-bug.png
Solutions There are hacks to combat many of the flaws in IE’s CSS rendering. Most exploit other IE bugs to provide workarounds. The Box Model hack (by Tantek Çelik) width: 350px; voice-family:”\”}\””; voice-family: inherit; width: 300px; IE doesn’t see the second width specification.
Selectors Recap:  selector{ property: value; } There are many different kinds of selectors: Standard element selectors Contextual selectors Class selectors ID selectors Pseudoselectors
Element selectors H1{ color: #456678; } You can group selectors: H1, p, li{ color: #456678; }  You can affect any element using the universal ( * ) selector.
Contextual Selectors Allow you to affect elements based on their context. That is, their relationship to other elements. Descendant selectors Elements that are contained within other elements: ul li{   color: #456678; } ul li a{   color: #456678; }
Contextual Selectors Child Selectors Similar to descendant selectors but it target only a  direct  child of an element: p > em{   color: #456678; }
Contextual Selectors Adjacent sibling selectors H1 + p{ margin-left: 5%; } Affects an element that comes immediately after another. In the above example the first paragraph after a top-level heading will be indented.
Class and ID selectors You can’t always style every element you want to with the standard selectors available and some times you need to make your own. There are two kinds of “custom-made” selectors:  class  and  ID . Class is used for a style that you may wish to use multiple times in a  single  html file. ID is used for a style you wish to be unique in any  one  html file. You must set the class or ID in your HTML code.
Class and ID selectors IDs take care of unique structural elements: Class is used for elements that are used multiple times : Heading Navigation Footer
 
Class and ID selectors In your style sheet: Class p.question{ color: navy; } Or  .question{   color: navy; }
Class and ID selectors In your style sheet: ID Div#footer{ border-top: solid 1px #555555; font-size: smaller; } Or  #footer{ border-top: solid 1px #555555; font-size: smaller; }
Class and ID selectors Resist the temptation to make everything a class. Make sure your id and class names are descriptive. “navigation” is descriptive of the function of the selector rather than the style. “red” is descriptive of a style - styles change. If you can use a contextual selector or the like instead of a class or ID, do so.
Pseudoselectors Some things that you want to style in an html document are not possible to represent with standard element selectors because there is no element in your HTML document to represent them. Different link states - visited, unvisited etc. are good examples. Pseudoselectors enable you to identify these portions of elements. Pseudoselectors are identified in your CSS using a colon. a:link
Pseudoselectors - Anchors Pseudoselectors are most commonly used to influence different link states. a:link a:visited a:hover a:active In transitional code the equivalents are the  link ,  vlink  and  alink  attributes of body. In CSS you get an extra link state (hover) and you can influence any aspect of a links style, not just colour.
Pseudoselectors - Anchors a:link{ text-decoration: none; ... } a:visited{ color:#555555; ... } a:hover{ text-decoration: underline; } a:active{ color:#f6dc3b;   ... }
Pseudoselectors - Anchors Anchor pseudoselectors should appear in your CSS in the order: a  a:link a:visited a:hover a:active If you don't, they may not function as they should. Putting : link  last for example would override the : hover  and : active  states.
Other pseudoselectors There are other pseudoselectors, but they aren't as commonly used as those associated with anchors because of poor browser support. :focus :first-child :first-line :first-letter :before :after :first-line  and  :first-letter  are currently the only ones of this list that have any reasonable browser support.
Block-level and Inline elements In html there are block elements and inline elements. The distinction between the two is important for how they may be nested.  You know from transitional code that  <font>  tags only validate when used inside  <p>  tags. This is because  <font>  is an inline element and  <p>  is a block level element. Block-level elements can contain other block-level elements and inline elements. Inline elements can only contain content and other inline elements.
 
Block-Level and Inline in CSS CSS allows you to explicitly state if an element is block-level or inline. BUT! These have nothing to do with the structure of your HTML and will not change the rules of validation. The notions of block-level and inline in CSS are presentation concepts only. display: block; display: inline;
 
 
Recommended sites: Hicks Design Box Model diagram: http://www.hicksdesign.co.uk/3dboxmodel/ Interactive Box Model diagram: http://www.redmelon.net/tstme/box_model/ W3C's Box Model description: http://www.w3.org/TR/REC-CSS2/box.html Tantek Çelik's Box Model hack: http://tantek.com/CSS/Examples/boxmodelhack.html Further reading: Web Design in a Nutshell,  3rd Edition   by Jennifer Niederst Robbins

More Related Content

What's hot (20)

PPT
Lecture 1 - Comm Lab: Web @ ITP
yucefmerhi
 
PPT
Lecture 3 Javascript1
Sur College of Applied Sciences
 
DOCX
Lesson plan: HTML Formatting Texts and Paragraphs
Keith Borgonia Manatad
 
PPTX
Introduction to basic HTML [Librarian edition]
Kosie Eloff
 
PPTX
Introduction to html fundamental concepts
Tsebo Shaun Masilo
 
PPT
Module 2 Lesson 1
claytors
 
PPTX
Html 5 tutorial - By Bally Chohan
ballychohanuk
 
PPTX
Lecture 2 introduction to html basics
AliMUSSA3
 
PPTX
Getting Started in Custom Programming for Talent Sourcing
Glenn Gutmacher
 
PPT
HTML validation, microformats, jQuery
Jeffrey Barke
 
DOCX
Lesson plan htmltextformattingtag
Keith Borgonia Manatad
 
DOC
Lesson plan 3
Keith Borgonia Manatad
 
PPT
Html for beginners part I
Unaib Aslam
 
PPTX
Hypertext markup language (html)
Aksa Sahi
 
PPT
Basic html
Drew Eric Noftle
 
DOC
javscript
rcc1964
 
PDF
Deck 893ff61f-1fb8-4e15-a379-775dfdbcee77-12-53
Ivy Rueb
 
PPT
PL2235 2009 1 HTML
Aliamat UBD
 
PPTX
Chapter 2 introduction to html5
nobel mujuji
 
PDF
Rails Girls - Introduction to HTML & CSS
Timo Herttua
 
Lecture 1 - Comm Lab: Web @ ITP
yucefmerhi
 
Lecture 3 Javascript1
Sur College of Applied Sciences
 
Lesson plan: HTML Formatting Texts and Paragraphs
Keith Borgonia Manatad
 
Introduction to basic HTML [Librarian edition]
Kosie Eloff
 
Introduction to html fundamental concepts
Tsebo Shaun Masilo
 
Module 2 Lesson 1
claytors
 
Html 5 tutorial - By Bally Chohan
ballychohanuk
 
Lecture 2 introduction to html basics
AliMUSSA3
 
Getting Started in Custom Programming for Talent Sourcing
Glenn Gutmacher
 
HTML validation, microformats, jQuery
Jeffrey Barke
 
Lesson plan htmltextformattingtag
Keith Borgonia Manatad
 
Lesson plan 3
Keith Borgonia Manatad
 
Html for beginners part I
Unaib Aslam
 
Hypertext markup language (html)
Aksa Sahi
 
Basic html
Drew Eric Noftle
 
javscript
rcc1964
 
Deck 893ff61f-1fb8-4e15-a379-775dfdbcee77-12-53
Ivy Rueb
 
PL2235 2009 1 HTML
Aliamat UBD
 
Chapter 2 introduction to html5
nobel mujuji
 
Rails Girls - Introduction to HTML & CSS
Timo Herttua
 

Viewers also liked (7)

PPT
Hidden Profit
Linctel Ltd
 
PPS
Friendship
Sonja Ridden
 
PPS
Life On A Train
MJD Management Group
 
PPS
Friendship
Sonja Ridden
 
PPTX
Race
sabinecharette
 
PDF
Presentation at Milagrow SME Conference
rajeshjain
 
PPT
Lecture 10 Image Format
Sur College of Applied Sciences
 
Hidden Profit
Linctel Ltd
 
Friendship
Sonja Ridden
 
Life On A Train
MJD Management Group
 
Friendship
Sonja Ridden
 
Presentation at Milagrow SME Conference
rajeshjain
 
Lecture 10 Image Format
Sur College of Applied Sciences
 
Ad

Similar to Lecture2 CSS 2 (20)

PPT
Css class-02
Md Ali Hossain
 
PPT
Basic css
Gopinath Ambothi
 
ODP
Cascading Style Sheets - Part 01
Hatem Mahmoud
 
PPTX
CSS
Akila Iroshan
 
PPTX
Module 2 CSS . cascading style sheet and its uses
BKReddy3
 
PPT
An Introduction to CSS
John Catterfeld
 
PPTX
Introduction to Web Development.pptx
GDSCVJTI
 
PPTX
Introduction to Web Development.pptx
Alisha Kamat
 
PPTX
Introduction to Web Development.pptx
Alisha Kamat
 
PDF
css-tutorial
tutorialsruby
 
PDF
css-tutorial
tutorialsruby
 
PPT
CSS Basic and Common Errors
Hock Leng PUAH
 
PDF
CSS3 and Selectors
Rachel Andrew
 
PDF
CSS Foundations, pt 2
Shawn Calvert
 
PPTX
Designing for the web - 101
Ashraf Hamdy
 
PDF
Front End Good Practices
Hernan Mammana
 
PPTX
css v1 guru
GuruPada Das
 
PDF
CSS3 Refresher
Ivano Malavolta
 
PPT
Make Css easy(part:2) : easy tips for css(part:2)
shabab shihan
 
ODP
CSS memo
VDMK
 
Css class-02
Md Ali Hossain
 
Basic css
Gopinath Ambothi
 
Cascading Style Sheets - Part 01
Hatem Mahmoud
 
Module 2 CSS . cascading style sheet and its uses
BKReddy3
 
An Introduction to CSS
John Catterfeld
 
Introduction to Web Development.pptx
GDSCVJTI
 
Introduction to Web Development.pptx
Alisha Kamat
 
Introduction to Web Development.pptx
Alisha Kamat
 
css-tutorial
tutorialsruby
 
css-tutorial
tutorialsruby
 
CSS Basic and Common Errors
Hock Leng PUAH
 
CSS3 and Selectors
Rachel Andrew
 
CSS Foundations, pt 2
Shawn Calvert
 
Designing for the web - 101
Ashraf Hamdy
 
Front End Good Practices
Hernan Mammana
 
css v1 guru
GuruPada Das
 
CSS3 Refresher
Ivano Malavolta
 
Make Css easy(part:2) : easy tips for css(part:2)
shabab shihan
 
CSS memo
VDMK
 
Ad

More from Sur College of Applied Sciences (8)

PPT
Lecture11 A Image
Sur College of Applied Sciences
 
PPT
Lecture 11 B Security
Sur College of Applied Sciences
 
PPT
Accessibility Usability Professional Summry
Sur College of Applied Sciences
 

Recently uploaded (20)

PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PDF
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PDF
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Geographical diversity of India short notes by sandeep swamy
Sandeep Swamy
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
Isharyanti-2025-Cross Language Communication in Indonesian Language
Neny Isharyanti
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Horarios de distribución de agua en julio
pegazohn1978
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Controller Request and Response in Odoo18
Celine George
 

Lecture2 CSS 2

  • 1. Lecture 2: CSS II SFDV2001 Web Development
  • 2. The Box Model In CSS every element in your HTML file is contained within a rectangular box. There are four main components to this box: Content Margin Padding Border Additionally there a couple of background components: background colour background image
  • 3. Image source: http://www.hicksdesign.co.uk/boxmodel/
  • 4. The Box Model Margins are always transparent. Padding is the space between the content and the border. Background colour and/or image extend throughout the box model to the border. If a style of border used has gaps in it (e.g. dashed), the background shows through those gaps. The background of the parent element shows through the margin area of the box.
  • 5. Width The width set for an element is the width of the content . Padding, border and margin are additional to this width. h1{ width: 300px; padding: 5px; border: solid 10px #ff69b4; margin: 10px; } Total width on page for this element = 350 pixels Unless your IE: IE implements the box model incorrectly.
  • 6.  
  • 7. IE 5 (Mac) Safari Firefox
  • 9. Solutions There are hacks to combat many of the flaws in IE’s CSS rendering. Most exploit other IE bugs to provide workarounds. The Box Model hack (by Tantek Çelik) width: 350px; voice-family:”\”}\””; voice-family: inherit; width: 300px; IE doesn’t see the second width specification.
  • 10. Selectors Recap: selector{ property: value; } There are many different kinds of selectors: Standard element selectors Contextual selectors Class selectors ID selectors Pseudoselectors
  • 11. Element selectors H1{ color: #456678; } You can group selectors: H1, p, li{ color: #456678; } You can affect any element using the universal ( * ) selector.
  • 12. Contextual Selectors Allow you to affect elements based on their context. That is, their relationship to other elements. Descendant selectors Elements that are contained within other elements: ul li{ color: #456678; } ul li a{ color: #456678; }
  • 13. Contextual Selectors Child Selectors Similar to descendant selectors but it target only a direct child of an element: p > em{ color: #456678; }
  • 14. Contextual Selectors Adjacent sibling selectors H1 + p{ margin-left: 5%; } Affects an element that comes immediately after another. In the above example the first paragraph after a top-level heading will be indented.
  • 15. Class and ID selectors You can’t always style every element you want to with the standard selectors available and some times you need to make your own. There are two kinds of “custom-made” selectors: class and ID . Class is used for a style that you may wish to use multiple times in a single html file. ID is used for a style you wish to be unique in any one html file. You must set the class or ID in your HTML code.
  • 16. Class and ID selectors IDs take care of unique structural elements: Class is used for elements that are used multiple times : Heading Navigation Footer
  • 17.  
  • 18. Class and ID selectors In your style sheet: Class p.question{ color: navy; } Or .question{ color: navy; }
  • 19. Class and ID selectors In your style sheet: ID Div#footer{ border-top: solid 1px #555555; font-size: smaller; } Or #footer{ border-top: solid 1px #555555; font-size: smaller; }
  • 20. Class and ID selectors Resist the temptation to make everything a class. Make sure your id and class names are descriptive. “navigation” is descriptive of the function of the selector rather than the style. “red” is descriptive of a style - styles change. If you can use a contextual selector or the like instead of a class or ID, do so.
  • 21. Pseudoselectors Some things that you want to style in an html document are not possible to represent with standard element selectors because there is no element in your HTML document to represent them. Different link states - visited, unvisited etc. are good examples. Pseudoselectors enable you to identify these portions of elements. Pseudoselectors are identified in your CSS using a colon. a:link
  • 22. Pseudoselectors - Anchors Pseudoselectors are most commonly used to influence different link states. a:link a:visited a:hover a:active In transitional code the equivalents are the link , vlink and alink attributes of body. In CSS you get an extra link state (hover) and you can influence any aspect of a links style, not just colour.
  • 23. Pseudoselectors - Anchors a:link{ text-decoration: none; ... } a:visited{ color:#555555; ... } a:hover{ text-decoration: underline; } a:active{ color:#f6dc3b; ... }
  • 24. Pseudoselectors - Anchors Anchor pseudoselectors should appear in your CSS in the order: a a:link a:visited a:hover a:active If you don't, they may not function as they should. Putting : link last for example would override the : hover and : active states.
  • 25. Other pseudoselectors There are other pseudoselectors, but they aren't as commonly used as those associated with anchors because of poor browser support. :focus :first-child :first-line :first-letter :before :after :first-line and :first-letter are currently the only ones of this list that have any reasonable browser support.
  • 26. Block-level and Inline elements In html there are block elements and inline elements. The distinction between the two is important for how they may be nested. You know from transitional code that <font> tags only validate when used inside <p> tags. This is because <font> is an inline element and <p> is a block level element. Block-level elements can contain other block-level elements and inline elements. Inline elements can only contain content and other inline elements.
  • 27.  
  • 28. Block-Level and Inline in CSS CSS allows you to explicitly state if an element is block-level or inline. BUT! These have nothing to do with the structure of your HTML and will not change the rules of validation. The notions of block-level and inline in CSS are presentation concepts only. display: block; display: inline;
  • 29.  
  • 30.  
  • 31. Recommended sites: Hicks Design Box Model diagram: http://www.hicksdesign.co.uk/3dboxmodel/ Interactive Box Model diagram: http://www.redmelon.net/tstme/box_model/ W3C's Box Model description: http://www.w3.org/TR/REC-CSS2/box.html Tantek Çelik's Box Model hack: http://tantek.com/CSS/Examples/boxmodelhack.html Further reading: Web Design in a Nutshell, 3rd Edition by Jennifer Niederst Robbins

Editor's Notes

  • #5: Show boxModel.html
  • #7: Top = doctype - “standards mode” As pages will appear in IE 6 and up with a doctype Bottom = no doctype - “quirks mode” As pages appear in IE 5 and prior and for files without the right doctype. In version 6, Microsoft addressed this issue by implementing the W3C box model, if &amp;quot;standards mode&amp;quot; was enabled, which is done by defining a supported DOCTYPE on the first line of the HTML output.
  • #10: The &amp;quot;box model hack&amp;quot; developed by Tantek Çelik . Developed during his time at Microsoft working on Internet Explorer for the Macintosh, which is not affected by the bug — involves specifying a width for Internet Explorer for Windows which is then over-ridden by another width specification. This second specification is hidden from Internet Explorer for Windows by exploiting a bug in the way that browser parses CSS rules. width: 350px; - Width for IE voice-family:””}””; - IE is tricked into thinking rule has finished voice-family: inherit; - voice-family is reset for any browser that understands the property width: 300px; - the &amp;quot;real&amp;quot; width is set for all other browsers.
  • #11: Also attribute selectors but these are so poorly supported at present that I’m not going to talk about them here.
  • #14: First em is affected. Second is not.
  • #23: Don&apos;t forget plain a
  • #25: a
  • #26: Focus - can be applied to any element but in the few browsers that offer any support for it, generally only form elements can make use of it. Firstchild - as it suggests - e.g. Li:first-child - targets the first li of a list. Before and after allow for generated content to be inserted before or after an element: Blockquote:before{content:&amp;quot;&amp;#8220;&amp;quot; font-size: 24px} Blockquote:after{content:&amp;quot;&amp;#8220;&amp;quot; font-size: 24px} There is also :lang(ab).