By Hristo


2010-08-10 20:42:38 8 Comments

I just stumbled across this question and I noticed the user is using some notation I've never seen before:

@font-face {
   /* CSS HERE */
}

So is this @ symbol something new in CSS3, or something old that I've somehow overlooked? Is this something like where with an ID you use #, and with a class you use .? Google didn't give me any good articles related to this. What is the purpose of the @ symbol in CSS?

5 comments

@BoltClock 2010-08-10 20:44:21

@ 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, as I mentioned, 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.

@kurdtpage 2015-03-18 22:32:28

@media print{ /* Are you trying to print a video or audio file?? */ }

@Tcll 2013-10-04 19:14:54

The ProBoards CSS style also uses these as variables.

Here's a small snipptt from one of their CSS pages:

@wrapper_width: 980px;
@link_color: #c06806;
@link_font: 100% @default_forum_text_font_family;
@link_decoration: none;

#wrapper { width: @wrapper_width; margin: 0 auto; overflow-x: hidden; }
table { table-layout: fixed; }
a { cursor: pointer; color: @link_color; font: @link_font; text-decoration: @link_decoration; }

NOTE: not native, see first comment.

@Abacus 2014-09-10 21:14:00

This is not "native", see lesscss.org

@Tcll 2014-09-12 14:24:50

ok thanks, I'll be sure to add that :)

@Frankie 2010-08-10 20:48:24

@ is used to define a rule.

@import
@page
@media
@font-face
@charset
@namespace

The above are called at-rules.

@Hristo 2010-08-10 20:49:42

What do you mean by "define a rule"?

@Frankie 2010-08-10 20:51:43

At-rules encapsulate a bunch of CSS rules and apply them to something specific. (htmldog.com/guides/cssadvanced/atrules)

@BobRodes 2013-05-17 18:22:36

An example of @media that might be useful to illustrate it further:

@media screen and (max-width: 1440px)
{
    span.sizedImage
    {
        height:135px;
        width: 174px;
    }
}    

@media screen and (min-width: 1441px)
{
    span.sizedImage
    {
        height:150px;
        width: 200px;
    }
}

This will vary the size of the image conditionally on the size of the screen, using a smaller image on a smaller screen. The first block would address screens up to width 1440px; the second would address screens larger than 1440px.

This comes in handy with things like tabs that float drop or scroll on smaller screens; you can often drop the font size of the tab labels down a point size on smaller screens and have them all fit.

@r3st0r3 2010-08-10 21:37:23

@ is used as a rule specification. Like @font-face

Related Questions

Sponsored Content

46 Answered Questions

[SOLVED] How can I transition height: 0; to height: auto; using CSS?

18 Answered Questions

[SOLVED] Reference — What does this symbol mean in PHP?

27 Answered Questions

[SOLVED] What does "use strict" do in JavaScript, and what is the reasoning behind it?

18 Answered Questions

[SOLVED] How do CSS triangles work?

35 Answered Questions

[SOLVED] Change an HTML5 input's placeholder color with CSS

21 Answered Questions

[SOLVED] Stretch and scale a CSS image in the background - with CSS only

  • 2009-07-19 15:51:52
  • Fábio Antunes
  • 1684963 View
  • 824 Score
  • 21 Answer
  • Tags:   css background

19 Answered Questions

[SOLVED] Is it possible to apply CSS to half of a character?

31 Answered Questions

[SOLVED] Is there a CSS parent selector?

  • 2009-06-18 19:59:36
  • jcuenod
  • 1831480 View
  • 2973 Score
  • 31 Answer
  • Tags:   css css-selectors

26 Answered Questions

[SOLVED] Set cellpadding and cellspacing in CSS?

Sponsored Content