SlideShare a Scribd company logo
1 of 21
CSS Selectors
What the ^#.*:()~[]="">+$
Thank You
     Smashing CSS:
Professional Techniques
  for Modern Layout
     by Eric Meyer
Selectors
•   Used to select an element
•   Elements
•   Pseudo-elements
•   By element, ID, class, other
         H1      # . ^*:()~[]="">+$
Specificity
• Who will win?
                 HTML                                            CSS
<h1 id="title">Chuck ipsum</h1>            h1 { color: red; }
<p>                                        h1#title { color: green; }
Chuck Norris' tears cure cancer. Too bad
he has never cried.
</p>                                                           Output
                                           Chuck ipsum
                                           Chuck Norris' tears cure cancer. Too bad he
                                           has never cried.
Specificity
• Green
• 0,1,0,0 versus 0,0,0,1
                 HTML                                            CSS
<h1 id="title">Chuck ipsum</h1>            h1 { color: red; }
<p>                                        h1#title { color: green; }
Chuck Norris' tears cure cancer. Too bad
he has never cried.
</p>                                                           Output
                                           Chuck ipsum
                                           Chuck Norris' tears cure cancer. Too bad he
                                           has never cried.
Specificity
• 0,0,0,1 vs. 0,0,0,2 vs. 0,0,0,3
                HTML                                            CSS
<p>                                     p { color: red; }
Chuck Norris <span>is</span> a man      p span { color: green; }
of few words. Chuck Norris <span>is     p span i { color: blue; }
<i>not</i></span> a man of few
roundhouse kicks to the face.                                 Output
</p>                                    Chuck Norris is a man of few words. Chuck
                                        Norris is not a man of few roundhouse kicks to
                                        the face.

“Decorating your markup with classes and ids and matching purely on those
while avoiding all uses of sibling, descendant and child selectors will actually
make a page perform significantly better in all browsers.”
~ David Hyatt
Architect for Safari and WebKit, also worked on Mozilla, Camino, and Firefox
Specificity
• They can build on each other
              HTML                                            CSS
<h1 id="title"                       h1 { font-weight: bold; }
class="green">Chuck ipsum</h1>       h1#title { font-size: 2em; }
<p>                                  .green { color: green; }
Chuck Norris does not use spell
check. If he happens to misspell a                          Output
word, Oxford will simply change
the actual spelling of it.
                                     Chuck ipsum
                                     Chuck Norris does not use spell check. If he
</p>
                                     happens to misspell a word, Oxford will simply
                                     change the actual spelling of it.
Specificity
• !important
              HTML                                          CSS
<h1 id="title">Chuck ipsum</h1>     h1 { color: red !important; }
<p>                                 h1#title { color: green; }
When Chuck Norris had surgery,
the anesthesia was applied to the
doctors.                                                  Output
</p>                                Chuck ipsum
                                    When Chuck Norris had surgery, the anesthesia
                                    was applied to the doctors.

“Even though there are solutions, it is still a massive pain for users, so please,
don’t use ‘!important’ unless you have no other option!”
~ James Padolsey
H1 # .
•   Element name
•   # for ID (there can be only one per page)
•   . for class (as many as you want)
•   Multi-classes
                  HTML                                          CSS
<p>                                      .red { color: red; }
Chuck Norris counted to infinity -       .italic { font-style: italic; }
<span class="red italic">twice</span>.
</p>
                                                              Output
                                         Chuck Norris counted to infinity - twice.
*
• Universal selector
• Not wild card (H* does not work)
                  HTML                                              CSS
<p>                                          p * { color: red; }
If you <i>can see</i> Chuck Norris, he can
see you. If you <b>can't see</b> Chuck
Norris you may be only seconds away from
<a href="death.htm">death</a>.                                     Output
</p>                                         If you can see Chuck Norris, he can see
                                             you. If you can't see Chuck Norris you
                                             may be only seconds away from death.
[]
• Attribute selection
                  HTML                                         CSS
<p>                                         a[href] { background: url("data:image…")
The <a href="http://greatwall-of-           no-repeat scroll right center transparent;
china.com">Great Wall of China</a> was        padding-right: 13px;
originally created to keep <a               }
href="chuck.htm">Chuck Norris</a> out. It                    Output
failed miserably.
</p>                                        The Great Wall of China was originally
                                            created to keep Chuck Norris out. It
                                            failed miserably.
[=""]
• Attribute selection with criteria
• Exact match
                  HTML                                        CSS
<p>                                         a[href="http://greatwall-of-china.com"] {
The <a href="http://greatwall-of-           background: url("data:image…") no-
china.com">Great Wall of China</a> was      repeat scroll right center transparent;
originally created to keep <a                 padding-right: 13px;
href="chuck.htm">Chuck Norris</a> out. It   }
failed miserably.                                            Output
</p>
                                            The Great Wall of China was originally
                                            created to keep Chuck Norris out. It
                                            failed miserably.
[*=""]
• Attribute selection with wild card criteria
• Contains (*="http" not ="http*")
                  HTML                                         CSS
<p>                                         a[href*="http"] {
The <a href="http://greatwall-of-           background: url("data:image…") no-
china.com">Great Wall of China</a> was      repeat scroll right center transparent;
originally created to keep <a                 padding-right: 13px;
href="chuck.htm">Chuck Norris</a> out. It   }
failed miserably.                                            Output
</p>
                                            The Great Wall of China was originally
                                            created to keep Chuck Norris out. It
                                            failed miserably.
[^=""]
• Attribute selection with starting letters
                  HTML                                         CSS
<p>                                         a[href^="http"] {
The <a href="http://greatwall-of-           background: url("data:image…") no-
china.com">Great Wall of China</a> was      repeat scroll right center transparent;
originally created to keep <a                 padding-right: 13px;
href="chuck.htm">Chuck Norris</a> out. It   }
failed miserably.                                            Output
</p>
                                            The Great Wall of China was originally
                                            created to keep Chuck Norris out. It
                                            failed miserably.
[$=""]
• Attribute selection with ending letters
                  HTML                                         CSS
<p>                                         a[href$=".com"] {
The <a href="http://greatwall-of-           background: url("data:image…") no-
china.com">Great Wall of China</a> was      repeat scroll right center transparent;
originally created to keep <a                 padding-right: 13px;
href="chuck.htm">Chuck Norris</a> out. It   }
failed miserably.                                            Output
</p>
                                            The Great Wall of China was originally
                                            created to keep Chuck Norris out. It
                                            failed miserably.
>
• Child selection
                   HTML                                                  CSS
<div>                                              div li { color: red; }
<p>                                                div > li { color: green; }
Although it is not common knowledge,
there are actually three sides to the Force:
</p>                                                                  Output
<ol>                                               Although it is not common knowledge,
  <li>the light side</li>                          there are actually three sides to the
  <li>the dark side</li>                           Force:
  <li>and Chuck Norris</li>                        the light side
</ol>                                              the dark side
</div>                                             and Chuck Norris
+
• Immediate child selection
                   HTML                                                 CSS
<div>                                              p + ol { color: red; }
<p>
Although it is not common knowledge,
there are actually three sides to the Force:
</p>                                                                  Output
<ol>                                               Although it is not common knowledge,
  <li>the light side</li>                          there are actually three sides to the
  <li>the dark side</li>                           Force:
</ol>                                              the light side
<ol>                                               the dark side
  <li>and Chuck Norris</li>                        and Chuck Norris
</ol>
</div>
~
• Sibling selection
                   HTML                                                 CSS
<div>                                              p ~ ol { color: red; }
<p>
Although it is not common knowledge,
there are actually three sides to the Force:
</p>                                                                  Output
<ol>                                               Although it is not common knowledge,
  <li>the light side</li>                          there are actually three sides to the
  <li>the dark side</li>                           Force:
</ol>                                              the light side
<ol>                                               the dark side
  <li>and Chuck Norris</li>                        and Chuck Norris
</ol>
</div>
:
• Pseudo Elements
    CSS 2.1            CSS 2.1                      CSS 3
 Pseudo-classes    Pseudo-elements              Pseudo-classes
:link             ::first-line       :target               :last-of-type
:visited          ::first-letter     :root                 :only-of-type
:hover            ::before           :nth-child()          :only-child
:focus            ::after            :nth-of-type()        :last-child
:active                              :nth-last-of-type()   :empty
:first-child                         :first-of-type        :not()
:lang()                                                    :enabled
                                                           :disabled
                                                           :checked
Pseudo Elements
                 HTML                             CSS
<table>                        td, th { border: 1px black solid; padding:
  <tr>                         5px; }
    <th>Name</th>              th { background-color: black; color:
    <th>Super Power</th>       white; }
  </tr>                        tr:nth-child(odd) td {
  <tr>                         background-color: gray;
    <td>Chuck Norris</td>      color: white;
    <td>roundhouse kick</td>   }
  </tr>
  <tr>                                           Output
    <td>MC Hammer</td>
    <td>parachute pants</td>
  </tr>
  <tr>
    <td>Mister Miyagi</td>
    <td>waxing fools</td>
  </tr>
</table>
Connect
                 StewShack.com
              @ StewShack
  facebook.com/ StewShack
linkedin.com/in/ StewShack

More Related Content

Recently uploaded

TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024Stephen Perrenod
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxFIDO Alliance
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxFIDO Alliance
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfFIDO Alliance
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfFIDO Alliance
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxFIDO Alliance
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxFIDO Alliance
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...FIDO Alliance
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightSafe Software
 
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The InsideCollecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The InsideStefan Dietze
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingScyllaDB
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...panagenda
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuidePixlogix Infotech
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGDSC PJATK
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...FIDO Alliance
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...ScyllaDB
 
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Paige Cruz
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsLeah Henrickson
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?Mark Billinghurst
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc
 

Recently uploaded (20)

TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024TopCryptoSupers 12thReport OrionX May2024
TopCryptoSupers 12thReport OrionX May2024
 
Intro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptxIntro to Passkeys and the State of Passwordless.pptx
Intro to Passkeys and the State of Passwordless.pptx
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptx
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
 
ADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptxADP Passwordless Journey Case Study.pptx
ADP Passwordless Journey Case Study.pptx
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The InsideCollecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream Processing
 
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
Easier, Faster, and More Powerful – Alles Neu macht der Mai -Wir durchleuchte...
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate Guide
 
Google I/O Extended 2024 Warsaw
Google I/O Extended 2024 WarsawGoogle I/O Extended 2024 Warsaw
Google I/O Extended 2024 Warsaw
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
 
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
Observability Concepts EVERY Developer Should Know (DevOpsDays Seattle)
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
TrustArc Webinar - Unified Trust Center for Privacy, Security, Compliance, an...
 

Featured

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 

Featured (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

Css selectors

  • 1. CSS Selectors What the ^#.*:()~[]="">+$
  • 2. Thank You Smashing CSS: Professional Techniques for Modern Layout by Eric Meyer
  • 3. Selectors • Used to select an element • Elements • Pseudo-elements • By element, ID, class, other H1 # . ^*:()~[]="">+$
  • 4. Specificity • Who will win? HTML CSS <h1 id="title">Chuck ipsum</h1> h1 { color: red; } <p> h1#title { color: green; } Chuck Norris' tears cure cancer. Too bad he has never cried. </p> Output Chuck ipsum Chuck Norris' tears cure cancer. Too bad he has never cried.
  • 5. Specificity • Green • 0,1,0,0 versus 0,0,0,1 HTML CSS <h1 id="title">Chuck ipsum</h1> h1 { color: red; } <p> h1#title { color: green; } Chuck Norris' tears cure cancer. Too bad he has never cried. </p> Output Chuck ipsum Chuck Norris' tears cure cancer. Too bad he has never cried.
  • 6. Specificity • 0,0,0,1 vs. 0,0,0,2 vs. 0,0,0,3 HTML CSS <p> p { color: red; } Chuck Norris <span>is</span> a man p span { color: green; } of few words. Chuck Norris <span>is p span i { color: blue; } <i>not</i></span> a man of few roundhouse kicks to the face. Output </p> Chuck Norris is a man of few words. Chuck Norris is not a man of few roundhouse kicks to the face. “Decorating your markup with classes and ids and matching purely on those while avoiding all uses of sibling, descendant and child selectors will actually make a page perform significantly better in all browsers.” ~ David Hyatt Architect for Safari and WebKit, also worked on Mozilla, Camino, and Firefox
  • 7. Specificity • They can build on each other HTML CSS <h1 id="title" h1 { font-weight: bold; } class="green">Chuck ipsum</h1> h1#title { font-size: 2em; } <p> .green { color: green; } Chuck Norris does not use spell check. If he happens to misspell a Output word, Oxford will simply change the actual spelling of it. Chuck ipsum Chuck Norris does not use spell check. If he </p> happens to misspell a word, Oxford will simply change the actual spelling of it.
  • 8. Specificity • !important HTML CSS <h1 id="title">Chuck ipsum</h1> h1 { color: red !important; } <p> h1#title { color: green; } When Chuck Norris had surgery, the anesthesia was applied to the doctors. Output </p> Chuck ipsum When Chuck Norris had surgery, the anesthesia was applied to the doctors. “Even though there are solutions, it is still a massive pain for users, so please, don’t use ‘!important’ unless you have no other option!” ~ James Padolsey
  • 9. H1 # . • Element name • # for ID (there can be only one per page) • . for class (as many as you want) • Multi-classes HTML CSS <p> .red { color: red; } Chuck Norris counted to infinity - .italic { font-style: italic; } <span class="red italic">twice</span>. </p> Output Chuck Norris counted to infinity - twice.
  • 10. * • Universal selector • Not wild card (H* does not work) HTML CSS <p> p * { color: red; } If you <i>can see</i> Chuck Norris, he can see you. If you <b>can't see</b> Chuck Norris you may be only seconds away from <a href="death.htm">death</a>. Output </p> If you can see Chuck Norris, he can see you. If you can't see Chuck Norris you may be only seconds away from death.
  • 11. [] • Attribute selection HTML CSS <p> a[href] { background: url("data:image…") The <a href="http://greatwall-of- no-repeat scroll right center transparent; china.com">Great Wall of China</a> was padding-right: 13px; originally created to keep <a } href="chuck.htm">Chuck Norris</a> out. It Output failed miserably. </p> The Great Wall of China was originally created to keep Chuck Norris out. It failed miserably.
  • 12. [=""] • Attribute selection with criteria • Exact match HTML CSS <p> a[href="http://greatwall-of-china.com"] { The <a href="http://greatwall-of- background: url("data:image…") no- china.com">Great Wall of China</a> was repeat scroll right center transparent; originally created to keep <a padding-right: 13px; href="chuck.htm">Chuck Norris</a> out. It } failed miserably. Output </p> The Great Wall of China was originally created to keep Chuck Norris out. It failed miserably.
  • 13. [*=""] • Attribute selection with wild card criteria • Contains (*="http" not ="http*") HTML CSS <p> a[href*="http"] { The <a href="http://greatwall-of- background: url("data:image…") no- china.com">Great Wall of China</a> was repeat scroll right center transparent; originally created to keep <a padding-right: 13px; href="chuck.htm">Chuck Norris</a> out. It } failed miserably. Output </p> The Great Wall of China was originally created to keep Chuck Norris out. It failed miserably.
  • 14. [^=""] • Attribute selection with starting letters HTML CSS <p> a[href^="http"] { The <a href="http://greatwall-of- background: url("data:image…") no- china.com">Great Wall of China</a> was repeat scroll right center transparent; originally created to keep <a padding-right: 13px; href="chuck.htm">Chuck Norris</a> out. It } failed miserably. Output </p> The Great Wall of China was originally created to keep Chuck Norris out. It failed miserably.
  • 15. [$=""] • Attribute selection with ending letters HTML CSS <p> a[href$=".com"] { The <a href="http://greatwall-of- background: url("data:image…") no- china.com">Great Wall of China</a> was repeat scroll right center transparent; originally created to keep <a padding-right: 13px; href="chuck.htm">Chuck Norris</a> out. It } failed miserably. Output </p> The Great Wall of China was originally created to keep Chuck Norris out. It failed miserably.
  • 16. > • Child selection HTML CSS <div> div li { color: red; } <p> div > li { color: green; } Although it is not common knowledge, there are actually three sides to the Force: </p> Output <ol> Although it is not common knowledge, <li>the light side</li> there are actually three sides to the <li>the dark side</li> Force: <li>and Chuck Norris</li> the light side </ol> the dark side </div> and Chuck Norris
  • 17. + • Immediate child selection HTML CSS <div> p + ol { color: red; } <p> Although it is not common knowledge, there are actually three sides to the Force: </p> Output <ol> Although it is not common knowledge, <li>the light side</li> there are actually three sides to the <li>the dark side</li> Force: </ol> the light side <ol> the dark side <li>and Chuck Norris</li> and Chuck Norris </ol> </div>
  • 18. ~ • Sibling selection HTML CSS <div> p ~ ol { color: red; } <p> Although it is not common knowledge, there are actually three sides to the Force: </p> Output <ol> Although it is not common knowledge, <li>the light side</li> there are actually three sides to the <li>the dark side</li> Force: </ol> the light side <ol> the dark side <li>and Chuck Norris</li> and Chuck Norris </ol> </div>
  • 19. : • Pseudo Elements CSS 2.1 CSS 2.1 CSS 3 Pseudo-classes Pseudo-elements Pseudo-classes :link ::first-line :target :last-of-type :visited ::first-letter :root :only-of-type :hover ::before :nth-child() :only-child :focus ::after :nth-of-type() :last-child :active :nth-last-of-type() :empty :first-child :first-of-type :not() :lang() :enabled :disabled :checked
  • 20. Pseudo Elements HTML CSS <table> td, th { border: 1px black solid; padding: <tr> 5px; } <th>Name</th> th { background-color: black; color: <th>Super Power</th> white; } </tr> tr:nth-child(odd) td { <tr> background-color: gray; <td>Chuck Norris</td> color: white; <td>roundhouse kick</td> } </tr> <tr> Output <td>MC Hammer</td> <td>parachute pants</td> </tr> <tr> <td>Mister Miyagi</td> <td>waxing fools</td> </tr> </table>
  • 21. Connect StewShack.com @ StewShack facebook.com/ StewShack linkedin.com/in/ StewShack

Editor's Notes

  1. Hello, my name is Dan Stewart. My presentation is on CSS Selectors. What the… every symbol on this slide can be used to select an element on a web page using CSS. We are going to be talking about each. Many of you have selected elements using their name, but have you used any of these symbols? Maybe the pound (#), maybe the dot (.).Color theme: http://kuler.adobe.com/#themeID/1931107Titles: R 238 G 145 B 63 #EE913FBlue: R 25 G 80 B 115 #195073Green: R 127 G 140 B 31 #7F8C1FBeige: R 242 G 229 B 189 #F2E5BDTeal: R 159 G 215 B 199 #9FD7C7
  2. I don’t want you to think I came up with all of this on my own. Much of what you will see on these slides came from Eric Meyer’s excellent book, “Smashing CSS”.Buy it today!
  3. Selectors give us a way to… wait for it… select an element on a webpage.An element is an HTML tag, the stuff inside the tag, or the stuff containing the tag.A pseudo element isn’t really an element, it’s a state of an element. For example, hover. You can apply a style to an element when you hover over it. I’ll be showing more examples of pseudo elements and elements later.We can use the name of the element, like H1, the ID with a pound #. Do you call it pound or hash? The class with a dot. Saying dot is shorter than period. And other with other symbols.
  4. Spes-eh-fis-ityIt’s a hard word to say and even harder to understand.Basically it comes down to, who will win?I used JSFiddle.net to test this code. JS fiddle is a great website that allows you to play with HTML, CSS, and JavaScript.So I have some Chuck Norris Lorem Ipsum. http://chuckipsum.com/?p=5&amp;mode=chuckIn the HTML you see I have an H1 element. This element has an ID.Over in the CSS corner I have told the web page to display all H1 elements with red font. Then I say that an H1 element with the ID of title should have a green font.Which one wins? Who says red? Who says green?
  5. Green wins.How can we know which rule will win? Spes-eh-fis-ityMaybe making a little array of numbers will help.The first number represents inline styles. So, you put an inline style, it will win.The second number is the ID. So putting an ID on an element and applying a style… will win.The third is a class, pseudo-class, attribute descriptor.The last is the element.So blanket element rules like all H1’s are red is easily overruled by classes and ID’s.
  6. One more example. All paragraph text should be red. The p tag is an element, so we put a 1 in the last number.A span tag inside a p tag is green. So, we add 1 for the p tag and 1 for the span tag.An i tag in a span tag in a p tag is blue. 1 + 1 + 1 = 3.Can I take a moment to rant about nested element rules?They slow down your webpage and development.We’ve heard the dangers of “classitis” where you put the class attribute on every little thing.Well, these nested selectors are slow and not having classes slows development by adding complexity.Let’s say you don’t want italics inside a span to be blue. You have to create a class to undo the rule. Now your adding classes, not change a style, but to undo a style.That’s the opposite of what adding a class is supposed to do.Say it with me, “classes are ok”.Quote source: http://www.stevesouders.com/blog/2009/03/10/performance-impact-of-css-selectors/
  7. I’ve talked a lot about how rules override each other, but they can build upon each other too.All H1&apos;s should be bolded. The H1 with an ID of title is larger, and the class &quot;green&quot; gets a green font color.
  8. You do have a trump card. It’s the bang important statement. Bang or exclamation point?When should you use it? Never! Ok, when it’s important.This trump card make your CSS break the rules, which is not a good thing.I hate to even bring it up.Quote source: http://james.padolsey.com/usability/dont-use-important/
  9. We&apos;ve seen selecting an element by its name, ID, and class.The drawback of using ID is that there can only be one per page.I can’t tell you how many times I have had to convert a rule for an ID into a class. You’re better off using classes than ID’s.It’s hard to promise that an element will ever only show up once on a page.You can have multiple classes in an element.This is good because classes can just do one thing instead of having a &quot;red dash italic&quot; rule and a &quot;red dash bold&quot; rule, Just have a red class and an italic class.Just separate them with a space.
  10. The aste-risk or star. I like to say star because it’s easier.The star is referred to as the universal selector. It’s not a wild card. So you can’t do H* and expect to select all the H tags like H1 – H6.It is a wild card for all the children inside an element.In this example my CSS has a rule for p star. This rule selects all the elements in the p tag. No matter what element falls inside a p tag, it will be red.
  11. Square brackets allow you to select an attribute.There are two kinds of anchor tags. Ones with the attribute href takes you too another page, ones with an ID allow you to link from another page to a specific location on a page.I wrote a rule that anchor tags with the attribute href could get a Wikipedia style icon at the end.Now if you look closely, the link to Great Wall of China is external, but the link to Chuck Norris is internal. Only the link to the Great Wall site should have the external link icon, right?So how can we select that?Full CSS Code a[href] { background: url(&quot;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAVklEQVR4Xn3PgQkAMQhDUXfqTu7kTtkpd5RA8AInfArtQ2iRXFWT2QedAfttj2FsPIOE1eCOlEuoWWjgzYaB/IkeGOrxXhqB+uA9Bfcm0lAZuh+YIeAD+cAqSz4kCMUAAAAASUVORK5CYII=&quot;) no-repeat scroll right center transparent;    padding-right: 13px;}
  12. Inside the square brackets you can create criteria.The problem is, the criteria has to exactly match.So www Great Wall of China dot com won’t match. Even capitalizing the G in Great will cause it to fail.
  13. The star equals acts like a contains statement.So, if the href attribute contains http anywhere, it will match.Again it&apos;s not a wild card. So stars equals http works, but not equals http star like you would think.
  14. The carrot means “starts with”.We know that http is the first thing in the URL. So we could use the carrot to find it.
  15. The opposite of the carrot is the dollar sign.The dollar sign means the ending characters.
  16. Putting a space after a selector followed by another selector gets all of the descendants, but not the direct descendants.Putting a greater than symbol gets the direct descendants. Since the LI is a descendant of DIV then it will be red.LI is not a direct descendant of DIV so it will not turn green.
  17. Putting a plus after a selector followed by another selector gets the next sibling.The first OL after the P tag is selected, but the second one is not.
  18. This is a CSS 3 thing, it&apos;s a little buggy, but it does select siblings.
  19. Finally we get to pseudo elements.These aren&apos;t real elements and classes. They are pseudo elements.
  20. Do you need a table with alternating row colors?You can do that with CSS 3 now.
  21. Thank you for coming. Hopefully you learned something helpful.I would love to connect with you. My website is stew shack dot com. I&apos;m on Twitter, Facebook, Linked In.Thanks