Gambar latar belakang css responsif

195

Saya memiliki situs web (g-floors.eu) dan saya ingin membuat latar belakang (dalam css saya telah mendefinisikan gambar-bg untuk konten) juga responsif. Sayangnya saya benar-benar tidak punya ide tentang bagaimana melakukan ini kecuali untuk satu hal yang dapat saya pikirkan tetapi ini cukup solusinya. Membuat banyak gambar dan kemudian menggunakan ukuran layar css untuk mengubah gambar tetapi saya ingin tahu apakah ada cara yang lebih praktis untuk mencapai ini.

Pada dasarnya apa yang ingin saya capai adalah bahwa gambar (dengan tanda air 'G') secara otomatis mengubah ukuran tanpa menampilkan lebih sedikit gambar. Jika memungkinkan tentu saja

tautan: g-floors.eu

Kode yang saya miliki sejauh ini (bagian konten)

#content {
  background-image: url('../images/bg.png');
  background-repeat: no-repeat;
  position: relative;
  width: 85%;
  height: 610px;
  margin-left: auto;
  margin-right: auto;
}
Jochemke
sumber
7
Lihatlah tautan ini, mungkinkah itu yang Anda cari? css-tricks.com/perfect-full-page-background-image
Christofer Vilander
Saya merasa ini solusi yang diposting Christofer Vilander adalah solusi yang lebih baik daripada jawaban yang dipilih.
CU3ED
Hanya ingin tahu mengapa situs tersebut tidak benar-benar menerapkan fitur pengubahan ukuran yang Anda tanyakan ini, pada Februari 2015? Apakah ada masalah dengan itu?
LegendLength

Jawaban:

368

Jika Anda ingin skala gambar yang sama berdasarkan ukuran jendela browser:

background-image:url('../images/bg.png');
background-repeat:no-repeat;
background-size:contain;
background-position:center;

Jangan mengatur lebar, tinggi, atau margin.

EDIT: The previous line about not setting width, height or margin refers to OP's original question about scaling with the window size. In other use cases, you may want to set width/height/margins if necessary.

Andrei Volgin
sumber
3
Is it also possible to auto resize the width and let the height be fixed?
jochemke
3
Ya, Anda dapat mengatur tinggi gambar dalam piksel dan lebar dalam persen, tetapi gambar akan rusak. Itu tidak akan terlihat bagus.
Andrei Volgin
2
Anda dapat menggunakan gambar yang jauh lebih kecil jika Anda menggunakan format vektor: en.m.wikipedia.org/wiki/Scalable_Vector_Graphics
Andrei Volgin
9
Apa? Tidak mengatur lebar, tinggi ke wadah tidak akan menampilkan gambar sama sekali.
salju
3
(1) Pertanyaan awal adalah tentang penskalaan berdasarkan ukuran jendela browser. (2) Elemen suka divmendapatkan ketinggian mereka sendiri dari konten mereka sendiri, atau mengambil ruang yang tersedia dalam model flexbox. Mengatakan bahwa tanpa lebar / tinggi eksplisit, wadah tidak akan ditampilkan sama sekali tidak benar. Jelas, jika Anda ingin menampilkan div kosong dengan gambar latar belakang, Anda perlu mengatur ketinggian dan, mungkin, lebar, tetapi kemudian Anda hanya akan melihat sebagian gambar, kecuali jika rasio aspeknya persis sama dengan gambar itu sendiri.
Andrei Volgin
69

dengan kode ini gambar latar belakang Anda pergi ke tengah dan memperbaikinya ukuran apa pun ukuran div Anda berubah, baik untuk ukuran kecil, besar, normal, terbaik untuk semua, saya menggunakannya untuk proyek-proyek saya di mana ukuran latar belakang atau ukuran div saya dapat berubah

background-repeat:no-repeat;
-webkit-background-size:cover;
-moz-background-size:cover;
-o-background-size:cover;
background-size:cover;
background-position:center;
Pnsadeghy
sumber
Pada ukuran layar 800x1280px, gambar terpotong di sisi kanan oleh kotak. Semua ukuran lainnya tampak baik-baik saja.
Mugé
52

Coba ini :

background-image: url(_images/bg.png);
background-repeat: no-repeat;
background-position: center center;
background-attachment: fixed;
background-size: cover;
Ashok Dey
sumber
33

CSS:

background-size: 100%;

Itu harus melakukan trik! :)

Timothy Miller
sumber
27
This will totally distort the image based on the browser window aspect ratio. Never do it.
Andrei Volgin
8
Just for future reference, it actually won't look bad this way. background-size: 100% 100% would, but declaring only the x as 100% keeps it from looking terrible. Test case: codepen.io/howlermiller/pen/syduB
Timothy Miller
1
it won't look "distorted" but it will stretch the image beyond its intended proportions if it is smaller than the container...
Code Novitiate
1
So will the accepted answer. The OP didn't ask about that problem, just whether it was possible to make it resize nicely.
Timothy Miller
1
This solution fixed my problem. +1
JPeG
22

Here is sass mixin for responsive background image that I use. It works for any block element. Of course the same can work in plain CSS you will just have to calculate padding manually.

@mixin responsive-bg-image($image-width, $image-height) {
  background-size: 100%;
  height: 0;
  padding-bottom: percentage($image-height / $image-width);
  display: block;
}


.my-element {
  background: url("images/my-image.png") no-repeat;

  // substitute for your image dimensions
  @include responsive-bg-image(204, 81);
}

Example http://jsfiddle.net/XbEdW/1/

lanan
sumber
the code in the fiddle (uses a framework) is different than the code in the answer.
snow
14

This is an easy one =)

body {
    background-image: url(http://domains.com/photo.jpeg);
    background-position: center center;
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: cover;
}

Take a look at the jsFiddle demo

Omar
sumber
13

Here is the best way i got.

#content   {
    background-image:url('smiley.gif');
    background-repeat:no-repeat;
    background-size:cover;
}

Check on the w3schools

More Available options

background-size: auto|length|cover|contain|initial|inherit;
Piyush
sumber
9

I used

#content {
  width: 100%;
  height: 500px;
  background-size: cover;
  background-position: center top;
}

which worked really well.

Holly
sumber
That is the code I needed! Thanks :)) I'm trying to do parallax on 100% of the screen but 200px height and I need only the top part of the background but keeping the proportions of the whole image.
thinklinux
8
#container {
    background-image: url("../images/layout/bg.png");
    background-repeat: no-repeat;
    background-size: 100% 100%;
    height: 100vh;
    margin: 3px auto 0;
    position: relative;
}
amit bende
sumber
6

Responsive website by add padding into bottom image height/width x 100 = padding-bottom %:

http://www.outsidethebracket.com/responsive-web-design-fluid-background-images/


More complicated method:

http://voormedia.com/blog/2012/11/responsive-background-images-with-fixed-or-fluid-aspect-ratios


Try to resize background eq Firefox Ctrl + M to see magic nice script i think best one:

http://www.minimit.com/demos/fullscreen-backgrounds-with-centered-content

user956584
sumber
4

You can use this. I have tested and its working 100% correct:

background-image:url('../images/bg.png'); 
background-repeat:no-repeat;
background-size:100%;
background-position:center;

You can test your website with responsiveness at this Screen Size Simulator: http://www.infobyip.com/testwebsiteresolution.php

Clear Your cache each time you make changes and i would prefer to use Firefox to test it.

If you want to use an Image form other site/URL and not like: background-image:url('../images/bg.png'); //This structure is to use the image from your own hosted server. Then use like this: background-image: url(http://173.254.28.15/~brettedm/wp-content/uploads/Brett-Edmonds-Photography-14.jpg) ;

Enjoy :)

JackSparrow
sumber
4

If you want the entire image to show irrespective of the aspect ratio, then try this:

background-image:url('../images/bg.png');
background-repeat:no-repeat;
background-size:100% 100%;
background-position:center;

This will show the entire image no matter what the screen size.

Mubashar Abbas
sumber
3
    <style> 
         * {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

#res_img {
  background: url("https://s15.postimg.org/ve2qzi01n/image_slider_1.jpg");
  width: 100%;
  height: 500px;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  background-position: center;
  border: 1px solid red;
}

@media screen and (min-width:300px) and (max-width:500px) {
  #res_img {
    width: 100%;
    height: 200px;
  }
}

    </style>

<div id="res_img">
</div>
Indranil
sumber
3

Just two lines of code, it works.

#content {
  background-image: url('../images/bg.png');
  background-size: cover;
}
Rajkumar Bansal
sumber
2

Adaptive for square ratio with jQuery

var Height = $(window).height();
var Width = $(window).width();
var HW = Width/Height;

if(HW<1){
      $(".background").css("background-size","auto 100%");
    }
    else if(HW>1){
      $(".background").css("background-size","100% auto");
    }
OVNI
sumber
2
background:url("img/content-bg.jpg") no-repeat;
background-position:center; 
background-size:cover;

or

background-size:100%;
bsngr
sumber
0

I think, the best way to do it is this:

body {
    font-family: Arial,Verdana,sans-serif;
    background:url("/images/image.jpg") no-repeat fixed bottom right transparent;
}

In this way there's no need to do nothing more and it's quite simple.

At least, it works for me.

I hope it helps.

user3487225
sumber
0

Try using background-size but using TWO ARGUMENTS One for the width and the other one for the height

background-image:url('../images/bg.png'); 
background-repeat:no-repeat;
background-size: 100% 100%; // Here the first argument will be the width 
// and the second will be the height.
background-position:center;
Runsis
sumber
0
background: url(/static/media/group3x.6bb50026.jpg);
background-size: contain;
background-repeat: no-repeat;
background-position: top;

the position property can be used to align top bottom and center as per your need and background-size can be used for center crop(cover) or full image(contain or 100%)

44kksharma
sumber