In CSS we can define styles for id's with the hash ( #
) and for classes with the dot ( .
), but there are some rules that are preceded by the at sign ( @
) such as:
@font-face {
/* definición de fuente propia */
}
@media print {
/* estilos a la hora de imprimir */
}
@keyframes miAnimacion {
/* definición de pasos para una animación */
}
What does the @
before period mean? How many are there and what are they used for? What's so special about them?
Rules of @
(At-Rules or at-rules )
They are CSS declarations that encapsulate a set of rules, but are not directly related to HTML/XML elements. They control how styles are applied by extending the power of CSS and each have their own syntax.
General rules
They apply to all CSS
@charset
Defines the character set used in the CSS.@import
Allows you to include other CSS.@namespace
Sets the XML to be used.nested rules
They can be as a style statement or as conditionals.
@media
Set conditional rules according to output device characteristics (screen size, printer type, braille devices, etc.)@supports
Set conditional rules based on the browser used.@document
Restricts contained rules based on URL.@page
Restricts the rules contained for when you want to print (on a printer) the document.@font-face
Includes external (typeface) fonts.@keyframes
Controls the intermediate steps in an animation sequence.@viewport
Restrict rules based on window size and orientation, especially useful on mobile.@counter-style
Define specific counter styles.@font-feature-values
, @swash, @ornaments, @annotation, @stylistic, @styleset, and @character-variant Define common names for the propertyfont-variant-alternates
.examples
1. Define the UTF-8 character set
It must be the first element in the CSS, and cannot be preceded by any other character.
2. Define different rules depending on the document that loads it
With @document we can define, within the same CSS, a background image for the main page and another one for those that start with "forum".
3. Use a font from Google Fonts
This example references the Source Sans Pro font .
and then can be used in the CSS like:
4. Include another CSS when the device is in landscape mode
In the following, the external CSS is included
ruta_de_otro.css
.5. Define 2 property sets based on compatibility
In this way 2 groups are defined, according to whether the browser supports flex properties or not.
6. Use a @media query to differentiate between devices