惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
小众软件
小众软件
Engineering at Meta
Engineering at Meta
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
G
Google Developers Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
酷 壳 – CoolShell
酷 壳 – CoolShell
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Help Net Security
Martin Fowler
Martin Fowler
G
GRAHAM CLULEY
Simon Willison's Weblog
Simon Willison's Weblog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
V
Vulnerabilities – Threatpost
云风的 BLOG
云风的 BLOG
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
量子位
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
I
Intezer
Scott Helme
Scott Helme
A
About on SuperTechFans
博客园 - 司徒正美
Hacker News: Ask HN
Hacker News: Ask HN
The GitHub Blog
The GitHub Blog
Forbes - Security
Forbes - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Spread Privacy
Spread Privacy
T
Tailwind CSS Blog
S
Security Affairs
宝玉的分享
宝玉的分享

CSS Wizardry

Front-End’s Missing Metric: The TBT Window Meet Your Users Where They Are with Obs.js Better Browser Caching with No-Vary-Search font-family Doesn’t Fall Back the Way You Think What Is CSS Containment and How Can I Use It? When All You Can Do Is All or Nothing, Do Nothing Obs.js: Context-Aware Web Performance for Everyone Low- and Mid-Tier Mobile for the Real World (2025) The Fastest Site in the Tour de France Making Sense of the Performance Extensibility API Why Do We Have a Cache-Control Request Header? HTML Is Not a Programming Language… Build for the Web, Build on the Web, Build with the Web Licensing Code on CSS Wizardry A Layered Approach to Speculation Rules Designing (and Evolving) a New Web Performance Score Core Web Vitals Colours The Ultimate Contract Templates for Tech Consultants: Protect Your Business and Get Paid Optimising for High Latency Environments Cache Grab: How Much Are You Leaving on the Table? blocking=render: Why would you do that?! Correctly Configure (Pre) Connections The Three Cs: 🤝 Concatenate, 🗜️ Compress, 🗳️ Cache What Is the Maximum max-age? How to Clear Cache and Cookies on a Customer’s Device The Ultimate Low-Quality Image Placeholder Technique Core Web Vitals for Search Engine Optimisation: What Do We Need to Know? The HTTP/1-liness of HTTP/2 In Defence of DOM­Content­Loaded Site-Speed Topography Remapped Why Not document.write()? Speeding Up Async Snippets Critical CSS? Not So Fast! Measure What You Impact, Not What You Influence Optimising Largest Contentful Paint Measuring Web Performance in Mobile Safari Site-Speed Topography Speed Up Google Fonts Real-World Effectiveness of Brotli Performance Budgets, Pragmatically Lazy Pre-Browsing with Prefetch Making Cloud.typography Fast(er) Time to First Byte: What It Is and How to Improve It Self-Host Your Static Assets Tips for Technical Interviews Cache-Control for Civilians Bandwidth or Latency: When to Optimise for Which ITCSS × Skillshare What If? CSS and Network Performance The Three Types of Performance Testing Getting to Know a Legacy Codebase Image Inconsistencies: How and When Browsers Download Images Identifying, Auditing, and Discussing Third Parties My Digital Music Setup Measuring the Hard-to-Measure Finding Dead CSS The Fallacies of Distributed Computing (Applied to Front-End Performance) Ten Years Old Relative Requirements Airplanes and Ashtrays Performance and Resilience: Stress-Testing Third Parties Refactoring Tunnels Little Things I Like to Do with Git Writing Tidy Code Configuring Git and Vim Base64 Encoding & Performance, Part 2: Gathering Data Base64 Encoding & Performance, Part 1: What’s Up with Base64? Code Smells in CSS Revisited Typography for Developers Moving CSS Wizardry onto HTTPS and HTTP/2 A New Year, a New Focus Preparing Vim for Apple’s Touch Bar Choosing the Correct Average CSS Shorthand Syntax Considered an Anti-Pattern CSS Wizardry Newsletter Nesting Your BEM? Improving Perceived Performance with Multiple Background Images Continue Normalising Your CSS Pure CSS Content Filter Pragmatic, Practical, and Progressive Theming with Custom Properties Refactoring CSS: The Three I’s Speaker’s Checklist: Before and After Your Talk Improving Your CSS with Parker The Importance of !important: Forcing Immutability in CSS Mixins Better for Performance Managing Typography on Large Apps White October Events Workshop Partnership BEMIT: Taking the BEM Naming Convention a Step Further Travelling Like You Want to, When You Have To Contextual Styling: UI Components, Nesting, and Implementation Detail Subtleties with Self-Chained Classes Cyclomatic Complexity: Logic in CSS Immutable CSS Can CSS Be Too Modular? More Transparent UI Code with Namespaces When to use @extend; when to use a mixin The Specificity Graph CSS Wizardry Ltd.: Year 1 in review CSS Guidelines 2.0.0
Ack for CSS Developers
Harry Roberts · 2017-01-18 · via CSS Wizardry

Written by on CSS Wizardry.

Table of Contents

Independent writing is brought to you via my wonderful Supporters.

  1. Installing and Configuring
    1. .ackrc
    2. A Note on Regex
  2. Find Decimal Pixel Values
  3. Find IDs
  4. Background Shorthand
  5. Find Unitted Zeroes

Ack is a powerful and very user-friendly alternative to grep. Designed for programmers, it’s purpose-built to run searches over source code. By default it will ignore your .git or .svn directories, and you can tell it which types of files you’d like it to search in. For example:

…will search for the string foo only in your HTML files.

$ ack --html --js --css "bar"

…will search for the string bar in your HTML, JS, and CSS files. Neat, huh?

Get into the habit of always wrapping your search terms in quotes. Without this, we run the risk of our search terms and regexes being interpreted as command line arguments.

I use Ack as part of auditing a project, or when trying to find my way around a larger codebase. It’s also pretty useful for tracking down bugs and oddities. In this short post, we’ll look at a small number of ways CSS developers can make use of Ack in their workflow.

Installing and Configuring

If you’re on a Mac, you can install Ack using Homebrew:

As simple as that!

For other operating systems, check the official installation guide.

To use Ack, you’ll probably want to cd into the root of your project, or the root of your CSS directory. Ack searches all files on and below your current location. Here’s what my terminal looks like whilst writing this article:

harryroberts in ~/Sites/csswizardry.com on (master)
» ack <search term>

.ackrc

Ack is extensible and configurable, making use of its own .ackrc dotfile. This lives in your home directory, alongside your .vimrc, .bashrc, etc. Here are some things that I added to mine that you’ll probably like as well:

--ignore-directory=is:node_modules
--ignore-directory=is:bower_components
--type-set=sass:ext:scss,sass
--type-set=markdown:ext:md,markdown

Here I’m telling Ack to exclude my node_modules and bower_components directories, as well as adding Markdown and Sass as new filetypes. This means that if I run:

…Ack will search every .scss and .sass file in my project. To see what filetypes Ack will search, run:

If you think something is missing, add it to your .ackrc.

A Note on Regex

To use Ack, you will need to know a little bit of regex. Don’t worry though: my regex is pretty weak and even I can still manage.

Find Decimal Pixel Values

We don’t want to have any decimal places in any of our pixel values (e.g. 7.5px), so let’s search for those:

  • \d* will match zero or more (*) \digits. The reason I’m looking for zero or more is because I want to find both .5px or 11.2px.
  • \. will match a literal full-stop/period character.
  • \d* is the same as before. This means I will find both 97.856px, or 52.px (which is actually just an error).
  • px will match the literal string px: I don’t want to look for ems or rems or percentages because they accept float values.

See the demo.

Find IDs

We definitely shouldn’t be using IDs in CSS, so let’s look for them:

$ ack --css -i "#[-_a-z][-_a-z0-9]*(?=[^}]*\{)"

Firstly, notice the -i flag. This tells Ack to run our regexes case insensitively.

This pretty complex regex does a few things:

  • # finds a literal hash (#) symbol.
  • [-_a-z] finds a single hyphen (-), underscore (_), or case insensitive a-z, denoting a valid start to an ID.
  • [-_a-z0-9]* does almost exactly the same as above, except this time we’re looking for zero or more (*) characters, and for digits as well (0-9). We’re doing this separately from the previous lookup because IDs can’t start with numbers.
  • (?=[^}]*\{) is running a positive lookahead, just checking that we encounter an opening curly brace (\{) next, and not a closing one ([^}]).

This means we will find IDs, but not hex values.

N.B. Because of Sass’ nesting abilities, nested code containing hex values becomes a false positive. As such, only run this regex over your compiled CSS. Please refer to the demo.

Background Shorthand

$ ack --css -i "background:\s*#[a-f0-9]*;"

I recently wrote about how I consider background shorthand syntax an anti-pattern, so we should probably look for that whilst we’re here.

  • background: finds the literal string background followed by a colon (:).
  • \s* matches zero or more units of whitespace between the colon and the beginning of a hex value.
  • # is the literal hash symbol (#), the beginning of a hex value.
  • [a-f0-9]* is a valid hex value (e.g. #f00, #BADA55). Setting the -i flag means that our regex is now case insensitive.
  • ; matches the literal semi-colon character (;) which denotes the termination of the declaration.

See the demo.

Find Unitted Zeroes

$ ack --css "\b0(px|r?em)"

This is a little pedantic of me, but putting a unit (e.g. px) on a zero (0) is redundant: zero pixels is the same distance as zero lightyears. Let’s look for them.

  • \b looks for a word boundary. This means we’re looking for zeroes at the start of a string (i.e. 0px) and not midway through one (e.g. 210px).
  • 0 is looking for the literal zero character (0).
  • (px|r?em) looks for the literal strings px or rem or em.
  • r?em makes the r optional, so we can match either rem or em at the same time.

See the demo.

Those are just a few of the most common things I search CSS files for as part of an audit, but of course Ack can help me find all manner of things, from finding specific classes, properties, selectors, or values, right the way to finding errors and bugs.