简体   繁体   English

如何在不添加更多类的情况下将背景图像添加到 Div 内的 html?

[英]how can I add a background image to my html inside a Div without adding more classes?

I'm working on my project which involves working with a map and adding locations inside pop-ups.我正在处理我的项目,该项目涉及使用 map 并在弹出窗口中添加位置。 In those locations, I added the name of the location, an image, and the address.在这些位置中,我添加了位置名称、图像和地址。 Unfortunately, the image doesn't appear.不幸的是,图像没有出现。 I've tried several source links but it either doesn't show or it shows a very small image that appears to have been cut.我已经尝试了几个源链接,但它要么不显示,要么显示一个似乎已被剪切的非常小的图像。 Here's what I've done so far:这是我到目前为止所做的:

//javascript (This is just one of the locations but I have a lot more) //javascript(这只是其中一个位置,但我还有更多)

var restaurantMarkers = [
      {
        coords: { lat: 59.9178541, lng: 10.7339675 },
        content:
          '<div class="marker"><h6>Den Glade Gris</h6><img src="../images/restaurant1.jpg" alt="1" /><br><p>St. Olavs Gate 33</p></div>',
      },

//CSS (if I add backgroung-image to the css the image appears, but that means I would have to create several classes to add a background image to my locations and I have around 60 of them. //CSS(如果我将背景图像添加到 css 中,图像就会出现,但这意味着我必须创建几个类来为我的位置添加背景图像,并且我有大约 60 个。

.marker{
    
    width: 120px;
    height: 110px;
}

The routing in separate JS files start from the location where the JS files was "included" from.单独 JS 文件中的路由从 JS 文件“包含”的位置开始。 So if your file structure looks like:因此,如果您的文件结构如下所示:

root/
├── index.html
└── assets/
    └── images/
      └── restaurant1.jpg
    └── js/
      └── googlemaps.js

And you're javascript file is linked in index.html , you need to start your routing from there (eg, background-image: url(assets/images/restaurant1.jpg) ).而你的 javascript 文件链接在index.html中,你需要从那里开始你的路由(例如background-image: url(assets/images/restaurant1.jpg) )。

Or if you're file structure looks like:或者,如果您的文件结构如下所示:

root/
├── index.html
└── views/
    └── map.html
└── assets/
    └── images/
      └── images.jpg
    └── js/
      └── googlemaps.js

And you're javascript file is linked in view/map.html , you need to start your routing from there (eg, background-image: url(../assets/images/restaurant1.jpg) ).而你的 javascript 文件链接在view/map.html中,你需要从那里开始你的路由(例如, background-image: url(../assets/images/restaurant1.jpg) )。

Keep in mind, these are examples, your file structure is probably different.请记住,这些只是示例,您的文件结构可能不同。

ok so this is what I did好的,这就是我所做的

background-image: url(assets/images/restaurant1.jpg);

with this the image appears, the problem now is that it's repeating so I tried this:有了这个图像出现了,现在的问题是它正在重复,所以我尝试了这个:

background-image: url(assets/images/restaurant1.jpg) no-repeat;

but when I add "no-repeat" the image is gone again但是当我添加“不重复”时,图像又消失了

I managed to figure it out!我设法弄清楚了! Thanks everyone!感谢大家!

here's what I did:这就是我所做的:

<div class="marker" style="background: url(assets/images/restaurant1.jpg) no-repeat center">

暂无
暂无

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

相关问题 如何在HTML页面的div中为文本内容赋予背景色。 - How can I give background color to the text content inside a div in my html page. 如何在CSS中添加顶部横幅而不触摸或添加任何HTML div? - How can I add a top banner in CSS without touching or adding any HTML div? 我可以通过类将图像添加到div吗? - Can i add image into div with classes? 如何在不添加更多 html 的情况下单独切换淘汰赛 foreach div? - How do I toggle a knockout foreach div individually without adding more html? 如何在父div中使用Jquery而不影响子div的情况下淡入和淡出背景图像? - How can I fade are background image in and out using Jquery in a parent div without having it affect the child div? 通过 javascript,如何将一个 div 添加到我的 html 脚本中,其中包含另一个 div? - Through javascript, how do I add a div to my html script with another div inside of it? 在jQuery中,如何在不使用JavaScript对图像的URL进行硬编码的情况下设置元素的背景图像? - In jquery, how can I set the background-image of an element without hardcoding the url of the image in my javascript? 如何为我的页面提供不仅显示背景图像,而且填充页面的 rest 的 div? - How can I provide my page with a div that not only displays background image, but fills the rest of the page? 我如何在 React JS 中更改 div 中的 background_image - how can i change my background_image in a div in react js 如何在 div 上添加图像效果 - how can i add image effect on div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM