React component fails to display inline CSS

I am experiencing an issue with my React application. I have created an App component that is supposed to render an array of components on the screen, but for some reason, the inline CSS styles are not displaying.

//App component
import data from "./data.js"
import Item from "./Item"

export default function App(){
   const cardElements = data.map(item => <Item/>)
   return (<div className='app'>
               {cardElements}
           </div>);
}
//Item component
export default function Item(){
   const customStyle = {border: "2px solid black"};
   return (<div style={customStyle} >Item component</div>);
}

Despite setting inline CSS in the Item component, the styles do not seem to be applied correctly when the webpage is loaded.

Answer №1

Looking at the snippet below, it's evident that the inline style is functioning correctly. It's possible that your styles are being overridden, but further information would be needed to confirm.

On a side note, remember to include the key prop when using .map in React.

const data = [1, 2, 3, 4];

function App() {
   const cardElements = data.map(item => <Item key={item} />)
   return (
    <div className='app'>
      {cardElements}
    </div>
   );
}

function Item() {
   const customStyle = { border: "2px solid black" };
   return <div style={customStyle}>Item component</div>;;
}

ReactDOM.createRoot(
    document.getElementById("root")
).render(
    <App />
);
<div id="root"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.development.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.development.js"></script>

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

Unlocking the full potential of Konva Node in your ReactJs projects

I'm looking to implement node nesting in konva.js within my react app. Any guidance on how to incorporate this functionality would be greatly appreciated. Thanks in advance! ...

Bootstrap's nested rows do not adhere to the correct width specifications

After reading the post titled How to make a Bootstrap column span multiple rows?, I attempted to replicate the concept but encountered some difficulties. This is what I came up with: https://codepen.io/saka-rauka1/pen/MWodjao?editors=0010 render() { r ...

Encountering a connection issue while attempting to install styled components using npm

Hello everyone, I am currently in the process of setting up my development environment. To start off, I created a new React project using npx create-react-app PROJECTNAME Now, I'm exploring the use of Styled Components. However, when I try to inst ...

Spin the div in the opposite direction after being clicked for the second time

After successfully using CSS and Javascript to rotate a div by 45 degrees upon clicking, I encountered an issue when trying to rotate it back to 0 degrees with a second click. Despite my searches for a solution, the div remains unresponsive. Any suggestion ...

Table with a responsive c3 chart

Is there a way to integrate a responsive C3 chart into a table? I am trying to set up a table with a responsive layout, where one of the cells contains a C3 chart. The initial dimensions of the cell are 200 width and 50 height, but I want the chart to upd ...

I'm having trouble getting my HTML <button> element to function correctly

I'm currently working on a project where I need to animate a green bar moving upward over a blue bar when a button is clicked. However, no matter what I try, the button doesn't seem to be working. I've experimented with using both the button ...

Renew the Look of Dynamic HTML with Updated Styles

In the middle of my website, there is a console that contains links to dynamically update its content using javascript. Everything works smoothly, but the new HTML added dynamically does not have any styling applied to it. Is there a way to refresh the CS ...

Align text to the left but keep the image centered using Bootstrap 4

Using Bootstrap 4 with a row setup like this: <link rel="stylesheet" href="https://stackpath.bootstrapcdn.combootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anony ...

What is the purpose of using overflow:hidden; in our code?

I am curious about the rationale behind using these three properties on this menu. I don't quite understand why they are necessary. I am eager to delve into the technical reasoning behind this. margin: 0; padding: 0; overflow: hidden; Snippet of HTM ...

The fullscreen API allows for the creation of a full-screen element containing internal elements, while also enabling the functionality

Is it possible to utilize the fullscreen API to make any element fullscreen, even if it contains multiple internal elements, while still maintaining the functionality of dropdowns and other custom elements that may be located in different areas of the page ...

Mix-up of inheritance and specificity within CSS

Trying to comprehend the discrepancy between two CSS rules, I am currently reviewing this HTML file. <!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>specificity</title> <!--[if lt IE 9]> <s ...

The animation of the splash screen in Angular is quite jarring and lacks fluidity

I am experiencing some issues with my angular splash screen animation. It works perfectly when there is no activity in the background, but when I simulate a real-life application scenario, the animation becomes stuttered, choppy, or sometimes does not anim ...

Adjust picture to fit in div and align vertically

JSFiddle In my fluid layout, I have a div with a required aspect ratio of 1:1. This div can contain text, an image, or both. The challenge is to vertically align the text and ensure the image fits within the div without exceeding its boundaries or losing ...

Issue with readonly is preventing the ability to alter the font color of the input

I need to change the font color of a disabled input. When it is disabled, it appears gray and I want it to be black instead. I attempted to use readonly but that did not have the desired effect, and now the input is showing [object Object]. Below is my HTM ...

Ways to verify if all javascript and css files have been successfully loaded

I have been exploring different ways to notify users if certain resources do not load correctly. So far, I have come across the following methods: For CSS, I stumbled upon an example in the source code of Trello: <div id="nocss"> Your browser ...

Divs that are 30% of their parent's width will not align vertically and will not fit in a parent div that is 100% width

I am attempting to vertically center the three child-divs <div class="section"> as 3 rows in one column within <div class="container_page_2">. When I refer to vertical alignment, I mean having an equal distance between the top of the page and ...

Unable to get the DIV to float within its parent div

I'm having an issue with 2 DIV elements (the ones labeled "Module" below) not behaving as expected! When I try to float them so they appear side by side, they end up jumping outside of the parent container and falling below it. I've tried to trou ...

What causes the cursor in an editable div to automatically move to the front of the div?

<div className="min-w-[600px] min-h-[36.8px]" > <div id={`editableDiv-${Object.keys(item)}-${index}`} className="p-3" contentEditable suppressContentEditableWarning onInput={(e) => onChange(e)} > ...

Prevent Bootstrap 5 input fields from automatically adjusting to the height of a CSS grid cell

Below is some Bootstrap 5 markup that I need help with. The first example is incorrect. I don't want the Bootstrap input to be the same height as the grid cell. The second example is right, but I want to achieve the same result without using a wrapp ...

Updating the configuration settings for CKEditor 5 using Reactjs

I followed the configuration provided in another answer at But, I am facing an issue: Failed to compile. ./src/components/cards/CreateCard.js Line 59:22: Parsing error: Unexpected token, expected "}" 57 | editor={ClassicEditor} 58 | ...