Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

javascript - 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. 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. 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 Datei:

<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:

#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;
  }
question from:https://stackoverflow.com/questions/65943954/js-css-font-color-and-thickness-of-a-single-word-in-the-same-sentence-line

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's happening because all of your elements are set to just 15% of width.

You can see how narrow #user-info is.

enter image description here

So, your solution is to remove width: 15% from #user-info.

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...