I have a less file containing a simple css class that needs to have different styles applied for various screen sizes. The following code only works on desktop (the first #desktop media query). Nothing happens for mobile. I have tried many variations of this syntax with no luck and haven't found anything about this in the docs. You can also see the live demo here (notice how if you stretch the screen wider than 1024px the div turns orange, but it does not turn red or green when smaller than 1024px as it should). Thanks.
html
<div class="derp">
Hello
</div>
less
#desktop-min-width: 1024px;
#phone-max-width: 768px;
#desktop: ~"only screen and (min-width: #{desktop-min-width})";
#tablet: ~"only screen and (min-width: #{phone-max-width}) and (max-width: #{desktop-min-width}";
#phone: ~"only screen and (max-width: #{phone-max-width})";
#appHeight: 749px;
#appWidth: 421px;
.derp {
#media #desktop {
background-color: orange;
}
#media #tablet {
background-color: red;
}
#media #phone {
background-color: green;
}
}
There is a problem with
#tablet: ~"only screen and (min-width: #{phone-max-width}) and (max-width: #{desktop-min-width}";
Remove it for a moment, and you will see green background is displayed for phone screen.
You need to add ")" for a #tablet at the end
Related
A screenshot is below of the webpage that I need to optimize the screen sizes for. The horizontal bar above the navigation bar with the logos, white background and the clubs on the left is what I'm wanting to optimize the screen size CSS code for. I've only started working with html and css a few month ago.
Webpage
From 1230px the margin-left increases by 0.8px. Is there a way to increase the webpages performance with the 35+ "#media screen and (min-width: #px)" that will be needed to cater to screen min-width of 1930px or more? Or will this many not affect the pages performance at all?
/* Team Logos spacing for different screen sizes */
#media screen and (min-width: 1200px)
{
.clubs .llul .logoli
{
margin-left: 0px;
}
}
#media screen and (min-width: 1210px)
{
.clubs .llul .logoli
{
margin-left: 0.45px;
}
}
#media screen and (min-width: 1220px)
{
.clubs .llul .logoli
{
margin-left: 0.9px;
}
}
#media screen and (min-width: 1230px)
{
.clubs .llul .logoli
{
margin-left: 1.3px;
}
}
#media screen and (min-width: 1250px)
{
.clubs .llul .logoli
{
margin-left: 2.1px;
}
}
.
.
./* Rest of code between 1250px and 1630px */
.
.
#media screen and (min-width: 1630px)
{
.clubs .llul .logoli
{
margin-left: 17.3px;
}
}
#media screen and (min-width: 1650px)
{
.clubs .llul .logoli
{
margin-left: 18.1px;
}
}
I've also added a snippet of the code behind the element. The rest of the CSS for it is not shown.
<div class="clubs">
<p style="display: inline;">Clubs</p>
<ul class="llul">
<li class="logoli"> <img class="logoT" style="" src="media/teams/smlafel.JPG" alt="fl">
<ul class="llul">
<li class="logoli" style="padding-left: 10px;"> <b>Africa Elite</b> </li>
<li class="logoli"> <img class="logoT" style="" src="media/teams/smlafel.JPG" alt="fl"> <b>Africa Elite</b> [Men's Premier Division] </li>
</ul>
</li>
</ul>
</div>
Please don't do stuff like that. You're just copying & pasting your code for .clubs .llul .logoli and adjusting the margin-left rule every 10px. There's definitely something wrong with that from a programming point of view. This does not affect the performance (well, maybe when resizing your browser window vary fast), but your CSS file will be much messy and heavier than it has to be, so when the browser will download this file at the beginning, then loading the page, it can take a while.
The solution here is to go with this:
#media screen and (max-width: 1200px) {
.clubs .llul .logoli {
margin-left: 0px;
}
}
#media screen and (min-width: 1200px) {
.clubs .llul .logoli {
margin-left: calc((100vw - 1200px) / NUMBER_OF_ELEMENTS);
}
}
Where 100vw is always the window width (from CSS's point of view). So when it's bigger than 1200px, you get the size you want to share between all your logos and divide it by some constant than can be the number of those logos, more or less than that. You can adjust it as you like.
Even more clever way to go with this is to take a look at display: flex and justify-content rules. I'll leave this link here for more infos:
https://tailwindcss.com/docs/flexbox-justify-content/
Hope this was useful, good luck!
Why don't you use svg for the logos if you have them in that format, they will scale up and down to perfection and read like code, therefore no optimisation required
I'm trying to write some css that will only work in firefox, that is within a media query so it only works after 767px. Below is what I've currently written but it doesn't work.
#media only (min-width: 767px) {
#-moz-document url-prefix() {
.SearchBlock input {
width:88% !important;
}
}
}
It works just fine without the "#media only" section, but I only want it to work after 767px. Is this possible?
Edit: Changed resolution to width.
Don't use min-resolution. Use
min-width: if you need to apply CSS to the devices which are more than 767px, and use
max-width: If you need to apply CSS to the devices having size less than 767px - for mobiles
Example:
#media screen and (min-width: 767px) {
#-moz-document url-prefix() {
.SearchBlock input {
width:88% !important;
}
}
}
Hope this helps!
You are not getting the result because you are using min-resolution correctly.
You can do one of the 2 :
Either change min-resolution to min-width
Enter resolution value in min-resolution for example min-resolution(192dpi)
Px is not the unit of resolution.
Change min-resolution to min-width.
Resolution is for the pixel density of the device. width refers to the actual width which is what you want.
The url-prefix portion needs to have the url of the document the style rules refer to, such as
url-prefix("https://example.com/")
The #document CSS at-rule restricts the style rules contained within
it based on the URL of the document.
https://developer.mozilla.org/en-US/docs/Web/CSS/#document
Try this. Should work! :)
.SearchBlock input {
display: block;
width: 30%;
}
#-moz-document url-prefix() {
#media screen and (min-width: 767px) {
.SearchBlock input {
width:88% !important;
}
}
}
<div class="SearchBlock">
<input type="text" placeholder="text"/>
</div>
I've got a problem which I have no idea how to get around of.
I use a shop script where I can only edit CSS file.
I have a div with background-image and in there I have a normal image:
<style type="text/css">
.someclassforcss img{
some:attributes;
}
.someclassforcss {
background-image:url(/link.png);
}
</style>
<div class="someclassforcss">
<img src="/link2.png">
</div>
Everything's good, but I want to use media queries (or any other method) to hide background-image of div for mobile devices, but I have no idea how to make it, because media queries doesn't work for specific attributes, only for whole elements, so if I would've hided the div, my img is also hided which i don't want.
You DO can change attribute regarding media dimensions.
Your CSS:
.someclassforcss {
background-image:url(/link.png);
}
#media screen and (max-width: 762px){
.someclassforcss {
background-image: none;
}
}
Try this:
#media only screen and (max-width: 767px){
.someclassforcss {
background: none;
}
}
You can hide the div with a media query. eg:
#media (max-width: 600px) {
.someclassforcss {
display: none;
}
}
or if you just want to remove the background image:
#media (max-width: 600px) {
.someclassforcss {
background-url: none;
}
}
Using the #media tag, how do I display a new image, for example I have a logo I want to use for my main site and a mobile logo for my mobile site, how to I display the smaller logo only on the mobile site using #media?
I've tied using "display:url('xxxx') but that hasn't seemed to work.
You'll have to put two logos on your HTML, like this:
<img src="" class="desktop-logo">
<img src="" class="mobile-logo">
Then, you'll have to hide the mobile-logo by default:
.mobile-logo {
display: none;
}
Then, on your media query, you'll have to hide the desktop-logo and show the mobile-logo:
#media screen and (max-width: 480px) {
.desktop-logo {
display: none;
}
.mobile-logo {
display: block;
}
}
What you'll want to do is create an element and set a background on it. You'll use #media control what the background image is based on the size of the window.
#media only screen and (min-width: 768px) {
.logo {
background: url('image.jpg');
}
}
#media only screen and (max-width: 767px) {
.logo {
background: url('imagesmall.jpg');
}
}
And this is what your element would look like.
<div class="logo"></div>
Create a tag with a class then use mediascreen to apply new styles to said class
create a div tag
css/
#media screen .... etc {
.image {background-image: url('wwww....');
}
So, when the exact pixels are reached at the screen it will apply a backround image to that image class I created above.
I have a PhoneGap app that displays an unordered list utilizing jQuery mobile's layout. When you view the app on a smaller screen, the text overlaps and you can't read it anymore. I can't figure out how to have the line "break" so that it appears as two lines when the screen size is reduced, and one line when it is not reduced.
Full screen
Reduced screen
On the second line the text disappears, and is "hidden" by the numerical values. The code of that chunk looks like this:
HTML:
<div data-role="content">
<ul data-role="listview" data-divider-theme="a">
<li data-role="list-divider"></li>
<li><b>Revenues</b></li>
<li>Gross Sales<span class="right">$543,600</span></li>
<li>Less Sales Returned and Allowances<span class="right">$9,200</span></li>
<li>Less Sales Discounts<span class="right">$5,100</span></li>
(continues on)
CSS:
span.right {
float: right;
}
Add a class to <br>.
Example:
Say u want to break <br> only at a custom screen size 358px n below.
HTML:
<br class="hidden-ss">
CSS:
#media (min-width: 359px) {
.hidden-ss {
display: none !important;
}
}
In reality u may have multiple #media screen breaking points already defined, e.g when using default Bootstrap:
HTML:
<br class="visible-ss hidden-xs hidden-sm hidden-md hidden-lg">
Define CSS:
#media (max-width: 358px) {
.visible-ss {
display: block !important;
}
Already pre-defined in Bootstrap:
#media (max-width: 767px) {
.hidden-xs {
display: none !important;
}
}
#media (min-width: 768px) and (max-width: 991px) {
.hidden-sm {
display: none !important;
}
}
#media (min-width: 992px) and (max-width: 1199px) {
.hidden-md {
display: none !important;
}
}
#media (min-width: 1200px) {
.hidden-lg {
display: none !important;
}
}
This is a job for a table:
<!doctype html>
<title>Table demo</title>
<style>
td:nth-child(2) { text-align: right }
</style>
<table><caption>Revenues</caption>
<tr><td>Gross Sales <td>$543,600
<tr><td>Less Sales Returned and Allowances <td>$9,200
<tr><td>Less Sales Discounts <td>$5,100
</table>
If you really want the figures placed on the very right, you can add the CSS rule table { width: 100% }, but the presentation is much more readable without it.
so you can use
white-space: normal !important;
I was using white-space: normal; without the !important tag it does not become "wrap text" with JQuery mobile.
For smaller screens please use the CSS below so the span automatically comes in with the next line:
#media only screen and (max-device-width: 480px) {
span.right {
display:block;
text-align:right;
clear:both;
}
}
If you use Bootstrap, you can put a breaking point depending on the screen size.
For example, to break line only on screens smaller than medium, you could put that in your html:
<br class="d-md-none">
More info on https://getbootstrap.com/docs/5.2/utilities/display/#hiding-elements