疑似要素(before
/after
)を利用する方法もご紹介させてください。
※ サンプルの画像は、違いがわかりやすいように半透明にしています。
親要素のbeforeで横幅いっぱい線を敷いて、上に画像を重ねる方法
html
1<div class="sample-1">
2 <img src="https://placehold.jp/3d4070/ffffff/150x150.png" alt="150x150">
3</div>
css
1.sample-1 {
2 position: relative;
3}
4
5.sample-1::before {
6 content: "";
7 display: block;
8 width: 100%;
9 height: 1px;
10 position: absolute;
11 top: 50%;
12 left: 0;
13 z-index: -1;
14 transform: translateY(-50%);
15 background-color: #ccc;
16}
親要素のafterを線にして、flexで横に並べる方法
html
1<div class="sample-2">
2 <img src="https://placehold.jp/3d4070/ffffff/150x150.png" alt="150x150">
3</div>
css
1.sample-2 {
2 display: flex;
3 align-items: center;
4}
5
6.sample-2::after {
7 content: "";
8 display: block;
9 width: 100%;
10 height: 1px;
11 background-color: #ccc;
12}