3

index.css

  @font-face{
    font-family:"Bauhaus";
    src:local('BAUHS'),
      url('./BAUHS.ttf') format('truetype');
  }

I want to use the "Bauhaus" font in my react component index.js file based on certain condition. Could you please help how to use it.

Here is my code:

 import './index.css';
 class NameForm extends React.Component {
   constructor(props) {
      super(props);
      this.state = {fontFamilyName: ''};

      this.handleChange = this.handleChange.bind(this);
   }
    handleChange(event) { 
     this.setState({fontFamilyName: event.target.value});
    } 
    render() { 
      return ( 
            <div> 
                <select onChange={this.handleChange}>
                   <option value='Arial'>Arial</option>
                   <option value='Bauhaus'>Bauhaus</option>
                   <option value='Times New Roman'>Times New Roman</option> 
                </select> 
                <p style={{fontFamily:this.state.fontFamilyName}}>Some Text here</p>
            </div> ); 
     }
   }
6
  • what's the issue? share how you are using it? Commented Jun 21, 2018 at 10:16
  • I have used font family like <p style={{fontFamily:this.state.fontFamilyName}}></p> in my component. Here this.state.fontFamilyName is dymanically getting based on condition. Commented Jun 21, 2018 at 10:24
  • can you please share that code Commented Jun 21, 2018 at 10:32
  • I have shared my code. Commented Jun 21, 2018 at 10:54
  • You are expecting the value from your traget but, see it's undefined. Try to pass value attribute in option like value='Arial' . Commented Jun 21, 2018 at 10:59

1 Answer 1

3

Remember that "truetype" works for Safari, Android and iOS (https://css-tricks.com/snippets/css/using-font-face/).

Declare a font-face this way (just an example in one of my projects):

@font-face {
font-family: '3dumbregular';
src: url('fonts/3Dumb-webfont.eot');
src: url('fonts/3Dumb-webfont.eot?#iefix') format('embedded-opentype'),
     url('fonts/3Dumb-webfont.woff') format('woff'),
     url('fonts/3Dumb-webfont.ttf') format('truetype'),
     url('fonts/3Dumb-webfont.svg#3dumbregular') format('svg');
}

Multiple formats for multiple browsers support.

In your example you miss even the "select" value. Add value to select:

<select onChange={this.handleChange} value = {this.state.fontFamilyName}>
           <option value='Arial'>Arial</option>
           <option value='Bauhaus'>Bauhaus</option>
           <option value='Times New Roman'>Times New Roman</option> 
 </select>
Sign up to request clarification or add additional context in comments.

5 Comments

Thank You for response. I tried this but font family is not applying.
@user3278227 could you please create a jsFiddle (or something similar) with your code?
hi, I have added my code here - jsfiddle.net/n5u2wwjg/56014 , I am checking Jenkins font family online links to run this exapmle.
The change from Arial to Times New Roman works. The option for Jenkins should have as value 'Jenkins 2.0' because it is the font family name. You have still '3dumbregular' in SVG and there is not fonts folder
This is a jsfiddle with your code. I have taken a font from Tumblr. In your code use the font you prefer (possibily in your font folder): jsfiddle.net/stxdovh5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.