简体   繁体   English

如何为背景图片CSS设置样式

[英]How To Style background-image CSS

Hello I am trying to figure how I can style an image that I have loaded with CSS, I did this by using the code: 您好,我试图弄清楚如何设置已用CSS加载的图像的样式,我是通过使用以下代码来做到这一点的:

.news1 {
content: url("https://i.vimeocdn.com/portrait/58832_300x300");
}

I then tried to style this by doing: 然后,我尝试通过以下方式设置样式:

.news1 img:hover {
 -moz-transform: scale(1.1);
 -webkit-transform: scale(1.1);
  transform: scale(1.1);
}

I then also tried: 然后,我也尝试了:

 .news1 content:hover {
     -moz-transform: scale(1.1);
     -webkit-transform: scale(1.1);
      transform: scale(1.1);
    }

If you are trying to set the background image in CSS you should set the image use the background property. 如果尝试在CSS中设置背景图片,则应使用background属性设置图片。

 .news1 { background-image: url("https://i.vimeocdn.com/portrait/58832_300x300"); background-repeat: no-repeat; background-position: 50% 50%; background-size: cover; height:300px; width: 300px; transition: all .4s ease; } .news1:hover { transform: scale(1.1); } 
 <div class="news1"></div> 

Background images and content images are not separate elements, they are styling of the element they're declared on. 背景图像和内容图像不是单独的元素,它们是声明它们的元素的样式。

For example, rather than using: 例如,而不是使用:

.news1 img:hover {
  /* ... */
}

You would use: 您将使用:

.news1:hover {
  /* ... */
}

If you actually want to style the image separately from the element, the image will have to have it's own element. 如果您实际上想与元素分开设置图像样式,则图像将必须具有自己的元素。 This could be in the form of an inner img element, or by placing the image as the background of some other inner element. 这可以采用内部img元素的形式,也可以将图像放置为其他内部元素的背景。

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

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