35
Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Embed Size (px)

Citation preview

Page 1: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Using CSS(Cascading Style Sheets)

Effie Nadiv

Edited by permission from author by Amir Kirsh

Page 2: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Using CSS Linking CSS Selector Styling Hacking

Page 3: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Quick History First CSS proposal by Hakon Lie in Oct 94 W3C established and CSS workshop run in

95 CSS1 becomes a recommendation in Dec

96 CSS2 becomes a recommendation in May

98 Drafts of first 3 CSS3 modules published

in June 99

Page 4: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

CSS Linking

<link href="http://styleguide/build/241/script/yui/calendar/assets/calendar.css" type="text/css" rel="stylesheet" />

Alternate:

<link title="Verizon" href="skins/vzw/vzw.css" media="screen" type="text/css" rel="alternate stylesheet" />

Page 5: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Use Relativity When linking to a style sheet use a relative

path:

href="skins/sg/narrow.css"

When pointing a resource inside a style sheet use a relative path:

src=”images/my-image.png”

Page 6: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

CSS Rule selector { property: value;}

Page 7: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Selector ID (#main-menu) Class (.sidebar) HTML tag (body)

body{background-color:#fff;}

body #wrapper {background-color:#ccc;}

body div.menu {background-color:#fff;}

Page 8: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

CSS 2.1 selector syntax

Universal * Matches any element

Type E Matches any E element.

Class .info

Matches any element

whose class attribute

contains the value info.

Id #footer Matches any element with an id equalto footer

Descendant E F Matches any F element that is a descendant of an E element.

Child E > F Matches any F element that is a child of an E element.

Page 9: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

CSS 2.1 selector syntax

Attribute E[att] Matches any E element that has an att attribute, regardless of its value.

Attribute E[att=val] Matches any E element whose att attribute value is exactly equal to val.

Attribute Matches any E element E[att~=val]

whose att attribute valueis a list of space-separatedvalues, one of which is exactly equal to val.

pseudo-class E:first-child

Matches element E whenE is the first child of its parent.

Adjacent sibling selectors p + pmatches an element which is the next sibling to the first element

Page 10: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Descendant selectors div p { color:#f00; } div#myid li p.info { color:#f00; }

Page 11: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Child selectors div > span { color:#f00; }

Page 12: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Pseudo-classes a:link a:visited a:hover a:active a.error:visited {color: #f00}

Page 13: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Pseudo-elements

p:first-letter {

color:#0000ff;

font-variant:small-caps

}

a:link:after {

content: " (" attr(href) ") ";

}

Page 14: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Adjacent sibling selectors p + p { color:#f00; }

<div>

<p>Paragraph one</p>

<p>Paragraph two</p>

</div>

Page 15: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Attribute selectors p[title] { color:#f00; } div[class=error] { color:#f00; } td[headers~=col1] { color:#f00; } p[lang|=en] { color:#f00; }

Page 16: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Grouping Selectors

h1,h2,h3,h4,h5,h6

{

color: #000;

}

Page 17: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

CSS Selector Specificity

Page 18: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

A selector's specificity is calculated as follows: count the number of ID attributes in the selector (= a) count the number of other attributes and pseudo-

classes in the selector (= b) count the number of element names in the selector (=

c) ignore pseudo-elements. Concatenating the three numbers a-b-c (in a number

system with a large base) gives the specificity.

Page 19: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Some examples: * {} /* a=0 b=0 c=0 -> specificity = 0 */

LI {} /* a=0 b=0 c=1 -> specificity = 1 */

UL LI {} /* a=0 b=0 c=2 -> specificity = 2 */

UL OL+LI {} /* a=0 b=0 c=3 -> specificity = 3 */

H1 + *[REL=up]{} /* a=0 b=1 c=1 -> specificity = 11 */

UL OL LI.red {} /* a=0 b=1 c=3 -> specificity = 13 */

LI.red.level {} /* a=0 b=2 c=1 -> specificity = 21 */

#x34y {} /* a=1 b=0 c=0 -> specificity = 100 */

Page 20: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Styling Fonts Colors Position Navigation Forms Images

Page 21: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Fonts Do not change font-family Use presents (%) size Do not bold more than 5% of text Mind the line-height (longer lines – more height)

Page 22: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Colors The less, the better Use spark colors Vary chroma and lumina to pick near colors Never use red unless it is an error!

Page 23: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Position Do not override the style guide template When you think about width:100% use “auto” Remember the box model (paddings are ontop

of the 100%) Float, float float Make floats “display: inline;”

Page 24: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Navigation Don't mask link color schema

Page 25: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Forms Use standard forms Do not style form elements (inputs, buttons) Use fieldset and legend Use a dl element to structure the form

Page 26: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Images

Use image maps#layout-switcher #layout-wide{

background: transparent url( images/tb-layout.gif ) no-repeat left top;

}

#layout-switcher #layout-normal{

background: transparent url( images/tb-layout.gif ) no-repeat -36px top;

}

#layout-switcher #layout-wide:hover{

background-position: left -25px;

}

#layout-switcher #layout-normal:hover{

background-position: -36px -25px;

}

Page 27: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Hacking – Fixing behaviour by errors Aiming a rule at a specific browser in order to

workaround a bug (usually an IE bug) Logical errors Scope errors Syntax errors Priority errors Woodo hacks CSS2 filters

Page 28: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Logical errors Adding “display: inline” to a float.

Floats are block elements (like div). Declaring them as “float” makes them block elements automatically. Why are we telling them they are “in-line”?

Page 29: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Scope errors

* html body{

Background-ccolr: #fff;

}

Page 30: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Syntax errors

.class{

height: 22px; /* all browsers*/

_height: 24px; /* IE6 or less */

*height: 26px; /* IE7 or less */

}

Page 31: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Priority errors

.class{

height: 22px!important;

height: 24px;

}

Page 32: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Woodo hacks Picabu bug – zoom: 1; overflow: hidden; Flickering list elements – height: 1%; HasLayout - zoom: 1; Change rendering order

Page 33: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

CSS2 filters

.menu > ul a {width:auto;}

Page 34: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

General rules

Try to hang your attribute where it is most effective. (minimal repeats and minimal side-effects)

Use context. Our main menu has only one class: menu.

Prefix your class names. (We should have done it as well)

Test in various browsers (IE, FF, Op, Saf/Chr)

Page 35: Using CSS (Cascading Style Sheets) Effie Nadiv Edited by permission from author by Amir Kirsh

Thank You