How to hide background image on mobile devices (SP Page Builder) - css

I am currently working on a website using Joomla and I am using SP Page Builder extension to construct my content pages. I have a background image on one of these content pages and it just messes up the mobile display of the webpage so i want hide the background image on mobile devices. I have used this css code which has worked for me before (on pages without SP Page Builder) but it seems not to be working, here is the code:
#media all and (max-width: 768px) {
section.sppb-section.bg {
background-color: #ffffff;
background-image: none;
}
}
here is the auto generated HTML code of the element with a background image:
This is a screenshot of page the layout of a row with two columns, somehow this row and columns are constructed using <div></div> tags
Is there a solution to this or a work around. Thank You.

In your html you have inline style which force the background to be there so you will need to use !important to force background-image to be none
#media all and (max-width: 768px) {
section.sppb-section.bg {
background-color: #ffffff !important;
background-image: none !important;
}
}

Just have the background image display on screens > 768px, not the other way around.
.bg {
background:#fff;
}
#media screen and (min-width:768px){
.bg {
background-image:url(img.jpg);
}
}

Inline CSS is always take preference over external css that's why your css code is not working. Try to keep that inline css style code in external css file.

It looks like the bg image is being added in the style attribute which will override your css class. You'll need to decorate your css with important.
#media all and (max-width: 768px) {
section.bg {
background-color: #ffffff !important;
background-image: none !important;
}
}

Related

map / area tag on img loaded through css

I'm working on a homepage, where, depending on the screen size, image A or B is loaded. Image A is using the
usemap
tag to display captions upon mouseover.
For now, both images are loaded into cache and I'd like to only load one, which I've achieved through the use of css's
#media screen and (min-width: 601px) {
.mobile {
display: none !important;
}
.desktop {
content: url("../assets/a.jpg");
}
}
. Problem being, the usemap tag won't work anymore. How can this be resolved?

Responsive CSS on Display-Listings-Shortcode

I installed a plug-in called Display-listings-shortcode, and added the columns-extension to allow for columns the blogs halfway down the homepage at RitaNaomi.com will be horizontally displayed on a web browser. It looked whacky at first with titles being scrunched beside and underneath the image, but eventually i figured out how to edit the .display-posts-listing class to change the display
.display-posts-listing .listing-item {padding-bottom:30;}
.listing-item
{
float:left;
width:22%;
margin: 40px
}
But when I look at it on a mobile device, they're all scrunched together as if it was still being displayed on a laptop. I want to have it listed vertically and not horizontally, because thats the way it would fit best.
I tried (and it didn't work) to use #media to change it through the css, but it didn't work.
#media handheld {
.display-posts-listing .listing-item {
clear: both;
display: block;
}
.display-posts-listing img {
float: left;
margin: 0 10px 10px 0;
}
}
You shouldn't be using #media handheld {} since it's been deprecated according to MDN.
You're better off targeting pixel-width values. You may need a couple queries, and some of the oldschool standards were 1023px, 767px. Feel free to replace the 900px below with whatever works for you.
#media only screen and ( max-width: 900px ){
.display-posts-listing .listing-item {
/* CSS Here */
}
}
Removed the custom CSS that was already added from the original theme. It was interfering with the Columns display.
Not using #media handheld {} because it was deprecated (thanks to xhynk for the response), and instead used the command (max-width: 768) , the point at which the title and image css look funky.
To make the title display on its own line on a bigger screen, i added this to my CSS:
.display-posts-listing .listing-item .title { display: block; }
And now i'm using the above media query to figure out how to style it on smaller devices.
Complete CSS: https://gist.github.com/billerickson/17149d6e77b139c868640a0ed3c73b3a

Why do I have to put media queries at the bottom of the stylesheet?

I am new to learning responsive design. What I have noticed on my journey is that when I put media queries at the bottom of the stylesheet, everything works flawlessly in regards to breakpoints. If I put the media queries at the top of the stylesheet, nothing works, and only recently I found out that I need to add !important and max-DEVICE-width ( as opposed to max-width) to the css that is being changed.
Why is this? Why do the media queries work on both desktop and mobile when put at the bottom of the stylesheet.
Why is it that when I put media queries on the top of the stylesheet I need to add !important and also max-DEVICE-width in order for the breakpoints to work on desktop and mobile?
Because css is read from top to bottom. The rule that is set last, is the one that will be executed.
Translating, it is like this:
#media (max-width: 600px) { //If my screen fits this size
.text {
color: red; //Paint it red
}
}
.text {
color: yellow; //Now, forget about everything and paint it yellow!
}
When you add !important is like saying:
#media (max-width: 600px) { //If my screen fits this size
.text {
color: red !important; //Paint it red, and don't change it ever!!!
}
}
.text {
color: yellow; //Ok, I'm not going to paint it yellow....
}
CSS is read from top to bottom.
Everything that is below some other css will overwrite what's on top of it.
It is possible however to use !important at the end of a CSS parameter to make it overwrite everything else
body{
background-color: black !important;
}
body{
background-color: pink;
}
The background-color will be black.
If you remove the !important, it will be pink.
Media queries cascade with the rest of the stylesheet. You can intersperse media queries within your stylesheet, and so you can also cascade styles as needed.
For example:
.my-class {
color: red;
}
.my-class--modifier {
color: blue;
}
#media screen and (min-width: 760px) {
.my-class--modifier {
color: green;
}
}
.some-other-class {
width: 200px;
}
#media screen and (min-width: 760px) {
.some-other-class {
width: 700px;
background-color: gray;
}
.some-other-class .my-class {
border: 2px solid red;
border-radius: 4pt;
}
}
This works precisely due to CSS's cascading nature. You can organize media queries as required based on sections, individual selectors and more.
Basically you are using media queries when you want to apply CSS styles depending on a device's general type (such as print vs. screen), specific characteristics (such as the width of the browser viewport, or environment (such as ambient light conditions).
When you started designing, you generally started doing it for one device of known specifications. So you design it according to you current device and then apply it for other screen sizes.
Hence the order goes like this: Make complete design --> Add the media query to fit for desired screen sizes at the bottom.
It is preferrable to write the query at the bottom became of precedence. That will save you from stress of using important! everytime.

Do background images load if they are replaced with media queries?

I'm creating a responsive design so my website is viewed well on both Desktop and Mobile devices. On my website I have a pretty large background image on one of my divs.
Now what I have been wondering is: Does the background image on my div load if it is replaced by a color in a media query?
Example:
div {
background: url(img/large-image.jpg);
}
#media screen and (min-width: 240px) and (max-width:830px) {
div {
background: #000;
}
}
No it wouldn't as you're completely overriding the background property*.
For reference, however, if you wish to keep your image and add in a colour, rather than using the background property, use the individual background-image and background-color properties:
div {
background-image: url(img/large-image.jpg);
}
#media screen and (min-width: 240px) and (max-width:830px) {
div {
background-color: #000;
}
}
* Note that this is browser-specific; to save time the majority of browsers will only load resources when they're required. See also: Are unused CSS images downloaded?
Your images are still loaded by browser, as long as they are in your CSS. You can check it by using Chrome Debugger Tool > Network.
There is a interesting article about image load & performance in responsive design:
http://mobile.smashingmagazine.com/2013/09/16/responsive-images-performance-problem-case-study/

Adding .hidden-phone in an existing css file to hide background image

I'm sure there is an easy answer to this one...
Using Twitter Bootstrap, I have this in my style.css.erb file:
.login_page body {height:100%;
max-width:inherit;
margin:0 20px;
background-image: url(<%= image_path 'oap_dotlogo.png' %>);
background-repeat: no-repeat;
background-position: 5%, 80%;
background-color: #E3F5FD;
}
That is great, except on a phone where the background logo is a pain in the ass. I'd like to drop that logo on a phone.
I see that bootstrap-responsive.css already defines the .hidden-phone:
#media (max-width: 767px) {
.hidden-desktop {
display: inherit !important;
}
.visible-desktop {
display: none !important;
}
.visible-phone {
display: inherit !important;
}
.hidden-phone {
display: none !important;
}
}
So, how do I modify the style.css.erb so that the background-image tag has the .hidden-phone class?
I've tried a few permutations of adding .hidden-phone in the style.css.erb but nothing seems to work - I must have the syntax wrong.
Thanks!
Dave
Sounds like you're confusing CSS and HTML here. When you say:
[H]ow do I modify the style.css.erb so that the background-image tag has the .hidden-phone class?
... you can't add a class to a CSS declaration. You don't actually want to hide your element (the body in this case), rather at small screen sizes you want to modify the background property. So, within the media query that you already have, add this:
#media (max-width: 767px) {
.login_page body { background-image: none; }
...
all your other small-screen styles
}
Then, the default will be to show your background-image and you are removing it for small screens. If the image is large, you might consider a mobile-first approach. i.e. do the opposite, as #po228 mentioned - only add the background-image for larger viewport widths.
Looks like you were very close. To define styles that won't be displayed on a phone you need to use min-width, not max-width. Try the following:
#media only screen and (min-width: 768px) {
.login_page body {
background-image: url(<%= image_path 'oap_dotlogo.png' %>);
background-repeat: no-repeat;
background-position: 5%, 80%;
}
}

Resources