简体   繁体   English

使用 sass 更改悬停的卡片背景颜色

[英]Change hovered card background color using sass

I need background color lightgreen on card hover.我需要的背景颜色lightgreen卡上悬停。

    <div class="card">
      <img src="https://placekitten.com/200/300" 
alt="card-theme"/>
      <div class="hovered-text">
        Open
      </div>
    </div>

styles样式

.card {
  background: white;
  width: 200px;
  height: 200px;
  position: relative;
  border: 5px solid lightgrey;
  border-radius: 2px;
  display: flex;
  justify-content: center;
  align-items: center;
  .hovered-text {
    display: none;
    position: absolute;
    color: salmon;
  }
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    opacity: 1;
    &:hover {
     opacity: 0.1;
     background: lightgreen;
    }
    &:hover~.hovered-text{
      display: block
    }
  }
}

Code inSandbox 沙盒中的代码

Please help to make it work.请帮助使其工作。 Thanks谢谢

All you need to do is add hover to the class card.您需要做的就是将悬停添加到课程卡上。 Eg:例如:

.card {
  &:hover {
    background: lightgreen;
   }

Example:例子:

card {
  background: white;
  width: 200px;
  height: 200px;
  position: relative;
  border: 5px solid lightgrey;
  border-radius: 2px;
  display: flex;
  justify-content: center;
  align-items: center;
&:hover {
        background: lightgreen;
       }
  .hovered-text {
    display: none;
    position: absolute;
    color: salmon;
  }
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    opacity: 1;
    &:hover {
     opacity: 0.1;
     background: lightgreen;
    }
    &:hover~.hovered-text{
      display: block
    }
  }
}

Change your .card backgroundcolor to lightgreen将您的 .card 背景颜色更改为浅绿色

.App {
  font-family: sans-serif;
  text-align: center;
}

.card {
  background: lightgreen;
  width: 200px;
  height: 200px;
  position: relative;
  border: 5px solid lightgrey;
  border-radius: 2px;
  display: flex;
  justify-content: center;
  align-items: center;
  .hovered-text {
    display: none;
    position: absolute;
    color: salmon;
  }
  img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    opacity: 1;
    &:hover, ::after {
     opacity: 0.3;
    //  background: #lightgreen;
    }
    &:hover~.hovered-text{
      display: block;
      
    }
  }
}

Code in Sandbox 沙盒中的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CSS 在另一个 div 悬停时更改单独 div 的背景颜色 - CSS change background color of seperate div when another div is hovered 将鼠标悬停在背景图像上时,文本更改颜色 - Text change color when hovered over background image 悬停时导航栏上的背景颜色更改问题 - Issue with background color change on nav-bar when hovered 当父 Div 悬停时更改子 Div 的背景颜色 - Change Background-Color of Child Div When Parent Div is Hovered 使用CSS / HTML / Python / SASS(但不使用JS)根据值更改表格单元格背景的颜色? 无需动态 - Change the color of the background of a table cell based on the value using CSS/HTML/Python/SASS (but no JS)? No need to be dynamically jQuery-悬停时更改背景图像 - jQuery - change background image if hovered 在SASS中,我想更改每偶数行的背景颜色 - In SASS I want to change the background color of every even row of items 当鼠标悬停任一元素时,如何使多个元素改变背景颜色? - How to make multiple elements change background color when either element is hovered over by the mouse? 当盘旋时,如何使菜单项的整个宽度改变背景颜色? - How do I make the entire width of a menu item change background color when hovered over? 文本的实时预览使链接悬停在链接上时不会更改背景颜色 - Live preview of text makes the link not change background color when hovered over
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM