将 css 类应用于多个元素。它适用于他们所有人。我该如何指定?
我按照该线程第一个答案中发布的说明进行操作:
如何使此悬停代码更短?我怎样才能创建一个CSS文件来实现多个按钮的设置?
但是按钮被添加到所有链接中。我想做的就是创建更多按钮,同时保持代码简短,以便将一项 css 设置应用于其他类似的按钮。请帮忙?谢谢。
I followed the instructions as posted in the first answer of this thread:
But the button was added to all the links. All I want to do is create more buttons while keeping the code short so that one piece of css settings could be applied to other similar buttons. Please help? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了访问 CSS 中的元素而不影响页面上该类型的所有元素,您可以执行以下操作。
首先,我建议您查看 CSS 选择器 以了解有关您可以通过不同的方式选择元素。
这是我的一些想法,可能对您有用。
按 ID 或类选择
您可以按 ID 选择单个元素,或按类名称选择多个项目。请记住,页面上只有一个元素可以具有相同的 ID,但许多元素可以具有相同的类。
HTML
和
CSS
ID 使用
#
符号引用,类在 CSS 中使用.
(句点)符号引用< strong>后代选择
这基本上是指根据某个元素在某种程度上是另一个元素的后代来选择该元素。最简单的形式是子选择器。请考虑以下事项:
这是引用 div(的子级)内部的所有段落标签。
另一个将 ID 选择器与子选择器相结合的示例:
这次我们对列表元素内部的所有锚元素进行样式设置,而列表元素又位于 ID 为
myNavMenu
的元素内部。In order to access elements in your CSS witout affecting all elements of that type on the page there are a couple of things you can do.
Firstly I'd advise you to look into the CSS selectors to learn more about the different ways you can select elements.
Here's a couple of the top of my head that may be useful to you.
Selecting by ID or Class
You can select a single element by its ID or multiple items by their class name. Bare in mind that only one element on a page may have the same ID but many elements can have the same class.
HTML
and
CSS
ID's are referenced using the
#
symbol and classes using the.
(period) symbol in CSSDecendent selection
This basically refers to selecting an element based on it being a decendent in some way to another element. The simplest form of this is the child selector. Consider the following:
This is referencing all paragraph tags that are inside of (children of) a div.
Another example combining the ID selector with the child selector:
This time we're styling all anchor elements that are inside list elements, which in turn are inside something with an ID of
myNavMenu
.