简体   繁体   English

JS & CSS: 同句行单个词的字体颜色和粗细

[英]JS & CSS: Font-color and thickness of a single word in the same sentence line

I have the following problem:我有以下问题:

I want to change the color and thickness of a single word (->(Admin)) in the same sentence line.我想更改同一句子行中单个单词 (->(Admin)) 的颜色和粗细。 My code does do what it should, but unfortunately it is totally unformated now and does have line skips.我的代码确实做了它应该做的,但不幸的是它现在完全没有格式化并且确实有跳行。 I used JavaScript and CSS, but I am a totally beginner in this field.我用过 JavaScript 和 CSS,但我是这个领域的初学者。 Probably there is much redundancy in the code.代码中可能有很多冗余。

I used "span", h1 -> child (1)/(2) and so on in the.js file and corresponding entries in the.css file:我在 .js 文件和 .css 文件中使用了“span”、h1 -> child (1)/(2) 等:

.js Datei: .js 日期:

<div id="user-info">
      <div><h1><span>{this.props.dataHandler["userData"]["firstname"]} {this.props.dataHandler["userData"]["lastname"]}</span><span> (Admin)</span></h1></div>
      <div><h2><span>Mitarbeiternummer: {this.props.dataHandler["userData"]["userID"]}</span></h2></div>
      <div><a href="/#"onClick={this.logout.bind(this)}><u>Logout</u></a></div>
      </div>

.css Datei: .css 达泰:

#user-info {
    color: white;
    width: 15%;
    margin: 0.3vh 0 0 0;
  }

#user-info h1 :nth-child(1) {
    color: white; 
    font-size: 1.5vh;
    font-weight: normal;
    width: 15%;
    margin: 0 0 0 0; 
     
  }
    
#user-info h1 :nth-child(2) {
    color: red;
    font-size: 1.5vh;
    font-weight: normal;
    width: 15%;
    margin: 0.3vh 0 0 0; 
  }

#user-info h2 {
    color: white;
    font-size: 1.5vh;
    font-weight: normal;
    width: 15%;
    margin: 0.3vh 0 0 0; 
  }

#user-info div{
    font-size: 1.5vh;
  }

It's happening because all of your elements are set to just 15% of width.发生这种情况是因为您的所有元素都设置为宽度的 15%。

You can see how narrow #user-info is.您可以看到#user-info有多窄。

在此处输入图像描述

So, your solution is to remove width: 15% from #user-info .因此,您的解决方案是从#user-info中删除width: 15%

 body { background: black; } #user-info { color: white; margin: 0.3vh 0 0 0; } #user-info h1:nth-child(1) { color: white; font-size: 1.5vh; font-weight: normal; width: 15%; margin: 0 0 0 0; } #user-info h1:nth-child(2) { color: red; font-size: 1.5vh; font-weight: normal; width: 15%; margin: 0.3vh 0 0 0; } #user-info h2 { color: white; font-size: 1.5vh; font-weight: normal; width: 15%; margin: 0.3vh 0 0 0; } #user-info div{ font-size: 1.5vh; }
 <div id="user-info"> <div><h1><span>{this.props.dataHandler["userData"]["firstname"]} {this.props.dataHandler["userData"]["lastname"]}</span><span> (Admin)</span></h1></div> <div><h2><span>Mitarbeiternummer: {this.props.dataHandler["userData"]["userID"]}</span></h2></div> <div><a href="/#"onClick={this.logout.bind(this)}><u>Logout</u></a></div> </div>

The most important rule is always to keep it simple, I think this should help you:最重要的规则始终是保持简单,我认为这应该对您有所帮助:

 body { font-size: 12px; } h1 { color: orange; } h1 span { font-weight: 600; color: red; } h2 span { color: lightblue; }
 <div id="user-info" class="user-info"> <h1>{this.props.dataHandler["userData"]["firstname"]} {this.props.dataHandler["userData"]["lastname"]}<span> (Admin)</span></h1> <h2>Mitarbeiternummer: <span>{this.props.dataHandler["userData"]["userID"]}</span></h2> <a href="/#" onClick={this.logout.bind(this)}>Logout</a> </div>

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

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