首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使子元素在父元素上方悬停时进行更改

如何使子元素在父元素上方悬停时进行更改
EN

Stack Overflow用户
提问于 2017-02-01 18:36:19
回答 3查看 65关注 0票数 1

当我在父元素width + .credit-card上盘旋时,我试图获得图像#add-cc-img#add-new-cc.credit-card。如何保持父级的widthheight保持原来的形式,并且只更改图像的大小?

代码语言:javascript
复制
.credit-card {
  border: 1px solid #CDCDCD;
  width: 30%;
  height: 250px;
  display: inline-block;
  margin: 20px 3% 20px 0;
  vertical-align: top;
}
.credit-card-inner {
  margin: 25px;
}
#add-new-cc {
  cursor: pointer;
  transition: .5s;
  -webkit-transition: .5s;
}
#add-new-cc:hover,
#add-cc-img {
  background: #F7F7F7;
  transition: .5s;
  -webkit-transition: .5s;
  height: 125px;
  width: 125px;
}
#add-new-cc-title {
  font-size: 1.3em;
  margin-bottom: 40px;
  text-align: center;
}
#add-cc-img {
  height: 100px;
  width: 100px;
  display: block;
  margin: 0 auto;
}
代码语言:javascript
复制
<div class="credit-card" id="add-new-cc">
  <div class="credit-card-inner">
    <h3 class="blue sans-pro" id="add-new-cc-title">Add New Card</h3>
    <img src="images/add-circle.png" alt="Add New Credit Card" id="add-cc-img">
  </div>
</div>

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-02-01 18:46:03

使用transform: scale,它将以更好的方式调整图像的大小

(并删除#add-new-cc:hover #add-cc-img { ... }__中的逗号)

我将transition: .5s;添加到#add-cc-img { ... }规则中,因此它也将在un上显示动画。

代码语言:javascript
复制
.credit-card {
  border: 1px solid #CDCDCD;
  width: 30%;
  height: 250px;
  display: inline-block;
  margin: 20px 3% 20px 0;
  vertical-align: top;
}
.credit-card-inner {
  margin: 25px;
}
#add-new-cc {
  cursor: pointer;
  transition: .5s;
  -webkit-transition: .5s;
}
#add-new-cc-title {
  font-size: 1.3em;
  margin-bottom: 40px;
  text-align: center;
}
#add-cc-img {
  height: 100px;
  width: 100px;
  display: block;
  margin: 0 auto;
  transition: .5s;
}
#add-new-cc:hover #add-cc-img {
  background: #F7F7F7;
  transition: .5s;
  transform: scale(1.25);
}
代码语言:javascript
复制
<div class="credit-card" id="add-new-cc">
  <div class="credit-card-inner">
    <h3 class="blue sans-pro" id="add-new-cc-title">Add New Card</h3>
    <img src="images/add-circle.png" alt="Add New Credit Card" id="add-cc-img">
  </div>
</div>

票数 2
EN

Stack Overflow用户

发布于 2017-02-01 18:40:32

似乎您不需要在这里使用逗号:#:悬浮,#。没有逗号,它似乎正常工作。

代码语言:javascript
复制
.credit-card {
  border: 1px solid #CDCDCD;
  width: 30%;
  height: 250px;
  display: inline-block;
  margin: 20px 3% 20px 0;
  vertical-align: top;
}
.credit-card-inner {
  margin: 25px;
}
#add-new-cc {
  cursor: pointer;
  transition: .5s;
  -webkit-transition: .5s;
}
#add-new-cc:hover
#add-cc-img {
  background: #F7F7F7;
  transition: .5s;
  -webkit-transition: .5s;
  height: 125px;
  width: 125px;
}
#add-new-cc-title {
  font-size: 1.3em;
  margin-bottom: 40px;
  text-align: center;
}
#add-cc-img {
  height: 100px;
  width: 100px;
  display: block;
  margin: 0 auto;
}
代码语言:javascript
复制
<div class="credit-card" id="add-new-cc">
  <div class="credit-card-inner">
    <h3 class="blue sans-pro" id="add-new-cc-title">Add New Card</h3>
    <img src="images/add-circle.png" alt="Add New Credit Card" id="add-cc-img">
  </div>
</div>

票数 1
EN

Stack Overflow用户

发布于 2017-02-01 18:40:40

在选择器之间放置逗号时,您将创建一个将执行一条规则的不同选择器的列表。这就是你所拥有的:

代码语言:javascript
复制
#add-new-cc:hover, #add-cc-img {
  background: #F7F7F7;
  transition: .5s;
  -webkit-transition: .5s;
  height: 125px;
  width: 125px;
}

相反,您需要一个子代选择器来完成这项工作(用空格替换逗号):

代码语言:javascript
复制
#add-new-cc:hover #add-cc-img {
  background: #F7F7F7;
  transition: .5s;
  height: 125px;
  width: 125px;
}

代码语言:javascript
复制
.credit-card {
  border: 1px solid #CDCDCD;
  width: 30%;
  height: 250px;
  display: inline-block;
  margin: 20px 3% 20px 0;
  vertical-align: top;
}
.credit-card-inner {
  margin: 25px;
}
#add-new-cc {
  cursor: pointer;
  transition: .5s;
  -webkit-transition: .5s;
}
#add-new-cc:hover #add-cc-img {
  background: #F7F7F7;
  transition: .5s;
  height: 125px;
  width: 125px;
}
#add-new-cc-title {
  font-size: 1.3em;
  margin-bottom: 40px;
  text-align: center;
}
#add-cc-img {
  height: 100px;
  width: 100px;
  display: block;
  margin: 0 auto;
}
代码语言:javascript
复制
<div class="credit-card" id="add-new-cc">
  <div class="credit-card-inner">
    <h3 class="blue sans-pro" id="add-new-cc-title">Add New Card</h3>
    <img src="http://i.imgur.com/60PVLis.png" alt="Add New Credit Card" id="add-cc-img">
  </div>
</div>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41987156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档