<button>
元素之间的边距是不同的,这取决于它是由react呈现的还是纯旧的html。普通的旧html在按钮之间添加了很小的空白,而react则将其完全删除了。
有谁知道为什么这些不同?有没有办法带回React中的默认保证金?
这是使用create react app以500%缩放创建的简单react应用的结果:
我将产生反应的html源代码生成,复制并放入浏览器中打开的另一个index.html文件中。再次以500%显示如下:
请注意,没有添加任何其他CSS。开发人员工具看起来完全一样。
npx create-react-app <some name>
index.css
index.js
文件中使用以下jsx : <React.StrictMode>
<button>Vanilla</button>
<button>Vanilla</button>
</React.StrictMode>
我从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>
多亏了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] 删除。
我来说两句