iPhoneサイト向けCSSサンプル(CSS3)
使いまわしがききそうなものをまとめた個人的なサンプルです。随時更新致します。
HTML側のviewportの設定
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
初期設定
* {word-break: break-all;-webkit-touch-callout:none;-webkit-tap-highlight-color:rgba(255,255,0,0.4);-webkit-text-size-adjust:none;}
word-break: break-all;
URLなどの文字列の強制改行。ただし句読点なども先頭にくるため要検討。
-webkit-touch-callout:none;
長押しした際のとび先URL非表示。ただし画像リンクをタップした際のハイライトがおかしくなる。aタグ要素にdisplay: block ;を指定すると直る。
-webkit-tap-highlight-color:rgba(255,255,0,0.4);
リンクをタッチした際のカラー。
-webkit-text-size-adjust:none;
横画面にした際フォントサイズの自動調整を無効。
※フォントサイズを%指定している場合。
body {color: #ffffff;font-size: 15px;line-height: 1.3;font-family: ArialMT, "Hiragino Kaku Gothic ProN", "ヒラギノ角ゴ ProN W3"; /* iPhoneバンドル */-webkit-text-size-adjust: none; /* 文字の拡大縮小を防ぐ */}/* ---------------
EXAMPLES FONT-SIZE------------------10px = 66.7%11px = 73.3%12px = 80%13px = 86.7%14px = 93.3%15px = 100%16px = 106.7%17px = 113.3%18px = 120%19px = 126.7%20px = 133.3%21px = 140%22px = 146.7%23px = 153.3%24px = 160%25px = 166.7%26px = 173.3%--------------- */
BASE
/* clearfix */*:after { display:block; clear:both; }/* はみ出し対策 */
/* body { width:100%; overflow:hidden; } */※Androidでスクロールできなくなる機種があるので指定しないことにする/* HTMLに対応していないOS対策 */
article, aside, figure, footer, header,hgroup, nav, section { display:block; }/* ↓以下通常CSSを記載していく */
a, a:link, a:visited, a:hover, a:active {
color: #103092;text-decoration: underline;}em{ color: #db0000;}
p { margin: 0 0 10px 0 ; padding: 0 ; }
strong {font-weight: bold;}
b {font-weight: normal ;}
input[type="checkbox"],
input[type="radio"],select { display:inline; vertical-align: baseline ;margin-right: 5px ;}input::-webkit-input-placeholder,
textarea::-webkit-input-placeholder {color: #999 ;}textarea {
width: 100% ;height: 100px ;margin-bottom: 10px ;}input[type="text"],textarea,select {
color: #5a5c63;font-size: 100% ;width: 100% ;padding: 3px ;border: 1px solid #999999 ;-webkit-box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.2) inset ;-webkit-box-sizing: border-box;}select { width: 40% ; }
input[type="checkbox"],
input[type="radio"],
select { display:inline; vertical-align: baseline ;}
テキストとフォームの位置を揃える。vertical-align: middle ; 等試したがbaselineが一番揃ってるように見える。
画像をきれいに見せる
※2倍にした分もちろん容量は大きくなるのでボタンなど汎用的なものは可能な限りスタイルシートで作成。
○imgタグ
画像を2倍サイズで作成し、imgタグのwidth、heightを半分で指定。
○背景
画像を2倍サイズで作成し、background-sizeを半分で指定。
-webkit-background-size: 12px 12px ;
h1サンプル
h1 {height: 44px ;background: #103092 url(/img/bgHeader.png) repeat-x ;font-family: HiraKakuProN-W6 ;font-size: 20px ;color: #fff ;font-weight:bold;text-align: center ;line-height: 44px ;text-shadow: rgba(0, 0, 0, 0.4) 0 -1px 0 ;overflow: hidden ;white-space: nowrap ; /* セル内で自動改行させない */text-overflow: ellipsis ; /* 領域からはみ出た場合に「...」を表示する */}
ボタンサンプル
.btn_base {color: #fff ;font-size: 113.3% ;font-weight: bold ;text-decoration: none ;text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.3) ;text-align: center ;background-image: -webkit-gradient(linear, left top, left bottom, from(#ffbe00), to(#ff5600));-webkit-box-sizing: border-box;-webkit-box-shadow: 0 -2px 0 rgba(0, 0, 0, 1) inset, 0 2px 0 rgba(0, 0, 0, 1) inset, -2px 0 0 rgba(0, 0, 0, 1) inset, 2px 0 0 rgba(0, 0, 0, 1) inset;-webkit-border-radius: 20px;width: 180px ;height: 40px ;display: block ;overflow: hidden ;padding: 7px 0 0 5px ;margin: 0 auto ;border: 2px solid #fff ;}.bullet_box {
font-size: 80% ;font-weight: bold ;text-decoration: none ;text-align: left ;text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.3) ;letter-spacing: 2px ;background-image: url(/img/bullet.png), -webkit-gradient(linear, left top, left bottom, from(#ffba00), color-stop(0.5, #ff7700), color-stop(0.5, #ff4200), to(#ff7700));background-repeat: no-repeat, repeat-x ;background-position: 2px 2px, 0 0 ;-webkit-background-size: 12px 12px, auto;-webkit-box-sizing: border-box;-webkit-border-radius: 2px;display: inline-block ;overflow: hidden ;width: 60px ;height: 18px ;border: 1px solid #ff7700 ;padding: 1px 0 0 20px ;margin-left: 5px ;}
タブサンプル
ul.tab {padding: 0 5px ;margin: 0 auto ;}ul.tab li {
width: 50% ;float: left ;}ul.tab li a {
color: #fff ;font-size: 93.3% ;font-weight: bold ;text-shadow: 0 -1px 1px #000 ;text-align: center ;text-decoration: none ;width: 100% ;display: block ;margin: 0 auto ;-webkit-border-top-left-radius: 5px;-webkit-border-top-right-radius: 5px;-webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.5) inset;-webkit-box-sizing: border-box;padding: 3px 0 ;}ul.tab li.tab_a a {
background-image: -webkit-gradient(linear, left top, left bottom, from(#0084ff), color-stop(0.5, #0027ba), color-stop(0.5, #001873), to(#0061ce));}ul.tab li.tab_b a {
color: #95d2ff ;background-image: -webkit-gradient(linear, left top, left bottom, from(#005fb8), color-stop(0.5, #001a79), color-stop(0.5, #000c3a), to(#001a79));-webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.3) inset, 0 -2px 2px rgba(0, 0, 0, 0.5) inset;}
テキストにドロップシャドウ(text-shadow)
text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.5) ;text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.3) ;
ボックスにドロップシャドウ(box-shadow)
-webkit-box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.5) ;-webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.5) inset, 0 -1px 0 rgba(0, 0, 0, 0.5) inset, 0 1px 1px rgba(255, 255, 255, 0.6), 1px 0 1px rgba(255, 255, 255, 0.3), -1px 0 1px rgba(255, 255, 255, 0.3);
※insetで内側指定
○内側2pxに黒の境界線
-webkit-box-shadow: 0 -2px 0 rgba(0, 0, 0, 1) inset, 0 2px 0 rgba(0, 0, 0, 1) inset, -2px 0 0 rgba(0, 0, 0, 1) inset, 2px 0 0 rgba(0, 0, 0, 1) inset;
グラデーション(text-gradient)
○オレンジ(通常)
background-image: -webkit-gradient(linear, left top, left bottom, from(#ffbe00), to(#ff5600));
○オレンジ(ラスター)
background-image: -webkit-gradient(linear, left top, left bottom, from(#ffba00), color-stop(0.5, #ff7700), color-stop(0.5, #ff4200), to(#ff7700));
○赤(ラスター)
background-image: -webkit-gradient(linear, left top, left bottom, from(#f33529), color-stop(0.5, #8a0d05), color-stop(0.5, #6a0b05), to(#9e1209));
○青(ラスター)
background-image: url(/img/bullet.png), -webkit-gradient(linear, left top, left bottom, from(#006ed3), color-stop(0.5, #004380), color-stop(0.5, #002a4f), to(#004380));
○緑(ラスター)
background-image: url(/img/bullet.png), -webkit-gradient(linear, left top, left bottom, from(#92dd00), color-stop(0.5, #51ab00), color-stop(0.5, #118d00), to(#92dd00));
○黒(ラスター)
background-image: -webkit-gradient(linear, left top, left bottom, from(#6e7588), color-stop(0.5, #464b5a), color-stop(0.5, #000), to(#464b5a));
背景の複数指定(multiple backgrounds)
○オレンジ(ラスター+画像)
background-image: url(/img/bullet.png), -webkit-gradient(linear, left top, left bottom, from(#ffba00), color-stop(0.5, #ff7700), color-stop(0.5, #ff4200), to(#ff7700));background-repeat: no-repeat, repeat-x ;background-position: 2px 2px, 0 0 ;-webkit-background-size: 12px 12px, auto;
角丸(border-radius)
○四隅全て
-webkit-border-radius: 20px;
○上右
-webkit-border-top-right-radius: 20px;
○上左
-webkit-border-top-left-radius: 20px;
○下右
-webkit-border-bottom-right-radius: 20px;
○下左
-webkit-border-bottom-left-radius: 20px;
ボックスサイズの算出指定(box-sizing)
○通常
box-sizing: content-box;
○widthからborderとpaddingを引いた指定
box-sizing: border-box;
透過(rgba)
background:rgba(255, 255, 255, 0.2) ;border-bottom: 1px solid rgba(255, 255, 255, 0.2) ;
フォームガイド(placeholder)
○css
input::-webkit-input-placeholder,textarea::-webkit-input-placeholder {color: #999 ;}
○html
<input type="text" name="~" placeholder="~" /><textarea name="~" placeholder="~"></textarea>
フォームフォーカス(autofocus)
○html
<input type="text" name="~" autofocus />
その他
・iPhone 3G/OS4.0.1のSafariでcssで背景にjpg画像を指定し、その上に< img src="~.png"> とpng画像を載せたところ透過されない事例が発生。一旦横画面にすると透過された。またpngもしくはgifで指定すると問題なく表示された。このOSのバージョンのみで発生し、OS4.2.1の3Gでは問題なく透過され、また、条件に全く一致するiPhoneが他になかったため、OSが問題なのかこのiPhone自体の問題なのかは不明。




