今天來分享一下 hover
物件時更換背景顏色的方法,
我是看到這個 Youtube 影片,
但我的寫法跟他的沒有完全一樣,
可以兩邊都看看比較一下,
當然也有不同種寫法,
我知道變換背景這種事用 JS 做是比較合理的方式,
但 HTML/CSS 就能做到,
也是滿好玩的啦~
效果
HTML
<div class="container">
<div class="item-group">
<div class="item">
<h3>Title</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Assumenda, culpa. Perferendis obcaecati itaque
asperiores! Officia illum odio optio, reprehenderit distinctio aliquid ullam veritatis soluta voluptatibus
rerum
quae, totam ex hic.</p>
</div>
<div class="item">
<h3>Title</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Assumenda, culpa. Perferendis obcaecati itaque
asperiores! Officia illum odio optio, reprehenderit distinctio aliquid ullam veritatis soluta voluptatibus
rerum
quae, totam ex hic.</p>
</div>
<div class="item">
<h3>Title</h3>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Assumenda, culpa. Perferendis obcaecati itaque
asperiores! Officia illum odio optio, reprehenderit distinctio aliquid ullam veritatis soluta voluptatibus
rerum
quae, totam ex hic.</p>
</div>
</div>
</div>
CSS
* {
padding: 0;
margin: 0;
list-style: none;
box-sizing: border-box;
}
.container {
width: 960px;
margin: auto;
height: 100vh;
font-size: 0;
}
.container::before {
content: '';
display: inline-block;
height: 100%;
width: 0;
background-color: #faa;
vertical-align: middle;
}
.item-group {
font-size: 0;
display: inline-block;
vertical-align: middle;
}
.item {
display: inline-block;
font-size: 16px;
width: 290px;
margin: 0 15px;
padding: 30px;
border-radius: 10px;
box-shadow: 10px 10px 20px rgba(0, 0, 0, .5);
}
.item::after {
content: '';
position: absolute;
z-index: -1;
left: 0;
top: 0;
width: 100%;
height: 100%;
transition: .5s;
}
.item:hover {
color: #fff;
}
.item:first-child:hover,
.item:first-child:hover::after {
background-color: #16a085;
}
.item:nth-child(2):hover,
.item:nth-child(2):hover::after {
background-color: #2980b9;
}
.item:last-child:hover,
.item:last-child:hover::after {
background-color: #c0392b;
}
.item h3 {
margin-bottom: .4em;
}
今天就先到這裡啦~
我們明天見。