Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use @media in an if/else kind of way?

I have this form placed in the footer of my page (has just one text box and one button). I want to try and apply @media to it, based on the viewport width available ... So for example, in my case the full width this form needs it about 270px or so. So I want to apply CSS to say that:

if the width available is at least 270px      apply first set of CSS rules else      apply second set of CSS rules 

How can I do this using @media ?

like image 684
Ahmad Avatar asked Jul 14 '13 10:07

Ahmad


People also ask

What is @media rule?

The @media rule is used in media queries to apply different styles for different media types/devices. Media queries can be used to check many things, such as: width and height of the viewport. width and height of the device. orientation (is the tablet/phone in landscape or portrait mode?)

What is the difference between @media and @media only screen?

@media is the actually media query. The word screen is adding the 'conditions' to the media query. So @media screen is telling the media query to apply (whatever other conditions) to screens. For example, @media screen and (max-width: 360px) will target only screens with a max-width of 360px.

What is @media in CSS used for?

The @media CSS at-rule can be used to apply part of a style sheet based on the result of one or more media queries. With it, you specify a media query and a block of CSS to apply to the document if and only if the media query matches the device on which the content is being used.

Can @media be used inline CSS?

It is not possible to use CSS @media rules and media queries in the inline style attribute as it can only contain property: value pairs. According to the W3 specification, the style attribute's value should match the syntax of contents of a CSS declaration block.


1 Answers

There's no if/else syntax for @media, so you would need to repeat the same media query in two separate @media rules and use not for one of them to mean "else".

In your case, the media query would be all and (min-width: 270px). (You need to have a media type for not to work; the default is all so I'm just using that.)

Your CSS would then look like this:

@media all and (min-width: 270px) {     /* Apply first set of CSS rules */ }  @media not all and (min-width: 270px) {     /* Apply second set of CSS rules */ } 

I should add that one popular way is to make use of the cascade by having just one @media rule for overriding styles:

/* Apply second set of CSS rules */  @media all and (min-width: 270px) {     /* Apply first set of CSS rules */ } 

However this falls short if you have any styles outside the @media rule that aren't (or cannot be) overridden inside it. Those styles would continue to apply, and you have no way of undoing them short of actually redeclaring them. Sometimes you cannot undo them by redeclaring them, and that's when you cannot use this method to apply styles. See my answer to this question for a detailed explanation.

In this example, the height: 200px declaration will always apply:

.example {     width: 200px;     height: 200px; }  @media all and (min-width: 270px) {     .example {         width: 400px;     } } 

Of course, if that's not a problem then you can use this in order to avoid duplicating media queries. But if you're looking for a strict if/else construct, then you'll have to use not and a duplicated media query.

like image 136
BoltClock Avatar answered Sep 19 '22 15:09

BoltClock