Take Your CSS Skills to the Next Level
Published: Dec 23, 2023
CSS (Cascading Style Sheets) allows you to control the appearance and layout of web pages. Mastering intermediate CSS techniques will add more tools to your development toolbox.
Recap: The ABCs of CSS
Let’s first do a quick recap. The basic building blocks of CSS are:
- Selectors - target web page elements like headings, paragraphs, etc.
- Properties - determine styling and behavior, like
color
,font-size
,display
. - Values - set what the property will be, like
red
,16px
,block
.
With just selectors, properties and values you can modify text, colors, sizes and more.
Advanced Selectors
As an aspiring web developer, getting a solid grip on crafting precise CSS selectors is a must-have skill. They allow you to target any element or groups of elements on a page for styling.
We’ll explore some advanced selector techniques to step up your CSS mastery.
/* Element selector */
p {
color: gray;
}
/* ID selector */
#intro {
font-size: 20px;
}
/* Class selector */
.highlight {
background: yellow;
}
These allow simple targeting by element type, id or class name.
Introducing Advanced Selectors
But CSS selectors can get way more advanced by using:
- Pseudo-classes - target based on state e.g. hovered
- Pseudo-elements - target specific parts like first letter
- Attribute selectors - target elements with specific attributes
- Combinators – select based on relationship between elements
…and more!
Let’s look at some examples.
Target Element States Dynamically
Pseudo-classes help select elements when users interact with them.
For example, change link colors on hover:
/* Unvisited link */
a:link {
color: blue;
}
/* Visited link */
a:visited {
color: purple;
}
/* Hovered link */
a:hover {
color: red;
}
You can target ALL kinds of states - focus, active, checked, etc.
Style Specific Portions
Pseudo-elements allow styling parts of an element.
Some common examples include:
/* First letter */
p::first-letter {
font-size: 36px;
}
/* First line */
p::first-line {
font-weight: bold;
}
/* Before content */
blockquote::before {
content: open-quote;
}
/* After content */
blockquote::after {
content: close-quote;
}
This granular control takes styling deeper.
Attribute and Combinator Superpowers
Attribute selectors target elements with specific HTML attributes or values.
Combinators select elements based on their location relative to others.
Here’s an example using both:
/* Paragraphs inside elements with class="intro" */
.intro p {
color: green;
}
/* Images with alt text */
img[alt] {
border: 1px solid gray;
}
Advancing your selector skills unlocks unlimited styling possibilities!
There’s much more we could cover - child selectors, adjacent sibling selectors and more. But this should give you a taste of what’s attainable on your CSS journey.
Pseudo-Classes for Dynamic Control
Pseudo-classes like :hover
, :nth-child()
trigger styling when users interact with elements or based on their position.
For example, change color when hovering over links:
a:hover {
color: red;
}
Or style every 3rd item differently:
li:nth-child(3n) {
background: grey;
}
Combinators Target Elements in Relation
Combinators allow selecting elements based on their location relative to others.
For instance, style <em>
only inside .intro
paragraphs:
.intro em {
color: rebeccapurple;
}
Skillful use of advanced selectors gives extensive control over styling.
Responsive Design with Media Queries
Responsive design enables your site to adapt across mobiles, tablets, laptops and desktops - ensuring optimal viewing and interaction everywhere.
Fluid Layouts Rescale Automatically
Using %
rather than px
for width values in CSS creates stretchy fluid layouts that flex to fill space.
Powerful Media Queries for Breakpoints
Media queries allow applying CSS only under certain conditions like specific viewport widths.
For example, rearrange layouts for smaller screens:
@media (max-width: 768px) {
.sidebar {
width: 100%;
}
}
This scales sites beautifully across device sizes.
Animations and Transitions
Subtle CSS animations and transitions greatly boost user experience by adding motion and interactivity.
From Point A to B with Transitions
Use the transition
property to animate changes on hover or click, like fading buttons or growing thumbnails.
For example, fade links in on hover:
a {
opacity: 0.5;
transition: opacity 0.3s;
}
a:hover {
opacity: 1;
}
Grow image thumbnails on mouseover:
img {
width: 100px;
transition: width 0.5s;
}
img:hover {
width: 150px;
}
Keyframe Animations
For more advanced sequences, @keyframes
define stages of the animation allowing bouncing icons, rotating widgets and dynamic effects.
For example, a pulsating animation:
@keyframes pulse {
0% {
transform: scale(1);
}
50% {
transform: scale(1.5);
}
100% {
transform: scale(1);
}
}
.icon {
animation: pulse 1s infinite;
}
Bouncing loading dots:
@keyframes bounce {
0%, 100% {
transform: translateY(0);
}
50% {
transform: translateY(-30px);
}
}
.loader span {
animation: bounce 0.5s infinite alternate;
}
Preprocessors Automate Repeated CSS
Preprocessors like SASS provide features lacking in native CSS - variables, nesting, mixins and more - improving workflow efficiency.
SASS Streamlines Style Sheets
SASS gets compiled into regular CSS. Advantages include:
Variables help centralize values like colors and fonts
Mixins define reusable blocks of styling to insert anywhere
Nesting structures selectors neatly
There are many more tools to handle complex project demands that you’ll discover as you evolve as a developer!
The web moves fast – flex your CSS muscles by trying new techniques and see firsthand how they expand your capabilities. What aspects of intermediate or advanced CSS are you seeking to learn? Join the discussion below!
Help Improve This Page
Spot an error or have a suggestion? This page is a Markdown file, making it easy to contribute:
- Click the "Edit on GitHub" button below
- Look for the pencil icon in the GitHub interface
- Make your changes and submit a pull request
Can't edit directly? Share your feedback or report issues in the comments below. We appreciate your input!