Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css media query adding class to HTML

I have this HTML:

<li><a href=""><i class="fa fa-iconname" style="vertical-align: middle;"></i>Link Name</a></li>

I am then using this media query in my CSS:

@media (max-width: 1000px) {
    ...
}

how can i change my tag to:

<i class="fa fa-iconname lg-2x" style="vertical-align: middle;"></i>

when the media query takes effect?

like image 832
CharlesF Avatar asked Apr 12 '15 19:04

CharlesF


People also ask

Can you put media queries in HTML?

Media queries are commonly associated with CSS, but they can be used in HTML and JavaScript as well.

How do you add or remove a class in media query?

You don't. Instead, you define new rules that apply to the class which do the styling you want done. Media queries can't add or remove classes to elements, they merely change what rules apply to elements.

What is @media in HTML CSS?

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.

Can you use CSS custom properties in media queries?

Well... if you're using CSS custom properties (CSS variables) and wanted to reference them in a media query, you probably discovered that you can't use custom properties in this context. CSS custom properties weren't designed for that use case.


2 Answers

You can use pure css to achieve this by just replicating the list-item and toggle with media query like this:

HTML:

<li class="bigScreen"><a href=""><i class="fa fa-iconname"></i>Link Name</a></li>

<li class="smallScreen"><a href=""><i class="fa fa-iconname lg-2x"></i>Link Name</a></li>

CSS:

@media (max-width: 1000px) {
  .bigScreen {
    display:none;
  }
  .smallScreen {
    display:block;
  }
}

@media (min-width: 1001px) {
  .bigScreen {
    display:block;
  }
  .smallScreen {
    display:none;
  }
}
like image 103
AndrewL64 Avatar answered Sep 19 '22 12:09

AndrewL64


You can not do that with css, but you can with JavaScript or jQuery.

fa-2x is essentialy: font-size: 2em; . So, you can do this:

@media (max-width: 1000px) {
    .fa-iconname {
    font-size: 2em;
    }
} 
like image 33
Danko Avatar answered Sep 19 '22 12:09

Danko