What causes the variation in default margin between React and traditional HTML?

There seems to be a difference in margin between <button> elements when rendered by React compared to plain HTML. Plain HTML adds a small margin between the buttons, while React removes it completely.

Does anyone have an explanation for this discrepancy? And is there a way to reintroduce the default margin in React?

Details

After creating a simple React app with create react app at 500% zoom, here are the results: https://i.sstatic.net/tpIlR.png

I copied the source HTML generated by React and pasted it into another index.html file which I opened in my browser. The result at 500% zoom was as follows: https://i.sstatic.net/t8d3R.png

No additional CSS was added and the developer tools showed no differences.

Steps to replicate:

React

  1. Create a React app using
    npx create-react-app <some name>
  2. Remove all the CSS from the 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 structure 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.
    -->
    <link rel="manifest" href="/manifest.json" />
    <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>

  <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>

Answer №1

Upon consultation with G-Cyrillus, it was revealed that the issue arises due to react minifying the HTML code. In its original JSX form:

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

after being minified, it appears as:

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

To maintain the whitespace gap, a solution is to structure the JSX like this:

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

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

"Why is the callback function failing to pass the Jest test in React? The callback function is not being recognized as a function during testing of the Checkbox.Group component in React with

Running a test on my MultipleDaySelector function reveals an error. The issue seems to revolve around the parentCallbackMultipleDaySelector not being recognized as a function. The core of the problem lies in this section of code: TypeError: parentCallb ...

Guide on Modifying the CSS File of an Installed Plugin on WordPress

I am looking to customize the design of a WordPress plugin by editing its CSS file. When trying to locate the CSS file of the installed plugin through Plugins --> Installed Plugin --> Editor, I could only find .php files instead of the desired CSS f ...

Encountering a frustrating Npm error while trying to install a package, which persists in throwing

Encountering an error message while trying to run npm install npm ERR! Windows_NT 6.3.9600 npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\ node_modules\&bsol ...

Utilize a component's CSS styles by inheriting them and applying them to another component

I have created custom styles for an object as shown below: form#sign_in{ position:relative; margin:85px auto 50px auto; width:455px; background:#fff; border:#dfdfdf 1px solid; border-radius:5px; -moz-border-radius:5px; -web ...

Navigational dropdown menus in next.js with multiple levels of subcategories

Encountering an issue with a nested dropdown implementation in Next.js using the Next UI version 2.2.9. Issue occurs when clicking on the top-level button of the dropdown, sublevel contents briefly appear for less than a second before the entire dropdown c ...

Struggling to display blob URL on "pdf-viewer" component with "ng2-pdf-viewer" in Angular version 10

I am facing an issue with an API that returns uploaded files as blobs. When trying to bind the src with a blob URL, nothing is shown. However, if I bind it with a direct URL, the PDF file is displayed successfully. Below is my code snippet. My TypeScript ...

Unforeseen issues arise when working with CSS selectors

Exploring CSS with a series of buttons and encountering some unusual behavior. Here's the code: https://codepen.io/buoyantair/pen/JNgqXG?editors=1100 Let me share the snippets for Pug Script and Sass file: Pug Script header.row .col ...

Retrieve information through a Button Click, not by selecting

As a beginner in react, I encountered an issue that I'm struggling to comprehend. Initially, I was successfully fetching weather data from an API by sending a POST request with the input value from a normal input field using a ref. However, after inte ...

Utilize Javascript ES6 to Sort Through Data in a Table

I require assistance with my project using Material UI and React. There are two key implementations that I need: I am looking to create a filtering system for all rows in each column as shown in the attached image. Additionally, I need a button that can a ...

Incorporating an image prior to the "contains" term with the help of JavaScript

Seeking assistance with the code snippet below without altering the text targeted in my "a:contains" statement. The challenge lies in determining the appropriate placement of ::before in the script provided. Link: https://jsfiddle.net/onw7aqem/ ...

The form action now triggers the download of the PHP file instead of simply loading

I'm encountering an issue with the form on my website: <div class="searchBar"> <form action="searchDB.php" method="get"> <input type="text" class="searchBarInput" bdo dir="rtl" name="k"> <input type="image" cl ...

Using jQuery, JavaScript, and PHP, we can easily implement the functionality to disable the form and submit button

After submitting a form, I am trying to disable both the submit button and the form itself. However, every solution I have attempted either disables the form and button without submitting it or submits the form without disabling the button/form. This is t ...

Having trouble with the Material UI Select component? Getting an error message that says "Error: choices.map is not a function"? Let's troub

I'm having difficulty implementing a select component to display a dropdown of Brazil's states, which I need for various forms in the application (clients, suppliers, etc.). To address this, I created a separate Select component and now want to i ...

Invalid export field 'query' encountered while attempting to deploy NextJS13 and Sanity.io on Vercel

Currently, I am diving into the world of NextJS13 and following a blog tutorial by Sonny Sangha called 'Let’s build a BLOG with Next.js 13 (Sanity v3, TypeScript, Tailwind CSS, Auth, CMS, Preview Mode)'. After completing the tutorial, my goal i ...

I'm trying to display hidden forms on a webpage when a button is clicked using the DojoToolkit, but I'm having trouble figuring out what's going wrong with my code

Currently, I am trying to grasp the concepts of Dojotoolkit and my objective is to display a form when a button is clicked. Upon reviewing other examples, my code seems correct to me; however, there appears to be something crucial that I am overlooking but ...

Improving the App() function in React and Typescipt

When working on my React/Typescript app, I encountered a challenge with the length of the code in one of the sections. Despite watching tutorials and searching for solutions, I couldn't find a clear way to refactor it. The specific issue is with refa ...

What is the best way to effectively apply a mask within a PixiJS container so that its color does not display upon page refresh?

After implementing the code snippet below to add a mask in a container, I encountered an issue where upon clicking the refresh button on the page (chrome), the pixi stage would turn completely white until the refreshing process is completed. Can anyone p ...

"Retrieve data from an Excel file and either present it in a textarea or store it as an array

Is it possible to use JavaScript to read an Excel file and convert a single column (identified by name or index) into an array in JavaScript? ...

Enhance Reactjs material ui Select by dynamically incorporating a new property

I am curious about dynamically setting props for the material UI Select component. In an attempt to customize the material UI Select (https://material-ui.com/components/selects/) component, I'm wrapping it in my CompanySelect to add some extra stylin ...

Error message: Trying to use the data type 'String' as an index in the React dynamic component name map won't work

I have successfully implemented the code below and now I am attempting to convert it to Typescript. However, even though I can grasp the error to some extent, I am unsure of how to correct it. At present, I am allowing a component to have a prop called "i ...