【HTML/CSS/JavaScript】ウェブサイトの背景画像を全画面表示する方法まとめ +α
US Rolandのウェブサイトかっこいいなあと思います。色もきれい。
ウェブサイトの背景画像を全画面表示する
ウェブ標準の変化に伴い、960pxの固定幅で中央寄せするコンテンツレイアウトは少なくなってきました。かわりに、背景画像に高画質の写真素材やイメージ画像を用意し、ブラウザ幅いっぱいに拡大して魅せる、というのがオシャレな最新デザインとして定着しつつあります。
これは考えてみれば自然なことで、基本的にウェブサイトの閲覧はユーザーの任意のサイズ(ブラウザ幅、ウィンドウ幅)で見ることができるのが当然であり、制作側の意図で強制的に任意の幅までしか表示しないというのは、おかしなことです。
いまだに地方自治体や行政のウェブサイトでは、左寄せで固定幅のデザインレイアウトとなっており、ブラウザをPCの幅いっぱいに表示しても、右側には無意味な余白が表示される、なんてことはよくありますが、この記事ではウェブサイトの背景画像を全画面表示する方法について解説したいと思います。
一番単純な方法―background-size
最も基本的で誰でも理解できるのは、bodyタグに背景画像を当て込み、background-sizeでサイズを調整するという方法です。
body {
background: url(画像のパス);
background-size: 100%;
-webkit-background-size: 100%;
-moz-background-size: 100%;
-ms-background-size: 100%;
-o-background-size: 100%;
}
この場合、指定した画像がブラウザ幅いっぱいに表示されます。旧世代のInternet Explorer等では互換性がありませんが、まあ無視してよい機会がほとんどでしょう。
通常、「background」にはno-repeatやtop leftなどその他のプロパティの指定が必要ですが、画面いっぱいに表示されることが前提なので、repeat設定やポジション設定はあっても意味がありません。
気をつけなければいけないのが、画像が横長の場合、ブラウザ幅が小さめの場合は下部に画像gあリピートされてしまうということです。これはかっこ悪い。
画像の縦横比が比例していなくても違和感のない画像(アブストラクトなイメージだったり、空や海などの自然界を写した写真の場合など)を利用する場合は、以下のように強制的に縦のサイズも抑えてしまえばよいです。
body {
background: url(画像のパス);
background-size: 100% 100%;
-webkit-background-size: 100% 100%;
-moz-background-size: 100% 100%;
-ms-background-size: 100% 100%;
-o-background-size: 100% 100%;
}
最初の例の場合、縦のパーセントが省略されているので、内部では「auto」が当てはめられています。
問題は、縦横比を統一しないと気持ち悪くなってしまう場合、平気な顔をして写真を引き伸ばして表示するのはあまりにも素人っぽいので、メディアクエリによる実装がスマートです。
body {
background: url(画像のパス);
@media screen and (min-width: 画像の横幅サイズ ) {
background-size: 100%;
-webkit-background-size: 100%;
-moz-background-size: 100%;
-ms-background-size: 100%;
-o-background-size: 100%;
}
}
これにより、ブラウザの幅が画像サイズを超えた場合にのみ拡大表示されます。画面が小さい場合は背景画像が途切れて表示されるので、backgroundのなかにcenter等の任意のポジション属性を設定してください。
スマートフォンなど、縦長の画面を想定している場合
スマートフォンの場合、bodyのサイズをユーザーが任意に変更できるというケースは基本的には無いので、以下のように指定するだけで問題ありません。
body {
background: url(画像のパス);
background-size: auto 100%;
-webkit-background-size: auto 100%;
-moz-background-size: auto 100%;
-ms-background-size: auto 100%;
-o-background-size: auto 100%;
}
先にautoを指定してから100%と記述すればよいです。用意している画像が正方形や横長などの場合は、同じようにbackgroundのなかにcenter等の任意のポジション属性を設定してください。
さて、印刷できるようにしたい場合はどうするか。
意外とやっかいなのがこのパターンです。すなわち、印刷をかけた時に背景画像がなくなるのが困るというケース。どの程度事例があるのかはわからないが、基本的にはユーザーのインク代を考慮して派手で大きな画像の印刷は避けるべきだが、どうしてもというのなら背景ではなくimg画像として設置するしかない。
他にも、「右クリックで背景画像を保存できるようにしたい」という場合、同じようにbackgroundではなくimgタグで設置する必要があります。
<body> <div id="bg"><img src="画像のパス"></div> <div id="contents"> <!--ここにコンテンツを書く--> </div> </body>
まず、bodyタグ直下に#bgなどのdivを用意し、imgタグを書いてあげる。次にCSSです。
#bg {
position: absolute;
top: 0;
left: 0;
width: 100%;
}
#bg img {
width: 100%;
}
#contents {
position: relative;
z-index: 9999;
}
基本的な部分なこれでOK。ちなみに、#contentsのposition:relativeとz-index:9999の指定は、#bgの下に#contentsが隠れてしまわないようにする処置です。
ちなみに、#bgに指定しているtop:0とleft:0については、不要なのではという考えもあるようですが、Mozilla系のブラウザなど、指定しなければaboslute指定がしっかり反映されないブラウザもあるので、top:0とleft:0もしっかり記載しておきましょう。
imgタグで配置した背景画像を真ん中に寄せるには?
さて、画像を真ん中に寄せなければならない場合、けっこうやっかいです。なぜなら、text-align:centerやmargin:0 autoを指定したからといって、ブラウザ幅のサイズをもとに画像が自動的に真ん中に寄せられる(左側の不要な部分が消える)ということはありません。この場合、JavaScriptによる位置の上書きが必要になります。
まず、CSSの「img { width: 100%;}」の部分は削除しておき、imgタグ自体にはサイズを指定しません。
次に、jQueryによる位置の調整方法です。
var bg = $("#bg"),
img = bg.find("img"),
imgW = img.width();
$(window).load(function(){
bg.css({
left: ( $(window).width() - imgW ) / 2 ,
});
});
ブラウザ幅と画像の幅を比較し、不足分を2で割ってCSSのleftに当てはめればOKです。同様のやり方で、縦の位置(top)も揃えることができます。
ただし、気をつけなければならないのはこの指定では、DOMと画像が読み込まれた段階でしか動作しない、つまり、PC上でブラウザのサイズを拡げたり縮めたりした場合、ずれが生じるということです。
以下のようにすることで、ブラウザの大きさが変更された時でもリアルタイムに位置が調整されるように設定します。
var bg = $("#bg"),
img = bg.find("img"),
imgW = img.width();
$.fn.imgChange = function() {
bg.css({
left: ( $(window).width() - imgW ) / 2 ,
});
};
$(window).load(function(){
$("body").imgChange();
});
$(window).resize(function(){
$("body").imgChange();
});
imgChangeという関数(fn)の名前は任意の者に変更してかまいませんが、loadとresizeの両方でその関数を実行することで、DOMと画像を読み込んだタイミングと、ブラウザの大きさを変更した場合の両方で実行することができる、というわけです。
I was starting to doubt that it was possible to find some good content for once, I am getting tired of the continual drivel I find recently, respect.
|Good way of explaining, and fastidious piece of writing to take facts on the topic of my presentation subject, which i am going to present in school.
|What’s up, its understandable paragraph along with this YouTube video; I can?t think that one can not understand this straightforward article having with video demonstration.
|This post is actually a pleasant one it assists new web users, who are wishing for blogging.
Thanks very interesting blog!|
will
Extremely durable and well constructed material.I am currently using it for my first year in college This handbag gives you the option of a long strap to sling over shoulder or you can carry it by the hand strap. The canvas material is extremely durable and with the multi color pallet the wife loves it because it does not clash with any of her outfits and it is very designer like.
Extremely durable and well constructed material. This handbag gives you the option of a long strap to sling over shoulder or perhaps you can hold it by the hand strap. The canvas material is very durable along with the multi color pallet the wife loves it because it does not clash with any of her outfits and it is very designer like.
|Hi, always i used to check web site posts here in the early hours in the morning, for the reason that i love to find out more and more.
|This piece of writing is really a pleasant one it helps new net viewers, who are wishing for blogging.
|Hi, is it rite to just study from publications not to go to see world wide web for most up-to-date updates, what you say guys?
I am currently using it for my first year in college. I am nearing the end of my second week and I also am really glad that I found this! This might be well made and I also love the color material for the bag. It is sturdy and has now 4 pockets, not such as the main compartment for the bag. If you want a good bag that is big enough for school, I would personally recommend this to you!
Extremely durable and well constructed material.I am currently using it for my first year in college This handbag gives you the option of a long strap to sling over shoulder or you can hold it by the hand strap. The canvas material is extremely durable and with the multi color pallet the wife loves it because it does not clash with any of her outfits and it is very designer like.