Passa al contingut principal

CSS3 Conditional Statements

In times gone by a web designer had only the properties and values in his head to style a website. Nowadays however, things are a little more complex, and they’re about to get even more so with the CSS Working Group’s conditional CSS statements.



This article is about new CSS which may not be supported in every browser! Be careful.

I say almost introductive because you might have heard of these new things called media queries. Media queries are a type of conditional statement where you can style a webpage based on the properties of a user’s computer. For example, one might say the following in their CSS stylesheet:



@media screen and (min-width: 1000px) {

/* CSS Goes here! */

}

The stuff you put in the curly brackets will only apply to screens with a width greater than 1000px. There are a bunch of other media queries regarding resolution, etc. I’m sure you’re quite familiar with them. What you may not know however is the CSS Conditional Statement Specification also talks about two other CSS conditional statements. Currently these two features are only supported in Firefox (6.0 for @document and 17.0 for @supports)


@supports



Suppose a particular brand of browser supports a feature, but another doesn’t. You then have a back up CSS script planned for browsers that do not support that particular feature. Well, @supports can help you with that! @supports checks to see if the property you call is supported in the browser.


So lets say (for some reason) you only want CSS to apply to a browser with the CSS flexible box model supported. You might do something like this:



@supports (display: flex) {
/* Put some CSS here */
}

The CSS in the curly brackets will only be enabled should the browser support the flex box. Then you can have other CSS which overrides that CSS should the browser not support the flex box, i.e.:



@supports (display: flex) {
/* Flexible box CSS goes here */
}

/* If flexbox isn't supported, backup CSS goes here */

Similar to media queries, you can check for multiple things using or and and.



@supports (display: flex) and (box-shadow: 0 0 10px rgba(0,0,0,0.1) {
/* If box shadows and the flex box are supported */
}
@supports ((display: flex) and (animation-duration: 1s)) or ((diplay: flex) and (transition-duration: 1s)) {
/* If flex box and animation duration are supported, or if flex box and transition duration are supported */
}

Negation


You can check if a browser doesn’t support a feature too using not(). For example, the CSS in the curly brackets below will only be applicable when flexbox isn’t supported:



@supports not(display: flex) {
/* Back up CSS goes here */
}

@document



@document styles CSS based on the page you are on. For example, it would be totally possible to do this to target a specific URL with CSS:



@document url('http://www.inserthtml.com/') {
/* CSS goes here */
}

Or you could apply it to everything in a particular folder:



@document url-prefix('http://www.inserthtml.com/css') {
/* CSS goes here */
}

The Benefits


Faster loading times


Presumably you can use this little trick to have a single unified header which selects which files to include based more on the user’s browser’s capabilities. For example, lets suppose you have some additional extras (box shadows, border-radius, etc), which are intended for newer browsers. When it comes to IE this can be a big problem because IE doesn’t support a lot of these features but you still have to load the CSS file. Using @supports you could reduce your primary CSS file and include the added extras as necessary in separate CSS files, thus improving page load times.




<link rel="stylesheet" type="text/css" href="style.css" />

<!-- Optional extras -->
<style>

@supports (box-shadow: 0 0 10px rgba(0,0,0,0.1)) {
@import url('box-shadow.css');
}

</style>

One Stylesheet for everything


Alternatively, you can have one stylesheet for everything since you can load the page specific and support specific CSS within these conditional statements. This is a good idea since the single CSS file will be stored in the user’s cache, so the page will load faster after the first visit.




@supports (display: flexbox) {
/* If supported, do this CSS */
}

/* Some regular CSS here */
div {
...
}

@document url('http://www.inserthtml.com/page.html') {
/* CSS for specific page */
}

@media print {
/* Media CSS */
}


Current Support



























FeatureChromeSafariFirefoxIEOpera
@documentNoneNone6.0NoneNone
@supportsNoneNone17.0NoneNone







via InsertHTML http://www.inserthtml.com/2012/09/css3-conditional-statements/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+inserthtml+%28InsertHTML%29

Comentaris

Entrades populars d'aquest blog

Learn Composition from the Photography of Henri Cartier-Bresson

“Do you see it?” This question is a photographic mantra. Myron Barnstone , my mentor, repeats this question every day with the hopes that we do “see it.” This obvious question reminds me that even though I have seen Cartier-Bresson’s prints and read his books, there are major parts of his work which remain hidden from public view. Beneath the surface of perfectly timed snap shots is a design sensibility that is rarely challenged by contemporary photographers. Henri Cartier-Bresson. © Martine Franck Words To Know 1:1.5 Ratio: The 35mm negative measures 36mm x 24mm. Mathematically it can be reduced to a 3:2 ratio. Reduced even further it will be referred to as the 1:1.5 Ratio or the 1.5 Rectangle. Eyes: The frame of an image is created by two vertical lines and two horizontal lines. The intersection of these lines is called an eye. The four corners of a negative can be called the “eyes.” This is extremely important because the diagonals connecting these lines will form the breakdown ...

El meu editor de codi preferit el 2024, que això ja se sap que va canviant 😄

Visual Code Visual Code és un editor de codi font lleuger, però potent que s’executa al teu escriptori i està disponible per a Windows, macOS i Linux. Compta amb suport integrat per a JavaScript, TypeScript i Node.js i té un ric ecosistema d’extensions per a altres llenguatges i entorns d’execució (com C++, C#, Java, Python, PHP, Go, .NET).  És una eina ideal per a desenvolupar i depurar aplicacions web i en el núvol. Per què Visual Code? Visual Code té molts avantatges com a editor de codi font, com per exemple: És gratuït, ràpid i fàcil d’instal·lar i actualitzar. Té un ampli ecosistema d’extensions que et permeten afegir funcionalitats i personalitzar la teva experiència de desenvolupament. Té un suport integrat per a molts llenguatges i entorns d’execució, i et permet depurar i executar el teu codi des del mateix editor. Té una interfície senzilla i elegant, amb diferents temes i modes de visualització. Té un sistema de sincronització de configuracions que et permet guardar les...

Las Mejores Aplicaciones Gratis para iPad de 2012

Las Mejores Aplicaciones Gratis para iPad de 2012 : ¿No tienes ni un duro? No te preocupes, pues hoy os traemos una extensa selección de las mejores apps gratuitas que puedes conseguir en la App Store para que llenes tu iPad de calidad, sin gastar nada de nada.   ¿Estás buscando juegos o apps gratis para tu iPad? En la App Store hay más de 500,000 apps y juegos, y una gran cantidad de ellos está disponible de forma totalmente gratuita. Aquí vamos con la selección de las mejores Apps gratis para iPad (todos los modelos), organizada por categoría. ¿Estás preparado? Las Mejores Apps Gratis de Redes Sociales para iPad Nombre Facebook Gratis Categoría Redes sociales Facebook es la red social más famosa del mundo , con casi mil millones de usuarios. Su app para iPad ha tardado, pero aquí está. Nombre Twitter Gratis Categoría Redes sociales Twitter es la red de microblogging por excelencia. La forma más rápida y directa de informar y mantenerse informado de las cosa...