可以使用HTML元素的ID或类作为CSS的标识符吗?

huangapple go评论63阅读模式
英文:

Can you use an ID or class of a HTML element as an identifier for CSS?

问题

我正在学习更多编程语言的路上。自从HTML 4.01以来,我从未练习过HTML。但这更多是与CSS有关的事情。

我以为,因为我看到H1和P1是有效的,所以我尝试了button1。感到愚蠢,然后考虑使用ID作为标识符。我想要在左边制作一个按钮,另一个在右边。

在w3schools和其他网站上找不到任何信息。感觉我在表达一切的时候都错了。

英文:

I'm on my road trip roundabout of learning more coding languages. Never practiced HTML since HTML 4.01. But this is more of a CSS thing.

I was expecting, since I see H1 and P1 being a thing, so I try button1. Feel stupid, then think about using an ID as an identifier. I want to make one button on the left and the other on the right side.

I can't find anything on w3schools, and other sites. I feel like I'm wording everything wrong.

答案1

得分: 2

是的,可以。确保类名或ID已设置为您的元素:

<div class="my-div-class" id="my-div-id"></div>

然后在样式表中,您可以使用“选择器”来访问它们。类使用前置的.选择,而ID使用前置的#选择:

.my-div-class {
  /* 一些样式... */
}

#my-div-id {
  /* 一些样式... */
}

您可以在W3Schools这里查看所有的CSS选择器:https://www.w3schools.com/cssref/css_selectors.php

或者在MDN这里查看:https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors

英文:

Yes, you can. Ensure the class name or ID are set to your elements:

<div class="my-div-class" id="my-div-id"></div>

And then in your stylesheet, you can access these using 'selectors'. Classes are selected with a preceding ., and IDs with a preceding #:

.my-div-class {
  /* Some styles... */
}

#my-div-id {
  /* Some styles... */
}

You can see all CSS selectors here from W3Schools: https://www.w3schools.com/cssref/css_selectors.php

Or here from MDN: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors

huangapple
  • 本文由 发表于 2023年2月18日 04:42:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75489092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定