How do I make the style=“background-image: url()” in the html div go into css so that I could style it?

送分小仙女□ 提交于 2019-12-06 05:56:43
  • Make sure you remove the inline styles once you set it on the external css, otherwise inline will override external.

From this:

<div class="item active" style="background-image: url(images/logo.png)">

to

<div class="item active">
  • If you are unable to remove the inline styles, you can override it using !important.

Like so:

.item.active {
  background-image: url(images/logo.png) !important;
}

Snippet below (switched to a placehold.it image just for demonstration):

.item.active {
  background-image: url(https://placehold.it/200x200);
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<div id="home-slider" class="carousel slide carousel-fade" data-ride="carousel">
  <div class="carousel-inner">
    <div class="item active">
      <div class="caption">
        <h1 class="animated fadeInLeftBig">The **** Organization  </h1>
        <p class="animated fadeInRightBig">****</p>
        <a data-scroll class="btn btn-start animated fadeInUpBig" href="#rescue">****</a>
      </div>
    </div>
  </div>
</div>

Classes in HTML are separated by a space, so for that div you're trying to style what you have right now is 2 classes -- ".item" and ".active". In your CSS file or between <style></style> tags you would need to add:

.item { background-image: url(images/logo.png); }

or

.active { background-image: url(images/logo.png); }

depending on the class you're trying to target.

If you want to put both classes you need a "," among them. otherwise you are looking for a class inside a class

Try this .item, .active { background-image: url(images/logo.png); }

I believe it will work using .item.active { background-image: url(images/logo.png); } If it did not, I think maybe it is something wrong with the codes you define it. Could you please show the codes how you define the css which does not work for you?

-----edit-----

I think the problem is the relative path. The css file must be in the different directory from the html file, right? So when you define the backgournd-url in the html file, the root path of the relative path is the directory of the html file, when you define it in css file, the root path is the directory of the css file. So I advice you use the absolute path to define the image url.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!