When using React JSX, the style attribute does not recognize the object-fit property for images

When working with bootstrap cards, I encountered the need to make all images within the cards the same size. While there are various ways to achieve this, I used the following styling approach:

Take a look at the image style below

<div className="card " style={{width: "18rem"}}>
    <img src={imageUrl} className="card-img-top" style={{width: "100%", height: "40vh", object-fit: contain}} alt="..."/>
    <div className="card-body">
        <h5 className="card-title">{title}...</h5>
        <p className="card-text">{description}...</p>
        <a href={newsUrl} target="_blank" className="btn btn-sm rounded-pill btn-primary">Read More</a>
    </div>
</div>

However, I encountered an error:

SyntaxError: C:\Users\hpoddar\Desktop\WebDev\ReactComplete\newsapp\src\components\NewsItem.js: Unexpected token, expected "," (10:110)

https://i.sstatic.net/0eoj8.png

Removing the object-fit attribute resolved the issue, eliminating the wiggly line. Everything now works as expected.

Any suggestions on how to approach this?

Bootstrap version : 5.1
react: 17.0.2
react-dom: 17.0.2
react-scripts: 4.0.3
web-vitals: 1.1.2

UPDATE : I questioned the use of object-fit as objectFit, which was clarified in the comments by Andrew.

As inline CSS is written within a JavaScript object, properties with two names, such as background-color, should be in camel case syntax:

Answer №1

It is my belief that using object-fit: contain may not be suitable for JSX. You may want to consider using objectFit: 'contain' instead.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Combining Blazor with Bootstrap for seamless form validation

After gaining some experience with Razor, I decided to explore Blazor. However, I encountered a familiar challenge - integrating validation with Bootstrap. The issue lies in the mismatch between Blazor's validation result classes and those of Bootstra ...

Could it be a path problem if you're directed to the incorrect slug without any errors popping up?

I have two slugs. I'm encountering an issue where I am trying to access the second slug [product] but instead, it keeps redirecting me to the first one [slug]. The slugs are stored in separate folders and there aren't any error messages being dis ...

Customize Material UI Button Text

I'm experiencing an issue with Material UI buttons where the text within them defaults to uppercase. I've been trying to override this behavior so that the text remains as I type it in, without being automatically converted to uppercase. Despite ...

What is the best way to eliminate the text-decoration underline from a flex child when the parent is designated as the anchor?

I need help removing the text-decoration of a flex child when the parent is the anchor. Despite trying various CSS code, it doesn't seem to work as expected. On my WordPress site, the underline only shows on hover, but in the jsFiddle example I create ...

Changing the background color of the legend text when hovering over a d3 doughnut chart

I have a doughnut chart that displays values on mouse hover. However, I would like to change the background of the legend text when hovering over the respective area of the doughnut chart. return { restrict: 'E', scope: { values: ...

Code for Custom Controllers in Strapi Beta version 3.0

I have encountered some discrepancies in the beta version of Strapi's controllers compared to the previous version. The new version includes multipart/sanitization boilerplate, and I am having trouble integrating my order object and Stripe charge. Be ...

React Native: error - unhandled action to navigate with payload detected by the navigator

Having trouble navigating from the login screen to the profile screen after email and password authentication. All other functions are working fine - I can retrieve and store the token from the auth API. However, when trying to navigate to the next screen, ...

Getting the hang of using and translating typescript .tsx files with jsx in React-native

Recently, I have ventured into the world of React-native after having experience with reactjs+typescript. Wanting to test its capabilities, I decided to set up a simple project. My tool of choice for development is VS Code. After following a basic tutoria ...

Missing inkbar in Material UI tabs when using ReactJS

For some reason, the small inkbar is not appearing under this tab. I suspect it might be a css issue that I can't seem to figure out. It's possible that I'm missing something I don't know about or maybe the height of the tab is too high ...

What is causing the error "unable to access property 'map' of undefined" in my code?

Currently working on React + Redux and trying to implement a likes counter. However, encountering an error 'cannot read property 'map' of undefined'. Here is the code snippet for my Reducer: const initialState = { comments: [], ...

The MUI switch fails to function properly on the initial attempt due to issues with the React state

I'm facing a simple issue that has me stumped: My MUI react switch with state tracker is causing trouble The problem is, it doesn't work on the first try, but works fine every time after that... EDIT: specifically, the state fails to update the ...

Tips on widening a paper to its fullest width while also keeping it versatile and adaptable

How can we ensure that in a React application using Material-UI, the width of a paper always expands to a maximum of 600px, regardless of the width of its contents? We also want to keep the paper flexible, so that when the parent container's width shr ...

What causes the CSS Position Fixed Property to fail?

Can someone help me fix my navbar so that it remains fixed when I scroll? Currently, it moves up as I scroll down. Below are the source codes I'm using: * { margin: 0; padding: 0; box-sizing: border-box; } h1 { margin: 1rem; } ul { posit ...

Click on a div to smoothly scroll to the top, then automatically scroll to the bottom if already at the top position

I've implemented a JQuery code on my website that allows the page to scroll to the top when clicking on a div with the class of .bottom. The code is working perfectly fine, here it is: function scrollToTop(){ $('.bottom').click(function ...

Looking to discover how to optimize a website for various Android resolutions?

Currently, I am working on designing websites specifically for Android phones. However, I have encountered a major challenge due to the wide range of screen resolutions found on these devices. Some phones boast a height of 800px while others are limited to ...

Content within iframe is failing to load correctly on both Firefox and Internet Explorer browsers

On my website, I have 4 iframes embedded in 4 different tabs. I've noticed that the content is being cropped when viewed on Firefox, but it loads properly when using Chrome. Strangely, if I manually reload the iframe, the content displays correctly. T ...

Facing an issue where the data returned in React is showing up as undefined

I am currently facing an issue with passing data down to a component using react router. After confirming that the endpoint is functioning correctly, I have ruled out any server-related problems. Here is the function responsible for fetching page data bas ...

React hooks - Issue with updating state properties within sidebar display

When resizing the window, I have an event that will display either the desktop sidebar or the mobile sidebar. However, there are variables that are not immediately updated to show the sidebar correctly based on the window size. In a desktop window, this ca ...

Placing a div with absolute positioning inside another div with absolute positioning raises the question of how it functions when the outer div is not set to position: relative

Trying to grasp the concept, I experimented with the following setup: <div id="Outer"> <div id="Inner"></div> </div> In this scenario, the outer div has a relative position and the inner div has an absolute position. This allo ...

How to Dynamically Update Sass Styles with Webpack 2?

Currently, I am in the process of setting up a React application that utilizes Webpack2, webpack-dev-middleware, and HMR for the development phase. One peculiar issue that I have encountered is related to updating .scss files. When I make changes to my .sc ...