重叠的HTML格式标记是否总是一种不好的做法? [英] Are overlapping HTML formatting tags always a bad practice?

查看:73
本文介绍了重叠的HTML格式标记是否总是一种不好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我需要一个文本,该文本带有粗体斜体部分,它们相互重叠,就像您看到的这句话一样.

Suppose I need to have a text with a bold and an italic part that overlap each other, like you see this sentence have.

现在,我知道正确的方法是将CSS和HTML完全分开,如上面的示例所示,但这似乎有点过头了.像这样在3个跨度中编写出来,并为粗体和斜体添加一个单独的类,这意味着与第二个示例中仅使用重叠的b和i标记相比,您的用户将需要下载更多的数据:

Now, I know the proper way is to completely separate CSS and HTML as in the top example, but it just seems like overkill. Writing it out in 3 spans like that and adding a separate class for bold and italic means your user will need to download quite a bit more data than if you just use an overlapping b and i tag like in the second example:

.bold{
  font-weight: bold;
  }

.italic{
  font-style: italic;
  }

<div>I want this text to have both <span class="bold">bold</span> <span class="bold italic">and</span> <span class="italic">italic</span></div>

<div>I want this text to have both <b>bold <i>and</b> italic</i></div>

结果是完全一样的,但是第二个使用的消费者带宽使用量较小(可能只有几个字节,但全部累加了).我知道正确的方法仍然是第一个方法,但是那始终成立吗? html开发的准则是20年前制定的,当时带宽级别的消费级移动互联网还不算什么.

The result is exactly the same, but the second one has a marginally smaller consumer bandwidth use (probably only a few bytes, but that all adds up). I know the correct method is still the first one, but does that always hold true? The guidelines for html development were determined 20 years ago, when consumer-grade mobile internet with bandwidth caps was not yet a thing.

此外,正如马丁(Martin)正确地评论的那样,以这种方式进行大范围的跨度阅读不仅比看到那些B和I标签而且知道从这里到这里是大胆的,从那里到这里是斜体的",还很难读.在那里".

In addition, as Martin rightfully comments, having a large amount of spans in this manner is also trickier to read later on than just seeing those B and I tags and knowing "it's bold from here to here, and italic from there to there".

推荐答案

ib组合在一起是完全可以的,但是,您需要确保正确嵌套它们.

Combining i and b is totally fine, however, you need to make sure to nest them correctly.

错误:

I want this text to have both <b>bold <i>and</b> italic</i>

正确 :(当然,这取决于您要实现的目标)

Right: (depending on what you want to achieve, of course)

I want this text to have both <b>bold <i>and</i></b> <i>italic</i>

这篇关于重叠的HTML格式标记是否总是一种不好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆