添加tick选定的CSS

发布于 2025-02-08 08:20:05 字数 848 浏览 4 评论 0原文

.showcase-components-colorlist color {
  border: 2px solid transparent;
  margin: 1px;
  padding: 2px;
  border-radius: 50%;
  cursor: pointer;
}
<div class="showcase-components-colorlist color showcase-components-colorlist color-- active">
  <svg width="40" height="40">
    <circle cx="20" cy="20" r="19" fill="#c5145d" stroke="#fff" stroke-width="1"></circle>
  </svg>
</div>
<div class="showcase-components-colorlist color">
  <svg width="40" height="40">
    <circle cx="20" cy="20" r="19" fill="#db2586" stroke="#fff" stroke-width="1"></circle>
  </svg>
</div>

如何在上面的代码生成的一个圆圈中添加复选标记。

.showcase-components-colorlist color {
  border: 2px solid transparent;
  margin: 1px;
  padding: 2px;
  border-radius: 50%;
  cursor: pointer;
}
<div class="showcase-components-colorlist color showcase-components-colorlist color-- active">
  <svg width="40" height="40">
    <circle cx="20" cy="20" r="19" fill="#c5145d" stroke="#fff" stroke-width="1"></circle>
  </svg>
</div>
<div class="showcase-components-colorlist color">
  <svg width="40" height="40">
    <circle cx="20" cy="20" r="19" fill="#db2586" stroke="#fff" stroke-width="1"></circle>
  </svg>
</div>

How to add a check mark to one of the circles as generated by the code above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

路还长,别太狂 2025-02-15 08:20:05

一种方法是通过伪内容。您需要给出您将伪内容设置为相对容器的元素。然后,您可以创建伪内容(在这种情况下为复选标记)。定位将相对于父元素的尺寸。

.showcase-components-colorlist {
  border: 2px solid transparent;
  margin: 1px;
  padding: 2px;
  border-radius: 50%;
  cursor: pointer;
  position: relative;
}

.showcase-components-colorlist.selected::before {
  content: '✅';
  position: absolute;
  left: 11px;
  top: 9px;
}
<div class="showcase-components-colorlist color showcase-components-colorlist color-- active">
  <svg width="40" height="40"><circle cx="20" cy="20" r="19" fill="#c5145d" stroke="#fff" stroke-width="1">
    </circle>
  </svg>
</div>
<div class="showcase-components-colorlist selected">
  <svg width="40" height="40">
    <circle cx="20" cy="20" r="19" fill="#db2586" stroke="#fff" stroke-width="1"></circle>
  </svg>
</div>

One way to do this is through pseudo content. You need to give the element which you'll set the pseudo content a relative container. Then, you create your pseudo content (in this case, a check mark). The positioning will be relative to the parent element's dimensions.

.showcase-components-colorlist {
  border: 2px solid transparent;
  margin: 1px;
  padding: 2px;
  border-radius: 50%;
  cursor: pointer;
  position: relative;
}

.showcase-components-colorlist.selected::before {
  content: '✅';
  position: absolute;
  left: 11px;
  top: 9px;
}
<div class="showcase-components-colorlist color showcase-components-colorlist color-- active">
  <svg width="40" height="40"><circle cx="20" cy="20" r="19" fill="#c5145d" stroke="#fff" stroke-width="1">
    </circle>
  </svg>
</div>
<div class="showcase-components-colorlist selected">
  <svg width="40" height="40">
    <circle cx="20" cy="20" r="19" fill="#db2586" stroke="#fff" stroke-width="1"></circle>
  </svg>
</div>

笨笨の傻瓜 2025-02-15 08:20:05

创建自己的&lt; svg-option&gt;本机JavaScript Web组件。

<svg-option></svg-option>
<svg-option fill="red" selected></svg-option>
<svg-option fill="rebeccapurple" selected-fill="yellow" selected></svg-option>

创建:

“”

<script>
customElements.define("svg-option",class extends HTMLElement{
  connectedCallback(){
    this.style.display = "inline-block";
    this.innerHTML=`<svg viewBox="0 0 50 50">
                      <circle cx="50%" cy="50%" r="49%" fill="${this.getAttribute("fill") || "green"}"/>
                      <circle cx="50%" cy="50%" r="30%" fill="${this.getAttribute("selected-fill") || "beige"}" visibility="hidden"/>
                    </svg>`;
    this.select();
    this.onclick = (evt) => this.toggle();
  }
  select(state = this.hasAttribute("selected")) {
    this.querySelector("circle:nth-child(2)").setAttribute("visibility" , state ? "visible" :"hidden");
  }
  toggle(){
    this.select( this.toggleAttribute("selected") );
  }
})
</script>

<style>
  svg-option {
    --svg-option-size:180px;
    width: var(--svg-option-size);
    height: var(--svg-option-size);
    cursor: pointer;
  }
  svg-option[selected]{
    background:lightgreen;
  }
  svg-option:not([selected]){
    background:pink;
  }
</style>

<svg-option></svg-option>
<svg-option fill="red" selected></svg-option>
<svg-option fill="rebeccapurple" selected-fill="yellow" selected></svg-option>

Create your own <svg-option> Native JavaScript Web Component.

<svg-option></svg-option>
<svg-option fill="red" selected></svg-option>
<svg-option fill="rebeccapurple" selected-fill="yellow" selected></svg-option>

creates:

<script>
customElements.define("svg-option",class extends HTMLElement{
  connectedCallback(){
    this.style.display = "inline-block";
    this.innerHTML=`<svg viewBox="0 0 50 50">
                      <circle cx="50%" cy="50%" r="49%" fill="${this.getAttribute("fill") || "green"}"/>
                      <circle cx="50%" cy="50%" r="30%" fill="${this.getAttribute("selected-fill") || "beige"}" visibility="hidden"/>
                    </svg>`;
    this.select();
    this.onclick = (evt) => this.toggle();
  }
  select(state = this.hasAttribute("selected")) {
    this.querySelector("circle:nth-child(2)").setAttribute("visibility" , state ? "visible" :"hidden");
  }
  toggle(){
    this.select( this.toggleAttribute("selected") );
  }
})
</script>

<style>
  svg-option {
    --svg-option-size:180px;
    width: var(--svg-option-size);
    height: var(--svg-option-size);
    cursor: pointer;
  }
  svg-option[selected]{
    background:lightgreen;
  }
  svg-option:not([selected]){
    background:pink;
  }
</style>

<svg-option></svg-option>
<svg-option fill="red" selected></svg-option>
<svg-option fill="rebeccapurple" selected-fill="yellow" selected></svg-option>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文