css3 第2回
css3の学習 border-imageとgradient
今回はcss3のborder-imageとgradientについて学ぶ。
border-image
borderに画像を指定できる。用意する画像は3の倍数のpxを持つ正方形で作る。cssで指定する際、何pxずつ取得するかを決める。例えば用意した画像が20x20の小さな画像が9つ、正方形に並んでいるなら、cssでは取得pxを20で、太さを20pxで指定する。すると、画像の左上20pxがborderの左上角となり、画像の右上20pxがborderの右上角となる。画像の中央をどうするかについては任意に指定できる。
- round 画像を連続配置し、隙間が出来る場合は各々を引き伸ばし隙間が出来ないようにする。
- repeat 画像を連続配置し、隙間が出来る場合はそのままの画像で埋める。
- stretch 画像が引き伸ばされて領域を埋める
上記のうち、roundが最も使いやすいと思われる。
<div id="bi"> <h5 id="bi1">ボーダーイメージ round<br>ボーダーイメージ round<br>ボーダーイメージ round</h5> <h5 id="bi2">ボーダーイメージ repeat<br>ボーダーイメージ repeat<br>ボーダーイメージ repeat</h5> <h5 id="bi3">ボーダーイメージ stretch<br>ボーダーイメージ stretch<br>ボーダーイメージ stretch</h5> </div>
#bi{ clear:both; float:left; margin:100px 50px; } h5{ clear:both; padding:10px; border:#00a solid 20px; width:210px; text-align:center; } #bi1{ border-image:url(border_img60x60.gif)20 20 round; } #bi2{ border-image:url(border_img60x60.gif)20 20 repeat; } #bi3{ border-image:url(border_img60x60.gif)20 20 stretch; }
gradient
boxにグラデーションを入れられる。始点、終点、色を指定する。「,」区切りにより複数の指定が可能で、複雑なグラデーションを設定できるが、記述が複雑になる。そこで、Webサービスにジェネレータがあるため、それを活用する。サンプル画像の3,4番目は、このジェネレータを活用した。
<div id="grad"> <h6 id="vt">背景グラデーション 縦</h6> <h6 id="sq">背景グラデーション 横</h6> <h6 id="rb">背景グラデーション 生成1</h6> <h6 id="rd">背景グラデーション 生成2</h6> </div>
#grad{ float:left; margin:100px 50px; padding:10px; background:#484; text-align:center; } #vt{ width:140px; height:140px; line-height:140px; background:-moz-linear-gradient(white,gray); background:-webkit-linear-gradient(left top, left bottom,white,gray);/*不要?*/ background:-webkit-gradient(linear,left top, left bottom,from(white),to(gray));/*win safari用*/ background:linear-gradient(white,gray);/*chromeはこれで表示可能の模様*/ } #sq{ width:140px; height:140px; line-height:140px; background:-moz-linear-gradient(left,red,white); background:-webkit-gradient(linear,left top,right top,from(red),to(white));/*win safari用*/ background:linear-gradient(to right,#ff0000,rgba(255,255,255,1));/*chromeはこれで表示可能の模様*/ } #rb{ height:45px; line-height:45px; padding:5px; width:140px; /*ジェネレータで生成*/ background: rgb(254,204,177); /* Old browsers */ background: -moz-linear-gradient(top, rgba(254,204,177,1) 0%, rgba(255,255,255,1) 50%, rgba(232,122,67,1) 55%, rgba(251,197,158,1) 100%); /* FF3.6+ */ background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(254,204,177,1)), color-stop(50%,rgba(255,255,255,1)), color-stop(55%,rgba(232,122,67,1)), color-stop(100%,rgba(251,197,158,1))); /* Chrome,Safari4+ */ background: -webkit-linear-gradient(top, rgba(254,204,177,1) 0%,rgba(255,255,255,1) 50%,rgba(232,122,67,1) 55%,rgba(251,197,158,1) 100%); /* Chrome10+,Safari5.1+ */ background: -o-linear-gradient(top, rgba(254,204,177,1) 0%,rgba(255,255,255,1) 50%,rgba(232,122,67,1) 55%,rgba(251,197,158,1) 100%); /* Opera 11.10+ */ background: -ms-linear-gradient(top, rgba(254,204,177,1) 0%,rgba(255,255,255,1) 50%,rgba(232,122,67,1) 55%,rgba(251,197,158,1) 100%); /* IE10+ */ background: linear-gradient(to bottom, rgba(254,204,177,1) 0%,rgba(255,255,255,1) 50%,rgba(232,122,67,1) 55%,rgba(251,197,158,1) 100%); /* W3C */ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#feccb1', endColorstr='#fbc59e',GradientType=0 ); /* IE6-9 */ } #rd{ height:45px; line-height:45px; padding:5px; width:140px; /*ジェネレータで生成 円形*/ background: rgb(240,249,255); /* Old browsers */ background: -moz-radial-gradient(center, ellipse cover, rgba(240,249,255,1) 0%, rgba(203,235,255,1) 47%, rgba(240,249,255,1) 67%, rgba(161,219,255,1) 100%); /* FF3.6+ */ background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%,rgba(240,249,255,1)), color-stop(47%,rgba(203,235,255,1)), color-stop(67%,rgba(240,249,255,1)), color-stop(100%,rgba(161,219,255,1))); /* Chrome,Safari4+ */ background: -webkit-radial-gradient(center, ellipse cover, rgba(240,249,255,1) 0%,rgba(203,235,255,1) 47%,rgba(240,249,255,1) 67%,rgba(161,219,255,1) 100%); /* Chrome10+,Safari5.1+ */ background: -o-radial-gradient(center, ellipse cover, rgba(240,249,255,1) 0%,rgba(203,235,255,1) 47%,rgba(240,249,255,1) 67%,rgba(161,219,255,1) 100%); /* Opera 12+ */ background: -ms-radial-gradient(center, ellipse cover, rgba(240,249,255,1) 0%,rgba(203,235,255,1) 47%,rgba(240,249,255,1) 67%,rgba(161,219,255,1) 100%); /* IE10+ */ background: radial-gradient(ellipse at center, rgba(240,249,255,1) 0%,rgba(203,235,255,1) 47%,rgba(240,249,255,1) 67%,rgba(161,219,255,1) 100%); /* W3C */ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f0f9ff', endColorstr='#a1dbff',GradientType=1 ); /* IE6-9 fallback on horizontal gradient */ }