Languages

React - how to remove npx create-react-app default padding/margin?

0 points
Asked by:
ArcadeParade
666

Can you help me with the following problem?

I've just created a new react app using:

npx create-react-app app_name

And when I assigned a simple red background style to it, I have unwanted white space on both sides and at the top, above the red component. How can I get rid of this?

My component:

const App = () => {
    return <div style={{ background: 'red', height: '50px' }}></div>;
};

Result:

React - how to remove npx create-react-app default padding/margin?

1 answer
0 points
Answered by:
ArcadeParade
666

1. Go to the public/ folder of your application,

2. create a <style> tag inside theindex.html file,

3. inside the <style> tag reset margin and padding properties for all components.

Practical example

Add the following line to the public/index.html:

<style>
    * {
        margin: 0;
        padding: 0;
    }
</style>

Your index.html file should now look like this:

<!DOCTYPE html>
<html lang="en">
<head>
    <!--
        some meta data and link components here
    -->
    <title>React App</title>
</head>
<style>
    * {
        margin: 0;
        padding: 0;
    }
</style>
<body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
</body>
</html>
0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join