I need to add one className to a component which is being passed down from the parent, and another className that doesn't have anything to do with its parent, but with the component itself.
example
<div className={this.state.className} className={this.props.content.divClassName}> <div className={this.props.content.containerClassName}> <div className="row"> <div className="col-sm-6 col-sm-offset-6"> <p>{this.props.content.subTitle}</p> <h1 className={this.props.content.titleClassName}>{this.props.content.headerText}</h1> <p className={this.props.content.subTitleClassName}>{this.props.content.paragraph}</p> <p>{this.props.content.quote}</p> <Button showMe={this.props.content.showButton} buttonText={this.props.content.buttonText} /> </div> </div> </div> </div>
When I try this, neither class is applied, I can do one or the other or not both. How do I achieve this?
Absolutely, divs can have more than one class and with some Bootstrap components you'll often need to have multiple classes for them to function as you want them to. Applying multiple classes of course is possible outside of bootstrap as well.
To add multiple classes in React Material UI using the classes props, we can use the classnames package. We call makeStyles with an object with the class names as the property names. And we set each property to an object with the CSS styles as the value. Next, we call useStyles to return the classes object.
To specify multiple classes, separate the class names with a space, e.g. <span class="left important">. This allows you to combine several CSS classes for one HTML element.
<div className={`${this.state.className} ${this.props.content.divClassName}`}>
Or if you can't use template strings:
<div className={[this.state.className, this.props.content.divClassName].join(" ")}>
Using template literals (Template strings),
Combo of string and props class is here
className={`${props.myClass} MyStringClass`}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With