[CSS]ウェブ制作に役立つ、スタイルシートの有用なスニペットのまとめ
Post on:2012年11月2日
sponsorsr
よく使うclearfixから、見出しの画像置換や見た目非表示の新しいスニペット、レスポンシブによく利用されるスニペットなど、スタイルシートのよく使用する有用なスニペットを紹介します。

A Collection of Handy CSS Snippets
下記は各ポイントを意訳したものです。
ショートハンドのclass
よく使用するショートハンドのclassから始めましょう。
ポイントはセレクタでエレメントを定義するのではなく、すぐに利用できることを前提にしています。
まずは、テキストと画像を配置する時に簡単に使えるfloatです。
.float-left /* こんな感じに直観的に分かる名前で */ {
float: left;
}
.float-right /* こんな感じに直観的に分かる名前で */ {
float: right;
}
基本的にはこれだけです。あとは画像に「.float-left」を指定してください。
あ、もう一つだけ例を。
.hide {
display: none;
}
.show {
display: block;
}
エレメントを何らかの理由で表示・非表示したい時のclassです。
エレメントに「display:none;」を加えるのではなく、シンプルに「.hide」をclassに加えます。
デザイン関連のスニペット
デザイン関連のスニペットです。ここではデザインのプロセスをよりシンプルにして共通のスタイルをエレメントに定義できるようにしたものです。
最初は、フォントのスタイルです。下記のスタイルは英語圏内のものですが、ポイントはfont-size, line-height, fontを魅力的に組み合わせることです。
.content {
font: 1em/1.4 Segoe, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
}
.title {
font: 1.7em/1.2 Baskerville, "Baskerville old face", "Hoefler Text", Garamond, "Times New Roman", serif;
}
.code {
font: 0.8em/1.6 Monaco, Mono-Space, monospace;
}
「.content」は標準となるフォントのスタイルで、主にコンテンツで使用します。「.title」は高級感をだすためにセリフフォントにしました。最後の「.code」はコード用で少し小さいサイズにしています。
次は、もう少し実用的なclass「.disabled」です。これは「機能しない」役割で、半透明にし、ポインタイベントを全て停止させます。
.disabled {
pointer-events: none;
opacity: 0.5;
}
もう一つ、強力なセレクタを紹介しましょう。
テーブルでセルをシマシマにする時によく使うスニペットです。
table tr:nth-child(even) {
background: rgba(0,0,0,0.1);
}
デザイン関連の最後のスニペットは、リンクのスタイルを改善します。
a {
text-decoration: none;
color: #08C;
transition: all 0.3s ease-out;
position: relative;
padding: .5em;
margin: -.5em;
}
a:hover { color: #0AF; }
「text-decoration:none;」でアンダーラインを取り除き、デフォルトのブルーをもう少しいい感じのブルーにしました。
ホバー時にはちょっとアニメーションを加え、このテキストリンクがクリック可能であることを伝えます。
開発関連のスニペット
開発関連と書くと御幣が生むかもしれませんが、ここではデザイン関連ではあるけれど、構造によりいっそう関係があるスニペットを紹介します。
最初のスニペットは非常に簡単です。おそらく何度も見たことがあるでしょう。
*, *:before, *:after {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
このスニペットを使うと、デフォルトの「box-model」が「content-box」にセットされます。レスポンシブデザインでは特によく使用するスニペットです。
人気の高いスニペットをもう一つ紹介しましょう、clearfixです。
コンテナがフロートさせたエレメントを含んでいる時、自身でフロートを解除します。
.clearfix:before,
.clearfix:after {
content: " ";
display: table;
}
.clearfix:after {
clear: both;
}
/* IE6/7 support */
.clearfix {
*zoom: 1;
}
次はちょっと深いとこに入ってみましょう。
.visuallyhidden {
position: absolute;
width: 1px; /* 0にしてしまうとダメ */
height: 1px; /* 0にしてしまうとダメ */
padding: 0;
margin: -1px;
border: 0;
clip: rect(0 0 0 0);
overflow: hidden;
}
これはHTML5BoilerPlateで使用されている「見た目非表示」のclassです。これは「display:none;」とは違い、スクリーンリーダーや検索エンジンのロボットからはコンテンツを隠しません。これは非常に重要なポイントです。大事なコンテンツを非表示にする場合はこのclassを使ってください。
非表示についてでてきたので、今度は画像の置換を紹介しましょう。置換のフローは3つ。
- エレメントを選択する
- 背景画像を与える
- テキストを隠す
この3番目の隠すで「position: absolute; left: -9999px;」は確かに機能しますが、ブラウザが10000pxの箱を作ってしまうのでよろしくありません。
ここではそれに代わる新しい方法を紹介します。
.ir {
text-indent: 100%;
white-space: nowrap;
overflow: hidden;
}
このスニペットは信じられないほど、よく出来ています。テキストをボックスから移動するだけです。
今度はテキスト関連のスニペットです。
.break {
-ms-word-break: break-all;
word-break: break-all;
word-break: break-word;
-webkit-hyphens: auto;
-moz-hyphens: auto;
hyphens: auto;
}
.ellipsis {
width: 250px;
white-space: nowrap;
overflow: hidden;
-ms-text-overflow: ellipsis; /* Required for IE8 */
-o-text-overflow: ellipsis; /* Required for Opera */
text-overflow: ellipsis;
}
「.break」はIE8, Firefox, Safari, Chromeで動作しますが、残念ながらOperaでは動作しません。
「.ellipsis」はIE8, Safari ,Chrome, Operaで動作しますが、Firefoxでは動作しません。
最後にコードなどの長い文字列をpreタグを使って、どのように表示するかのスニペットです。
pre {
white-space: pre-wrap; /* Chrome & Safari */
white-space: -moz-pre-wrap; /* Mozilla since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}
このスニペットを使うことで、連続する半角スペース・タブ・改行はそのままで、コンテナの端になると改行して表示します。
印刷・Retina・検証用のスニペット
ここでは上記に属さない雑多なスニペットを紹介します。
まずは、プリントのスニペットから始めます。
@media print {
* {
background: none !important;
color: black !important;
box-shadow: none !important;
text-shadow: none !important;
/* Images, vectors and such */
filter: Gray(); /* IE4-8: depreciated */
filter: url('desaturate.svg#grayscale'); /* SVG version for IE10, Firefox, Safari 5 and Opera */
-webkit-filter: grayscale(100%); /* Chrome + Safari 6 */
-moz-filter: grayscale(100%); /* Future proof */
-ms-filter: grayscale(100%); /* Future proof */
-o-filter: grayscale(100%); /* Future proof */
filter: grayscale(100%); /* Future proof or polyfilled */
}
a {
text-decoration: underline;
}
a[href]:after {
content: " (" attr(href) ")";
}
a[href="#"],
a[href="javascript:"] {
content: "";
}
}
多くの制作者がプリント時にかっこよく見えることに関心がないことを知っています。しかし、このスニペットを使うだけで、ある程度かっこよさを保つことができます。
次は、高解像度で表示するRetinaディスプレイを判別するスニペットです。
@media
only screen and (-webkit-min-device-pixel-ratio: 2),
only screen and ( min--moz-device-pixel-ratio: 2), /* Looks like a bug, so may want to add: */
only screen and ( -moz-min-device-pixel-ratio: 2),
only screen and ( -o-min-device-pixel-ratio: 2/1),
only screen and ( min-device-pixel-ratio: 2),
only screen and ( min-resolution: 192dpi),
only screen and ( min-resolution: 2dppx) {
/* ここにRetinaディスプレイ用のスタイルを記述 */
}
最後は、検証用のスニペットです。
ドキュメントのルートに「.debug」を加え、ページの検証に役立ててください。
/* Empty Elements */
.debug div:empty, .debug span:empty,.debug li:empty,.debug p:empty,.debug td:empty,.debug th:empty {
padding: 20px;
border: 5px dotted yellow !important;
}
/* Empty Attributes */
.debug *[alt=""], .debug *[title=""], .debug *[class=""], .debug *[id=""], .debug a[href=""] {
border: 5px solid yellow !important;
}
/* Deprecated Elements */
.debug applet, .debug basefont, .debug center, .debug dir, .debug font, .debug isindex, .debug menu, .debug s, .debug strike, .debug u {
border: 5px dotted red !important;
}
/* Deprecated Attributes */
.debug *[background], .debug *[bgcolor], .debug *[clear], .debug *[color], .debug *[compact], .debug *[noshade], .debug *[nowrap], .debug *[size], .debug *[start],.debug *[bottommargin], .debug *[leftmargin], .debug *[rightmargin], .debug *[topmargin], .debug *[marginheight], .debug *[marginwidth], .debug *[alink], .debug *[link], .debug *[text], .debug *[vlink],.debug *[align], .debug *[valign],.debug *[hspace], .debug *[vspace],.debug *[height], .debug *[width],.debug ul[type], .debug ol[type], .debug li[type] {
border: 5px solid red !important;
}
/* Proposed Deprecated Elements */
.debug input[type="button"], .debug big, .debug tt {
border: 5px dotted #33FF00 !important;
}
/* Proposed Deprecated Attributes */
.debug *[border], .debug table[cellpadding], .debug table[cellspacing] {
border: 5px solid #33FF00 !important;
}
sponsors











