position:absoluteで前面に配置した要素のサイズをposition:relativeとなっている親要素にあわせて相対指定するテクニックをご紹介します。
実例でいうと、たとえばこんな感じ。
リキッドレイアウトの要素へグラデーションの縁をつけてみました。
absolute要素の幅を指定せず、left,rightを両方とも指定するとrelative要素に合わせた相対値の幅になり
absolute要素の高さを指定せず、top,bottomを両方とも指定するとrelative要素に合わせた相対値の高さになります。
ソースはこちら
<div id="sampleBorder"></div>
#sampleBorder { position: relative; display: block; margin: 24px auto; padding: 1em; width: 80%; background: #666666; background: -moz-linear-gradient(top, #666666 0%, #0e0e0e 100%); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#666666), color-stop(100%,#0e0e0e)); background: -webkit-linear-gradient(top, #666666 0%,#0e0e0e 100%); background: -o-linear-gradient(top, #666666 0%,#0e0e0e 100%); background: -ms-linear-gradient(top, #666666 0%,#0e0e0e 100%); background: linear-gradient(to bottom, #666666 0%,#0e0e0e 100%); filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#666666', endColorstr='#0e0e0e',GradientType=0 ); border-radius: 6px; } #sampleBorder:before { content: ''; position: absolute; top: 3px; left: 3px; right: 3px; bottom: 3px; z-index: 1; display: block; background: #222; border-radius: 3px; box-shadow: inset 0 0 24px rgba(0, 0, 0, 0.1); }
topとbottom、leftとrightが対になっているので
こんな使い方もできます。
スマートフォン画面の回転に対応したリキッドデザインの3等分タブ
リキッドレイアウトで三分割したタブを絶対位置指定で配置しています。
A
Aの内容です。
B
Bの内容です。
C
Cの内容です。
<div id="sampleArea"> <div id="sampleA" class="sampleContent current"> <h2>A</h2> <div>Aの内容です。</div> </div> <div id="sampleB" class="sampleContent"> <h2>B</h2> <div>Bの内容です。</div> </div> <div id="sampleC" class="sampleContent"> <h2>C</h2> <div>Cの内容です。</div> </div> </div>
#sampleArea { position: relative; border: 1px solid #ccc; } .sampleContent h2 { position: absolute; top: 0; display: block; height: 30px; line-height: 30px; text-align: center; margin: 0; cursor: pointer; } .sampleContent h2:hover { top: -6px; height: 36px; line-height: 36px; } .sampleContent div { margin-top: 30px; padding: 12px; display: none; } .sampleContent.current div { display: block; } #sampleA h2, #sampleA div { background: #458C6B; color: #fff; } #sampleA h2 { left: 0; width: 33.3%; } #sampleB h2, #sampleB div { background: #D9A86C; color: #fff; } #sampleB h2 { left: 33.3%; right: 33.3%; } #sampleC h2, #sampleC div { background: #A62424; color: #fff; } #sampleC h2 { right: 0; width: 33.3%; }
$(function(){ $('.sampleContent h2').on('click',function(){ $('.sampleContent').removeClass('current'); $(this).parent().addClass('current'); }); });