Hide/Show Div Depending on Screen Width - javascript

I'm currently using CSS to hide a div section when the screen goes under a certain width. That bit works fine, but I'm struggling to get my head round, how to show a replacement div when I hide the div.
I might be going about it the wrong way, but here's my code:
#media all and (max-width: 955px) {
div.grid12-11 {
display: none;
}
}
#media all and (max-width: 954px) {
div.grid12-12 {
display: block;
}
}
What I'm trying to achieve is when the screen width goes below 955px grid12-11 is hidden and replaced with grid12-12.
Currently both the div's are being shown > 955px rather than replacing the block.
Is there a better/correct way to doing this?

There is no need to change the media query
div.grid12-12 {
display: none; // make it hidden on default view
}
#media all and (max-width: 955px) {
div.grid12-11 {
display: none;
}
div.grid12-12 {
display: block;
}
}

Check this: http://jsfiddle.net/Mohamed_nabil/cHQcD/
#media (max-width: 955px) {
div.gridFirst {
display: none;
}
}
#media (min-width: 955px) {
div.gridSecond {
display: none;
}
}

How are your initial rules set up? I think you're running into a problem of specificity. If your 'standard' rules are the same or more specific than your rules in your media query, the media query will not override them.
This seems to do what you expect:
#media all and (max-width: 955px) {
div.grid12-11 {
display: none;
}
div.grid12-12 {
display: block;
}
}
.grid12-11 { display:block; }
.grid12-12 { display:none; }
Note that if your standard rules (non-media) are prefixed with "div", it will NOT function (depending on what order the rules are processed in, and which browser you are using).

Related

how to set a div made of 3 buttons that in the moment in which the broswer's window is shrunk turns into a pop-up menu

I'm writing this message as I'd need to know how to set a div made of 3 buttons that, in the moment in which the broswer's window is shrunk and it's no longer on full screen, turns into a pop-up menu visible through the conventional three lines.
You can define #media in your css.
An example has been set here:
https://jsfiddle.net/jfgm9r2v/8/
The html code is like this:
<div class="hideOnSmall">
<button>Text1</button>
<button>Text2</button>
<button>Text3</button>
</div>
<div class="displayOnSmall">
<div class="hamburger-box">
<button>MyHambuger</button>
</div>
The magic happens in the css:
.hideOnSmall {
display: block;
}
.displayOnSmall {
display: none;
}
#media only screen and (max-width: 768px) {
.displayOnSmall {
display: block;
}
.hideOnSmall {
display: none;
}
}

print media query not working on iOS(chrome, safari, mozilla)

I'm trying to figure why the #media print doesn't seem to work on the iOS, on android the functionality works fine. Basically I've made a functionality to hide the contents that aren't part of the print
body.printing *{display : none}
and only display the contents that will be printed
body.printing .print-me, body.printing .print-me > div{display : block}.
$('.print-modal').on('click', function() {
$('body').addClass('printing');
window.print();
$("body").removeClass("printing");
})
#media print {
/* Hide everything in the body when printing... */
body.printing * { display: none; }
/* ...except our special div. */
body.printing .print-me, body.printing .print-me > div { display: block; }
}
#media screen {
/* Hide the special layer from the screen. */
.print-me { display: none; }
}
Maybe the issue is, you are adding a class printing and removing the same class immediately. That's could be the reason.
Try changing the logic. You don't need to add a class for printing. Instead use print media query #media print to handle print logic.
$('.print-modal').on('click', function() {
window.print();
})
#media print {
/* Hide everything in the body when printing... */
body * { display: none; }
/* ...except our special div. */
body .print-me, body .print-me > div { display: block; }
}
#media screen {
/* Hide the special layer from the screen. */
.print-me { display: none; }
}

Change text of <h1> if screen is reduced

I have a code trying to change the text of an h1 if the screen is reduced in size, here's the actual code I have, but is not working. Someone could help me to correct it and to understand it? thanks!
HTML
<h1 id="text1">Test</h1>
JAVASCRIPT
if($(window).width() <= 500){
$('#text1').Text('testing');
}else{
$('#text1').Text('buuu');
}
You need to work along with window resize event. Other option is using CSS pseudo code.
#media (min-width: 500px) {
#text1:after {
content: "foo";
}
}
#media (max-width: 499px) {
#text1:after {
content: "bar";
}
}
<h1 id="text1"></h1>
Edited As per Niet the Dark Absol suggestion.
#media (min-width: 500px) {
#text1 > .tiny {
display: none;
}
}
#media (max-width: 499px) {
#text1 > .big {
display: none;
}
}
<h1 id="text1">
<span class="big">Bigger Content</span>
<span class="tiny">Smaller Content</span>
</h1>
Maybe CSS could help you here:
use data- attributes to validate HTML5 and mediaqueries to show its value. Old text-indent method to erase at screen text hold in the tag itself.
You can use it without id or class for entire site, but data- attribute will need to be there and filled, else a class to set wherever needed, or id for a single use.
#media all and (max-width:500px) {
h1 {
text-indent:-9999px;
}
h1:before {
content:attr(data-text);
text-indent:0;
float:left;
}
}
<h1 data-text="short text">My long text stands here</h1>
You have to catch the window's resizing events. Pretty easy to do it with jquery.
Try this:
$(window).resize(function(){
if ($(window).width() <= 500){
$('#text1').Text('testing');
}
else {
$('#text1').Text('buuu');
}
});
http://jsbin.com/puzanajepe/1/edit?html,js,output

Style with media queries inside ngView for IE work after refresh

I have problem with css rules inside IE10 when they are inside page included by ngView (I'm using Angular 1.0.6). It work fine for other brower (I think that it work for IE11, didn't tested myself):
I have css like this:
.checker {
display: none;
}
#media (min-width: 1041px) {
.m1 { display: block; }
}
#media (min-width: 980px) and (max-width: 1040px) {
.m2 { display: block; }
}
#media (max-width: 767px) {
.m3 { display: block; }
}
#media (max-width: 979px) {
.m4 { display: block; }
}
and html:
<span class="checker m1">m1</span>
<span class="checker m2">m2</span>
<span class="checker m3">m3</span>
<span class="checker m4">m4</span>
the css is inside <style> tag on my page that's rendered inside ngView. And for page lareger then 1041px it show both m1 and m3. When I resize the browser the m3 disappear. It also work when I have the style and html inside the page not inside ngVew.
What is the problem here? is there a bug in IE10? How to fix my css? It's something with media queries, maybe the order of them?
I was able to fix it by adding min-width to that rule:
#media (max-width: 767px) and (min-width: 100px) {
No idea why but it work.

JavaScript and MediaQuerys

I'm trying to make some responsive page begin by mobile first and i'm always coming back to the same mistake.
When I have my pull-down menu in mobile version the menu it's ok, but when I try to change it to no-mobile version it doesn't works. i'd like a mobile version with one column and when the windows size oversteps the mark of 400px modify to two columns.
This is my code after lots of changes.
$('.slide ul').slideUp();
var ventana;
ventana=$('.wrapper').width();
$(window).on('load',preguntaTamano());
$(window).resize(preguntaTamano());
function preguntaTamano(e){
if(ventana<400){
mobile();
}
else if(ventana>400){
noMobile();
}
}
function mobile(e){
$('.especial').hide();
$('.toggle').on('click',controlaMenu);
}
function controlaMenu(e){
$('.slide ul').slideToggle();
e.preventDefault();
}
function noMobile(e){
$('.lista').show();
var listaElegidos;
listaElegidos= $('.elegidos').html();
$('.especial').preppend(listaElegidos);
}
Thank you very much!
I'd use CSS media queries instead:
#media (max-width: 400px) {
.lista {
display: block;
}
.especial {
display: none;
}
}
#media (min-width: 401px) {
.lista {
display: none;
}
.especial {
display:block;
}
}

Categories