introduction to css selectors
DESCRIPTION
CSS selectors form the basis of Cascading Style Sheets, allowing us to target specific elements within a HTML document and apply style.TRANSCRIPT
7/21/2019 Introduction to CSS Selectors
http://slidepdf.com/reader/full/introduction-to-css-selectors 1/4
Introduction to CSS selectorsJun 22, 2015 15:30 pm
Whether you're new to CSS or are a seasoned pro looking to reresh yourknowledge, this guide to CSS sele!tors is or you"
CSS sele!tors orm the #asis o Cas!ading Style Sheets, allowing us to target spe!ii!
elements within a $%& do!ument and apply style"Whether you're new to CSS or are a seasoned proessional looking to reresh yourknowledge, this guide to CSS sele!tors is or you"
Selector syntax (s you pro#a#ly know, CSS has a parti!ular synta) they re*uire in order to #eunderstood #y we# #rowsers:
%he sele!tor des!ri#es the element+s you would like to !hange" %he property des!ri#esthe whi!h aspe!t o the element you would like to alter +e"g", !olor, height, ont-amily,and the .alue pro.ides how you would like it to !hange +e"g", a !olor, num#er o pi)els,or typea!e"
Element selectors/lement sele!tors are the most #asi! CSS sele!tors you !an use: they !an #e used totarget spe!ii! elements within a page" or e)ample, the e)ample #elow would make allparagraph te)t in your do!ument red:
1" p 2" color : red3"
Descendant selectors4es!endant sele!tors !an #e used to sele!t spe!ii! elements within another element"or e)ample, to sele!t all list items in an unordered list in your $%& do!ument, you!ould use the ollowing des!endant sele!tor:
1" ul li 2" border-bottom: 1p) gray solid3"
(nd you !an mi) and mat!h the sele!tors #elow with other types o sele!tor, too
7/21/2019 Introduction to CSS Selectors
http://slidepdf.com/reader/full/introduction-to-css-selectors 2/4
Classes and IDs selectorsClass and 64 sele!tors are used to sele!t elements whi!h ha.e a spe!ii! !lass or 64.alue assigned to them" or e)ample, to sele!t all elements in a $%& do!ument with a!lass o 'spaghetti', you !ould use the ollowing !lass sele!tor:
1" "spaghetti
2" background-color : green3" Similarly, to sele!t an element in your do!ument with an 64 o 'usilli' you !an use theollowing synta):
1" 7usilli 2" background-color : yellow3" 6t's worth noting that, in .alid $%&, no two elements should ha.e the same 64 +that's
why it's an 64: it's a uni*ue .alue or that element, and many rontend we# de.elopersa.oid using 64s in your CSS sele!tors"%hese are a type o attri#ute sele!tor, #ut ha.e their own synta) as they were histori!allythe primary way we target elements in CSS"
Chained CSS selectorsChained CSS sele!tors allow you to target elements in your $%& do!ument with two or more !lasses assigned to them" or e)ample, to sele!t all elements with a !lass o #oth'spaghetti' and 'penne':1" "spaghetti"penne 2" background: blue
3" 8ote that there is no spa!e !hara!ter #etween the two !lasses a#o.e +otherwise youwould #e !reating a des!endant sele!tor"
Adjacency selector (d9a!en!y sele!tors allow you to target elements whi!h are ad9a!ent +ne)t to ea!hother" or e)ample, to sele!t all paragraphs whi!h dire!tly ollow a h1 element, you!ould use the ollowing CSS:
1" h1 p
2" color : blue3" %he ad9a!en!y sele!tor sele!ts the se!ond element listed only i it is dire!tly ne)t to theirst element in your $%& do!ument, and does not take ee!t i there is anotherelement in#etween"
7/21/2019 Introduction to CSS Selectors
http://slidepdf.com/reader/full/introduction-to-css-selectors 3/4
General sibling selectors;eneral si#ling sele!tors work a little like ad9a!en!y sele!tors des!ri#ed a#o.e, only witha more generi! s!ope" 6magine the ollowing $%& in your do!ument:
1" <arti!le=2" <p=Spaghetti<>p=
3" <p=usilli<>p=?" <div=@enne<>div=5" <p=%agliatelle<>p=A" <>arti!le= Bsing the ollowing general si#ling sele!tor sele!ts all #ut the irst paragraph within thearti!le element:
1" arti!le p p 2" color : red3"
Child selectorsCSS !hild sele!tors !an #e used to target only the irst-le.el !hild element within nestedelements:
1" ul = li 2" ont-!eight: #old3" %his is useul i you ha.e nested elements su!h as in a dropdown menu !ontained withinan unordered list +ul element, and only want to style the irst le.el list items"
Attribute selectors (ttri#ute sele!tors are an intensely powerul way o targeting elements with spe!ii!attri#utes, and there are *uite a ew ways to mat!h elements with attri#ute sele!tors"irstly, you !an mat!h an element whi!h has a parti!ular attri#ute +e.en i that attri#utehas no .alue using this synta):
1" inputDstyleE 2" display: inline3"
%he a#o.e e)ample would mat!h all input elements o any type whi!h ha.e a styleattri#ute"Fou !an also mat!h elements whi!h ha.e an attri#ute with an e)a!t .alue using theollowing CSS synta):
1" inputDtypeGHpasswordHE 2" background: yellow3"
7/21/2019 Introduction to CSS Selectors
http://slidepdf.com/reader/full/introduction-to-css-selectors 4/4
Fou !an use the ne)t attri#ute sele!tor synta) to mat!h elements whi!h ha.e an attri#utewhi!h !ontains an e)a!t .alue, where the .alues are separated #y spa!e !hara!ters"%his !an #e useul to mat!h a spe!ii! attri#ute .alue where there are multiple !lasseson an element, su!h as in this e)ample whi!h sele!ts all di. elements in your $%&do!ument whi!h ha.e a title attri#ute !ontaining 'spaghetti':
1" di.DtitleGHspaghettiHE 2" !idth: 20I3" Similarly, the ne)t attri#ute sele!tor !an #e used to mat!h elements with attri#ute .aluesseparated #y hyphen !hara!ters" %his is parti!ularly useul i you want to sele!t elementsin a parti!ular root language" %he #elow e)ample mat!hes all paragraphs in /nglish,whether it is (meri!an /nglish, ritish /nglish, (ustralian /nglish or any other .ariant"
1" pDlangKGHenHE 2" color : green3" %he prei) attri#ute sele!tor allows you to sele!t attri#ute .alues whi!h #egin with a!ertain .alue" %he e)ample #elow will sele!t all di. elements in your $%& do!umentwhi!h ha.e a !lass whi!h starts '!ol':
1" di.D!lassLGH!olHE 2" margin: 0 1I3" %he sui) attri#ute sele!tor gi.es you the power to sele!t elements whi!h ha.e anattri#ute .alue ending with a parti!ular phrase" %his !an #e useul to sele!t all links inyour $%& do!uments to @4s, or e)ample:
1" aDhre GMGH"pdHE 2" background-image: url+Hpd"pngH3" inally, the wild!ard attri#ute sele!tor allows you to sele!t elements with an attri#ute.alue whi!h mat!hes at least on!e in the attri#ute" %he e)ample #elow will mat!h anylink element whi!h !ontains 'spaghetti' in the hre attri#ute:
1" aDhre NGHspaghettiHE 2" ont-si"e: 1"25rem3"
(s you !an see, there are a wide range o CSS sele!tors to !hoose rom, and CSS?looks set to introdu!e many new sele!tors to gi.e you a greater !hoi!e when sele!tingelements"