Papa's Perspective

3 Things You Need to Learn About CSS Selectors

If you haven't checked out CSS, understanding the most often used core selectors is a good place to start.

I want to learn the CSS language, so how do I get started? I hear this question from developers with varying experience. I admit, jQuery makes it awfully easy to locate elements in code, manipulate their styles and toggle CSS classes. But much of the styling can be done directly in CSS, which leads to better separation of concerns.

In CSS, style sheets depend on a cascading order of rules. The selectors indicate the elements that styles are applied to in the markup. One of the easiest ways to get started with CSS is to understand the core selectors, child selectors and descendant selectors. With these three concepts, you can move a lot of styling to CSS and out of your code.

Core Selectors
So where do you start? I recommend learning id, class name and HTML tag selectors first. The id selector matches HTML elements with the same id. It's often used when you want to locate a specific element in the Document Object Model (DOM), and it's easiest to simply give it a unique id. The following CSS applies its styles to the DOM element with the id of tagline:

#tagline {text-align: center; margin-
top: -2%; display: block;}

Id selectors are prefixed with the # symbol. This applies its styles to the following span. The style is applied regardless of the type of HTML element, as long as its id matches the selector:

<span id="tagline">Today's News</span>

Sometimes you just want to style all DOM elements of a specific type. The following CSS is used to style all h1 tags (the most important header) in the page:

h1 {font-family: 'Segoe UI'; padding: 1px; 
text-transform: uppercase; }

This is an excellent way to standardize your fonts, margins and other styles for all elements of a specific type.

You can also create a named class (prefixed with a period) that contains a series of styles. The following class styles all elements that declare its class name:

.callout{font-weight: bold;}

In this example, the .callout class will make the text content bold.

You can style most of your page using these three basic CSS features. However, attaching class names and ids to all of your elements adds a lot of styling constructs to your HTML structure.

Another way to describe class selectors is explicitly named styles. Conversely, implicit styling is when your DOM elements are styled using CSS without requiring class or id names to be littered throughout your structure. But what do you do if you want to style DOM elements of a specific type that exist within a div?

You could add a class to the elements, or you could use descendant selectors. The following CSS will style all h1 elements that exist anywhere inside the header element, directly or several layers deep:

header h1 {   
font-family: 'Segoe UI';   
padding: 1px;   
text-transform: uppercase; }

This is a nice way to style just the h1s inside the header while leaving all other h1 tags alone.

Child Selectors
One common mistake is to use a descendant selector when you really want the CSS to only apply to a direct child of an element.

Child selectors locate elements that are directly beneath them (aka children):

nav > ul > li {   
text-shadow: rgba(0,0,0,.4) 1px 1px 1px;   
text-transform: lowercase;   
font-size: 150%;   
display: inline;   
margin-right: 5%; }

This implicit style will be applied to all li elements that are children of Unordered List (ul) elements that are children of nav elements. Thus, the structure could be written without having to write any styling code in the HTML:

    <li><a href="#/now" >Now</a></li>     
    <li><a href="#/scores" >Scores</a></li>     
    <li><a href="#/standings" >Standings</a></li>     
    <li><a href="#/statistics" >Statistics</a></li>   

There's so much more to CSS, but if you use the core styling techniques shown here, you can accomplish quite a bit. You can find all of the concepts shown in this article in an example I created using JsFiddle, available at

About the Author

John Papa is a Microsoft Regional Director and former Microsoft technical evangelist. Author of 100-plus articles and 10 books, he specializes in professional application development with Windows, HTML5, JavaScript, CSS, Silverlight, Windows Presentation Foundation, C#, .NET and SQL Server. Check out his online training with Pluralsight; find him at and on Twitter at

comments powered by Disqus


  • Uno Platform Ports Windows Calculator to Linux

    Uno Platform has ported the famed Windows Calculator, open sourced last year, to Linux as part of a continuing "proof point" effort to demonstrate the reach of what it describes as the sole UI offering available to target Windows, WebAssembly, iOS, macOS, Android and Linux with single-codebase applications coded in C# and XAML.

  • ASP.NET Core OData 8 Preview Supports .NET 5, but with Breaking Changes

    ASP.NET Core OData, which debuted in July 2018, is out in a v8.0 preview that for the first time supports the upcoming .NET 5 milestone release.

  • VS Code Java Team Details 5 Best Dev Practices

    Microsoft's Visual Studio Code team for Java development added a new Coding Pack for Java installer and detailed best practices for setting up a development environment.

  • Binary Classification Using PyTorch: Defining a Network

    Dr. James McCaffrey of Microsoft Research tackles how to define a network in the second of a series of four articles that present a complete end-to-end production-quality example of binary classification using a PyTorch neural network, including a full Python code sample and data files.

  • Blazor Debugging Boosted in .NET 5 RC 2

    In highlighting updates to ASP.NET Core in the just-launched second and final Release Candidate of .NET 5, Microsoft pointed out better debugging for Blazor, the red-hot project that allows for C# coding of web projects.

Upcoming Events