为什么React与默认html的默认边距不同?

sol

<button>元素之间的边距是不同的,这取决于它是由react呈现的还是纯旧的html。普通的旧html在按钮之间添加了很小的空白,而react则将其完全删除了。

有谁知道为什么这些不同?有没有办法带回React中的默认保证金?

细节

这是使用create react app以500%缩放创建的简单react应用的结果: 在此处输入图片说明

我将产生反应的html源代码生成,复制并放入浏览器中打开的另一个index.html文件中。再次以500%显示如下:在此处输入图片说明

请注意,没有添加任何其他CSS。开发人员工具看起来完全一样。

复制步骤:

反应

  1. 使用创建一个反应应用 npx create-react-app <some name>
  2. 从生成的所有CSS中删除 index.css
  3. index.js文件中使用以下jsx
  <React.StrictMode>
        <button>Vanilla</button>
        <button>Vanilla</button>
  </React.StrictMode>

纯HTML

我从react应用程序复制了浏览器中呈现的html并将其粘贴到单独的index.html文件中。html看起来像:

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

多亏了G-Cyrillus,我们发现问题发生是因为react缩小了html。因此,jsx:

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

一旦缩小,就会变成:

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

我发现保持差距的解决方案是像这样编写jsx:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么<body>元素上有默认边距?

来自分类Dev

为什么PyQt的pyuic会忽略默认边距?

来自分类Dev

设置杯子的默认边距?

来自分类Dev

更改ConstraintLayout默认边距

来自分类Dev

如何删除 create-react-app 中的默认边距?

来自分类Dev

如何删除html中的默认textarea边距?

来自分类Dev

为什么我的 Bootstrap 边距大小不同?

来自分类Dev

如何减少hr标签的默认边距?

来自分类Dev

如何删除默认布局的填充/边距

来自分类Dev

Material UI - 自动完成默认边距?

来自分类Dev

HTML为什么顶边距不起作用?

来自分类Dev

为什么要在<html>元素上设置零边距?

来自分类Dev

为什么网站默认不默认为index.html?

来自分类Dev

为什么在React中需要默认导出?

来自分类Dev

SVG <g>和<text>元素的默认宽度/边距/填充是什么?

来自分类Dev

带有正文的HTML高度和chrome上的默认边距

来自分类Dev

为什么img的边距可点击?

来自分类Dev

为什么.row的边距为负?

来自分类Dev

为什么枚举类的默认类型与基本枚举类型不同?

来自分类Dev

为什么 ipython、spyder 和 atom 的默认编码不同?

来自分类Dev

为什么默认排序以相同的行首产生不同的结果?

来自分类Dev

为什么此CustomExtract返回的结果与默认的Extract不同?

来自分类Dev

如何使PyCharm的“填充段落”命令覆盖默认边距?

来自分类Dev

Xamarin中的网格是否有默认边距或填充?

来自分类Dev

Angular 6是否正在删除默认元素边距?

来自分类Dev

如何在Angular应用中删除默认边距?

来自分类Dev

如何更改边距属性的默认值?

来自分类Dev

UL是否具有默认边距或填充

来自分类Dev

ggplot2的默认绘图边距是多少?

Related 相关文章

热门标签

归档