html - 如何从 HTML 而不是 CSS 调用图像 URL?

标签 html css less

我正在为我公司的网站制作 CMS。我们有一个要求。我们需要设计一个页面的标题。图片需要从 CMS 更改。为了实现这一点,我需要从 HTML 而不是 CSS 调用图像。

下面是我的代码:

html, body {
  width: 100%;
  height: 100%;
}

.bg-img {
  position: relative;
  width: 100%;
  height: 100%;
  background: url('http://unsplash.it/1200x800') center center no-repeat;
  background-size: cover;
  
  &:before {
    content: '';
	position: absolute;
	top: 0;
	right: 0;
	bottom: 0;
	left: 0;
	background-image: linear-gradient(to bottom right,#002f4b,#dc4225);
	opacity: .6; 
  }
}
<div class="bg-img"></div>

  1. 需要从html调用图片URL
  2. 不知道为什么代码在 Stack Overflow 中不工作,但在我的 codepen here 中工作

最佳答案

你必须使用 style 属性:

<div class="bg-img" style="background-image: url('http://unsplash.it/1200x800');"></div>

关于html - 如何从 HTML 而不是 CSS 调用图像 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50386465/

相关文章:

javascript - .innerHTML 编辑 HTML 但不显示

html - 逗号分隔的媒体查询列表不起作用

javascript - 检查对象、函数或元素何时准备就绪

html - 为什么我的 ASP Core MVC Razor 表单显示的输入框没有边距?

css - 仅将 CSS 样式应用于某些元素

html - 针对 Windows Phone 诺基亚 Lumia 的 CSS 媒体查询

html - Chrome (Mac/Windows) 中的第一个子列表项未对齐

jquery - 动态创建的 div 在 IE8 的 DOM 中没有 CSS 样式

css - 没有 Javascript 的 Twitter Bootstrap 和响应式断点

css - 使用 Grunt grunt-contrib-less) 在 Visual Studio 2013 中编译 Bootstrap 3.1 LESS