简体   繁体   English

使用CSS和javascript用一些具有不同样式的单词制作段落的正确方法是什么

[英]What is the proper way to make a paragraph with some words having different styles using CSS and javascript

I've a paragraph, with several long lines of texts. 我有一段,有几行长的文字。 I need to make some words into different style. 我需要把一些词变成不同的风格。 What would be the proper way to do it without creating too many tags? 不创建太多标签的正确方法是什么?

How about when using react-intl format message? 使用react-intl格式的消息怎么样?

<div className="main"> 
Select the Gear icon, and then choose Users > Manage users
</div>


.main {
font-size:14px;
}

I want to make words Gear, Users and Manage users, different style. 我想用不同的样式来表示Gear,Users和Manage users。

You can use one of the built-in tags to achieve this. 您可以使用内置标签之一来实现此目的。

  • <b> - Bold text <b> -粗体
  • <strong> - Important text <strong> -重要文本
  • <i> - Italic text <i> -斜体文字
  • <em> - Emphasized text <em> -强调文字
  • <mark> - Marked text <mark> -标记文字
  • <small> - Small text <small> -小文本
  • <del> - Deleted text <del> -删除的文字
  • <ins> - Inserted text <ins> -插入的文本
  • <sub> - Subscript text <sub> -下标文字
  • <sup> - Superscript text <sup> -上标文本

 .main { font-size:14px; } 
 <div className="main"> Select the <b>Gear</b> icon, and then choose <b><i>Users</i></b> > <em><ins><sub>Manage users</sub></ins></em> </div> 

Or you can use the span tag to create inline elements in your text and then you can style them as you want. 或者,可以使用span标签在文本中创建内联元素,然后可以根据需要设置样式。

 .main { font-size:14px; } #gear { font-size: 1.1rem; color: red; font-weight: bold; } #users { font-size: 1.3rem; color: blue; text-decoration: underline; } #manage-users { font-size: 0.9rem; color: gray; text-decoration: overline; } 
 <div className="main"> Select the <span id="gear">Gear</span> icon, and then choose <span id="users">Users</span> > <span id="manage-users">Manage users</span> </div> 

Update 更新资料

The code below is an example how to style each word differently when you use react-intl FormattedMessage . 下面的代码是一个示例,当您使用react-intl FormattedMessage时,如何为每个单词设置不同的样式。 StackBlitz link. StackBlitz链接。

render() {
    const style = {
      color: 'red',
      fontWeight: 'bold'
    }
    return (
      <div>
        <p>
          <FormattedMessage
              id="userHint"
              defaultMessage={`Select the {gear} icon, and then choose {users} > {manageUsers}`}
              values={{
                gear: <b>Gear</b>, 
                users: <i>Users</i>, 
                manageUsers: <span style={style}>Manage users</span>
              }}
          />
        </p>
      </div>
    );
  }

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

相关问题 使用 Javascript 替换段落中的单词 - Replace words in a paragraph using Javascript 使用Javascript数组将键和值与某个变量匹配的正确方法是什么? - What is the proper way to match key and value to some variable using Javascript array? 是否有比使用Javascript更好的方法来在2种不同样式之间进行切换 - Is there a better way to switch between 2 different styles than this using Javascript 如果段落元素的CSS为空,则使它们不同吗? - Make CSS of paragraph elements different if they are empty? 如何使用Jquery或Javascript知道一个段落包含多少个单词? 有什么作用? - How to know how many words a paragraph contains using Jquery or Javascript? What function? 使用ClientDependency Framework动态添加CSS或JS的正确方法是什么? - What is the proper way to dynamically add CSS or JS using ClientDependency Framework? 使用Jquery来停止css动画。 这样做的正确方法是什么? - Using Jquery to stop css animation. What is the proper way to do this? 尝试使用CSS / Javascript实现文本滑块,但遇到一些问题 - Trying to implement a text slider using CSS/Javascript, having some problems 使用Javascript的不同地图样式 - Different Map Styles using Javascript 将HTML添加到容器的末尾:使用JavaScript执行此操作的正确方法是什么? - Adding HTML to the end of a container: What is the proper way to do it using JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM