Remove element level display: none using CSS - html

I have a page that hides a div when the screen is small, showing another div with a clickable + to expand the hidden div.
#media screen and (max-width: 1230px) {
#details_Title {
display: none;
}
#details_Details {
display: inline; // THIS DOES NOT WORK AFTER JQUERY SLIDEUP
}
}
#media screen and (max-width: 990px) {
#details_Title {
display: inline;
}
#details_Details {
display: none;
}
}
The HTML
<div id="details_Title" onclick="showDetails()">
<b>Details</b>
<img id="imgPlusMinus" src="images/plus.png"/>
</div>
<div id="details_Details">
.... the details
</div>
JS
function showDetails() {
var img=document.getElementById('imgPlusMinus').src;
if (img.indexOf('plus.png')!=-1) {
document.getElementById('imgPlusMinus').src ='images/minus.png';
$("#details_Details").slideDown();
}
else {
document.getElementById('imgPlusMinus').src='images/plus.png';
$("#details_Details").slideUp();
}
}
Make the screen small, the divs show and hide correctly, click the + and the div details_Details expands as expected.
The problem is that if you close it and jQuery slides up the div details_Details, display: none is applied at the element level and the display: inline from the media query does not get applied. How do I get around this? Can I remove/overwrite the element level style from the media query?

$('#click').on('click', function() {
$('#toggle').toggle();
});
#media(min-width:767px){
#toggle{
display: block!important;
}
}
#click{
display: none;
}
#media(max-width:767px){
#click{
display: block;
}
#toggle {
display: none;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="click">click</div>
<div id="toggle">always show above 767px</div>
Add a declaration for min-width so it will show no matter what.
#media screen and (min-width: 1230px) {
#details_Details {
display: inline!important; // THIS WILL WORK AFTER JQUERY SLIDEUP
}
}

Related

Show section only in specified media query

I'm working on making my website responsive, but I encountered a problem.
I'm trying to hide a section to make it only phone visible, but as I try to set my display:none for my section and to enable it in my media query, it is overwritten by my non-media query code.
The 2 sections that I want to hide from PC users are .phone-services and .avis-phone. The problem is that, as I said if I state them as display:none, they will overwrite my media query.
Here is a part of my #media CSS:
#media only screen
and (min-device-width: 320px)
and (max-device-width: 480px)
and (-webkit-min-device-pixel-ratio: 2)
and (orientation: portrait) {
.services {
display:none !important;
}
.avis {
display:none !important;
}
.phone-services {
background:#02000A;
}
.avis-phone {
background:#02000A;
color:white;
}
}
Here is a part of the other CSS that overwrites it:
#import url('https://fonts.googleapis.com/css2?family=Shippori+Antique+B1&display=swap');
* {
margin:0; padding:0;
box-sizing: border-box;
text-decoration: none;
outline: none; border:none;
font-family: "Shippori Antique B1" , sans-serif;
transition: .2s linear;
}
html{scroll-behavior:smooth !important}
a:visited{
visibility:hidden;
}
.phone-services {
display:none; /*Overwrites my media query*/
}
.avis-phone {
display:none; /*Overwrites my media query*/
}
HTML:
<section class="phone-services">
Section need to be shown only for mobile
</section>
<section class="avis-phone">
Section need to be shown only for mobile
</section>
Thanks for your help!
#media only screen
and (min-device-width: 320px)
and (max-device-width: 480px)
and (-webkit-min-device-pixel-ratio: 2)
and (orientation: portrait)
min-device-width and max-device-width are only for actual devices. If you try to simulate that on a desktop, it won't work for you. You should use min-width and max-width instead.
Secondly, -webkit-min-device-pixel-ratio: 2 is to check device resolution, but we have various devices which we cannot simply cover with a particular resolution. I'd suggest removing it.
Another problem is from here
.phone-services {
background:#02000A;
}
.avis-phone {
background:#02000A;
color:white;
}
You set display: none, but you don't set display: block (or any other visible display values)
Another point I'd like to note down that the style priority is TOP to BOTTOM when they have the same selectors. Your display style in media-query is above display: none like below, that will cause display problem too
#media {
.phone-services {
display: block; /*NOT WORKING*/
}
}
.phone-services {
display:none;
}
Full possible change can be
#import url('https://fonts.googleapis.com/css2?family=Shippori+Antique+B1&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
text-decoration: none;
outline: none;
border: none;
font-family: "Shippori Antique B1", sans-serif;
transition: 0.2s linear;
}
html {
scroll-behavior: smooth !important;
}
a:visited {
visibility: hidden;
}
.phone-services {
display: none;
}
.avis-phone {
display: none;
}
#media only screen and (min-width: 320px) and (max-width: 480px) and (orientation: portrait) {
.services {
display: none !important;
}
.avis {
display: none !important;
}
.phone-services {
background: #02000A;
display: block;
}
.avis-phone {
background: #02000A;
color: white;
display: block;
}
}
<section class="phone-services">
Code in here
</section>
<section class="avis-phone">
Code in here
</section>
You always have to define display if you are hiding / showing them depending on #media, both in #media part and non-#media part.
Try adding it to the rules:
#media screen and ...
{
.phone-services {
background:#02000A;
display: block; // can be block, inline-block, flex...
}
.avis-phone {
background:#02000A;
color:white;
display: block; // can be block, inline-block, flex...
}
}
Note that if you have #media part loaded before the normal one, you have to make sure to load #media part after, so it does not get overridden, or you can use !important with the rule (not recommended).

#media (max-width: 992px). display: block does not work

#media (max-width: 992px) {
.menu__list {
display: none;
}
.btn__menu {
display: block;
}
}
.btn__menu div {
height: 5px;
background-color: #000;
margin-bottom: 5px;
}
.btn__menu {
width: 40px;
display: none;
}
The code above writes me that I have an error in display: block;. I need the burger menu to pop up when the screen is less than 992px wide but I have nothing. Where did I go wrong?
Swap the order - your general rules will overwrite the media query rules the way it's now, since they follow * after* them. So just move the media queries to the end.

Add a character to the text on mobile version using CSS

I have a text block on my website:
Specialtyreagents
<h1>Specialtyreagents</h1>
Can I somehow add a - symbol to this block within CSS, so it should looks like this:
Specialty-reagents
I can use only CSS in my case!
Thank you for your help.
Use a pseudoelement with the word you want on mobile, and use font-size to hide or show it.
example
codepen
h1:after {
content: 'Specialty-reagents';
font-size: 0;
}
#media screen and (max-width: 500px) {
h1 {
font-size: 0;
}
h1:after {
font-size: 32px;
}
}
<h1>Specialtyreagents</h1>
With just CSS you can't include that - but if you need to use it in a specific case you can write again the text like this with a pseudo-element:
h1 {
font-size: 0;
}
h1:before {
content: "Specialty-reagents";
display: block;
font-size: 1.5rem;
}
<h2>Specialtyreagents</h2>
<h1>Specialtyreagents</h1>
On the long ride you can have two html elements, one for mobile and one for desktop:
<h1 class="desktop">Specialtyreagents</h1>
and
<h1 class="mobile" >Specialty-reagents</h1>
Then, you should have some css code for handling it:
.desktop {
display: block;
}
.mobile {
display: none;
}
#media (max-width: 720px) {
.desktop {
display: none;
}
.mobile {
display: block;
}
}
With media query:
#media only screen and (max-width: 500px) {
h1 > span:after {
content: "-";
}
}
<h1>Specialty<span></span>reagents</h1>
As others have said, you can use a pseudo element to achieve this.
The best way to go is to just add a new pseudo element when viewing on a small viewport (ie. a mobile phone). Here is some extra info on pseudo elements and how they can be used.
Example...
#media screen and (max-width: 768px) {
h1 {
font-size: 0px;
}
h1::after {
font-size: 30px;
display: block;
content: "Specialty-reagents";
}
}
<h1>Specialtyreagents</h1>
The only thing you may need to change if the max-width for the media query and the font size of the heading.
Try this code for the solution please:
#media only screen and (max-width: 500px) {
h1 > span:after {
content: "-";
}
}

removing link on mobile

I have a div which I want to hide when the screen is mobile size. Currently I have
html
<div id='top-btn'>
<a class="fade-in" href="...">Top</a>
</div>
css
#top-btn a {
visibility: visible;
opacity: 1;
position: fixed;
...
}
#media screen and (max-width: 768px) {
#top-btn a { display: none; }
}
The div is hidden but the button is still there, so there's an area that still links (clickable). I want it to be completely gone so they can't click on it
You need to remove the whole button, rather than just the link itself:
#media screen and (max-width: 768px) {
#top-btn { display: none; }
}
Hope this helps!
For me the code works..without any changes.
Maybe your browser don't support #Media,
Here you can check what browsers support this command
Link: http://caniuse.com/#feat=css-mediaqueries
but before checking the link,try this..
#media screen and (max-width: 768px) {
#top-btn , .fade-in { display: none; }
}

Display:none items still retaining space on the page

I have six items in a 100%-width carousel. When the screen is in portrait orientation, I'd like only 3 items to appear, so i dropped 3 using a media query (here is the jfiddle with the complete script):
#media all and (orientation:portrait) {
.wrapper4 .carousel1{
display: none;
}
.wrapper4 .carousel3{
display: none;
}
.wrapper4 .carousel6{
display: none;
}
}
The 3 items disappear, but the space they occupy is still retained. And when I try to get the remaining items to fill out the space that was previously occupied, it doesn't work.
.wrapper4 .carousel {
flex: 1; min-width: 33.3333%;
}
ie. above doesn't help spread out the remaining items. The 3 remaining items in the carousel retain their size and there's blank space occupied by the previous items when the screen is contracted to mimic portrait mode. How can I get the space to be freed up and the remaining items to fill up this space?
Tested on Chrome in Samsung Note, this works as intended:
All carousel items disappear in portrait mode:
#media all and ( orientation: portrait ) {
.carousel1 { display: none; }
.carousel2 { display: none; }
.carousel3 { display: none; }
.carousel4 { display: none; }
.carousel5 { display: none; }
.carousel6 { display: none; }
}
https://jsfiddle.net/0oug0t3r/11/ (can also be tested on desktop by re-sizing window)
All carousel items disappear in landscape mode:
#media all and ( orientation: landscape ) {
.carousel1 { display: none; }
.carousel2 { display: none; }
.carousel3 { display: none; }
.carousel4 { display: none; }
.carousel5 { display: none; }
.carousel6 { display: none; }
}
https://jsfiddle.net/0oug0t3r/12/
Removing the three carousel items as described in the question:
#media all and ( orientation: portrait ) {
.carousel1 { display: none; }
.carousel3 { display: none; }
.carousel6 { display: none; }
}
https://jsfiddle.net/0oug0t3r/13/
The only adjustments made were to match the alt tags in the img element with the corresponding carousel ID number, and I simplified the selector.