SlideShare a Scribd company logo
1 of 32
Download to read offline
BEN KHALFALLAH HÉLA
CSSpatternmatchingrulestoselectelementsinthe
documenttree
CSSSELECTORS
2022
CSSRULESTRUCTURE
selector color : red ; background : yellow ; }
{
HTML TAG, CLASS NAME, UNIVERSAL SELECTOR (*)
PROPERTY
VALUE
DECLARATION
DECLARATION BLOCK
PROPERTY
VALUE
DECLARATION
SELECTORSTYPES
Element selectors


Class selectors


ID selectors


Attribute selectors


Combinators


Pseudo-class selectors


Pseudo-elements selectors
ELEMENTSELECTORS
element { property: value; property: value; }
Any (valid) html tag
h2 { color: silver; } Make the text of all h2 elements in the html
document appear in silver.
h1 { color: gray; }


h2 { color: gray; }


h3 { color: gray; }


h4 { color: gray; }


h5 { color: gray; }


h6 { color: gray; }
h1, h2, h3, h4, h5, h6 {


color: gray;


}
GROUPING SELECTORS
h1 { font: 18px Helvetica; }


h1 { color: purple; }


h1 { background: aqua; }
h1 {


font: 18px Helvetica;


color: purple;


background: aqua;


}
GROUPING DECLARATIONS
CLASSSELECTORS
.class-name { property: value; property: value; }


<p class="legal-notice--description">


Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.


</p>


<span>


Contrary to popular belief, Lorem Ipsum is not simply random text


</span>


<p class="legal-notice--description">


Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium
doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore
veritatis et quasi architecto beatae vitae dicta sunt explicabo.


</p>
.legal-notice--description{


font: 18px Helvetica;


color: purple;


background: aqua;


}
HTML :
CSS :
The css styling will only be applied to the element that has a
class attribute with a value of 'legal-notice--description'.
CASE SENSITIVE SELECTOR
COMBININGELEMENTANDCLASSSELECTORS
element.class-name { property: value; property: value; }
p.legal-notice--description{


font-weight: bold;


}
Rule applies only to a speci
fi
c type of element/class combination, it does
not leak over to other elements.


<p class="legal-notice--description">


Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.


</p>


<span class="legal-notice--description">


Contrary to popular belief, Lorem Ipsum is not simply random text


</span>


<p class="legal-notice--description">


Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium
doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore
veritatis et quasi architecto beatae vitae dicta sunt explicabo.


</p>
The selector will match this element (p having a class with the
value ‘legal-notice—description’).
The selector will match this element (p having a class with the
value ‘legal-notice—description’).
The selector will not match this element (span).
CASE SENSITIVE SELECTOR
MULTIPLECLASSESSELECTORS
.class-name-1.class-name-2….. { property: value; property: value; }
.alert {


font-size: 18px;


}


.error{


background-color: red;


}


.message--body{


font-size: 25px;


}


.alert.error{


font-weight: bold;


}


.alert.error.message--body{


background: white;


}


p.alert.error{


font-weight: lighter;


}
<span class="alert error">


This will match the .alert.error selector


</span>


<span class="error alert">


This will also match the .alert.error selector (order does not matter)


</span>


<span class="alert">


This will match .alert selector


</span>


<span class="error">


This will match .error selector


</span>


<span class="alert error message--body">


This will match .alert.error.message--body selector


</span>


<p class="alert error">


This will match the p.alert.error selector


</p>
The selector will match in any order, but the order in which the style
is applied will change (especially for common properties).
HTML :
CSS :
CASE SENSITIVE SELECTOR
IDSELECTORS
#html-id-value { property: value; property: value; }
#article-title {


font-size: 30px;


font-weight: bold;


background: gainsboro;


}


#article-description{


font-size: 18px;


font-weight: lighter;


background: white;


}
HTML :
CSS :
<h4 id="article-title">


1914 translation by H. Rackham


</h4>


<p id="article-description">


Lorem ipsum dolor sit amet, consectetur adipiscing elit.


Sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua.


Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea commodo consequat.


</p>


ID selectors can’t be combined with other IDs.


id attribute must match exactly the value given in the selector.


There should be only one element with a given ID in a document.
CASE SENSITIVE SELECTOR
IT’S NOT RECOMMENDED TO USE ID
SELECTORS, THEY ARE LESS FLEXIBLE,
HARD TO OVERRIDE (HIGHER SPECIFICITY
THAN CLASSES).
ATTRIBUTESELECTORS
Match elements that have a certain attribute with any value :
p[class]{


color: purple;


}
Select all p elements that
have a class attribute with
any value and make their text
color purple.
a[href][title]{


color: green;


}
<p class="error">Oups, this page is not available !</p>
Html tag (element)
Attribute
Content
attribute-name="attribute-value"
= End of html tag
<a


href="https://example.com"


title="Example Home Page"


>


Website (with title)


</a>


<a


href="https://new-site.com"


title="New Site Page"


>


New Website (with title)


</a>
Select any html hyperlink that has both an href and a title attribute (with any value) and make their
text color green.
<a


href="https://my-site.org"


>


Website (without title)


</a>


<a


title="This is not a link"


>


Incorrect link !


</a>
ATTRIBUTESELECTORS
Match elements that have a certain attribute with an exact value :
<p class="error">Oups, this page is not available !</p>
Html tag (element)
Attribute
Content
attribute-name="attribute-value"
= End of html tag
p[class="error"]{


color: purple;


}
Select all p elements that
have a class attribute with
exactly the value ‘error’ and
make their text color purple.
<a


href="https://example.com"


title="Example Home Page"


>


Website (with title)


</a>
a[href="https://example.com"][title="Example Home Page"]{


color: green;


}
CASE SENSITIVE SELECTOR
<a


href="https://new-site.com"


title="New Site Page"


>


New Website (with title)


</a>


<a


href="https://my-site.org"


>


Website (without title)


</a>


<a


title="This is not a link"


>


Incorrect link !


</a>
Select any html hyperlink that has an href attribute with exactly the value ‘https://example.com’ and a
title attribute with exactly the value ‘Example Home Page’ and make their text color green.
ATTRIBUTESELECTORS
Matching one word in a space-separated list (~) :
CASE SENSITIVE SELECTOR
<p


class="alert urgent info"


>


The is an information message!


</p>


<p


class="alert urgent warning"


>


The resource took too long to load!


</p>


<p


class="alert urgent error"


>


The service is unavailable at the moment


</p>
p[class~="alert"] {


color: purple;


font-weight: bold;


font-size: 18px;


}


p[class~="warning"] {


color: orange;


font-size: 12px;


}


p[class~="error"] {


color: red;


}
Select all p elements whose class attribute contains the word ‘alert’.
Select all p elements whose class attribute contains the word ‘warning’.
Select all p elements whose class attribute contains the word ‘error’.
a[title~="Nasa"] {


color: green;


font-weight: light;


font-size: 15px;


}
<a


href="https://www.nasa.gov/missions"


title="Nasa Missions Page"


>


Nasa Missions Page


</a>
<a


href="https://www.nasa.gov/"


title="Nasa Home Page"


>


Nasa Home Page


</a>
<a


href="https://www.spacex.com/"


title="Space X Home Page"


>


Space X Home Page


</a>
Space separated list
Space separated list
Space separated list
title="Nasa Missions Page"
title="NasaMissions Page"
title="Nasa-Missions Page"
THE ORDER OF SELECTORS MATTER !
The word only matches inside a space-separated list (case sensitive).
title="nasa Missions Page"
ATTRIBUTESELECTORS
Matching a substring within an attribute value (*) :
CASE SENSITIVE SELECTOR
THE ORDER OF SELECTORS MATTER !
<h4 class="article-details article-title">


1914 translation by H. Rackham


</h4>


<p class="article-details article-description">


Lorem ipsum dolor sit amet, consectetur adipiscing elit.


Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.


Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat.


</p>


<img


class="article-image"


alt="mission"


src="https://science.nasa.gov/files/science-pink/s3fs-public/styles/
large/public/thumbnails/image/ACE_0.jpg?itok=t2wtXFRN"


/>
h4[class*="details"] {


color: blue;


font-weight: bold;


font-size: 25px;


}


p[class*="details"] {


color: darkgray;


font-weight: light;


font-size: 15px;


}


*[class*="details"] {


text-decoration: underline;


}


*[class*="image"] {


width: 10rem;


height: 10rem;


}


Select all h4 elements whose class attribute contains the
substring ‘details’.
Select all p elements whose class attribute contains the
substring ‘details’.
Select all elements whose class attribute contains the
substring ‘details’.
Select all elements whose class attribute contains the
substring ‘image’.
h4[class~="details"] {


color: blue;


font-weight: bold;


font-size: 25px;


}
~ doesn’t much substring but one word in a
space-separated list.
input[title*="format"] {


background-color: red;


}
<input


type="tel"


title="Telephone number should be formatted as XXX-XXX-XXXX"


pattern="d{3}-d{3}-d{4}"


/>
<input


type="email"


title="Email is a mandatory field"


required


/>
img[class*="image"][alt*="details"] {


width: 20rem;


height: 20rem;


}


img[class~="article-image"][alt*="details"] {


width: 20rem;


height: 20rem;


}
Combining selectors
ATTRIBUTESELECTORS
Matching a substring at the beginning of an attribute value (^) :
CASE SENSITIVE SELECTOR
<img


class="article-details article-image"


alt="ps5-console"


src="https://pbs.twimg.com/media/EiGQrbFXsAAzrpq?format=jpg&name=small"


/>


<img


class="article-details article-image"


alt="xbox-activision"


src="https://image.jeuxvideo.com/medias-md/164253/1642526486-8301-card.jpg"


/>


<img


class="article-details article-image"


alt="xbox-serie-x"


src="https://static.actu.fr/uploads/2019/04/8c976cba-12d8-42ca-b419-c177f84b66bf.jpg"


/>


<img


class="article-details article-image"


alt="kindle-paperwhite"


src="https://img.20mn.fr/zBDkMRUzTDerDDNSZYSbWSk/648x415"


/>
img[alt^="xbox"] {


width: 30rem;


height: 30rem;


border: 2px solid red;


}
Selecting all img elements whose alt attribute starts with ‘xbox’.
<img


class="article-details article-image"


alt="xbox-activision game"


src="https://image.jeuxvideo.com/medias-md/164253/1642526486-8301-card.jpg"


/>
<img


class="article-details article-image"


alt="game xbox-serie-x"


src="https://static.actu.fr/uploads/2019/04/8c976cba-12d8-42ca-b419-c177f84b66bf.jpg"


/>
*[class^="alert"] {


border: 2px solid red;


}
<span class="alert error">


This will match the selector


</span>
<span class="error alert">


This will not match the selector


</span>
<p class="alert">


This will match the selector


</p>


<span class="alerterror">


This will match the selector


</span>


<span class="alert-error">


This will match the selector


</span>
<p class="Alert">


This will match the selector


</p>
ATTRIBUTESELECTORS
Matching a substring at the end of an attribute value ($) :
CASE SENSITIVE SELECTOR
<img


alt="X-ray glow"


src="https://www.sciencenews.org/wp-content/uploads/2022/01/012022_LK_COW_feat-1030x580.jpg"


/>


<img


alt="Neutron star collisions"


src=« https://www.sciencenews.org/wp-content/uploads/2021/11/110221_ec_heavy-elements_feat-1030x580.gif"


/>


<img


alt="Intense drought or flash floods"


src="https://www.sciencenews.org/wp-content/uploads/2022/01/011421_CG_flooding_feat-1030x580.png"


/>


<img


alt="The first magnetar flare"


src="https://www.sciencenews.org/wp-content/uploads/2021/01/011421_lg_magnetar_feat-1030x580.jpg"


/>
img[src$=".jpg"] {


width: 10rem;


height: 10rem;


border: 2px solid red;


}


img[src$=".png"] {


width: 15rem;


height: 15rem;


border: 2px solid yellow;


}
a[href$=".pdf"] {


border: 2px solid purple;


}
<a


href="https://cfm.ehu.es/ricardo/docs/python/Learning_Python.pdf"


title="Learning Python"


>


Learning Python


</a>
<a


href="https://cfm.ehu.es/ricardo/docs/python/Learning_Python.PDF"


title="Learning Python"


>


Learning Python


</a>
<a


href="https://cfm.ehu.es/ricardo/docs/python/Learning_Python.csv"


title="Learning Python"


>


Learning Python Stats


</a>
ATTRIBUTESELECTORS
Case insensitive identi
fi
er (i) :
a[href$=".pdf" i] {


border: 2px solid purple;


}
<a


href="https://cfm.ehu.es/ricardo/docs/python/Learning_Python.pdf"


title="Learning Python"


>


Learning Python


</a>
<a


href="https://cfm.ehu.es/ricardo/docs/python/Learning_Python.PDF"


title="Learning Python"


>


Learning Python


</a>
a[title~="Nasa" i] {


color: green;


font-weight: light;


font-size: 15px;


}
<a


href="https://www.nasa.gov/"


title="Nasa Home Page"


>


Nasa Home Page


</a>
<a


href="https://www.nasa.gov/missions"


title="nasa Misions Page"


>


Nasa Misions Page


</a>
input[title*="format" i] {


background-color: red;


}
<input


type="tel"


title="Phone number should be formatted as XXX-XXX-XXXX"


pattern="d{3}-d{3}-d{4}"


/>
<input


type="tel"


title="phone number should be Formatted as XXX-XXX-XXXX"


pattern="d{3}-d{3}-d{4}"


/>
ATTRIBUTESELECTORS(RECAP)
[my-attribute="attribute-value"]
Select any element with an attribute 'my-attribute'
whose value is exactly equal to ‘attribute-value'.
[my-attribute~="attribute-value"]
Select any element with an attribute ‘my-attribute’
whose value contains the word ‘attribute-value’ in
a space-separated list of words.
[my-attribute*="attribute-value"]
Select any element with an attribute 'my-attribute'
whose value contains the substring ‘attribute-
value'.
[my-attribute^="attribute-value"]
Select any element with an attribute 'my-attribute'
whose value begins with ‘attribute-value'.
[my-attribute$="attribute-value"]
Select any element with an attribute 'my-attribute'
whose value end with ‘attribute-value'.
[my-attribute="attribute-value" i] Case insensitive identi
fi
er (i)
DESCENDANTCOMBINATOR
selector1 selector2 { property: value; }
div span {


color: red;


}
Set the text color to red for any span
element descending from div
<div>


<h4>


1914 translation by H. Rackham


</h4>


<span>


1. This will match


</span>


<span>


<span>


2. This will match


</span>


</span>


</div>
<div>


<p>


<span>3.This will match</span>


</p>


</div>
.article-details span {


color: red;


}
Selector can be class names
<div className="article-details">


<span>


<span>


1. This will match


</span>


</span>


<p>


<span>


<span>


2. This will match


</span>


</span>


</p>


</div>
CHILDCOMBINATOR
selector1 > selector2 { property: value; }
div > span {


color: red;


}
Set the text color to red for any span
element descending from div
<div>


<h4>


1914 translation by H. Rackham


</h4>


<span>


1. This will match


</span>


<span>


<span>


2. This will match


</span>


</span>


</div>
<div>


<p>


<span>3.This will not match</span>


</p>


</div>
( stricter than the descendant combinator )
Elements matched by the second selector must be the immediate
children of the elements matched by the
fi
rst selector :
.article-details > span {


color: red;


}
Selector can be class names
<div>


<h4>


1914 translation by H. Rackham


</h4>


<span>


1. This will not match


</span>


<div className="article-details">


<span>


<span>


2. This will match


</span>


</span>


</div>


</div>
<div>


<p>


<span>3.This will not match</span>


</p>


</div>
ADJACENTSIBLINGCOMBINATOR
former_element + target_element { property: value; }
Select an element that immediately follows another element with the same parent :
h1 + p {


margin-top: 0;


}
Remove the top margin from a paragraph
immediately following an h1 .
Select any p element that immediately follows an h1
element that shares a parent with the p element.
<div>


<h1>


1914 translation by H. Rackham


</h1>


<p>


This paragraph will match


</p>


<p>


This will not match


</p>


</div>
This p isn’t immediately after h1.
GENERALSIBLINGCOMBINATOR
former_element ~ target_element { property: value; }
Select an element that follows another element when both elements share the same parent :
h2 ~ ol {


font-style: italic;


}
The two elements do not need to be adjacent sibling.
Italicize any ol element that follows an h2 element
and also share a parent with h2.
<div>


<h2>


1914 translation by H. Rackham


</h2>


<p>


Contrary to popular belief, Lorem Ipsum is not simply random text.


</p>


<ol>


<li>1. This will match</li>


<li>2. This will match</li>


<li>3. This will match</li>


</ol>


<p>


Contrary to popular belief, Lorem Ipsum is not simply random text.


</p>


<ol>


<li>4. This will match</li>


<li>5. This will match</li>


<li>6. This will match</li>


</ol>


<div>


<ol>


<li>1. This will not match</li>


<li>2. This will not match</li>


<li>3. This will not match</li>


</ol>


</div>


</div>


Follows h2 + shares the same parent
Follows h2 + shares the same parent
Follows h2 but doesn’t shares the same parent
SELECTINGEMPTYELEMENTS
element:empty { property: value; }
Select any element that has no children of any kind, including text nodes (text and whitespace) :
<div>


<p></p>


<p> </p>


<p>




</p>


<p><!--This is a comment--></p>


</div>
This will match
This will not match (white space)
This will not match
This will match (comments are not considered content).
*: empty { display:none;} can hide elements like input,
img, text area, because they don’t have children.
p:empty {


display: none;


}
SELECTINGUNIQUECHILDREN
Select elements when they are the only child element of another element :
element:only-child { property: value; }
img:only-child {


border: 1px solid red;


}
<a href="http://w3.org/">


<img


src="w3.png"


alt="W3C"


/>


</a>
<a href="http://w3.org/">


<img


src="w3.png"


alt="W3C"


/>


<em>The W3C</em>


</a>
This will match : img is the only child of a.
This will not match : img isn’t the only child of a.
Select img that are the only child.
<ol>


<li>


First


<ul>


<li>This list has just one element.</li>


</ul>


</li>


<li>


Second


<ul>


<li>This list has three elements.</li>


<li>This list has three elements.</li>


<li>This list has three elements.</li>


</ul>


</li>


</ol>
li:only-child {


color: red;


list-style-type: square;


}
SELECTINGFIRSTCHILDREN
Select elements that are the
fi
rst children of others elements :
element:
fi
rst-child { property: value; }
<div>


<p>This text is selected!</p>


<p>This text isn't selected.</p>


</div>


<div>


<h2>This text isn't selected: it's not a `p`.</h2>


<p>This text isn't selected.</p>


</div>
p:first-child {


color: lime;


background-color: black;


padding: 5px;


}
ul li:first-child {


color: red;


font-weight: bold;


}
<ul>


<li>Item 1</li>


<li>Item 2</li>


<li>


Item 3


<ul>


<li>Item 3.1</li>


<li>Item 3.2</li>


<li>Item 3.3</li>


</ul>


</li>


</ul>
SELECTINGLASTCHILDREN
Select elements that are the last children of others elements :
element:last-child { property: value; }
<div>


<p>This text isn't selected.</p>


<p>This text is selected!</p>


</div>


<div>


<p>This text isn't selected.</p>


<h2>This text isn't selected: it's not a `p`.</h2>


</div>
p:last-child {


color: lime;


background-color: black;


padding: 5px;


}
<ul>


<li>Item 1</li>


<li>Item 2</li>


<li>


Item 3


<ul>


<li>Item 3.1</li>


<li>Item 3.2</li>


<li>Item 3.3</li>


</ul>


</li>


</ul>
ul li:last-child {


border: 1px solid red;


color: red;


}
SELECTINGEVERYnthCHILDREN
Select elements based on their position among a group of siblings :
element:nth-child(number) { property: value; }
element:nth-child(odd) { property: value; }
element:nth-child(even) { property: value; }
element:nth-child(a * n + b) { property: value; }
element:nth-last-child(number) { property: value; }
element:nth-last-child(odd) { property: value; }
element:nth-last-child(even) { property: value; }
element:nth-last-child(a * n + b) { property: value; }
Count start from top to bottom Count start from bottom to top
n starts from 0 n starts from 0
NEGATIONPSEUDO-CLASS
element:not(selector) { property: value; }
.moreinfo:not(li) {


font-style: italic;


}
Select all elements with a class whose value contains the word
‘moreinfo’ as long as they are not li elements
p:not(:not(p))
Cannot be nested
*.article-link:not(li):not(p){


color: red;


}
Select all elements with class ‘article-link’ that are neither list items
nor paragraphs.
HYPERLINKPSEUDO-CLASSES
:link Matches links that have not yet been visited.
:visited Matches links that have been visited.
a:link{


color: blue;


}


a:visited{


color: red;


}
a.article-details-link:link, a[href^="http"]:link {


color: slateblue;


}


a.article-details-link:visited, a[href^="http"]:visited {


color: maroon;


}
a#article-link-id:link {


color: yellow;


}


a#article-link-id:visited {


color: gray;


}
Class name selectors Element ID selectors
USERACTIONPSEUDO-CLASSES
:hover Matches when an element is hovered.
:active
Matches when an item is being activated by the
user, for example clicked on.
:focus Matches when an element has focus.
UI-STATEPSEUDO-CLASSES
:enabled Represents a user interface element that is in an enabled state.
:disabled Represents a user interface element that is in a disabled state.
:read-only Represents any element that cannot be changed by the user.
:read-write Represents any element that is user-editable.
:checked
Matches when elements such as checkboxes and radiobuttons are
toggled on.
:indeterminate Matches when UI elements are in an indeterminate state.
:default
Matches one or more UI elements that are the default among a set of
elements.
:valid Matches an element with valid contents.
:invalid Matches an element with invalid contents.
:in-range
Applies to elements with range limitations, for example a slider control,
when the selected value is in the allowed range.
:out-of-range
Applies to elements with range limitations, for example a slider control,
when the selected value is outside the allowed range.
:required Matches when a form element is required.
:optional Matches when a form element is optional.
BEFORE&AFTERPSEUDOELEMENTSSELECTORS
::before
Inserts something before the content of each selected
element(s).


p::before {


content: "Read this: ";


}
::after
Inserts something after the content of each selected
element(s).


p::after {


content: " - Remember this";


}
FIRSTLETTERPSEUDOELEMENTSELECTOR
p::first-letter{


color: red;


}


p:first-of-type::first-letter{


font-size: 200%;


}
FIRSTLINEPSEUDOELEMENTSELECTOR
p::first-line{


font-size: 150%;


color: purple;


}

More Related Content

What's hot (20)

Intro to HTML and CSS basics
Intro to HTML and CSS basicsIntro to HTML and CSS basics
Intro to HTML and CSS basics
 
Html5 semantics
Html5 semanticsHtml5 semantics
Html5 semantics
 
Javascript Basics
Javascript BasicsJavascript Basics
Javascript Basics
 
Advanced Cascading Style Sheets
Advanced Cascading Style SheetsAdvanced Cascading Style Sheets
Advanced Cascading Style Sheets
 
Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)
 
CSS
CSSCSS
CSS
 
Basic-CSS-tutorial
Basic-CSS-tutorialBasic-CSS-tutorial
Basic-CSS-tutorial
 
Css box-model
Css box-modelCss box-model
Css box-model
 
CSS Grid
CSS GridCSS Grid
CSS Grid
 
Javascript 101
Javascript 101Javascript 101
Javascript 101
 
CSS notes
CSS notesCSS notes
CSS notes
 
Html / CSS Presentation
Html / CSS PresentationHtml / CSS Presentation
Html / CSS Presentation
 
Web 2 | CSS - Cascading Style Sheets
Web 2 | CSS - Cascading Style SheetsWeb 2 | CSS - Cascading Style Sheets
Web 2 | CSS - Cascading Style Sheets
 
Basic Details of HTML and CSS.pdf
Basic Details of HTML and CSS.pdfBasic Details of HTML and CSS.pdf
Basic Details of HTML and CSS.pdf
 
Css colors
Css   colorsCss   colors
Css colors
 
CSS ppt
CSS pptCSS ppt
CSS ppt
 
CSS
CSSCSS
CSS
 
Introduction to HTML and CSS
Introduction to HTML and CSSIntroduction to HTML and CSS
Introduction to HTML and CSS
 
Introduction to CSS3
Introduction to CSS3Introduction to CSS3
Introduction to CSS3
 
Cascading Style Sheets (CSS) help
Cascading Style Sheets (CSS) helpCascading Style Sheets (CSS) help
Cascading Style Sheets (CSS) help
 

Similar to CSS selectors

An Introduction to Cascading Style Sheets (CSS3)
An Introduction to Cascading Style Sheets (CSS3)An Introduction to Cascading Style Sheets (CSS3)
An Introduction to Cascading Style Sheets (CSS3)Ardee Aram
 
KMUTNB - Internet Programming 4/7
KMUTNB - Internet Programming 4/7KMUTNB - Internet Programming 4/7
KMUTNB - Internet Programming 4/7phuphax
 
The road to &lt;> styled-components: CSS in component-based systems by Max S...
The road to &lt;> styled-components: CSS in component-based systems by Max S...The road to &lt;> styled-components: CSS in component-based systems by Max S...
The road to &lt;> styled-components: CSS in component-based systems by Max S...React London 2017
 
CSS for Developers
CSS for DevelopersCSS for Developers
CSS for DevelopersNascenia IT
 
Cascading Style Sheets - Part 01
Cascading Style Sheets - Part 01Cascading Style Sheets - Part 01
Cascading Style Sheets - Part 01Hatem Mahmoud
 
9- Learn CSS Fundamentals / Pseudo-classes
9- Learn CSS Fundamentals / Pseudo-classes9- Learn CSS Fundamentals / Pseudo-classes
9- Learn CSS Fundamentals / Pseudo-classesIn a Rocket
 
HTML FOR BEGINNERS AND FOR PRACTICE .pdf
HTML FOR BEGINNERS AND FOR PRACTICE .pdfHTML FOR BEGINNERS AND FOR PRACTICE .pdf
HTML FOR BEGINNERS AND FOR PRACTICE .pdfArun Karthik
 
Wordpress plugin development from Scratch
Wordpress plugin development from ScratchWordpress plugin development from Scratch
Wordpress plugin development from ScratchOcaka Alfred
 
Exam 70 480 CSS3 at Jinal Desai .NET
Exam 70 480 CSS3 at Jinal Desai .NETExam 70 480 CSS3 at Jinal Desai .NET
Exam 70 480 CSS3 at Jinal Desai .NETjinaldesailive
 
GDI Seattle - Intro to JavaScript Class 4
GDI Seattle - Intro to JavaScript Class 4GDI Seattle - Intro to JavaScript Class 4
GDI Seattle - Intro to JavaScript Class 4Heather Rock
 
Software programming tools for creating/managing CSS files
Software programming tools for creating/managing CSS filesSoftware programming tools for creating/managing CSS files
Software programming tools for creating/managing CSS filesDinu Suman
 

Similar to CSS selectors (20)

An Introduction to Cascading Style Sheets (CSS3)
An Introduction to Cascading Style Sheets (CSS3)An Introduction to Cascading Style Sheets (CSS3)
An Introduction to Cascading Style Sheets (CSS3)
 
HTML and CSS part 2
HTML and CSS part 2HTML and CSS part 2
HTML and CSS part 2
 
KMUTNB - Internet Programming 4/7
KMUTNB - Internet Programming 4/7KMUTNB - Internet Programming 4/7
KMUTNB - Internet Programming 4/7
 
CSS3 and Selectors
CSS3 and SelectorsCSS3 and Selectors
CSS3 and Selectors
 
The road to &lt;> styled-components: CSS in component-based systems by Max S...
The road to &lt;> styled-components: CSS in component-based systems by Max S...The road to &lt;> styled-components: CSS in component-based systems by Max S...
The road to &lt;> styled-components: CSS in component-based systems by Max S...
 
Sass Essentials
Sass EssentialsSass Essentials
Sass Essentials
 
Broadleaf Presents Thymeleaf
Broadleaf Presents ThymeleafBroadleaf Presents Thymeleaf
Broadleaf Presents Thymeleaf
 
CSS for Developers
CSS for DevelopersCSS for Developers
CSS for Developers
 
Cascading Style Sheets - Part 01
Cascading Style Sheets - Part 01Cascading Style Sheets - Part 01
Cascading Style Sheets - Part 01
 
9- Learn CSS Fundamentals / Pseudo-classes
9- Learn CSS Fundamentals / Pseudo-classes9- Learn CSS Fundamentals / Pseudo-classes
9- Learn CSS Fundamentals / Pseudo-classes
 
Id and class selector
Id and class selectorId and class selector
Id and class selector
 
16-DOMTree.pptx
16-DOMTree.pptx16-DOMTree.pptx
16-DOMTree.pptx
 
HTML FOR BEGINNERS AND FOR PRACTICE .pdf
HTML FOR BEGINNERS AND FOR PRACTICE .pdfHTML FOR BEGINNERS AND FOR PRACTICE .pdf
HTML FOR BEGINNERS AND FOR PRACTICE .pdf
 
Wordpress plugin development from Scratch
Wordpress plugin development from ScratchWordpress plugin development from Scratch
Wordpress plugin development from Scratch
 
Exam 70 480 CSS3 at Jinal Desai .NET
Exam 70 480 CSS3 at Jinal Desai .NETExam 70 480 CSS3 at Jinal Desai .NET
Exam 70 480 CSS3 at Jinal Desai .NET
 
GDI Seattle - Intro to JavaScript Class 4
GDI Seattle - Intro to JavaScript Class 4GDI Seattle - Intro to JavaScript Class 4
GDI Seattle - Intro to JavaScript Class 4
 
Zend framework 04 - forms
Zend framework 04 - formsZend framework 04 - forms
Zend framework 04 - forms
 
Css Selectors
Css SelectorsCss Selectors
Css Selectors
 
Software programming tools for creating/managing CSS files
Software programming tools for creating/managing CSS filesSoftware programming tools for creating/managing CSS files
Software programming tools for creating/managing CSS files
 
Html advance
Html advanceHtml advance
Html advance
 

More from Héla Ben Khalfallah

More from Héla Ben Khalfallah (13)

DATABASE_DATA_STRUCTURE_DEVOXXFRANCE2024.pdf
DATABASE_DATA_STRUCTURE_DEVOXXFRANCE2024.pdfDATABASE_DATA_STRUCTURE_DEVOXXFRANCE2024.pdf
DATABASE_DATA_STRUCTURE_DEVOXXFRANCE2024.pdf
 
Yo messapp
Yo messappYo messapp
Yo messapp
 
Elixir cheatsheet
Elixir cheatsheetElixir cheatsheet
Elixir cheatsheet
 
Elixir in a nutshell - Fundamental Concepts
Elixir in a nutshell - Fundamental ConceptsElixir in a nutshell - Fundamental Concepts
Elixir in a nutshell - Fundamental Concepts
 
Elixir in a nutshell - Ecosystem (session 1)
Elixir in a nutshell - Ecosystem (session 1)Elixir in a nutshell - Ecosystem (session 1)
Elixir in a nutshell - Ecosystem (session 1)
 
FP Using ES6+ (Cheat Sheet)
FP Using ES6+ (Cheat Sheet)FP Using ES6+ (Cheat Sheet)
FP Using ES6+ (Cheat Sheet)
 
WONC DOVA
WONC DOVAWONC DOVA
WONC DOVA
 
Process & Methodologies (1.2)
Process & Methodologies (1.2)Process & Methodologies (1.2)
Process & Methodologies (1.2)
 
Process & Methodologies (1.1)
Process & Methodologies (1.1)Process & Methodologies (1.1)
Process & Methodologies (1.1)
 
Process & Methodologies (1.0)
Process & Methodologies (1.0)Process & Methodologies (1.0)
Process & Methodologies (1.0)
 
La gestion en boucle fermée
La gestion en boucle ferméeLa gestion en boucle fermée
La gestion en boucle fermée
 
Les règles de développement Angular
Les règles de développement AngularLes règles de développement Angular
Les règles de développement Angular
 
Architecture ASIS (iOS)
Architecture ASIS (iOS)Architecture ASIS (iOS)
Architecture ASIS (iOS)
 

Recently uploaded

BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 

Recently uploaded (20)

BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 

CSS selectors

  • 2. CSSRULESTRUCTURE selector color : red ; background : yellow ; } { HTML TAG, CLASS NAME, UNIVERSAL SELECTOR (*) PROPERTY VALUE DECLARATION DECLARATION BLOCK PROPERTY VALUE DECLARATION
  • 3. SELECTORSTYPES Element selectors Class selectors ID selectors Attribute selectors Combinators Pseudo-class selectors Pseudo-elements selectors
  • 4. ELEMENTSELECTORS element { property: value; property: value; } Any (valid) html tag h2 { color: silver; } Make the text of all h2 elements in the html document appear in silver. h1 { color: gray; } h2 { color: gray; } h3 { color: gray; } h4 { color: gray; } h5 { color: gray; } h6 { color: gray; } h1, h2, h3, h4, h5, h6 { color: gray; } GROUPING SELECTORS h1 { font: 18px Helvetica; } h1 { color: purple; } h1 { background: aqua; } h1 { font: 18px Helvetica; color: purple; background: aqua; } GROUPING DECLARATIONS
  • 5. CLASSSELECTORS .class-name { property: value; property: value; } <p class="legal-notice--description"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </p> <span> Contrary to popular belief, Lorem Ipsum is not simply random text </span> <p class="legal-notice--description"> Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. </p> .legal-notice--description{ font: 18px Helvetica; color: purple; background: aqua; } HTML : CSS : The css styling will only be applied to the element that has a class attribute with a value of 'legal-notice--description'. CASE SENSITIVE SELECTOR
  • 6. COMBININGELEMENTANDCLASSSELECTORS element.class-name { property: value; property: value; } p.legal-notice--description{ font-weight: bold; } Rule applies only to a speci fi c type of element/class combination, it does not leak over to other elements. <p class="legal-notice--description"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </p> <span class="legal-notice--description"> Contrary to popular belief, Lorem Ipsum is not simply random text </span> <p class="legal-notice--description"> Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. </p> The selector will match this element (p having a class with the value ‘legal-notice—description’). The selector will match this element (p having a class with the value ‘legal-notice—description’). The selector will not match this element (span). CASE SENSITIVE SELECTOR
  • 7. MULTIPLECLASSESSELECTORS .class-name-1.class-name-2….. { property: value; property: value; } .alert { font-size: 18px; } .error{ background-color: red; } .message--body{ font-size: 25px; } .alert.error{ font-weight: bold; } .alert.error.message--body{ background: white; } p.alert.error{ font-weight: lighter; } <span class="alert error"> This will match the .alert.error selector </span> <span class="error alert"> This will also match the .alert.error selector (order does not matter) </span> <span class="alert"> This will match .alert selector </span> <span class="error"> This will match .error selector </span> <span class="alert error message--body"> This will match .alert.error.message--body selector </span> <p class="alert error"> This will match the p.alert.error selector </p> The selector will match in any order, but the order in which the style is applied will change (especially for common properties). HTML : CSS : CASE SENSITIVE SELECTOR
  • 8. IDSELECTORS #html-id-value { property: value; property: value; } #article-title { font-size: 30px; font-weight: bold; background: gainsboro; } #article-description{ font-size: 18px; font-weight: lighter; background: white; } HTML : CSS : <h4 id="article-title"> 1914 translation by H. Rackham </h4> <p id="article-description"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </p> ID selectors can’t be combined with other IDs. id attribute must match exactly the value given in the selector. There should be only one element with a given ID in a document. CASE SENSITIVE SELECTOR IT’S NOT RECOMMENDED TO USE ID SELECTORS, THEY ARE LESS FLEXIBLE, HARD TO OVERRIDE (HIGHER SPECIFICITY THAN CLASSES).
  • 9. ATTRIBUTESELECTORS Match elements that have a certain attribute with any value : p[class]{ color: purple; } Select all p elements that have a class attribute with any value and make their text color purple. a[href][title]{ color: green; } <p class="error">Oups, this page is not available !</p> Html tag (element) Attribute Content attribute-name="attribute-value" = End of html tag <a href="https://example.com" title="Example Home Page" > Website (with title) </a> <a href="https://new-site.com" title="New Site Page" > New Website (with title) </a> Select any html hyperlink that has both an href and a title attribute (with any value) and make their text color green. <a href="https://my-site.org" > Website (without title) </a> <a title="This is not a link" > Incorrect link ! </a>
  • 10. ATTRIBUTESELECTORS Match elements that have a certain attribute with an exact value : <p class="error">Oups, this page is not available !</p> Html tag (element) Attribute Content attribute-name="attribute-value" = End of html tag p[class="error"]{ color: purple; } Select all p elements that have a class attribute with exactly the value ‘error’ and make their text color purple. <a href="https://example.com" title="Example Home Page" > Website (with title) </a> a[href="https://example.com"][title="Example Home Page"]{ color: green; } CASE SENSITIVE SELECTOR <a href="https://new-site.com" title="New Site Page" > New Website (with title) </a> <a href="https://my-site.org" > Website (without title) </a> <a title="This is not a link" > Incorrect link ! </a> Select any html hyperlink that has an href attribute with exactly the value ‘https://example.com’ and a title attribute with exactly the value ‘Example Home Page’ and make their text color green.
  • 11. ATTRIBUTESELECTORS Matching one word in a space-separated list (~) : CASE SENSITIVE SELECTOR <p class="alert urgent info" > The is an information message! </p> <p class="alert urgent warning" > The resource took too long to load! </p> <p class="alert urgent error" > The service is unavailable at the moment </p> p[class~="alert"] { color: purple; font-weight: bold; font-size: 18px; } p[class~="warning"] { color: orange; font-size: 12px; } p[class~="error"] { color: red; } Select all p elements whose class attribute contains the word ‘alert’. Select all p elements whose class attribute contains the word ‘warning’. Select all p elements whose class attribute contains the word ‘error’. a[title~="Nasa"] { color: green; font-weight: light; font-size: 15px; } <a href="https://www.nasa.gov/missions" title="Nasa Missions Page" > Nasa Missions Page </a> <a href="https://www.nasa.gov/" title="Nasa Home Page" > Nasa Home Page </a> <a href="https://www.spacex.com/" title="Space X Home Page" > Space X Home Page </a> Space separated list Space separated list Space separated list title="Nasa Missions Page" title="NasaMissions Page" title="Nasa-Missions Page" THE ORDER OF SELECTORS MATTER ! The word only matches inside a space-separated list (case sensitive). title="nasa Missions Page"
  • 12. ATTRIBUTESELECTORS Matching a substring within an attribute value (*) : CASE SENSITIVE SELECTOR THE ORDER OF SELECTORS MATTER ! <h4 class="article-details article-title"> 1914 translation by H. Rackham </h4> <p class="article-details article-description"> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </p> <img class="article-image" alt="mission" src="https://science.nasa.gov/files/science-pink/s3fs-public/styles/ large/public/thumbnails/image/ACE_0.jpg?itok=t2wtXFRN" /> h4[class*="details"] { color: blue; font-weight: bold; font-size: 25px; } p[class*="details"] { color: darkgray; font-weight: light; font-size: 15px; } *[class*="details"] { text-decoration: underline; } *[class*="image"] { width: 10rem; height: 10rem; } Select all h4 elements whose class attribute contains the substring ‘details’. Select all p elements whose class attribute contains the substring ‘details’. Select all elements whose class attribute contains the substring ‘details’. Select all elements whose class attribute contains the substring ‘image’. h4[class~="details"] { color: blue; font-weight: bold; font-size: 25px; } ~ doesn’t much substring but one word in a space-separated list. input[title*="format"] { background-color: red; } <input type="tel" title="Telephone number should be formatted as XXX-XXX-XXXX" pattern="d{3}-d{3}-d{4}" /> <input type="email" title="Email is a mandatory field" required /> img[class*="image"][alt*="details"] { width: 20rem; height: 20rem; } img[class~="article-image"][alt*="details"] { width: 20rem; height: 20rem; } Combining selectors
  • 13. ATTRIBUTESELECTORS Matching a substring at the beginning of an attribute value (^) : CASE SENSITIVE SELECTOR <img class="article-details article-image" alt="ps5-console" src="https://pbs.twimg.com/media/EiGQrbFXsAAzrpq?format=jpg&name=small" /> <img class="article-details article-image" alt="xbox-activision" src="https://image.jeuxvideo.com/medias-md/164253/1642526486-8301-card.jpg" /> <img class="article-details article-image" alt="xbox-serie-x" src="https://static.actu.fr/uploads/2019/04/8c976cba-12d8-42ca-b419-c177f84b66bf.jpg" /> <img class="article-details article-image" alt="kindle-paperwhite" src="https://img.20mn.fr/zBDkMRUzTDerDDNSZYSbWSk/648x415" /> img[alt^="xbox"] { width: 30rem; height: 30rem; border: 2px solid red; } Selecting all img elements whose alt attribute starts with ‘xbox’. <img class="article-details article-image" alt="xbox-activision game" src="https://image.jeuxvideo.com/medias-md/164253/1642526486-8301-card.jpg" /> <img class="article-details article-image" alt="game xbox-serie-x" src="https://static.actu.fr/uploads/2019/04/8c976cba-12d8-42ca-b419-c177f84b66bf.jpg" /> *[class^="alert"] { border: 2px solid red; } <span class="alert error"> This will match the selector </span> <span class="error alert"> This will not match the selector </span> <p class="alert"> This will match the selector </p> <span class="alerterror"> This will match the selector </span> <span class="alert-error"> This will match the selector </span> <p class="Alert"> This will match the selector </p>
  • 14. ATTRIBUTESELECTORS Matching a substring at the end of an attribute value ($) : CASE SENSITIVE SELECTOR <img alt="X-ray glow" src="https://www.sciencenews.org/wp-content/uploads/2022/01/012022_LK_COW_feat-1030x580.jpg" /> <img alt="Neutron star collisions" src=« https://www.sciencenews.org/wp-content/uploads/2021/11/110221_ec_heavy-elements_feat-1030x580.gif" /> <img alt="Intense drought or flash floods" src="https://www.sciencenews.org/wp-content/uploads/2022/01/011421_CG_flooding_feat-1030x580.png" /> <img alt="The first magnetar flare" src="https://www.sciencenews.org/wp-content/uploads/2021/01/011421_lg_magnetar_feat-1030x580.jpg" /> img[src$=".jpg"] { width: 10rem; height: 10rem; border: 2px solid red; } img[src$=".png"] { width: 15rem; height: 15rem; border: 2px solid yellow; } a[href$=".pdf"] { border: 2px solid purple; } <a href="https://cfm.ehu.es/ricardo/docs/python/Learning_Python.pdf" title="Learning Python" > Learning Python </a> <a href="https://cfm.ehu.es/ricardo/docs/python/Learning_Python.PDF" title="Learning Python" > Learning Python </a> <a href="https://cfm.ehu.es/ricardo/docs/python/Learning_Python.csv" title="Learning Python" > Learning Python Stats </a>
  • 15. ATTRIBUTESELECTORS Case insensitive identi fi er (i) : a[href$=".pdf" i] { border: 2px solid purple; } <a href="https://cfm.ehu.es/ricardo/docs/python/Learning_Python.pdf" title="Learning Python" > Learning Python </a> <a href="https://cfm.ehu.es/ricardo/docs/python/Learning_Python.PDF" title="Learning Python" > Learning Python </a> a[title~="Nasa" i] { color: green; font-weight: light; font-size: 15px; } <a href="https://www.nasa.gov/" title="Nasa Home Page" > Nasa Home Page </a> <a href="https://www.nasa.gov/missions" title="nasa Misions Page" > Nasa Misions Page </a> input[title*="format" i] { background-color: red; } <input type="tel" title="Phone number should be formatted as XXX-XXX-XXXX" pattern="d{3}-d{3}-d{4}" /> <input type="tel" title="phone number should be Formatted as XXX-XXX-XXXX" pattern="d{3}-d{3}-d{4}" />
  • 16. ATTRIBUTESELECTORS(RECAP) [my-attribute="attribute-value"] Select any element with an attribute 'my-attribute' whose value is exactly equal to ‘attribute-value'. [my-attribute~="attribute-value"] Select any element with an attribute ‘my-attribute’ whose value contains the word ‘attribute-value’ in a space-separated list of words. [my-attribute*="attribute-value"] Select any element with an attribute 'my-attribute' whose value contains the substring ‘attribute- value'. [my-attribute^="attribute-value"] Select any element with an attribute 'my-attribute' whose value begins with ‘attribute-value'. [my-attribute$="attribute-value"] Select any element with an attribute 'my-attribute' whose value end with ‘attribute-value'. [my-attribute="attribute-value" i] Case insensitive identi fi er (i)
  • 17. DESCENDANTCOMBINATOR selector1 selector2 { property: value; } div span { color: red; } Set the text color to red for any span element descending from div <div> <h4> 1914 translation by H. Rackham </h4> <span> 1. This will match </span> <span> <span> 2. This will match </span> </span> </div> <div> <p> <span>3.This will match</span> </p> </div> .article-details span { color: red; } Selector can be class names <div className="article-details"> <span> <span> 1. This will match </span> </span> <p> <span> <span> 2. This will match </span> </span> </p> </div>
  • 18. CHILDCOMBINATOR selector1 > selector2 { property: value; } div > span { color: red; } Set the text color to red for any span element descending from div <div> <h4> 1914 translation by H. Rackham </h4> <span> 1. This will match </span> <span> <span> 2. This will match </span> </span> </div> <div> <p> <span>3.This will not match</span> </p> </div> ( stricter than the descendant combinator ) Elements matched by the second selector must be the immediate children of the elements matched by the fi rst selector : .article-details > span { color: red; } Selector can be class names <div> <h4> 1914 translation by H. Rackham </h4> <span> 1. This will not match </span> <div className="article-details"> <span> <span> 2. This will match </span> </span> </div> </div> <div> <p> <span>3.This will not match</span> </p> </div>
  • 19. ADJACENTSIBLINGCOMBINATOR former_element + target_element { property: value; } Select an element that immediately follows another element with the same parent : h1 + p { margin-top: 0; } Remove the top margin from a paragraph immediately following an h1 . Select any p element that immediately follows an h1 element that shares a parent with the p element. <div> <h1> 1914 translation by H. Rackham </h1> <p> This paragraph will match </p> <p> This will not match </p> </div> This p isn’t immediately after h1.
  • 20. GENERALSIBLINGCOMBINATOR former_element ~ target_element { property: value; } Select an element that follows another element when both elements share the same parent : h2 ~ ol { font-style: italic; } The two elements do not need to be adjacent sibling. Italicize any ol element that follows an h2 element and also share a parent with h2. <div> <h2> 1914 translation by H. Rackham </h2> <p> Contrary to popular belief, Lorem Ipsum is not simply random text. </p> <ol> <li>1. This will match</li> <li>2. This will match</li> <li>3. This will match</li> </ol> <p> Contrary to popular belief, Lorem Ipsum is not simply random text. </p> <ol> <li>4. This will match</li> <li>5. This will match</li> <li>6. This will match</li> </ol> <div> <ol> <li>1. This will not match</li> <li>2. This will not match</li> <li>3. This will not match</li> </ol> </div> </div> Follows h2 + shares the same parent Follows h2 + shares the same parent Follows h2 but doesn’t shares the same parent
  • 21. SELECTINGEMPTYELEMENTS element:empty { property: value; } Select any element that has no children of any kind, including text nodes (text and whitespace) : <div> <p></p> <p> </p> <p> </p> <p><!--This is a comment--></p> </div> This will match This will not match (white space) This will not match This will match (comments are not considered content). *: empty { display:none;} can hide elements like input, img, text area, because they don’t have children. p:empty { display: none; }
  • 22. SELECTINGUNIQUECHILDREN Select elements when they are the only child element of another element : element:only-child { property: value; } img:only-child { border: 1px solid red; } <a href="http://w3.org/"> <img src="w3.png" alt="W3C" /> </a> <a href="http://w3.org/"> <img src="w3.png" alt="W3C" /> <em>The W3C</em> </a> This will match : img is the only child of a. This will not match : img isn’t the only child of a. Select img that are the only child. <ol> <li> First <ul> <li>This list has just one element.</li> </ul> </li> <li> Second <ul> <li>This list has three elements.</li> <li>This list has three elements.</li> <li>This list has three elements.</li> </ul> </li> </ol> li:only-child { color: red; list-style-type: square; }
  • 23. SELECTINGFIRSTCHILDREN Select elements that are the fi rst children of others elements : element: fi rst-child { property: value; } <div> <p>This text is selected!</p> <p>This text isn't selected.</p> </div> <div> <h2>This text isn't selected: it's not a `p`.</h2> <p>This text isn't selected.</p> </div> p:first-child { color: lime; background-color: black; padding: 5px; } ul li:first-child { color: red; font-weight: bold; } <ul> <li>Item 1</li> <li>Item 2</li> <li> Item 3 <ul> <li>Item 3.1</li> <li>Item 3.2</li> <li>Item 3.3</li> </ul> </li> </ul>
  • 24. SELECTINGLASTCHILDREN Select elements that are the last children of others elements : element:last-child { property: value; } <div> <p>This text isn't selected.</p> <p>This text is selected!</p> </div> <div> <p>This text isn't selected.</p> <h2>This text isn't selected: it's not a `p`.</h2> </div> p:last-child { color: lime; background-color: black; padding: 5px; } <ul> <li>Item 1</li> <li>Item 2</li> <li> Item 3 <ul> <li>Item 3.1</li> <li>Item 3.2</li> <li>Item 3.3</li> </ul> </li> </ul> ul li:last-child { border: 1px solid red; color: red; }
  • 25. SELECTINGEVERYnthCHILDREN Select elements based on their position among a group of siblings : element:nth-child(number) { property: value; } element:nth-child(odd) { property: value; } element:nth-child(even) { property: value; } element:nth-child(a * n + b) { property: value; } element:nth-last-child(number) { property: value; } element:nth-last-child(odd) { property: value; } element:nth-last-child(even) { property: value; } element:nth-last-child(a * n + b) { property: value; } Count start from top to bottom Count start from bottom to top n starts from 0 n starts from 0
  • 26. NEGATIONPSEUDO-CLASS element:not(selector) { property: value; } .moreinfo:not(li) { font-style: italic; } Select all elements with a class whose value contains the word ‘moreinfo’ as long as they are not li elements p:not(:not(p)) Cannot be nested *.article-link:not(li):not(p){ color: red; } Select all elements with class ‘article-link’ that are neither list items nor paragraphs.
  • 27. HYPERLINKPSEUDO-CLASSES :link Matches links that have not yet been visited. :visited Matches links that have been visited. a:link{ color: blue; } a:visited{ color: red; } a.article-details-link:link, a[href^="http"]:link { color: slateblue; } a.article-details-link:visited, a[href^="http"]:visited { color: maroon; } a#article-link-id:link { color: yellow; } a#article-link-id:visited { color: gray; } Class name selectors Element ID selectors
  • 28. USERACTIONPSEUDO-CLASSES :hover Matches when an element is hovered. :active Matches when an item is being activated by the user, for example clicked on. :focus Matches when an element has focus.
  • 29. UI-STATEPSEUDO-CLASSES :enabled Represents a user interface element that is in an enabled state. :disabled Represents a user interface element that is in a disabled state. :read-only Represents any element that cannot be changed by the user. :read-write Represents any element that is user-editable. :checked Matches when elements such as checkboxes and radiobuttons are toggled on. :indeterminate Matches when UI elements are in an indeterminate state. :default Matches one or more UI elements that are the default among a set of elements. :valid Matches an element with valid contents. :invalid Matches an element with invalid contents. :in-range Applies to elements with range limitations, for example a slider control, when the selected value is in the allowed range. :out-of-range Applies to elements with range limitations, for example a slider control, when the selected value is outside the allowed range. :required Matches when a form element is required. :optional Matches when a form element is optional.
  • 30. BEFORE&AFTERPSEUDOELEMENTSSELECTORS ::before Inserts something before the content of each selected element(s). p::before { content: "Read this: "; } ::after Inserts something after the content of each selected element(s). p::after { content: " - Remember this"; }