简体   繁体   English

与内容无关的图片。 背景图片样式属性与img标签

[英]Not content-relevant image. Background image style Attribute vs. img tag

I am currently creating a template for a CMS, which will probably have some images which are not relevant for the content. 我目前正在为CMS创建模板,其中可能会有一些与内容无关的图像。 They are going to be displayed behind the text. 它们将显示在文本后面。 Since they will be uploaded by the user i can't insert them via CSS as background-image. 由于它们将由用户上传,因此我无法通过CSS将它们作为背景图像插入。 So the other two possibilities would be too either add them as an image and the place them in the background via CSS or insert them as a background-image by adding a style attribute to the parent div. 因此,其他两种可能性也可能是将它们添加为图像,然后通过CSS放置在背景中,或者通过向父div添加style属性将它们插入为背景图像。 Both options aren't really perfect but i was wondering which of those two options would be the "more" correct one. 两种选择都不是很完美,但是我想知道这两种选择中哪一种是“更”正确的选择。

From the HTML spec : HTML规范

4.7.1.1.9 A purely decorative image that doesn't add any information 4.7.1.1.9不添加任何信息的纯装饰性图像

Purely decorative images are visual enhancements, decorations or embellishments that provide no function or information beyond aesthetics to users who can view the images. 纯粹的装饰性图像是视觉上的增强,装饰或点缀,向可以查看图像的用户提供了美学之外的任何功能或信息。

Mark up purely decorative images so they can be ignored by assistive technology by using an empty alt attribute ( alt="" ). 标记纯装饰性图像,以便辅助技术通过使用空的alt属性( alt="" )将其忽略。 While it is not unacceptable to include decorative images inline, it is recommended if they are purely decorative to include the image using CSS. 虽然内嵌包含装饰性图像并非不可接受,但建议使用纯CSS来包含装饰性图像。

So either way is acceptable, but using CSS is preferable, especially if you don't wish to pollute your markup with decorative images. 因此,任何一种方法都是可以接受的,但是使用CSS是更可取的,特别是如果您不希望用装饰性图像污染标记。

Using an inline style attribute is an acceptable alternative for when you need to style individual elements differently but you don't want to generate CSS rules for them. 当需要为各个元素设置不同的样式,但又不想为其生成CSS规则时,可以使用内联样式属性。 The most purist approach would be to specify the image URL in a custom data attribute instead of an inline style attribute, but you won't be able to take the value of that attribute and apply it to the background-image property using CSS since no browser supports that ability yet , so inline styles are your next best alternative. 最纯粹的方法是在自定义数据属性(而不是内联样式属性)中指定图像URL,但是您将无法使用该属性的值并将其应用于CSS的background-image属性,因为没有浏览器还支持该功能 ,因此内联样式是您的下一个最佳选择。

It's a complicated thing like both answer is semi-correct. 这很复杂,因为两个答案都是半正确的。 you can use both way. 您可以同时使用两种方式。

for step 1 对于步骤1

if you wan to use it as a background image then you have to give rest of css property to that div in css. 如果要使用它作为背景图像,则必须将其余的CSS属性赋予CSS中的div。 and the image-URL style you have to give in line. 以及您必须直接输入的图片网址样式。

for step 2 对于步骤2

for this step you have to use position tag to define their places. 对于此步骤,您必须使用位置标记来定义其位置。 as you want to keep image in background then below is the easiest way to do this by postion tag. 由于您想将图片保留在背景中,因此以下是通过位置标记执行此操作的最简单方法。

HTML 的HTML

<div class="parent-block">
    <div class="child-image-block">
        <img src="xyz.png"/>
    </div>
    <div class="child-content-block">
        " your content goes here"
    </div>
</div>

CSS 的CSS

.parent-block { position: relative;}
.child-image-block {position: absolute; z-index:1;}
.child-content-block {position: relative;  z-index:2;}

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

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