Working with rem in CSS

Working with rem in CSS

Is the relative measure rem new to you? Most people are familiar with the em unit of measure, but the rem hasn’t been around quite as long. It came into being with CSS3.

The definition of rem is “root relative em.” So a rem is really nothing new, it’s merely an em living in a very close relationship with the page root – typically the html element.

The default font size in most browsers is 16px. We equate this with a font-size value of 100% or 1em or 1rem. These are the relative font units used for responsive web pages.

To repeat, a rem is relative to the root. Why is this so important? Because an em bases font-size on the element it’s used on, and those sizes are inherited. For example, if a ul had a declared font-size of 0.75em, a nested ul within that list would have a font-size of 0.75em of the parent list.

On the other hand, if a rule for ul set the font-size at 0.75rem, any nested list would remain at that 0.75rem size because it is root relative, not relative to the parent element.

Browser support for the rem is very good among modern browsers. Why not give rem a try?

Attribute Selectors: All the Bells and Whistles

There are many types of selectors in CSS. If you’re still making your way through the darkness with nothing more than a few element selectors, a few classes, some ids, and the occasional pseudo selector, you need to find your way into the light with attribute selectors.

I wrote about Attribute Selectors in CSS back in 2008, but there are 3 new types of attribute selectors in CSS3. And they have good browser support.

Attribute Selector Basics

The basic syntax is to name an element, with an attribute in brackets following it, then give the CSS rule.

element[attribute] {
some rules here;
}

This rule, for example, would target any image with a title attribute.

img[title] {
some rules here;
}

This rule would target any anchor element with an href attribute.

a[href] {
some rules here;
}

Beyond that basic type of attribute selector, there are several operators can can be used to refine or broaden what you are selecting.

Using an equals sign, you can add specific values to the chosen attributes, making the selector even more precise. For example,

img[title="mybunny"] {
some rules here;
}

a[href="http://blogher.com"] {
some rules here;
}

The first example would select any image with the exact title attribute “mybunny.” The second would choose any link going to exactly “http://blogher.com,” but not to, say, “http://blogher.com/tech.”

The ~= operator select elements with an attribute value containing a specified word.

i [lang~="en"] {
some rules here;
}

This would match any italic element with a language attribute of English.

The |= uses the syntax |= to match hyphenated elements. This most often is used in rules targeting specific language declarations, where you might have hyphenated attributes in an HTML element such as en-US or en-GB. An example selector is:

body [lang|="en"]{
some rules here;
}

New in CSS3

The ^= operator uses the caret (^) with the equals sign to select what something begins with. It will match elements that have an attribute containing a value that starts with the specified value. For example,

a[href^="https:"] {
some rules here;
}

That selector would match only anchor elements with an href that began with https:.

The *= operator will match elements that have an attribute which contains the specified value somewhere in the attribute value. For example,

a[href*="blogher.com"] {
some rules here;
}

That would select any anchor element with an href value that contained blogher.com somewhere. This gives you a much wider sweep than the simple = operator.

Finally, the $= operator uses the dollar sign ($), which matches to elements that have a  specified value at the end. Examples:

img[src$=".gif"] {
some rules here;
}

a[href$=".doc"] {
some rules here;
}

These two examples would select only image elements ending with .gif or only href attributes ending in .doc.

Summary: How to Apply WCAG 2.0 to Mobile Devices

This slideshow requires JavaScript.

This post simply serves as a summary of the 4 posts published recently that distill the information from the W3C about how the guidelines and principles of WCAG 2.0 apply to mobile devices.

I thought it would be useful to have a single post with links to each of the previous posts. The posts take the 4 POUR principles and explain what they mean in terms of mobile application.

POUR

Mobile Accessibility and the Robust Principle

Mobile Accessibility and the Robust Principle

The W3C wants to help developers understand how WCAG 2.0 applies on mobile devices. They recently issued the first working draft on the topic. The guidelines and principles of WCAG are explained using the POUR method: Perceivable, Operable, Understandable, Robust.

In this post I will distill the W3C information on the Robust principle.

The Keyboard

Set the virtual keyboard to the type of data entry required.

Data Entry

Make data entry as easy as possible by,

  • On-screen keyboard
  • Bluetooth keyboard
  • Speech or Touch
  • Use select menus, radio buttons, check boxes or automatically enter known information (e.g. date, time, location)

Support the Platform

Don’t disable the device’s platform characteristics such as zoom, larger fonts, and captions.

Mobile device image: William Hook

Mobile Accessibility and the Understandable Principle

Mobile Accessibility and the Understandable Principle

The W3C wants to help developers understand how WCAG 2.0 applies on mobile devices. They recently issued the first working draft on the topic. The guidelines and principles of WCAG are explained using the POUR method: Perceivable, Operable, Understandable, Robust.

In this post I will distill the W3C information on the Understandable principle.

Orientation

  • Support both landscape and portrait orientations
  • Changes in orientation must be programmatically exposed to ensure detection by devices such as screen readers

Consistent Layout

  • Within a particular size and orientation, repeated items should be placed consistently
  • Consistency between different screen sizes and screen orientations is not a requirement

Positioning before the Scroll

Position important items before the scroll.

Group Operable Elements

When two elements (e.g., an icon and a text link) perform the same action, group both items within the same actionable element.

 Be Clear About Which Elements are Actionable

Actionable elements such as links or buttons should be indicated by more than one means. Use combinations of shape, color, style, positioning, text labels, and conventional iconography.

Custom Manipulations and Gestures

Provide easily accessible instructions to explain what gestures can be used to control a given interface and whether there are alternatives.

Mobile device image: William Hook

Mobile Accessibility and the Operable Principle

mobile accessibility and the operable principle

The W3C wants to help developers understand how WCAG 2.0 applies on mobile devices. They recently issued the first working draft on the topic. The guidelines and principles of WCAG are explained using the POUR method: Perceivable, Operable, Understandable, Robust.

In this post I will distill the W3C information on the Operable principle.

Touch Target Size and Spacing

  • touch targets should be at least 9 mm high by 9 mm wide
  • touch targets close to the minimum size should be surrounded by a small amount of inactive space

Touchscreen Gestures

  • Gestures should be as easy as possible to carry out and two-step gestures should be avoided
  • To prevent unintentional actions, “elements accessed via touch interaction should generally trigger an event (e.g. navigation, submits) only when the touchend event is fired (i.e. when all of the following are true: the user has lifted the finger off the screen, the last position of the finger is inside the actionable element, and the last position of the finger equals the position at touchstart).”

Device Manipulation Gestures

These suggestions are for manipulations such as shaking or tilting:

  • Developers should still provide touch and keyboard operable alternative control options
  • Provide onscreen indicators that remind people how and when to use device manipulation gestures

Button Placement

  • Place interactive elements where they can be easily reached when the device is held in different positions or by different hands
  • Flexible use is the goal

Mobile device image: William Hook

Mobile Accessibility and the Perceivable Principle

Mobile accessibility and the perceivable princple

The W3C wants to help developers understand how WCAG 2.0 applies on mobile devices. They recently issued the first working draft on the topic. The guidelines and principles of WCAG are explained using the POUR method: Perceivable, Operable, Understandable, Robust.

In this post I will distill the W3C information for the Perceivable principle.

Small Screen Size

Guidelines include:

  • Use a responsive design and minimize the amount of information that is put on each page
  • Use fewer images
  • Focus on the features needed to operate in the mobile environment
  • Use a “reasonable” default size for content and touch controls to reduce the need for zooming
  • Make sure link text does not exceed the width of the device
  • Put form fields below their labels

Zoom/Magnification

The suggestions include:

  • The user should be able to zoom to 200%. Do not block this with the page’s viewport meta element.
  • Support system fonts that follow platform level user preferences for text size
  • Support options on the page to allow the user to change text size

Contrast

Mobile device content is viewed on smaller screens and in different conditions so the allowable ratio for lessened contrast on larger text on desktop devices must be considered carefully for mobile apps. Therefore, the W3C suggestion at this point is that developers should strive to apply the lessened contrast ratio only when text is roughly equivalent to 1.2 times bold or 1.5 times (120% bold or 150%) that of the default platform size. Even then, the user must be able to zoom to magnify the text.

Mobile device image: William Hook