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

推荐订阅源

D
DataBreaches.Net
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Webroot Blog
Webroot Blog
AI
AI
P
Palo Alto Networks Blog
Attack and Defense Labs
Attack and Defense Labs
WordPress大学
WordPress大学
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
Spread Privacy
Spread Privacy
T
Tor Project blog
罗磊的独立博客
小众软件
小众软件
S
Security Affairs
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
Apple Machine Learning Research
Apple Machine Learning Research
T
Threatpost
NISL@THU
NISL@THU
博客园_首页
PCI Perspectives
PCI Perspectives
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
N
News and Events Feed by Topic
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Forbes - Security
Forbes - Security
博客园 - 叶小钗
D
Darknet – Hacking Tools, Hacker News & Cyber Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
L
LINUX DO - 热门话题
T
Threat Research - Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
Security Latest
Security Latest
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Cloudflare Blog
A
About on SuperTechFans
爱范儿
爱范儿
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
TaoSecurity Blog
TaoSecurity Blog
宝玉的分享
宝玉的分享
G
GRAHAM CLULEY
雷峰网
雷峰网
F
Full Disclosure
I
Intezer
Cloudbric
Cloudbric
博客园 - 三生石上(FineUI控件)
U
Unit 42

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 Ack for CSS Developers 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
The flag object
Harry Roberts · 2013-05-09 · via CSS Wizardry

Written by on CSS Wizardry.

Table of Contents

Independent writing is brought to you via my wonderful Supporters.

  1. Featured case study: NHS
  2. Differences to the media object

It all started with the media object. That one snippet of CSS, by Nicole, got me fully sold and hooked on OOCSS.

This article will only really make sense if you, too, are familiar with the media object; if you aren’t then you should totally go and check it out!

For me, the media object is more than just a layout construct; for me, the media object is the poster-child of everything OOCSS stands for. The media object is so glaringly simple that, when I first read about it, I laughed out loud. The idea of taking such a common design pattern and distilling it into a tiny, robust, reusable abstraction was so clever yet so simple that I fell in love straight away. It also made me realise that, for too long, CSS had been left to people with a designer’s mind; any code needs the mind of a developer applied to it, someone who gets DRYness, abstraction, efficiency, etc.

The media object’s job is simply to display an image with some text content beside it, both aligned to the tops of each other, with a little gap between. It also has the option have the image on either the left- or right-hand side of the text. This is the SRP to a tee.

+---------+ ~~~~~~~~~~ ~~~~~
|         | ~~~~~ ~~~~~ ~~~~
|         | ~~~~~~~~~ ~~~~~~
|         |
+---------+

However, as awesome as the media object is, there is one thing it can’t (and was probably never designed to) do well; vertical alignment. Oftentimes it’s desirable to have the image content and text content aligned to their vertical middles, like so:

+---------+
|         | ~~~~ ~~~~~~~~~~~
|         | ~~~~~~~ ~~~~~ ~~
|         | ~~~~~~~~~~~~
+---------+

It’s been something that’s been asked about a lot at work because, on the face of it, it looks like a really simple construct. However, looks can be deceiving… The only way to achieve this kind of thing with the media object would be by using margins and paddings and magic numbers, and we all know what bad news that is. I often had to tell bewildered looking product owners and designers that it’s not as simple as that. Never a good feeling, especially when you’re meant to be good at CSS.

Featured case study: NHS

How I helped the NHS rapidly build a brand new product.

Read case study…

The flag object

Today, on my lunch break, I decided enough was enough. I fired up jsFiddle and got tinkering; a few minutes later and I had the flag object: jsfiddle.net/csswizardry/hErrh

The flag object shares a lot of common traits with the media object; its sole purpose is displaying image- and text-like content side-by-side. Where the flag object differs, however, is its ability to vertically align the image and text to the tops, middles or bottoms of each other.

The code

.flag {
    display: table;
    width: 100%;
}

    .flag__image,
    .flag__body {
        display: table-cell;
        vertical-align: middle;

        .flag--top & {
            vertical-align: top;
        }

        .flag--bottom & {
            vertical-align: bottom;
        }

    }

    .flag__image {
        padding-right: 10px;

        > img {
            display: block;
            max-width: none;
        }

        .flag--rev & {
            padding-right: 0;
            padding-left: 10px;
        }

    }

    .flag__body {
        width: 100%;
    }

The CSS/Sass is pretty self explanatory; we’re making use of display:table[-cell]; which gives us the ability to use vertical-alignment. We have a series of modifiers to quickly swap between aligning the content to the top or bottom of the object, and we also have a reversal modifier, for having the image-like content on the right.

The width:100% on the .flag__body{} is an odd hack of sorts; this forces the text content to take up all of the space that the .flag__image{} does not.

Differences to the media object

Aside from the visual difference of vertical alignment, there isn’t too much difference between the two objects’ code. One thing to note is that the flag object is slightly more verbose in its implementation; the media object can have its classes applied directly to its content whereas the flag object needs its classes applied to wrappers around its content. For example, compare the following snippets for the media and flag objects respectively:

<div class="media">
    <img src="" alt="" class="media__image">
    <p class="media__body"></p>
</div>

<div class="flag">
    <div class="flag__image">
        <img src="" alt="">
    </div>
    <div class="flag__body">
        <p></p>
    </div>
</div>

The flag object needs its structural stuff on a higher element, abstracted away from the actual content. This, however, is a small price to pay for the ability to vertically align content.

Also, the media object is a lot more tried and tested; it works back as far as, I believe, IE6. The flag object plain will-not-work in IE7 and below. It’s been tested in IE8+ and it works a treat.

In a word, no.

I see these two objects/abstractions coexisting. They do similar things, yes, but:

  1. The media object is good enough for 90% of cases, namely where vertical alignment isn’t required, so use that wherever possible.
  2. The flag object is not backwards compatible with the media object, so you can’t just replace all existing instances of one with the other.
  3. Until the flag object has any quirks/bugs ironed out, I think it safer to only use it where really necessary.

So, there we have it; I finally got round to writing a solid abstraction to this super-simple design pattern. Let’s see how it serves me!