CSS

Topics related to CSS:

Getting started with CSS

Styles can be authored in several ways, allowing for varying degrees of reuse and scope when they are specified in a source HTML document. External stylesheets can be reused across HTML documents. Embedded stylesheets apply to the entire document in which they are specified. Inline styles apply only to the individual HTML element on which they are specified.

Backgrounds

  • CSS3 gradients will not work on versions of Internet Explorer less than 10.

Centering

Margins

Media Queries

Media queries are supported in all modern browsers, including Chrome, Firefox, Opera, and Internet Explorer 9 and up.


It is important to note that the orientation media feature is not limited to mobile devices. It is based on the width and height of the viewport (not window or devices).

Landscape Mode is when the viewport width is larger than viewport height.

Portrait Mode is when the viewport height is larger than viewport width.

This usually translates to a desktop monitor being in landscape mode, but can it sometimes be portrait.


In most cases mobile devices will report their resolution and not their real pixel size which can differ due to pixel density. To force them to report their real pixel size add the following inside your head tag:

<meta name="viewport" content="width=device-width, initial-scale=1">


Floats

Typography

  • The text-shadow property is not supported by versions of Internet Explorer less than 10.

Flexible Box Layout (Flexbox)

Vender Prefixes

  • display: -webkit-box; /* Chrome <20 */
  • display: -webkit-flex; /* Chrome 20+ */
  • display: -moz-box; /* Firefox */
  • display: -ms-flexbox; /* IE */
  • display: flex; /* Modern browsers */

Resources

Cascading and Specificity

CSS specificity intends to promote code conciseness by allowing an author to define some general formatting rules for a broad set of elements, and then to override them for a certain subset.

Animations

Selectors

Colors

The Box Model

About padding-box

This value was only implemented by Firefox and thus should not be used. It was removed in Firefox version 50.0.

Transitions

Some older browsers support only vendor-prefixed transition properties:

  • -webkit: Chrome 25-, Safari 6-, Safari & Chrome for iOS 6.1-, Android 4.3- Browser, Blackberry Browser 7-, UC Browser 9.9- for Android.
  • -moz: Firefox 15-.
  • -o: Opera 11.5-, Opera Mobile 12-.

Example:

-webkit-transition: all 1s;
-moz-transition: all 1s;
-o-transition: all 1s;
transition: all 1s;

Length Units

  • A whitespace cannot appear between the number and the unit. However, if the value is 0, the unit can be omitted.

  • For some CSS properties, negative lengths are allowed.

Pseudo-Elements

  • Sometimes you will see double colons (::) instead of just one (:). This is a way to separate pseudo-classes from pseudo-elements, but some older browsers like Internet Explorer 8 only supports single colon (:) for pseudo-elements.

  • One can use only one pseudo-element in a selector. It must appear after the simple selectors in the statement.

  • Pseudo-elements are not a part of the DOM, therefore are not targetable by :hover or other user events.

Positioning

Normal Flow is the flow of elements if the position of element is static.

  1. defining width is beneficial because in some cases it prevents overlapping of element's content.

2D Transforms

2D Coordiante system

Transforms are made according to a 2D X/Y coordiante system. The X axis goes from right to left and the Y axis goes downwards as shown in the following image:

2D CSS coordinate system

So a positive translateY() goes downwards and a positive translateX() goes right.

Browser support and prefixes

  • IE supports this property since IE9 with the -ms- prefix. Older versions and Edge don't need the prefix
  • Firefox supports it since version 3.5 and needs the -moz- prefix until version 15
  • Chrome since version 4 and until version 34 needs the -webkit- prefix
  • Safari needs the -webkit- prefix until version 8
  • Opera needs the -o- prefix for version 11.5 and the -webkit- prefix from version 15 to 22
  • Android needs the -webkit- prefix from version 2.1 to 4.4.4

Example of prefixed transform:

-webkit-transform: rotate(45deg);
    -ms-transform: rotate(45deg);
        transform: rotate(45deg);

Tables

These properties apply to both <table> elements (*) and HTML elements displayed as display: table or display: inline-table

(*) <table> elements are obviously natively styled by UA/browsers as display: table

HTML tables are semantically valid for tabular data. It is not recommended to use tables for layout. Instead, use CSS.

Browser Support & Prefixes

Vendor prefixes are used to allow preview support for new CSS functionality where the functionality is not yet recommended by the specification.

It is recommended that you do not use vendor prefixes in production environments. These prefixes exist to test new functionality that is not yet finalized, and behavior is inherently unexpected. Simply using prefixes does not grant browser support for old browsers as you cannot guarantee the feature hasn't changed over time to perform differently, and it could still be broken in those old browsers you claim to support.

If supporting older browsers is important, you should instead consider using JavaScript or other solutions to imitate the effects and truly guarantee support for old browsers.

Browsers will use their prefixes and ignore the properties they don't understand.

NOTE: Prefixes should always appear before the official, unprefixed syntax. Otherwise they would be overwritten with the prefixed properties, which can be another implementation in the end.

If a browser supports both an unprefixed and prefixed version of a property, the most recent property to be declared will take precedence.

Normalizing Browser Styles

Meyer's Reset, while effective, makes the same changes to nearly every commonly used element. This has the result of polluting web browser inspector windows with the same applied styles over and over again, and creates more work for the browser (more rules to apply to more elements). The Normalize technique, on the other hand, is much more focused and less of a broad-brush technique. This simplifies the work on the part of the browser, and results in less clutter in the browser inspection tools.

Padding

The padding property sets the padding space on all sides of an element. The padding area is the space between the content of the element and its border. Negative values are not allowed.

1: https://developer.mozilla.org/en/docs/Web/CSS/padding MDN

Also see this question, "Why does CSS not support negative padding?" and his answers.

Also please consider The Box Model when using padding. Depending on the box-sizing value, padding on an element can either add to the previously defined height/width of an element or not.

Related Properties:

margin

Padding on inline elements will only apply to the left and right of the element, and not the top and bottom, due to the inherent display properties of inline elements.

Layout Control

Filter Property

Comments

  • Comments in CSS always start with /* and end with */
  • Comments cannot be nested

Cursor Styling

box-shadow

Browser Support:

  • Chrome 10.0
  • IE 9.0
  • Firefox 4.0 3.5 -moz
  • Safari 5.1 3.1 -webkit-
  • Opera 10.5

Custom Properties (Variables)

CSS Variables are currently considered an experimental technology.


BROWSER SUPPORT / COMPATIBILITY

Firefox: Version 31+ (Enabled by default)

More info from Mozilla

Chrome: Version 49+ (Enabled by default).

"This feature can be enabled in Chrome Version 48 for testing by enabling the experimental Web Platform feature. Enter chrome://flags/ in your Chrome address bar to access this setting."

IE: Not Supported.

Edge: Under Development

Safari: Version 9.1+

Shapes for Floats

Browser support for the CSS Shapes module is very limited at this point in time.

It is supported in Chrome v37+ and Opera 24+ without browser/vendor prefixes. Safari supports it from v7.1+ but with the -webkit- prefix.

It is not yet supported in IE, Edge and Firefox.

Grid

CSS Grid Layout Module Level 1 is, as of 9 September 2016, a W3C Candidate Recommendation. It is considered to be in the Testing stage (https://www.w3.org/Style/CSS/current-work).

As of 3 July 2017, Microsoft's Internet Explorer 10 and 11 and Edge browsers only support an older version of the specification using a vendor prefix.

Border

Related properties:

  • border

  • border-bottom

  • border-bottom-color

  • border-bottom-left-radius

  • border-bottom-right-radius

  • border-bottom-style

  • border-bottom-width

  • border-color

  • border-image

  • border-image-outset

  • border-image-repeat

  • border-image-slice

  • border-image-source

  • border-image-width

  • border-left

  • border-left-color

  • border-left-style

  • border-left-width

  • border-radius

  • border-right

  • border-right-color

  • border-right-style

  • border-right-width

  • border-style

  • border-top

  • border-top-color

  • border-top-left-radius

  • border-top-right-radius

  • border-top-style

  • border-top-width

  • border-width

Functions

For calc(), white space is required around the "-" and "+" operators, but not the "*" or "/" operators.

All units must be of the same type; trying to multiply a height by a time duration, for example, is invalid.

3D Transforms

Coordinate system

3D transforms are made according to an (x, y, z) coordinate vector system in Euclidean space.

The following image shows an example of coordinates in Euclidean space:

Euclidean space

In CSS,

  • The x axis represents the horizontal (left and right)
  • The y axis represents the vertical (up and down)
  • The z axis represents the depth (forward and backward/closer and further)

The following image shows how these coordinates are translated in CSS:

CSS 3D coordinate system

Counters

Counters are not a new topic in CSS. It was a part of the CSS Level 2 Specifications (Revision 1 to be precise) itself and hence has very high browser support.

All browsers except IE6 and IE7 have support for CSS Counters.

Single Element Shapes

Opacity

Performance

Columns

Inline-Block Layout

Inheritance

CSS Image Sprites

Clipping and Masking

CSS Clipping and Masking are very new concepts and so the browser support for these properties are pretty low.

Masks:

As at the time of writing (Jul '16), Chrome, Safari and Opera support these properties with the -webkit- prefix.

Firefox doesn't require prefixes but it supports masks only when used with SVG mask elements. For inline SVG mask elements, the syntax is mask: url(#msk) whereas for using mask elements in an external SVG file the syntax is mask: url('yourfilepath/yourfilename.svg#msk'). #msk in both cases refers to the id of the mask element that is being referred to. As indicated in this answer, at present Firefox doesn't support any parameter other than mask-reference in the mask property.

Internet Explorer (and Edge) does not offer any support for this property as yet.

The mask-mode property is currently not supported by any browser with or without prefixes.

Clip-path:

As at the time writing (Jul '16) Chrome, Safari and Opera supports clip-path when the path is created using basic shapes (like circle, polygon) or the url(#clipper) syntax with inline SVG. They don't support clipping based on shapes that are part of external SVG files. Also, they require the -webkit prefix to be present.

Firefox supports only the url() syntax for clip-path whereas Internet Explorer (and Edge) offer no support.

List Styles

Although the list-style-type is actually a property that applies only to list items (normally <li>), it is often specified for the list tag (<ol> or <ul>). In this case, the list items inherit the property.

Outlines

outline is now described in Basic UI, a CSS Module Level 3 (it was already described in REC CSS2.1)


Outline property is defined by default in browsers for focusable elements in :focus state.
It shouldn't be removed, see http://outlinenone.com which states:

What does the outline property do?

It provides visual feedback for links that have "focus" when navigating a web document using the TAB key (or equivalent). This is especially useful for folks who can't use a mouse or have a visual impairment. If you remove the outline you are making your site inaccessible for these people. (…)

Interesting related examples on Stack Overflow:

Structure and Formatting of a CSS Rule

For ease of readability, keep all declarations indented one level from their selector, and the closing curly brace on its own line. Add a single space after selectors and colons, and always place a semicolon after the final declaration.

Good

p {
    color: maroon;
    font-size: 16px;
}

Bad

p{
 color: maroon;
font-size:16px }

One-Liner

If there are only one or two declarations, you might get away with this one. Not recommended for most cases. Always be consistent when possible.

p { color: maroon; font-size: 16px; }

Fragmentation

There is no page-break property in CSS. Only the 3 properties (page-break-before, page-break-after, page-break-inside).

Related: orphans, widows.

Overflow

The overflow property specifies whether to clip content, render scrollbars, or stretch a container to display content when it overflows its block level container.

When an element is too small to display it's contents, what happens? By default, the content will just overflow and display outside the element. That makes your work look bad. You want your work to look good, so you set the overflow property to handle the overflowing content in a desirable way.

Values for the overflow property are identical to those for the overflow-x and overflow-y properties, exept that they apply along each axis

The overflow-wrap property has also been known as the word-wrap property.

Important note: Using the overflow property with a value different to visible will create a new block formatting context.

CSS Object Model (CSSOM)

Feature Queries

Feature detection using @supports is supported in Edge, Chrome, Firefox, Opera, and Safari 9 and up.

Stacking Context

Internet Explorer Hacks

These “hacks” may be used to target a specific browser/client. This may be used to work around browser rendering differences by applying styles in one of those wrappers listed above.

Block Formatting Contexts

Vertical Centering

This is used when the element's dimensions (width and height) are not known or dynamic.

Prefer to use Flexbox over all other options as it is optimized for user interface design.

Object Fit and Placement

The properties object-fit and object-position are not supported by Internet Explorer.

Multiple columns

CSS Multi-column Layout Module Level 1 is, as of 12 April 2011, a W3C Candidate Recommendation. Since then, a few smaller changes were made. It is considered to be in the Stable stage.

As of 3 July 2017, Microsoft's Internet Explorer 10 and 11 and Edge browsers only support an older version of the specification using a vendor prefix.

CSS design patterns

These examples are for documenting CSS-specific methodologies / design patterns.

These methodologies include but are not exclusive to the following:

These examples are NOT for documenting CSS frameworks like Bootstrap or Foundation. While you may include examples of how to apply one or more CSS methodology / design pattern with a CSS framework, those examples are to focus on the methodologies / design patterns with that particular framework and on the use of the framework itself.