- WEB制作系
- CSS , CSS3 , メモ
- Comments (0)
擬似要素:beforeや:afterを使ったデザインの練習
今更なことだけど、擬似要素:beforeと:afterの練習をしてみました。
今まではclearfixの時にしか使っていなかった、この擬似要素。他のサイトなどを参考にして試してみた感想としては、IE7(CSS3を使う場合にはIE8以下)には対応していないけど、ちょっとしたデザインやスマートフォンのページではどんどん使っていきたいなと思いました。
ちょっとしたデザインに。ボックスシャドウ

この画像の左上と右下あたりに影をつけて、少し浮いている感じにしてみます。
HTML
1 2 3 | <div class="sample1"> <img src="http://mugimemo.com/wp/wp-content/uploads/20120129-1.jpg"> </div> |
CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | .sample1 { position: relative; width: 300px; border: 5px solid #000; background: #FFF; } .sample1 img { display: block; } .sample1:before { content: ""; position: absolute; width: 90%; height: 90%; top: -5px; left: -5px; box-shadow: 0 0 5px #666; -webkit-box-shadow: 0 0 5px #666; -moz-box-shadow: 0 0 5px #666; border-radius: 0 100% 0 10% / 0 10% 0 100%; -webkit-border-radius: 0 100% 0 10% / 0 10% 0 100%; -moz-border-radius: 0 100% 0 10% / 0 10% 0 100%; z-index: -1; } .sample1:after { content: ""; position: absolute; width: 90%; height: 90%; bottom: -5px; right: -5px; box-shadow: 0 0 10px #333; -webkit-box-shadow: 0 0 10px #333; -moz-box-shadow: 0 0 10px #333; border-radius: 0 10% 0 100% / 0 100% 0 10%; -webkit-border-radius: 0 10% 0 100% / 0 100% 0 10%; -moz-border-radius: 0 10% 0 100% / 0 100% 0 10%; z-index: -1; } |
これでこんな感じの影ができました。
完成図

img要素に:beforeや:afterを付けてCSSを指定してもボックスシャドウは表示されません。なぜか?ということに関しては、こちらのサイトがすごくわかりやすいと思います。
- JeffreyFrancesco.org:img:after { content: attr(alt); } としても何も表示されないのは、仕様的に正しい挙動です
パンくずリスト
Coliss:[CSS]シンプルなHTMLで、美しいパンくずを実装する4つのスタイルシートで紹介されていました。
そっくりそのままなものだけど、最初から作ってみました。
fHTML
1 2 3 4 5 6 | <ul class="sample2"> <li><a href="#">sample text one</a></li> <li><a href="#">sample text two</a></li> <li><a href="#">sample text three</a></li> <li><a href="#">sample text fore</a></li> </ul> |
CSS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | .sample2 li { float: left; margin-right: 1.2em; list-style-type: none; } .sample2 li a { display: block; position: relative; padding: 0.5em; font-size: 12px; text-decoration: none; color: #FFF; background: #000; } .sample2 li a:after { content: ""; position: absolute; margin-top: -1em; top: 50%; right: -1em; border-top: 1em solid transparent; border-bottom: 1em solid transparent; border-left: 1em solid #000; z-index: 1; } .sample2 li a:before { content: ""; position: absolute; margin-top: -1em; top: 50%; left: -1em; border-width: 1em 0 1em 1em; border-color: #000 #000 #000 transparent; border-style: solid; z-index: 2; } .sample2 li a:hover { background: #F00; } .sample2 li a:hover:after { border-left: 1em solid #F00; } .sample2 li a:hover:before { border-color: #F00 #F00 #F00 transparent; } |