How to use background:url(data:image/png;base64 with Retina? - css

I noticed that an image embedded into css with Data URI scheme is displayed on Retina screens with "regular" resolution. How to embed and image with resolution required by the Retina display?
I didn't try to embed a double resolution (#2) PNG into CSS, but I suspect that it would be displayed in double size, similar to regular images when size of the image is not defined.

Two images can have the same resolution but different dpi. This is the important part in retina displays, therefore, you need two versions (if you want to save some bandwith for those that doesn't have retina) of you image with different dpi's.
For further information on how to add a rule in css, and some dpi values take a look at: Retina Display Media Query.
EDIT
As pointed out by a comment from #carlos-r-balebona, bandwith will not be saved if embed the image into the CSS, as both versions will always be sent. It will only be saved if you use image URLs as only the proper one will be downloaded.
EDIT WITH EXAMPLE
OK, here's an example:
Suppose you have 2 images (data:image/png;base64,{192_dpi_encoded_image}) and (data:image/png;base64,{96_dpi_encoded_image}).
Now suppose you have a div you want to set the background image to:
<div id="a" style="width: 100px; height:100px;">
</div>
And in your css:
#media only screen
{
#a
{
background:url(data:image/png;base64,{96_dpi_encoded_image});
}
}
#media
only screen and (-webkit-min-device-pixel-ratio: 2),
only screen and ( min--moz-device-pixel-ratio: 2),
only screen and ( -o-min-device-pixel-ratio: 2/1),
only screen and ( min-device-pixel-ratio: 2),
only screen and ( min-resolution: 192dpi),
only screen and ( min-resolution: 2dppx){
#a
{
background:url(data:image/png;base64,{192_dpi_encoded_image});
}
}
The last #media with conditions will override the background image set before only if there's a retina screen present, thus, setting the large dpi image when needed.
Hope this helps.

You can just set the background size in pixels (the values should be the original dimensions divided by two, if your image has 2x resolution):
.element {
background-image: url(data:image/png;asdkfasdkf);
background-size: 25px 50px;
}

Related

Media Queries for Header Background Image

Trying to set the appropriate (meaning media query for the background header image in which will display the header image as it should be) of the website careertechpa.org. As you might see in the image attached, on the larger desktop screens, the bottom is getting cut off.
oversized-background
Here's the code for the header image:
.header-one{ height:200px;
background-image: url('careertechpa.org/wp-content/uploads/2020/02/…);
background-size:102%; }
I tried setting this media query but it didn't seem to work.
#media only screen and (min-width: 1440px) {
/* styles for MacBook Pro-sized screens and larger */
}
What am I missing?

How is the screen size measured for media queries?

I'm building a responsive website. There's a separate set of CSS to apply for smartphones. To do this, I use the following code
#media all and (min-width: 760px) {
.wrap {
/*css for large screens goes here*/
}
}
#media not all and (min-width: 760px) {
.wrap {
/*css for small screens goes here*/
}
}
My smartphone has a "Screen Resolution: 1080 x 1920." but it still displays the CSS for small screens. I'm surprised this is happening because 1080 > 760 so shouldn't the first block apply? Is screen resolution not actually measured in pixels? If not, then how does one find how many pixels in a screen?
I just found 760px from an example, is there a better way to decide when to switch from full size web page to compact for small screens?
There are two different concepts: the physical pixels of the screen, and the CSS pixels.
Initially, they were in most cases the same, but with so-called “retina” or “hidpi” screens, they are no longer the same. The idea is that a CSS pixel should retain about the same size, and be independent from the actual number of pixels on the screen: you don’t want to have text with CSS font-size 12px to have different sizes on screens with the same dimensions because their pixel density changes.
So the 1080 pixel width of your phone is probably mapped to 360 CSS pixels (x3 pixel ratio).
Instead of this
#media not all and (min-width: 760px) {
use this
#media all and (max-width: 759px) {
to address all viewports below 760 width.
ADDITION, answering the question in the comment "I'm asking what does px mean since it doesn't seem to be the physical pixel count on the screen":
It used to be the pixel count on the screen before retina and similar displays were introduced, which had a multiple amount of device pixels (AKA device pixel ratio, between 1.5 to 3 time as much).
Still, when those came up, the size reference for one "CSS pixel" remained the same (i.e. one CSS pixel would be 2 device pixels on a device with a device pixel ratio of 2), otherwise all websites would be displayed at half the size on these devices. So the pixel unit used in CSS refers to "CSS pixels" not to "device pixels" unless otherwise stated (which is only possible in media queries).
Go to development tools of your browser and select body tag and after that, you will find width and height by this:
due to some CSS issue this may not work properly will be great if you create a simple HTML with the following data to get width and height:
Change your media query with this.
#media only screen and (min-width: 760px) {
// for screens above 760px
.wrap {
color: blue;
}
}
#media only screen and (max-width: 760px) {
// for screens below 760px
.wrap {
color: lightblue;
}
}
If you want to change designing based on screen size then apply only screen with your media query.
Ask for more queries.
Thanks.

Wordpress responsive images - Selecting wrong image on retina screens

I've been working hard on trying to get responsive images working on this website I'm building and just when I thought it's going well, I look at it on the iPad retina screen and it's selecting the wrong image. Not only is it the wrong size but it's also displaying as landscape, not portrait. I have no idea why it's doing this as I've created custom image sizes for all the different screen sizes and also have created the XL version to be used on retina screens.
Here is the HTML of the image with the srcset and sizes.
<img class="scene_element scene_element--fadeinup" src="http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017-505x757.jpg"
srcset="http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017.jpg 3684w,
http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017-768x1151.jpg 768w,
http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017-1010x1514.jpg 1010w,
http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017-505x757.jpg 505w,
http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017-415x622.jpg 415w,
http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017-720x1080.jpg 720w,
http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017-360x540.jpg 360w,
http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017-293x440.jpg 293w,
http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017-1110x800.jpg 1110w,
http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017-455x306.jpg 455w,
http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017-355x238.jpg 355w,
http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017-640x600.jpg 640w,
http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017-320x300.jpg 320w,
http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017-2340x1258.jpg 2340w,
http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017-1170x629.jpg 1170w,
http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017-940x627.jpg 940w,
http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017-555x400.jpg 555w,
http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017-1910x1274.jpg 1910w,
http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017-955x637.jpg 955w,
http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017-1574x1250.jpg 1574w,
http://localhost:8888/wp-content/uploads/2017/11/Westfield-House-049-March-2017-787x625.jpg 787w"
sizes="(-webkit-min-device-pixel-ratio: 1.5) 1010px,
(max-width: 767px) 767px, (min-width: 768px) and (max-width: 991px) 415px,
(min-width: 992px) and (max-width: 1199px) 415px,
(min-width:1200px) 505px, 505px" itemprop="thumbnail" alt="Image description" >
As retina screens are double pixels i first tried adding this to the sizes to target iPads but nothing happened:
(min-width: 2048px) 1010px
The funny thing is i have an image with the dimensions of 1010px and have specified in the sizes that when it's double the pixels to use this image. Instead it's using this size: 940x627.jpg
Can anyone explain why this is happening please?
Right people, i managed to find out why the page wasn't loading the correct images. As i mentioned in my question, Wordpress by default will only load the images in the srcset with the same aspect ratio. If you want to include custom image sizes with a different ratio you need to add them via the wp_calculate_image_srcset function.
The problem with doing this, is that all custom sizes will be loaded into every images srcset and the browser will select the image based on the closest width and ratio.
Secondly, I realised that the image which Wordpress bases the aspect ratio on is the original image size rather than the custom size you load into the page. So where i had the portrait image with the custom size of add_image_size('portrait-case-study-lg', 505, 757, true); Wordpress was actually getting the aspect ratio from the original file which was 2000px x 1500px. As this had a different ratio, the image sizes i created for the portrait sizes were being ignored and instead the image with the closest aspect ratio was being chosen.
How i fixed this was to remove the function wp_calculate_image_srcsetwhich added the sizes into the srcset and instead re size the original images in Photoshop to have the same aspect ratio as the smaller images.
So for example, instead of having the custom image size of portrait-case-study-xl which was used to crop the original image file. I removed this and instead uploaded the original image at this size.
add_image_size('portrait-case-study-xl', 1010, 1514, true);
add_image_size('portrait-case-study-lg', 505, 757, true);
This means Wordpress now selects the 'portrait-case-study-lg' on different screen sizes as the aspect ratio is the same as the original.
It would good if Wordpress could allow you to remove the original image from the srcset as it now means i can't automatically the create the correct size when the client uploads an image to a page.

#media only screen resolution height issues

I am trying to align a bottom bar that is in homepage footer of my website. I want to adjust it so it will look good on common screen resolutions.
I have an issue with these resolutions 1280x960 and 1280x1024
#media only screen and (max-width:1280px) and (max-height:960px){
.tp-caption.black, .black {
margin-top: 496px!important;
}
And then use this code after that
#media only screen and (max-width:1280px) and (min-height:961px){
.tp-caption.black, .black {
margin-top: 464px!important;
}
The issue is it uses the same css, for both resolutions. 1280x960 media query
also applied in 1280x1024 resolution.
It displays the 2nd media query css code in browser, but ignores it and take
the above query.
I want to set the margin for both resolutions, for both heights 960 & 1024.
Can someone please explain me how to fix this?
Tried the same in fiddle, this works fine.
jsfiddle.net/5wx9qqyq/3/
One possible cause can be, missing closing braces for
#media only screen and (max-width:1280px) and (max-height:960px){ }
Please verify.

jQuery Mobile retina display image option?

Is there a simple way to display retina size images on the correct device and the standard on others? i've been search online a some people suggest using a jscript. Basically I want the 32x32 images to load on. Also what size should a retina icon be, 32, 36 width or higher? In comparison to a 16 standard width icon
#media only screen and (-webkit-min-device-pixel-ratio:1.5),only screen and (min--moz-device-pixel-ratio:1.5),only screen and (min-resolution:240dpi){
.shopping_icon
{background-image:url(../images/shop_icon.png);-moz-background-size:776px 18px;-o-background-size:776px 18px;-webkit-background-size:776px 18px;background-size:776px 18px;}
would something like this work?
#track .ui-icon{background:url(../simgs/track_icon.png) 50% 50% no-repeat;background-repeat:no-repeat;width:36px !important;height:36px !important;margin:-5px 0 0 -18px;}
#media screen and (-webkit-min-device-pixel-ratio: 1.5),
screen and (max--moz-device-pixel-ratio: 1.5) {
#track .ui-icon {
background: url(../simgs/track_icon.png);
background-size:36px 36px;
}
}
The device-pixel-ratio should be 2 for retina.
The icon image file should be 36x36 and CSS background-size 18x18 to squeeze it down.
Refer to the last section of this post: http://www.andymatthews.net/read/2011/02/13/Creating-and-using-custom-icons-in-jQuery-Mobile

Resources