I am new to React and trying to implement conditional styling based on screen size. Despite searching the internet, I couldn't find a solution that works for me. Specifically, I want to center some text based on the screen size. Can anyone suggest a better way to achieve this? Currently, my approach is not yielding the desired results.
Update: After adding some logs, I noticed that the 'textAlign' property is not being applied to the element when the screen size is greater than 993px initially. However, it does get set to 'center' between 578px and 993px, but fails to revert back to its original state once the screen size decreases below 578px. It remains centered instead of resetting.
const styles = {
footerColOne:{
textAlign: deviceSize > 993 ? "none" : (deviceSize < 993 && deviceSize > 578) ? "center" : "none",
paddingLeft: deviceSize < 993 ? "26px" : "80px",
paddingTop: deviceSize < 993 ? "28px" : "63px",
},
footerColTwo:{
textAlign: deviceSize > 993 ? "none" : (deviceSize < 993 && deviceSize > 578) ? "center" : "none",
paddingLeft: deviceSize < 993 ? "26px" : deviceSize < 578 ? "51px" : "50px",
paddingTop: deviceSize < 993 ? "40px" : "86px",
},
}
To implement these styles, I'm using the following:
<Col lg={3} style={ styles.footerColOne }>
Any assistance would be greatly appreciated.
Latest Update:
Upon further reflection, I've realized that using media queries would be a more suitable approach for this task. While my ternary condition above did work, I mistakenly set 'textAlign' to 'none', which is an invalid value for that property. Instead, it should be set to 'initial' in this scenario.