Precise Targeting with CSS Selectors

Writing CSS Selectors Like a Boss

Diagram of a CSS rule

CSS Selectors

ul li { … }
ul > li { … }
#main-menu { … }
.menu-item { … }
input[type=”checkbox”] { … }
a[href^=”https://”] { … }
li:first-child { … }

Selector Example

<header>
<img id=”logo” src=”logo.png” alt=”logo”>
<ul id=”social-menu”>
<li class=”menu-item”>
<a href=”www.facebook.com">
<img src=”facebook-icon.png” alt=”facebook”>
</a>
</li>
<li class=”menu-item”>
<a href=”www.twitter.com">
<img src=”twitter-icon.png” alt=”twitter”>
</a>
</li>
</ul>
</header>
ul { ... }
#social-menu { … }
header > :last-child { … }
li:first-child img { … }
img[alt=”facebook”] { … }
a[href*=”facebook”] img { … }

--

--

Designer and developer who occasionally writes.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store