table内でpositionを使った配置をするとFirefoxでうまくできなかった記憶があるのですが、いつのまにか大丈夫になっていたみたいなので、備忘録としてメモしておきます。
サンプルコード
HTML
<table> <tr> <th> お名前 <span class="required">必須</span> </th> <td> <input type="text" name="name" /> <div class="error-arrow"> <p>全角で入力してください</p> </div> <div class="required-check">入力済</div> </td> </tr> </table>
CSS
table {
border-collapse: collapse;
border-spacing: 0;
width: 600px;
margin: 0 auto;
}
th, td {
position: relative;
border: #cccccc 1px solid;
padding: 10px 15px;
}
th {
width: 150px;
padding-right: 30px;
font-weight: normal;
text-align: left;
background: #cccccc;
}
input {
width: 300px;
}
.required {
position: absolute;
top: 50%;
right: 5px;
margin-top: -11px;
padding: 3px 5px;
font-size: 10px;
color: #ffffff;
background: #ff0000;
}
.error-arrow {
position: absolute;
top: 0;
left: 0;
}
.error-arrow p {
position: absolute;
bottom: -8px;
left: 20px;
width: 250px;
margin: 0;
border-radius: 5px;
padding: 5px;
border: #999999 1px solid;
font-size: 14px;
background: #ffffff;
}
.error-arrow p:after {
content: '';
display: block;
position: absolute;
bottom: -6px;
left: 20px;
width: 10px;
height: 10px;
border: #999999 1px solid;
border-top: none;
border-left: none;
background: #ffffff;
transform: rotate(45deg);
}
.required-check {
position: absolute;
top: 0;
right: 0;
top: 50%;
right: 5px;
margin-top: -10px;
font-size: 12px;
}
以前はFirefoxだとtable系の要素でposition:relative;が効かなかったのですが、Firefox・chrome・IEで確認した限り問題なさそうでした。
table内でpositionを使った配をするデモページ
フォームでエラー文言を出すときだったり、意外とtable内でpositionを使いたい場面はあると思うので、こうして使えるようになると便利ですね。
【参考サイト】
- table系にはposition:relativeが効かない – No.1026
- table系タグのposition:relative – gomokulog
- お前は table-cell に position: relative できなかった人の数を覚えているのか – @debiru
- table関連の要素に対するposition:relative;の挙動について調べた – tohounikureru
コメントが承認されるまで時間がかかります。