CSS 调用和声明类

标签 css css-selectors

好的,所以我对在 css 中声明和调用一个类有点困惑。当我创建一个类时,它使用“。”但是当我调用它时它不会,例如。

.smaller-image {
    width: 100px;
}

<a href="#"><img src="someimageurl.com" class="smaller-image" alt="some image 
text."></a>

最佳答案

CSS 使用它们的 classid 或标签名称(例如 body)将样式应用于 HTML 元素。

在编写应用于 的 CSS 规则时,您可以在类名前加上 . 前缀。

在编写应用于 id 的 CSS 规则时,您可以在 id 名称前加上 # 前缀。请注意,一个 id 只能由页面上的一个元素使用,而一个类可以随意使用。

例子:

div {} /* This is applied to all div elements */

.button {} /* This is applied to all elements with the `button` class */

#header {} /* This is applied to the element with the `header` id */

a.link {} /* This combines both the `a` tag and the `link` class */

关于CSS 调用和声明类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52681767/

相关文章:

jquery - 与页面分开的 Canvas 外滚动

html页面布局,内容div高度填满所有剩余空间

javascript - 动画速度在 safari/chrome iphone 中没有改变

html - 具有固定宽度和内联元素的 Div

css - 我可以将 Firefox 和 Webkit 样式选择器分组吗?如果不是,为什么?

html - 我怎样才能让我的页脚粘在底部?

css - 在 CSS 中使用通配符属性选择器

html - 为什么我的导航栏链接的颜色不会改变

css - 通用选择器的用法和区别

html - 带有破折号下划线的命名类和 id 的 CSS 趋势?