I am developing an oscilloscope type of instrument where the screen is a mobile device. I am using canvas to draw the oscilloscope image and I have several buttons to perform functions like setup, settings, etc. For the buttons, I use .png icons because they have a transparent background. Everything works fine if the user uses a tablet but I want to be able to use other devices like smartphones or laptops.
My problem is that I cannot make the .png images responsive to screen size. Besides having several different images for different screen resolutions and sizes, is there a way to make .png icons responsive?
Here is my button/icon code (nothing fancy):
.thumbs {
position: fixed;
bottom: 20px;
right: 20px;
width: 64px;
}
.buttonPNG{
display:block;
cursor: pointer;
border: 1px;
margin:2px;
}
<div class="thumbs">
<div class="buttonPNG">
<img src="./home.png" alt="home" onclick="show_home()">
</div>
<!-- several such buttons -->
</div>
The only way I can resize PNG images is to use the picture tag. This is not the best solution in my case, because the server is a tiny ESP32 with limited resources and storing multiple images for each button takes a lot of flash memory. But since it is the only solution until now, I post it if somebody finds it helpful. Multiple source tags can be present and one img tag, as a fallback if none of the source tags matches.
<div class="buttonPNG">
<picture >
<source media="(min-width:650px)" srcset="./next_L.png" onclick="selectionUp()">
<img src="./next1_S.png" alt="next" onclick="selectionUp()">
</picture>
</div>
Setting the width and height of an image to a percentage
If Im understanding the question correctly, you want to scale the images to size depending on the screen size. This can be done by setting the width and height of the image to a percentage. For example:
width: 70%; height: 70%;
will set an image to 70% of the screen width and height. If you resize the window, the image will become responsive and adjust.
Using media queries
Another way to get responsive images would be media queries. What you can do is say if the screen width is a certain width or less, set an image to a specific width/height. Here is some code that will set the background of the body to lightblue if the user is on a mobile device with a small screen size:
#media only screen and (max-width: 600px) {
body {
background-color: lightblue;
}
}
Here is a link to a tutorial on media queries and how to use them: https://www.w3schools.com/css/css_rwd_mediaqueries.asp
EDIT
This is what I would do: I would set up some styling for your images meant for viewing them on a computer. Then I would use media queries to say if the viewer is on a small screen width (Phone) then change the styling and the same goes with a tablet. Some example code:
/* Extra small devices (phones, 600px and down) */
#media only screen and (max-width: 600px) {
/*Stying for phones*/
}
/* Small devices (portrait tablets and large phones, 600px and up) */
#media only screen and (min-width: 600px) {
/*Styling for large phones.*/
}
/* Medium devices (landscape tablets, 768px and up) */
#media only screen and (min-width: 768px) {
/*Styling for tablets and ipads*/
}
/* Large devices (laptops/desktops, 992px and up) */
#media only screen and (min-width: 992px) {
/*Stying for labtops*/
}
/* Extra large devices (large laptops and desktops, 1200px and up) */
#media only screen and (min-width: 1200px) {
.buttonPNG{
display:block;
width: 100px;
height: 100px;
cursor: pointer;
border: 1px;
margin:2px;
}
}
So there I set up a bunch of queries saying that if the user is on a certain device with a certain screen width, styling the buttons a specific way. If you want the buttons to have a width and height of 20px on mobile, just add the styling inside the mobile loop where the comment is like so:
/* Extra small devices (phones, 600px and down) */
#media only screen and (max-width: 600px) {
.buttonPNG{
display:block;
width: 20px;
height: 20px;
cursor: pointer;
border: 1px;
margin:2px;
}
}
Related
i'm new to html and css and i've been having a few issues dealing with media queries.
Basically, i have a website that only "actually works" when its been visualizated in a 1920x1080 resolution, so i created a few media queries on my css to support other resolutions as well. I'm having a little bit of trouble on making a media querie to support the 1280x1024px resolution. When the browser is not on fullscreen, on windowed mod, none of my changes written in the css are applied. But when i go fullscreen, everything works just fine.
Also, i cant set 1280 width for this cuz it'll mess up my other media querie which was created for the 1280x768 resolution
Can anybody help me with this please?
Appreciate it.
This is how it looks on windowed mode, with none of my changes written in the CSS applied
This is how it looks on fullscreen, now, its actually doing what it's supposed to do
#media screen and (height:1024px) {
.white_round_background{
margin-left: 320px;
height: 170vh;
width: 160vw;
background-color: rgb(197, 183, 183);
}
.menunav {
left: 38%;
top: 4%;
}
.system_selection {
margin: 420px 0 0 0px;
height: 95px;
}
#logo_sliding_menu {
margin-top: 710px;
}
}
Hum... Just a guess at this point, but pay attention to that: the sequential order of css code matters.
You can have a lot of media-queries definitions, but they have to be in a specific order (from the highest to lowest). EG:
#media only screen and (max-heigth: 600px) {}
and only then
#media only screen and (max-width: 500px){}
ALSO, instead of just a specific height, maybe try to use the max-height property (which will be applied to devices having a resolution small than that height. Because aiming just one height of 1024px will not work on windows being 1023px height or less or 1025 or more...
.yourClass {
/* CSS applied to all devices above 1024px height */
}
#media only screen and (max-width: 1024px){
.yourClass {
/* CSS applied to all devices smaller than 1024px height */
}
}
#media only screen and (max-width: 955px){
.yourClass {
/* CSS applied to all devices smaller than 955px height */
}
}
#media only screen and (max-width: 500px){
.yourClass {
/* CSS applied to all devices smaller than 500px height */
}
}
/* And so on */
You can also play with min-height and max-height in the same query :
#media screen and (min-height: 400px) and (max-height: 900px)
{
.yourClass {
/* CSS applied to all devices
no less than 400px height and no more than 900px height */
}
}
I am developing a blog website and have noticed that the blog posts are not proportionate to different screen sizes. I have mostly developed this on my desktop which has a larger screen compared to my laptop which I also tested it on. I am using the MDL css framework for the front end. Here are some examples of the issue:
The top image is the website displayed on the desktop and the bottom image is the website displayed on the laptop. As can be seen from the images, the blog posts on the laptop are considerably larger and not proportionate to the screen size at all. Does anyone have any idea on how to scale down the blog post divs so that it is more proportionate to the laptop screen and other screen sizes?
Thank you very much
You need to use Media Queries to give different styles to different sized devices.
Media queries are a popular technique for delivering a tailored style sheet to different devices.
/* Set the background color of body to tan */
body {
background-color: tan;
}
/* On screens that are 992px or less, set the background color to blue */
#media screen and (max-width: 992px) {
body {
background-color: blue;
}
}
/* On screens that are 600px or less, set the background color to olive */
#media screen and (max-width: 600px) {
body {
background-color: olive;
}
}
Hi am trying to apply the css when screen resolution is 1280*720 its not applied but when I manually enter width and height in google responsive check its working . Here is code of css
#media (min-height:720px) and (min-width: 1280px) {
.space
{
margin-top:24.5%;
}
}
You want it from 720px to 1280px then you have to use media query min-width:720px (i.e. from 720px) to max-width:1280px (i.e. less then 1280px) as below,
div {
width: 100px;
height: 100px;
background: #111;
}
#media screen and (min-width: 720px) and (max-width: 1280px) {
div {
background: red;
}
}
<div></div>
Scale your browser and see div background change.
On desktop systems, the size considered by media queries like min-height is the size of the content area of the browser, not the resolution of the screen. A system with a 1280x720 screen will not use rules in this media query unless the browser is in in full-screen mode, since some of the screen is being used for the browser toolbar and scrollbar, window decorations, a taskbar (on Windows) or menubar (on macOS), etc.
I got a problem on a website http://madamrimma.by/, when browser scale is less then 100%, the website is displaying incorrect: http://joxi.ru/qlrGUhjKTJBMAUGBReA. This website is not created by me and i don't understand how it happened.
This is because downscaling the browser actually increases the width of the page in pixels. While the browser may occupy say, 1024px, when the page is downscaled, the number of pixels as represented in the DOM is actually more than 1024px.
Additionally, there are media queries that control the appearance of the page. If you look at #wrappen, the following CSS exists:
#media (max-width: 1920px) and (min-width: 1025px)
#wrappen {
width: 1170px;
margin: 0 auto;
box-shadow: 0 0 20px #f25aeb;
background: #fff;
}
When you downscale your browser, the number of pixels as represented in the DOM is more than 1920px. Hence, the fixed-width layout imposed by #wrappen is ignored, and the layout breaks.
If you have an extremely high-resolution monitor, you can also resize your browser window beyond 1920 pixels and have the same effect.
The Fix
The fix for this is easy. Simply remove the offending max-width media query. Of course, this is not optimal for high resolution screens, as most space is wasted, but at least the layout does not break.
The main problem is having fixed widths to the div elements in the code. Change them to %'s so that it will be fixed. Every element should be center aligned.
I use this media quires:
/* Mobile styles go first, without media
queries. */
#media only screen and (min-width: 321px) {
/* Larger mobile styles (wider than 320
pixels) */
}
#media only screen and (min-width: 600px) {
/* Tablet styles (wider than 600 pixels)
*/
}
#media only screen and (min-width: 1024px) {
/* Large laptop styles (wider than 1024
pixels) */
}
#media only screen and (min-width: 1140px) {
/* Desktop styles (wider than 1140
pixels) */
}
for each resolutions and it works.
I am currently designing a landing page for a personal project and I thought about using a screen-covering image (100% width and 100% height of browser window) to round up the experience.
After the usual normalization, I've started with
html, body {
height: 100%;
overflow: hidden;
}
#hero {
width: 100%;
min-height: 100%;
height: auto;
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
and began with loads of media queries for both portrait and landscape orientation, you'll get the concept from this snippet:
...
#media only screen and (min-width: 2049px) and (max-width: 2560px) and (min-height: 1441px) and (max-height: 1600px) {
#hero {
background-image: url(../img/hero/landscape/cover-2560-1600.jpg); /* 8:5 */
}
}
#media only screen and (min-width: 2049px) and (max-width: 2560px) and (min-height: 1601px) and (max-height: 2048px) {
#hero {
background-image: url(../img/hero/landscape/cover-2560-2048.jpg); /* 5:4; */
}
}
#media only screen and (min-width: 2561px) and (min-height: 2049px) {
#hero {
background-image: url(../img/hero/cover-any-max.jpg); /* fallback for ultra high resolutions */
}
}
Apart from the insane amount of work that needs to be done to create a good quality image for the most used resolutions and write media queries for each single one of them, none of those queries is able to handle uncommon browser sizes, if, for example, a user drags his window to be very wide but also very short, he gets only a white background.
I would appreciate tips regarding this issue, as most coverage of responsive images I've found online was not very helpful, as it wasn't used as extensively as I desire.
One way would be to use a single large image, much larger than any resolution you would expect a visitor to your site to be using and reduce the quality right down. This does not look great at 100% size but when scaled down onto the smaller resolutions it will begin to look good. Doing this you can then use one image for portrait and one for landscape or just use one image with the following:
html, body {
height:100%;
margin:0;
}
body {
background-image:url('../img/hero/cover-any-max.jpg');
background-size:cover;
}
Portrait and landscape media queries:
#media only screen and (orientation : landscape)
#media only screen and (orientation : portrait)
For more information on the HiDPI low quality approach check out this article - html5rocks
You could use the aspect-ratio queries to capture when the viewport is getting very wide but also very short:
#media only screen (min-aspect-ratio: 8/5) and (max-aspect-ratio: 8/4)
{
#hero {
background-image: url(../img/hero/landscape/cover-2560-1600.jpg); /* 8:4 ~8:5 */
}
}
/* Your other aspect-ratio queries */
/* If browser is very wide + small */
#media only screen (min-aspect-ratio: 22/1) and (max-aspect-ratio: 22/2)
{
/* Assign other styles to handle it */
}
/* If browser is very narrow + tall */
#media only screen (min-aspect-ratio: 1/22) and (max-aspect-ratio: 2/22)
{
/* Assign other styles to handle it */
}
You obviously need to determine the the breakpoints yourself.
OK, if you just have one image this won't seem like a great solution, but if you have several, or you accept user submitted content which you don't control, then having a server side solution (RESS) which scales or crops your images on the fly to fit your visitor's screen perfectly, is really your best bet.
You'll go nuts writing out all the possible media queries and pre scaling your images. Also, as you mention, device aspect ratios are different or you may come across devices with an odd resolution...not to mention Retina displays etc.
If you really want a fool proof approach, go RESS. I'm aware of Pixtulate but I'm sure there are others.