Need to get rid of the outline effect?
The reason you're seeing an outline is because the paragraph element automatically gets a default browser outline when focused. To resolve this, you can modify the code as shown below. I have removed the focus-induced outline from the paragraph element:
class Greetings extends React.Component {
render() {
return <div > Hello {
this.props.name
} < /div>;
}
}
ReactDOM.render( <
div >
<
p tabIndex = {-1
}
style = {
{
outline: "none"
}
}
onKeyDown = {
event => alert(event.keyCode)
} >
Click to focus, then press a key. <
/p>
<
/div>,
document.getElementById('container')
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="container">
<!-- The contents of this element will be replaced with your component. -->
</div>
If you prefer, you can include the following snippet in your CSS file:
p {
outline: none;
}
Want to change the color of the outline?
Following @Akiba's suggestion, you can add a border instead of outlining in your CSS file. Borders offer more flexibility compared to outlines (Even though styling the outline directly is possible, it's not recommended for various reasons).
p {
outline: none; //Removes outline
border: 1px solid red; //Sets 1px solid red border
padding: 5px; //Optionally adds padding for spacing
}