【问题标题】:How to edit two IDs?如何编辑两个ID?
【发布时间】:2021-07-14 12:35:17
【问题描述】:

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

这是我的 HTML

<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

<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>

我尝试将这两个 ID 添加到所有的 css 中,

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

但由于某种原因,这种方法起作用了。 如果你能帮忙,谢谢! 不要犹豫,问问题!

【问题讨论】:

  • #play, #picture {} 这个代码块应该可以工作...如果不是它怎么不工作?
  • How to select multiple ids in CSS? 类似,但没有说明该块可能不起作用的原因。

标签: html css


【解决方案1】:

问题是这样的:

@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 添加到:

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

完整的 CSS:

/* 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 以供评论

【讨论】:

  • 盒子边框css怎么样。我怎样才能在那里添加两个ID?因为当我这样做时,一切都搞砸了
  • 它有点乱,但我会重复 #hover-2 的过程
【解决方案2】:

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

#elem1, #elem2 {
  color: red;
}
<div>
  <p id="elem1">Element 1</p>
  <p id="elem2">Element 2</p>
</div>

也就是说,ids 的想法是识别独特的元素和独特的风格。如果你想在多个元素之间共享样式,你应该使用class。您仍然可以将唯一的 id 与类结合起来,如下所示:

.text {
  color: red;
}

#elem1 {
  font-size: 18px;
}
#elem2 {
  font-size: 14px;
}
<div>
  <p class="text" id="elem1">Element 1</p>
  <p class="text" id="elem2">Element 2</p>
</div>

【讨论】:

  • 我很想上课,但使用网站构建器我使用 (ClickFunnels) 更容易获取 id 而不是类名。当我确实使用该选择器语法时,它会搞砸一切。
猜你喜欢
  • 2017-07-12
  • 1970-01-01
  • 2020-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多