use different css when button clicked with different media queries - css

Good afternoon. I have a little problem that is causing me a headache. I have a DIV with an ID of 'booking-md' This changes size with media query and works well like so.
<div id="booking-md" class="container-fluid booking-md"></div>
and media queries change it like so:-
#media screen and (max-width:991px) {
.booking-md { height: 320px}
}
#media screen and (max-width:767px) {
.booking-md { height: 220px}
}
#media screen and (max-width:560px) {
.booking-md { height: 320px}
}
I also have a button which makes the 'booking-md' height bigger like so
<button type="button" onclick="ret()" class="btn btn-return">Return</button>
function ret() {
$('#booking-md').css('height', '175px');
$('#return').show()
}
Now this adds 50px to the size ok but when It resizes it does not use the original media queries to change size. Ideally I need to have 2 different sets of media queries, one for if button has been clicked and one if not.
I have tried this with changing the class but doesnt do much after changing the size once. It looks like it loses all the media queries.
document.getElementById("booking-md").className = "booking-sm";
Is using php with this an option? Im at a loss and seem to have tryied everything, thanks

#wayneuk2, please have a look at the below working snippet, have made a few minor updates to your own code, hope it helps :)
.set-height {
height: 175px;
}
.booking-md {
border: 4px solid #ccc;
}
#media screen and (max-width:991px) {
.booking-md {
height: 320px
}
}
#media screen and (max-width:767px) {
.booking-md {
height: 220px
}
}
#media screen and (max-width:560px) {
.booking-md {
height: 320px
}
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet"/>
<div id="booking-md" class="container-fluid booking-md">
</div>
<br />
<button type="button" onclick="ret()" class="btn btn-primary btn-return">Return</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script>
function ret() {
$('#booking-md').addClass('set-height');
$('#return').show()
}
</script>

wow that was easy!! Dont know why I was trying the hard ways, I simply too out the change the height in the media queries and just set the 'booking-md' height like this to fit in the content.
.booking-md {height:auto;overflow: hidden;)
Works like a charm

Related

How to make print use desktop media queries?

I am attempting to add some print functionality to my page, but I am having an issue where when I print, it is printing as if it is mobile. How can I make it so that when someone hits print it prints the desktop version?
$('.print-btn').click(function(e) {
window.print();
});
.visible-desktop {
display:none;
}
#media only screen and (min-width:768px) {
.visible-desktop {
display:block;
}
.visible-mobile {
display:none;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="visible-desktop">Viewing from desktop</div>
<div class="visible-mobile">Viewing from mobile</div>
</div>
<a class="print-btn" href="#">Pring</a>
You can add an #media print rule at the bottom of your stylesheet:
#media print {
.visible-desktop {
display:block;
}
.visible-mobile {
display:none;
}
}
only screen
This excludes print. If you want to include print, don't write that.
You should pick one state (probably desktop) and make it the "default", by moving that state outside media queries.
The other states should override the default state's properties in media queries.

Remove horizontal line <hr> when screen width <767

How can you hide the horizontal line tag <hr> when the screen width is smaller than 767px?
How can this be done using css only.
You can use CSS to do that, it will take the device width in pixels and check if it's true.
#media (max-width:767px) { hr { visibility: none; } //This will make the hr hidden if the screen size is under 767px
Or you can just use JavaScript to check the width and see if you should deliver the hr to the page.
Kind regards
#media(max-width:767px) {
hr {
display:none;
}
}
<hr />
More info on media queries.
I know it's not tagged, but the Javascript would be as follows:
HTML:
<hr id="rule" />
JS:
if(window.innerWidth < 767){
document.getElementById("rule").style.visibility = 'hidden';
}
else{
document.getElementById("rule").style.visibility = 'visible';
}
use this:
#media screen and (max-width: 767px) {
hr{
display:none;
}
}
As inline css:
<style media="screen">
hr{
border-style: none;
}
</style>
As inside hr tag:
<hr style = "border-style: none">

Media Queries - Should we always set a default value on original/parent stylesheet?

On a mobile-first design, after changing values with Media Queries, Are we supposed to set them back to their original values on the parent stylesheet? or will it be handled by the browser?
<div id="mobile">
<div class="box1">
<div class="box2">
...
</div>
stylesheet:
( default style for mobile goes here )
#media (min-width: 500px) {
.box1 {
float: right;
}
.box2 {
float: left;
}
}
#media (min-width: 700px) {
.mobile {
display: none;
}
}
like in this case, should I to set the display value to, for example block on the default stylesheet? and/or reset the float attributes respectively?
No, setting default values won't be required in this case.
HTML has default display property with value inline, but say we want default value as block then we might want to specify default values.

How does one add padding to Bootstrap 3's container without screwing up the grid?

Many site designs call for a dark background with a lighter foreground page that contains the site's content. When using Bootstrap, it seems logical that one would merely apply the light color to the .container div and be done with it. However, Bootstrap's container does not provide any padding between its edges and the columns within, so this solution provides a light background but with the column content flush with the edges - not a good look.
This is a common problem and there are several solutions on stackoverflow and elsewhere for Bootstrap 2, but I couldn't find anything useful for Bootstrap 3.
One of the Bootstrap 2 solutions involved using .container-fluid which switches the internal grid from pixel to percentage based. Then one may apply padding to a a background div within .container-fluid and the internal columns will seamlessly adjust to fit. This seemed like a good approach, but the specific CSS used to tweak some of the other styles didn't work with Bootstrap 3.
After digging through the Bootstrap source, I noticed that the only difference between the .container and .container-fluid rules, in grid.less are three media queries. I wrapped my page's content in .container-fluid then overrode its definition with one that included the media queries so that the page content would respond the same as the header and footer, which use the standard .container.
Here's the HTML:
<html>
<head>
<title>Bootstrap 3 Container Padding Example</title>
</head>
<body>
<div class="page-container">
<div class="container-fluid">
<div class="page-bg">
<!-- PAGE CONTENT -->
</div>
</div>
</div>
</body>
</html>
And then the .less:
.page-container > .container-fluid:first-child {
.container-fixed();
#media (min-width: #screen-sm-min) {
width: #container-sm;
}
#media (min-width: #screen-md-min) {
width: #container-md;
}
#media (min-width: #screen-lg-min) {
width: #container-lg;
}
}
Then added padding to the .page-container div:
.page-container {
.page-bg {
padding: 15px;
background-color: #EFEFEF;
}
}
body {
background-color: #333
}
This seems to work. I haven't completed the styling for the interfaces that will reside within this container yet so there could be issues down the road but everything seems to render fine so far.
Here is a working example of this solution on codepen.io.
Note that the solution above uses less.css after including Bootstrap's variables and mixins .less files. Here's the compiled CSS:
.page-container > .container-fluid:first-child {
margin-right: auto;
margin-left: auto;
padding-left: 15px;
padding-right: 15px;
}
#media (min-width: 768px) {
.page-container > .container-fluid:first-child {
width: 750px;
}
}
#media (min-width: 992px) {
.page-container > .container-fluid:first-child {
width: 970px;
}
}
#media (min-width: 1200px) {
.page-container > .container-fluid:first-child {
width: 1170px;
}
}
.page-container .page-bg {
padding: 15px;
background-color: #EFEFEF;
}
body {
background-color: #333333;
}

How to show hidden button with css media query?

I have a hidden button it's means display:none. I tried with this.
#media screen and (max-width:1360px) {
#a10{display:none;}
#menu_btn{display:block;}
}
following code means: "display #menu_btn as inline-block when screen is more or equal to 1360, hide it when screen is less than 1360"
HTML:
<input id="menu_btn" type="button"/>
CSS:
#media screen and (min-width:1360px) {
#menu_btn { display:inline-block; }
}
#media screen and (max-width:1359px) {
#menu_btn { display:none; }
}
PS. remember that you cannot set inline style in html like this: <input id="menu_btn" type="button" style="display:none"/> because it will override style from media query and your button will not be displayed no matter what the screen size will be.

Resources