简体   繁体   English

在伪元素的内容中添加定位CSS规则

[英]Adding positioning css rules to content of pseudo element

So I have a button that has a pseudo element of :before. 所以我有一个按钮,其具有:before的伪元素。 If I set the content of it to an image using the content:url(); 如果我使用content:url();将其内容设置为图像 , how can I center that image? ,如何使该图像居中? The goal is for it to be centered in the middle of the white part of the switch(the pseudo element). 目的是使其居中在开关的白色部分(伪元素)的中间。 I believe it could be done with JavaScript if possible at all. 我相信,如果可以的话,可以使用JavaScript来完成。 Thanks alot. 非常感谢。

https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_switch https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_switch

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
  content:url(image link);
}

I looked at the w3 schools page and modified the bit of css below to make that happen: 我查看了w3学校页面,并修改了下面的CSS使其实现:

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-image: url("https://st2.depositphotos.com/4326917/9932/v/950/depositphotos_99325768-stock-illustration-black-vector-icon-isolated-on.jpg");
  background-size:cover;
  -webkit-transition: .4s;
  transition: .4s;
}

(I just added some random icon that I found on google) (我刚刚添加了一些我在Google上找到的随机图标)

What it looks like 看起来像什么

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM