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

推荐订阅源

T
Threatpost
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
D
DataBreaches.Net
The Cloudflare Blog
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
N
Netflix TechBlog - Medium
Microsoft Azure Blog
Microsoft Azure Blog
Microsoft Security Blog
Microsoft Security Blog
B
Blog
U
Unit 42
有赞技术团队
有赞技术团队
博客园 - 聂微东
GbyAI
GbyAI
宝玉的分享
宝玉的分享
F
Full Disclosure
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MyScale Blog
MyScale Blog
Jina AI
Jina AI
Martin Fowler
Martin Fowler
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Docker
P
Proofpoint News Feed
A
About on SuperTechFans
I
InfoQ
博客园 - 【当耐特】
C
Check Point Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog
Project Zero
Project Zero
WordPress大学
WordPress大学
小众软件
小众软件
AWS News Blog
AWS News Blog
博客园 - 司徒正美
T
The Exploit Database - CXSecurity.com
L
LINUX DO - 热门话题
I
Intezer
Engineering at Meta
Engineering at Meta
C
CXSECURITY Database RSS Feed - CXSecurity.com
J
Java Code Geeks
T
Tenable Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
C
CERT Recently Published Vulnerability Notes

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
Coding up a semantic, lean timeline
Harry Roberts · 2011-03-15 · via CSS Wizardry

Written by on CSS Wizardry.

Table of Contents

Independent writing is brought to you via my wonderful Supporters.

  1. The markup
  2. The CSS
    1. The spine
    2. The arrow and dot
    3. The branches
    4. Odd/even styling
  3. CSS feature detection?
  4. Final word

I absolutely love coding up the more semantic aspects of a build. Usually forms and tables, it’s these massively semantic (read; lots of elements with very specific jobs) that I really love coming up against. They’re not all that challenging, but they’re very fun (to me at least–is that sad?!)

After Séan shared the Financial Times’ timeline I got to wondering how I’d code my own (albeit massively more simple) timeline.

This is what I came up with: HTML/CSS timeline.

The markup

The markup is amazingly simple. Semantically I used an <ol>. This was quite an obvious choice as this is an ordered list of events. I used one id="" and some supporting datetime attributes where known (some exact dates are unknown, therefore not included (hopefully you’ll know your exact dates!)), and then that’s it:

<ol id="timeline">

  <li>
    <time datetime="1990-07-04">July 1990</time>
    <p>Born</p>
  </li>

  <li>
    <time>September 1994</time>
    <p>Started first school</p>
  </li>

  <li>
    <time>September 1999</time>
    <p>Started middle school</p>
  </li>

  <li>
    <time>September 2003</time>
    <p>Started high school</p>
  </li>

  <li>
    <time>September 2006</time>
    <p>Started 6th Form</p>
  </li>

  <li>
    <time datetime="2007-11-19">November 2007</time>
    <p>Registered csswizardry.com</p>
  </li>

  <li>
    <time datetime="2008-07-14">July 2008</time>
    <p>Joined Sense Internet as Web Developer</p>
  </li>

  <li>
    <time datetime="2009-03-16">March 2009</time>
    <p>Joined Twitter</p>
  </li>

  <li>
    <time datetime="2010-01-11">January 2010</time>
    <p>Joined Venturelab as Web Developer</p>
  </li>

  <li>
    <time>January 2011</time>
    <p>Became a member of the Smashing Magazine Experts Panel</p>
  </li>

  <li>
    <time datetime="2011-03-21">March 2011</time>
    <p>Joined Sky as Senior UI Developer</p>
  </li>

</ol>

I was initially marking the dates up as <b>s but as Mark pointed out I could use the new HTML5 <time> element.

The CSS

As far as basic styling goes, I won’t insult your intelligence; I merely set some type styles and background colours on the list items.

The really interesting bits are the odd/even styling, the ‘spine’, the dot and arrow ‘images’ and the branches off the centre of the timeline. I put images in quotes because they’re not actually images at all, they’re :before and :after pseudo-elements.

The spine

The spine running down the timeline is actually an image and is applied as a background using the fantastic Dummy Image:

#timeline{
    background:url(http://dummyimage.com/1x1/f43059/f43059.gif) top center repeat-y;
    width:820px;
    padding:50px 0;
    margin:0 auto 50px auto;
    overflow:hidden;
    list-style:none;
    position:relative;
}

The arrow and dot

The arrow and dot, as mentioned above, are actually pseudo-elements. Their CSS is:

#timeline:before, /* The dot */
#timeline:after{ /* The arrow */
    content:" ";
    width:10px;
    height:10px;
    display:block;
    background:#f43059;
    position:absolute;
    top:0;
    left:50%;
    margin-left:-5px;
    
    -webkit-border-radius:20px;
        -moz-border-radius:20px;
            border-radius:20px;
}
#timeline:after{
    margin-left:-7px;
    background:none;
    border:7px solid transparent;
    border-top-color:#f43059;
    width:0;
    height:0;
    top:auto;
    bottom:-7px;
    
    -webkit-border-radius:0;
        -moz-border-radius:0;
            border-radius:0;
}

Here we’ve created two empty pseudo-elements that are shaped like an arrow and a dot and then absolutely position them at the top and bottom of the ordered list. They sit over the top of the spine, giving the illusion of all being connected.

It’s worth saying that I’m not actually a fan of the border-arrow behaviour, but this is an experimental piece.

The branches

The branches that span between the list items and the spine are, again, pseudo elements. They are 70px wide and 1px high and have a gradient background which transitions from the colour of the spine to the colour of the list items. The CSS powering those is:

#timeline li:before,
#timeline li:after{
  content:" ";
  width:70px;
  height:1px;
  background:#f43059;
  position:absolute;
  left:100%;
  top:50%;
  background:-moz-linear-gradient(0,#d8d566,#f43059);
  background:-webkit-gradient(linear,left top,right top,from(#d8d566),to(#f43059));
}

So by now we’ve added start and end points, a spine and branches to our timeline with no extra markup whatsoever. Lean!

Odd/even styling

The odd/even styling of each list item is achieved, as you might expect, using nth-of-type() selectors. What we do is move every even list item over the right of the <ol> and move its branch to attach to the spine accordingly:

#timeline li:nth-of-type(even){
    float:right;
    text-align:left;
}
#timeline li:nth-of-type(even):after{ /* Move branches */
    background:-moz-linear-gradient(0,#f43059,#d8d566);
    background:-webkit-gradient(linear,left top,right top,from(#f43059),to(#d8d566));
    left:auto;
    right:100%;
}

CSS feature detection?

This next bit I found quite cool, though a little unorthodox. I set the <li>s to have a negative margin-top so that they had a slightly overlapped effect on the timeline, so either side is not below the previous one, rather next to and slightly along-side it.

The problem I had here is that, in browsers that don’t support nth-of-type() selectors, the list items bunched up, each one slightly covering the previous one.

What I did was use this:

#timeline li:nth-of-type(odd),
#timeline li:nth-of-type(even){
  margin:-10px 0 0 0;
}

Which basically says if the browser supports nth-of-type() selectors then give the list items a negative margin-top.

Any browsers that don’t support nth-of-type() selectors get the previously defined margin value for the list items, set here:

#timeline li{
    position:relative;
    clear:both;
    float:left;
    width:318px;
    padding:10px;
    background:#fef8c4;
    border:1px solid #d8d566;
    text-align:right;
    margin:0 0 10px 0;
    
    -webkit-border-radius:2px;
        -moz-border-radius:2px;
            border-radius:2px;
    -webkit-box-shadow:0 1px #fff inset;
        -moz-box-shadow:0 1px #fff inset;
            box-shadow:0 1px #fff inset;
}

I guess you could call that a small kind of feature detection…?

However, whatever you call it, it means that IE8 users still get a pretty good experience, a linear, one column timeline with no nth-of-type() styling.

IE7 however doesn’t support pseudo-elements or nth-of-type() selectors, so they just get a single column list with no branches or dots/arrows but they do get a lonely spine…

Final word

So there we have it, a super lean, semantic and progressively styled timeline in HTML and CSS and one image. No doubt you could make a completely imageless solution, but I’m personally not one for this ‘never use images’ phase that the industry seems to be going through…