首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何编辑两个ID?

如何编辑两个ID?
EN

Stack Overflow用户
提问于 2021-07-14 03:13:03
回答 2查看 53关注 0票数 1

向我的CSS添加1个以上的ID时遇到问题。我希望框边框有四个不同的按钮(悬停效果)和三个渐变按钮(例如:播放,图片等)。

这是我的HTML

代码语言:javascript
复制
<h2><a id="hover-1" href="">Hover Effect 1</a></h2>
<h2><a id="hover-2" href="">Hover Effect 2</a></h2>
<a href="" id="play">Play</a><br/>
<a href="" id="picture">Picture</a><br/>

这是CSS

代码语言:javascript
复制
<style>

/* Gradient*/
body {
  padding: 3em;
}
#play {
  color: blue;
  font: bold 3em/1.5 , sans-serif;
  text-decoration: none;
  text-transform: uppercase;
  -webkit-transition: .2s;
}
#play:hover {
  color: hotPink;
}
@media (-webkit-min-device-pixel-ratio:0) {
  #play {
    background-color: skyBlue;
    background-image: -webkit-linear-gradient(left, hotPink 0%, orange 50%, transparent 50%);
    background-position: 100% 0;
    background-size: 200% 200%;
    color: transparent;
    -webkit-transition: .1s .2s;
    -webkit-background-clip: text;
  }
  #play:hover {
    background-position: 0 0;
    color: transparent;
    transition: .4s 0;
  }
}

/*Box Border*/
html {
  box-sizing: border-box
}
*, *:before, *:after {
  box-sizing: border-box;
}

body {
  width: 80%;
  margin: 50px auto;
  color:black;
  font: 16px/1 'Catamaran', sans-serif;
  background: transparent;
}
body:after {
  content: '';
  display: flex;
  clear: both;
}


h2 {
  width: 50%;
  text-align: center;
  height: 44px;
  line-height: 24px;
  font-weight: 400;
  
}
@media screen and (max-width: 768px) {
  h2 {
    width: 100%;
  }
  h1 {
    margin-bottom: 15px;
    line-height: 30px;
  }
}

a, a > span {
  position: relative;
  color: inherit;
  text-decoration: none;
  line-height: 24px;
}
a:before, a:after, a > span:before, a > span:after {
  content: '';
  position: absolute;
  transition: transform .5s ease;
}

#hover-1 {
  padding: 10px;
}
#hover-1:before, #hover-1:after {
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  border-style: solid;
  border-color: black;
}
#hover-1:before {
  border-width: 2px 0 2px 0;
  transform: scaleX(0);
}
#hover-1:after {
  border-width: 0 2px 0 2px;
  transform: scaleY(0);
}
#hover-1:hover:before, #hover-1:hover:after {
  transform: scale(1, 1);
}

</style>

我尝试将这两个I添加到所有css中,如下所示:

代码语言:javascript
复制
#play, #picture {
  color: blue;
  font: bold 3em/1.5 , sans-serif;
  text-decoration: none;
  text-transform: uppercase;
  -webkit-transition: .2s;
}

但由于某些原因,这种力量是有效的。如果你能帮上忙,谢谢!并且不要犹豫去问问题!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-07-14 11:45:05

问题是:

代码语言:javascript
复制
@media (-webkit-min-device-pixel-ratio:0) {
  #play {
    background-color: skyBlue;
    background-image: -webkit-linear-gradient(left, hotPink 0%, orange 50%, transparent 50%);
    background-position: 100% 0;
    background-size: 200% 200%;
    color: transparent;
    -webkit-transition: .1s .2s;
    -webkit-background-clip: text;
  }

@media (-webkit-min-device-pixel-ratio:0)最小比率设置为0,因此#play将始终具有此样式。因此,您可以将#picture添加到此样式中,也可以删除此样式并将#picure添加到:

代码语言:javascript
复制
#play, #picture {
  color: blue;
  font: bold 3em/1.5 , sans-serif;
  text-decoration: none;
  text-transform: uppercase;
  -webkit-transition: .2s;
}

完整的CSS:

代码语言:javascript
复制
/* Gradient*/
body {
  padding: 3em;
}
#play, #picture {
  color: blue;
  font: bold 3em/1.5 , sans-serif;
  text-decoration: none;
  text-transform: uppercase;
  -webkit-transition: .2s;
}
#play:hover {
  color: hotPink;
}
@media (-webkit-min-device-pixel-ratio:0) {
  #play:hover {
    background-position: 0 0;
    color: transparent;
    transition: .4s 0;
  }
}
#picture:hover {
    background-position: 0 0;
    color: transparent;
    transition: .4s 0;
  }
}

/*Box Border*/
html {
  box-sizing: border-box
}
*, *:before, *:after {
  box-sizing: border-box;
}

body {
  width: 80%;
  margin: 50px auto;
  color:black;
  font: 16px/1 'Catamaran', sans-serif;
  background: transparent;
}
body:after {
  content: '';
  display: flex;
  clear: both;
}


h2 {
  width: 50%;
  text-align: center;
  height: 44px;
  line-height: 24px;
  font-weight: 400;
  
}
@media screen and (max-width: 768px) {
  h2 {
    width: 100%;
  }
  h1 {
    margin-bottom: 15px;
    line-height: 30px;
  }
}

a, a > span {
  position: relative;
  color: inherit;
  text-decoration: none;
  line-height: 24px;
}
a:before, a:after, a > span:before, a > span:after {
  content: '';
  position: absolute;
  transition: transform .5s ease;
}

#hover-1, #hover-2 {
  padding: 10px;
}
#hover-1:before, #hover-1:after {
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  border-style: solid;
  border-color: black;
}
#hover-1:before {
  border-width: 2px 0 2px 0;
  transform: scaleX(0);
}
#hover-1:after {
  border-width: 0 2px 0 2px;
  transform: scaleY(0);
}
#hover-1:hover:before, #hover-1:hover:after {
  transform: scale(1, 1);
}
#hover-2:before, #hover-2:after {
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  border-style: solid;
  border-color: black;
}
#hover-2:before {
  border-width: 2px 0 2px 0;
  transform: scaleX(0);
}
#hover-2:after {
  border-width: 0 2px 0 2px;
  transform: scaleY(0);
}
#hover-2:hover:before, #hover-2:hover:after {
  transform: scale(1, 1);
}

编辑:已更改注释的CSS

票数 2
EN

Stack Overflow用户

发布于 2021-07-14 11:20:55

使用该选择器语法应该可以很好地工作,如您在此示例中所见:

代码语言:javascript
复制
#elem1, #elem2 {
  color: red;
}
代码语言:javascript
复制
<div>
  <p id="elem1">Element 1</p>
  <p id="elem2">Element 2</p>
</div>

也就是说,ids的思想是确定一个独特的元素和独特的风格。如果要在多个元素之间共享样式,则应使用class。您仍然可以将唯一的id与类组合在一起,如下所示:

代码语言:javascript
复制
.text {
  color: red;
}

#elem1 {
  font-size: 18px;
}
#elem2 {
  font-size: 14px;
}
代码语言:javascript
复制
<div>
  <p class="text" id="elem1">Element 1</p>
  <p class="text" id="elem2">Element 2</p>
</div>

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

https://stackoverflow.com/questions/68371567

复制
相关文章

相似问题

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