jsolum
jsolum

Reputation: 198

Why is the default margin different with React than with plain html?

The margin is different between <button> elements depending on if its rendered by react or if it's just plain old html. Plain old html adds a small margin between the buttons, while react completely removes it.

Does anyone know why these are different? And is there a way to bring back that default margin in React?

Details

Here is the result from a simple react app created with create react app at 500% zoom: enter image description here

I took the source html that react generated, copied it, and put into another index.html file that I opened on my browser. Again at 500% this is what it shows: enter image description here

Note there is no additional CSS that is added. The developer tools look exactly the same.

Steps to replicate:

React

  1. Create a react app with npx create-react-app <some name>
  2. Remove all css from generated index.css
  3. Use the following jsx in the index.js file:
  <React.StrictMode>
        <button>Vanilla</button>
        <button>Vanilla</button>
  </React.StrictMode>

Plain Html

I copied the html that was rendered in the browser from the react app and pasted it into a separate index.html file. The html looked like:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link rel="apple-touch-icon" href="/logo192.png" />
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <link rel="manifest" href="/manifest.json" />
    <!--
      Notice the use of  in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <title>React App</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root">
        <button>Vanilla</button>
        <button>Vanilla</button>
    </div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  <script src="/static/js/bundle.js"></script><script src="/static/js/0.chunk.js"></script><script src="/static/js/main.chunk.js"></script></body>
</html>

Upvotes: 2

Views: 555

Answers (1)

jsolum
jsolum

Reputation: 198

Thanks to G-Cyrillus, we found out that the problem occurs because react is minifying the html. So the jsx:

<React.StrictMode>
  <button>Vanilla</button>
  <button>Vanilla</button>
</React.StrictMode>

once minified, turns into:

<button>Vanilla</button><button>Vanilla</button>

The solution I found to keep the gap is to write the jsx like this:

<React.StrictMode>
  <button>Vanilla</button> <button>Vanilla</button>
</React.StrictMode>

Upvotes: 1

Related Questions