webかたつむり ウェブデザインを勉強中 ウェブ初心者のおぼえがき

webかたつむり

WEB制作会社のフォトグラファー

疑似要素 :before :afrer - block要素の上下左右にcssで線を引く

<!DOCTYPE html>
<html>
<head>
<title>line</title>
<meta charset="UTF-8">
<style>
.square{
height:200px;
width:200px;
background: coral;
margin: 20px auto;
position: relative;
}
</style>

</head>
<body>
<div class="container">
<div class="square">

</div>
</div>

</body>
</html>

f:id:ohta-felica:20160924215306p:plain

  • たとえばこの正方形の右側に赤い線を引いてみる。

  <style>
.square{
height:200px;
width:200px;
background: coral;
margin: 20px auto;
position: relative;
}
.square:before{
content: "";
width:3px;
border-right: 3px solid red;
position: absolute;
right: -10px;
top: 0;
bottom: 0;
}
</style> 

  •  赤字の部分が肝です。top: 0; bottom: 0; とすると、疑似要素が直線に変身します。片方だけだとうまくいきません。

f:id:ohta-felica:20160924220007p:plain

  • dl, dt, dd と組み合わせるとおしゃれな表組みができそうです。