简体   繁体   English

我应该如何处理CSS继承难题?

[英]How should I handle this CSS Inheritance conundrum?

I have a class of images called glyphs that appear through out my site. 我有一类出现在我的网站上的字形图像。 Once upon a time they were all called just called glyph . 很久以前,它们都被称为glyph They used to only appear in one box. 它们曾经只出现在一个框中。 That box was a specific size and I used a javascript method to make sure the text always fit and the glyphs were always about the same height as the rest of the text. 该框是一个特定的大小,我使用了一种javascript方法来确保文本始终适合并且字形始终与文本其余部分的高度相同。 This was easy to do and the glyphs started off with a default due to my style sheet. 这很容易做到,由于我的样式表,字形以默认值开始。

Now, I've decided to include multiple boxes per page with variable amounts of text. 现在,我决定每页包含多个带有可变文本量的框。 Each box gets sized independently. 每个盒子都有独立的尺寸。 I've tried delaying the sizing routine, but this is highly reliant on a user's connection speed. 我尝试延迟调整大小例程,但这高度依赖于用户的连接速度。 If I don't run the sizing routine then things don't look right at all, so I'd at least like to pick a default starting size for everything. 如果我不运行调整大小的例程,那么看起来根本就不合适,所以我至少想为所有内容选择默认的起始大小。 Of course, you can't size stuff with javascript until it has been loaded. 当然,在加载之前,您无法使用javascript调整大小。 Snake eats tail. 蛇吃尾巴。

So basically, now that I have more than one box, each glyph gets a class glyph:1, glyph:2, etc. This number can go as large as the number of user submitted items on my site. 因此,基本上,既然我拥有多个框,每个字形都具有一个字形:1,字形:2等。此数字可以和我网站上用户提交的项目数一样大。 How does CSS handle this? CSS如何处理呢? These items basically need two class names as far as I can see. 据我所知,这些项目基本上需要两个类名。 But I'm pretty sure that's not allowed. 但是我很确定这是不允许的。

What I need: Set all images classes that begin with "glyph:" to 1em 我需要什么:将所有以“字形:”开头的图像类设置为1em

This doesn't exist, right? 这个不存在吧? glyph:* 字形:*

Also, : is probably bad to use in a css class name, huh? 另外,:可能在CSS类名中不好用,是吗?

Don't use the : symbol. 不要使用:符号。 you can have more than one class for an element so do it like so 您可以为一个元素拥有多个类,因此可以这样做

<div class="glyph glyph-1">Foo</div>
<div class="glyph glyph-2">Foo</div>
<div class="glyph glyph-3">Foo</div>
<div class="glyph glyph-4">Foo</div>

As others have said, you shouldn't use the colon symbol in class names. 正如其他人所说的,您不应在类名中使用冒号。 Hyphens and dashes are the only punctuation that is sensible to use. 连字符和破折号是唯一明智使用的标点符号。

It is possible to use colons in classes and escape them in your CSS code, but it gets really messy and is unnecessary. 可以在类中使用冒号,然后在CSS代码中将其转义,但这确实很麻烦,而且是不必要的。

Secondly (and this is where I'll go further than the other answers), if you're generating unique class names for your elements then you're probably doing something wrong. 其次(这是我将比其他答案更进一步的地方),如果您为元素生成唯一的类名,那么您可能做错了什么。

The id attribute is there to give your elements a unique name; 此处的id属性为您的元素赋予唯一的名称; the class is intended to allow you to apply the same class (or classes) to multiple elements, thus allowing you to style all those elements the same. class旨在允许您将相同的一个或多个类应用于多个元素,从而允许您对所有这些元素设置相同的样式。 You probably know this already; 您可能已经知道这一点; I guess what I'm saying is that it sounds like you should be using id rather than class . 我想我想说的是,听起来您应该使用id而不是class

Next: You say you're pretty sure two class names is not allowed, but in fact it is allowed. 下一步:您说您很确定两个类名是不允许的,但实际上是允许的。 It is perfectly permissible to have class="glyph bob" and your element will pick up styles from both class glyph and class bob . 具有class="glyph bob"是完全允许的,并且您的元素将从class glyph和class bob获取样式。 You can have as many classes as you like. 您可以根据需要选择任意多个类。 I would still say, however, that if you want to give them unique names, it should be an ID. 但是,我仍然要说的是,如果您要给它们指定唯一的名称,则应使用ID。

You also ask for CSS syntax to set all the glyph* classes. 您还要求CSS语法设置所有glyph*类。 Again, you're wrong: this syntax does exist, via the extended attr syntax : 再次,您错了:通过扩展attr语法 ,此语法确实存在:

[class^="glyph"] {
    /*styles here for classes beginning with 'glyph'
}

You talk about delaying the sizing routine, and the problems that introduces. 您谈论的是延迟调整大小例程,以及由此带来的问题。 One solution to this could be to have the items hidden entirely until the browser has finished working out how they should look. 一种解决方案是将项目完全隐藏,直到浏览器完成外观设计为止。 You could even fade them into view or something to make it look like it was a deliberate effect. 您甚至可以淡化它们到视图中或使其看起来像是有意的效果。

But after all that, I'm left wondering why you're putting glyphs in images and sizing them independently? 但是毕竟,我只想知道为什么要在图像中放置字形并单独调整其大小? It all sounds a bit odd; 听起来有点奇怪。 your description in the question leaves me wondering what you're trying to achieve. 您在问题中的描述使我想知道您要达到的目标。

Have you considered using scalable graphics (SVG/VML) or a custom font for your glyphs, rather than images? 您是否考虑过将可缩放图形(SVG / VML)或自定义字体用于字形,而不是图像? If you used a custom font, you could simply specify the font size as normal, and let the browser work it all out. 如果您使用自定义字体,则只需指定正常的字体大小,然后让浏览器即可使用。

Hope some of that was helpful. 希望其中的一些帮助。

CSS class names can't contain the : symbol. CSS类名称不能包含:符号。 Everything after the : will be interpreted as a pseudo-class (like :hover ) and will not be parsed properly. :之后的所有内容都将被解释为伪类(例如:hover ),并且将无法正确解析。

As for your classes, why do they all have to be unique? 至于您的课程,为什么它们都必须是唯一的? Classes are made to select multiple elements at once, so you can just do this: 通过创建类可以一次选择多个元素,因此您可以执行以下操作:

<div class="glyph">Foo</div>
<div class="glyph">Foo</div>
<div class="glyph">Foo</div>
<div class="glyph">Foo</div>

And select them all with one selector: 并使用一个选择器将它们全部选中:

.glyph {
  color: red;
}

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

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