【问题标题】:Make the text "figure x" inside paragraph to be bold使段落内的文本“figure x”变为粗体
【发布时间】:2015-09-06 04:39:40
【问题描述】:

我想将粗体格式应用于图 1、图 2、...直到 图 111。

我只能更改 HTML 和 CSS,并且需要在不使用 JavaScript 或 jQuery 的情况下执行此操作。除了在 HTML 中添加<strong> 之外还有什么办法吗?

CSS 中有::first letter,但没有::first word。我可以在 CSS 中使用 ~like %figure 吗?

<p>Figure 3 All incidences of ...</p>
<div id="imgcontainer" style="height: auto;">
  <p><img src="Images/misc/newsletters/msIssue7_monoAnti4_850x550.png"     style="margin-bottom: 10px;" /></p>
  <p>Figure 4 ...</p>
</div>
<div id="imgcontainer" style="height: auto;">
  <p><img src="Images/misc/newsletters/msIssue7_monoAnti5_350x370.png" style="float: left; width: 450px; margin: 8px 20px 8px 0px;" /></p>
  <p>Figure 5 Sequence information obtained from MS/MS analysis of capillary electrophoresis ...</p>
</div>
   <p><img src="Images/misc/newsletters/msIssue7_monoAnti6_850x350.png" style="width: 850px; height: 200px; margin: 8px 8px 8px 0px;" /></p>
   <p>Figure 6 Separation of ...</p> 

【问题讨论】:

  • 如果您希望文本“Figure x”比周围的文本更具影响力,或者您希望,例如,当您通过语音合成器运行网站时,它们的发音会更响亮,那么您将使用&lt;strong&gt;。否则,使用&lt; span&gt;s 和样式会更好。
  • 鉴于您可以更改 HTML + CSS 并且似乎不愿意添加额外的标签和样式,您可以使用类似this fiddle 的东西来自动生成文本并设置它的样式。如果您想使用这种方法,我会添加答案并进行解释。
  • @Harry 哦,这很好。我会赞成。
  • @MrLister:谢谢伙计。我添加了一个答案,因为它可能对其他用户也有帮助:)

标签: html css figure css-counter


【解决方案1】:

鉴于您能够更改页面的标记和 CSS,并且您不想添加额外的元素(如 &lt;strong&gt; 等),您可以尝试使用 CSS 计数器。专柜有很good browser support。

使用计数器,您可以自动编号数字,在内容之前添加文本“Figure X”,还可以设置样式,而无需对标记进行任何其他更改。这样做的最大好处是您不必手动编号,甚至不必担心将来对订单的任何更改。 CSS 会自动处理它。您还可以添加任何您需要的额外样式。

下面是一个示例 sn-p:

body {
  counter-reset: figure; /* Initializes the counter to 0 */
}
p {
  counter-increment: figure; /* Increment the counter everytime p tag is encountered in DOM */
}
p:before {
  content: "Figure " counter(figure)" "; /* Set Figure + Counter value as content */
  font-weight: bold;
  color: red;
}
<p>Lorem Ipsum...</p> <!-- Counter = 1, Before Content = Figure 1 -->
<p>Lorem Ipsum...</p> <!-- Counter = 2, Before Content = Figure 2 -->
<p>Lorem Ipsum...</p> <!-- Counter = 3, Before Content = Figure 3 -->
<p>Lorem Ipsum...</p> <!-- Counter = 4, Before Content = Figure 4 -->
<p>Lorem Ipsum...</p> <!-- Counter = 5, Before Content = Figure 5 -->

如果您不想对所有 &lt;p&gt; 标签进行编号,而只想对特定的 &lt;p&gt; 标签进行编号,那么您可以给它们一个唯一的类,如下面的 sn-p 所示。 (注意:即使您添加了一个额外的类,粗体文本也仅限于“图 X”文本,并且不适用于该 &lt;p&gt; 的全部内容。

body {
  counter-reset: figure;
}
p.count {
  counter-increment: figure;
}
p.count:before {
  content: "Figure " counter(figure)" ";
  font-weight: bold;
  color: red;
}
<p class='count'>Lorem Ipsum...</p>
<p>Lorem Ipsum...</p>
<p class='count'>Lorem Ipsum...</p>
<p class='count'>Lorem Ipsum...</p>
<p>Lorem Ipsum...</p>

如果像您的问题中提供的 sn-p,“图 x”文本仅适用于 div class='imgcontainer' 中的第二段,您仍然可以这样做而无需添加任何额外的类,如下面的 sn-p :

body {
  counter-reset: figure;
}
.imgcontainer > p + p {
  counter-increment: figure;
}
.imgcontainer > p + p:before {
  content: "Figure " counter(figure);
  padding-right: 1ch; /* More padding-right means more space between Figure x and text */
  font-weight: bold;
  color: red;
}
<div class='imgcontainer'>
  <p>
    <img src='http://placehold.it/100/100'>
  </p>
  <p>Some Description</p>
</div>
<div class='imgcontainer'>
  <p>
    <img src='http://placehold.it/100/100'>
  </p>
  <p>Some Description</p>
</div>
<div class='imgcontainer'>
  <p>
    <img src='http://placehold.it/100/100'>
  </p>
  <p>Some Description</p>
</div>
<div class='imgcontainer'>
  <p>
    <img src='http://placehold.it/100/100'>
  </p>
  <p>Some Description</p>
</div>

注意: 1ch 表示元素字体中“0”字符的宽度。 ch 单元对浏览器的支持较低,我仅将其用作示例。您可以使用px 或em 或任何此类单位代替ch。

【讨论】:

  • 不错。如果您不想使用连续的编号范围,您可以为新的起始编号添加一个类。例如。如果你想在中途继续使用 100 号,你可以写 &lt;p class="count from100"&gt; 和 .from100 {counter-reset:figure 99}。
【解决方案2】:

虽然您想使用 css 将 figure1 等更改为粗体,但您知道 css 只处理 decoration,而 javascript 处理 user action,现在这只能通过javascript来实现,先找出图形字,然后添加css类,css没有内置属性自动找出并改变html内部元素的装饰

【讨论】:

  • OP 根本不想使用 JavaScript。
【解决方案3】:

您可以将所需的属性放在一个类中,然后将元素添加到该类中。

您还使用了 id="imgcontainer"。 id 在 css 中是唯一的(至少应该是),当您想为多个元素提供相同的属性时,使用类而不是 id

尝试将所有图形放在一个具有 abc 类的 div 中。 现在将这些粗体属性放在那个类中。

这是您需要在 div 中添加的粗体属性。

font-weight: bold;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    相关资源
    最近更新 更多