Wednesday, 28 May 2014

Purpose of the '@' symbol in CSS 0

@ has been around since the days of @import in CSS1, although it's arguably becoming increasingly common in the recent @media (CSS2, CSS3) and @font-face (CSS3) constructs. The @ syntax itself, though, is not new.

These are all known in CSS as at-rules (@). They're special instructions for the browser, not directly related to styling of (X)HTML/XML elements in Web documents using rules and properties, although they do play important roles in controlling how styles are applied.

Some code examples:
/* Import another stylesheet from within a stylesheet */

@import url(style2.css);

/* Apply this style only for printing */

@media print {

    body {

        color: #000;

        background: #fff;

    }

}

/* Embed a custom web font */

@font-face {

    font-family: 'DejaVu Sans';

    src: local('DejaVu Sans Regular'), url(/fonts/DejaVuSans.ttf);

}
  • @font-face rules define custom fonts for use in your designs that aren't always available on all computers, so a browser downloads a font from the server and sets text in that custom font as if the user's computer had the font.
  • @media rules, in conjunction with media queries (formerly only media types), control which styles are applied and which aren't based on what media the page is being displayed in. In my code example, only when printing a document should all text be set in black against a white (the paper) background. You can use media queries to filter out print media, mobile devices and so on, and style pages differently for those.
At-rules have no relation to selectors whatsoever. Because of their varying nature, different at-rules are defined in different ways across numerous different modules.

More examples include:
(this list is far from exhaustive)

You can find another non-exhaustive list at MDN. 

Some at-rules (@) available in CSS
source page
Have any doubt, feel free to comment here! 

0 comments:

Post a Comment