0Logo HTML5imagens de fundo sensível para sites

Eu estive recentemente atualizando meus sites e servidor web e adicionando um novo site (LoveCrete.org). Eu queria ter uma imagem de fundo em LoveCrete e percebeu que, claro, 1 tamanho não serve para todos os ecrãs. Here is a sum­mary of what my think­ing was and what I did to cater for the numer­ous screen sizes and orientations.

que orientação

I star­ted by mak­ing myself a little table of all the com­mon res­ol­u­tions used on PCs and tab­lets (in hori­zont­al ori­ent­a­tion). I decided to focus on this because most images are avail­able in land­scape so this is a sens­ible place to start.

I decided that I would place images cent­rally on the page and let the edges be lost off the edge of the screen in some instances. When a phone is used in por­trait ori­ent­a­tion it will load the image based on height (não largura) e vai perder um monte de ambos borda da imagem. Doing it any oth­er way would mean need­ing images cropped to a totally dif­fer­ent shape or hav­ing a land­scape image “tile” on a tall screen, o que não é desejável.

The ratios of the max­im­um size image to make in each group were all around 1.6 with a few a bit high­er. To keep things sim­pler I decided to use a ratio of 16:10 para todas as imagens e veio com tamanhos para fazer em conformidade.

This means in most cases the image will not be a per­fect fit to the screen size, but all screens will get some­thing very close and no screen will end up with some­thing too small. Significa, também, uma imagem de origem pode ser cortada para 16:10 once and then mul­tiple sizes eas­ily created

grupos de resolução de paisagem comum

AlturaLarguraImage to makeComentários
4:316:1016:9
2160288038402400×38404k UHD
1600
1536
1440
-
2048
1920
2560
-
-
-
-
2560
1600×2560
1200
1080
1050
1600
1440
1400
1920
-
1680
-
1920
-
1200×19201080p Full HD
960
900
1280
1200
1536
1440
-
1600
1000×1600
800
768
720
-
1024
960
1280
-
-
-
-
1280
800×1280720p
600
576
800
768
960
-
-
1024
640×1024
480640768480×768

As you can see I have grouped quite a few res­ol­u­tions and will then make 1 image that will cov­er all heights up to the largest height in that group. I will make sure the image is also wide enough for the max­im­um width such a height might be paired with on a nor­mal screen. I haven’t taken account of unusu­al con­fig­ur­a­tions like 21:9 mon­it­ors or mul­tiple screens — there has to be a lim­it somewhere!

Porque CSS uses the view­port or browser size and not the screen size land­scape screens need to use width rather than height — as on most sys­tems the top and bot­tom lose space to title bars etc whilst the width is usu­ally max­im­ised. No entanto, for por­trait dis­plays it is bet­ter to use the height prop­erty. Isto pode ser feito como CSS can identi­fy the screen orientation

O CSS usado, em seguida, tem a seguinte aparência

corpo{fundo de repetição: no-repeat; background-attachment: fixo; -fundo posição: centro; background-size: cobrir;}

@meios de comunicação (largura máxima: 768px) {corpo { imagem de fundo: url(bg480.jpg); }}
@meios de comunicação (min-width:769px) e (largura máxima: 1024px) {corpo { imagem de fundo: url(bg640.jpg); }}
@meios de comunicação (min-width:1025px) e (largura máxima: 1280px) {corpo { imagem de fundo: url(bg800.jpg); }}
@meios de comunicação (min-width:1281px) e (largura máxima: 1600px) {corpo { imagem de fundo: url(bg1000.jpg); }}
@meios de comunicação (min-width:1601px) e (largura máxima: 1920px) {corpo { imagem de fundo: url(bg1200.jpg); }}
@meios de comunicação (min-width:1921px) e (largura máxima: 2560px) {corpo { imagem de fundo: url(bg1600.jpg); }}
@meios de comunicação (min-width:2561px) {corpo { imagem de fundo: url(../images / bg2400.jpg); }}

@meios de comunicação (orientação: retrato){
@meios de comunicação (altura máxima: 480px) {corpo { imagem de fundo: url(bg480.jpg); }}
@meios de comunicação (min-altura:481px) e (altura máxima: 640px) {corpo { imagem de fundo: url(bg640.jpg); }}
@meios de comunicação (min-altura:641px) e (altura máxima: 800px) {corpo { imagem de fundo: url(bg800.jpg); }}
@meios de comunicação (min-altura:801px) e (altura máxima: 1000px) {corpo { imagem de fundo: url(bg1000.jpg); }}
@meios de comunicação (min-altura:1001px) e (altura máxima: 1200px) {corpo { imagem de fundo: url(bg1200.jpg); }}
@meios de comunicação (min-altura:1201px) e (altura máxima: 1600px) {corpo { imagem de fundo: url(bg1600.jpg); }}
@meios de comunicação (min-altura:1601px) {corpo { imagem de fundo: url(bg2400.jpg); }}
}

Deixe uma resposta