div 和 span 中的 CSS 类 IMG

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19360423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 00:42:28  来源:igfitidea点击:

CSS class IMG inside div and span

cssimagecss-selectors

提问by Pawe? Domański

Hi on beginning I have code like this:

嗨,一开始我有这样的代码:

<div class="123_test" tabindex="0" role="button">

    <span class="">
        <img class="" alt="" src="picturetopic"></img>        
    </span>
</div>

How can I style this imgwith 123_testclass?

我怎样才能img123_test班级来设计这个?

回答by Mr. Alien

First of all you cannot start a class name using a digit, I assume that it's generating from somewhere as the classes are empty, but if you can, than consider changing it, like

首先,您不能使用数字开头的类名,我假设它是从某个地方生成的,因为类是空的,但是如果可以,请考虑更改它,例如

class="name_123_test"

And use the selector below like

并使用下面的选择器

.name_123_test > span img {
   /* Styles goes here */
}

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".

在 CSS 中,标识符(包括选择器中的元素名称、类和 ID)只能包含字符 [a-zA-Z0-9] 和 ISO 10646 字符 U+00A0 及更高,加上连字符 (-) 和下划线 ( _); 它们不能以一个数字、两个连字符或一个连字符后跟一个数字开头。标识符还可以包含转义字符和任何 ISO 10646 字符作为数字代码(参见下一项)。例如,标识符“B&W?” 可以写成“B\&W\?” 或“B\26 W\3F”。

W3C Reference

W3C 参考

回答by David

Why are your classattributes empty?

为什么你的class属性是空的?

If you want to apply the 123_testclass to the imgtag, just add it in the classattribute:

如果要将123_test类应用于img标签,只需将其添加到class属性中:

<img class="123_test" alt="" src="picturetopic"></img>

Or do you mean that you want to use the ancestor divas a style guide for the img? In your CSS, you can reference the imgas a descendant of the 123_testclass:

或者你的意思是你想使用祖先div作为 的风格指南img?在您的 CSS 中,您可以将 引用img123_test类的后代:

.123_test img
{
    // css rules
}

The space between the two identifiers means that the latter is a descendant of the former.

两个标识符之间的空格表示后者是前者的后代。

回答by roemel

You can't name a class beginning with a numer. So change your class name to class="test_123"or something that does not begin with a number.

你不能命名一个以数字开头的类。因此,将您的班级名称更改为class="test_123"不以数字开头的名称。

And now you can style your img in this way:

现在你可以用这种方式设置你的 img 样式:

.test_123 // here you put your class name after the "."
{
    // style your image here
}

回答by Roralee

In addition, use better HTML markup for the functionality:

此外,为功能使用更好的 HTML 标记:

<button type="button" class="test_123" tabindex="0">    
    <span>
        <img alt="" src="picturetopic" />       
    </span>
</button>

Buttons of type="button" can contain text or an image. If the image contains text, it would be better to use css to create the graphical effect for compliance w/ accessibility guidelines and display across multiple devices.

type="button" 的按钮可以包含文本或图像。如果图像包含文本,最好使用 css 来创建符合可访问性指南的图形效果并在多个设备上显示。