英文:
Horizontal centering of PayPal buttons whose width is specified as percentage
问题
请看看这个播放区,其中呈现了以下的React代码:
import React from "react";
import ReactDOM from "react-dom";
import {
PayPalScriptProvider,
PayPalButtons,
usePayPalScriptReducer
} from "@paypal/react-paypal-js";
import "./styles.css";
function App() {
return (
<div className="hv-center-container">
<div>
<div className="thank-you h-center-container">
<div>使视口尽可能宽,再宽一些</div>
</div>
<div className="h-center-container">
<PayPalScriptProvider>
<div className="paypal">
<PayPalButtons
style={{
layout: "vertical",
shape: "pill",
tagline: false,
height: 55
}}
/>
</div>
</PayPalScriptProvider>
</div>
</div>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
以及样式:
.hv-center-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
width: 100%;
}
.h-center-container {
display: flex;
flex-direction: column;
align-items: center;
width: 100%;
}
.thank-you {
font-size: 3em;
margin-bottom: 1em;
}
.paypal {
width: 100%;
}
如果我指定了.paypal
规则的width
属性为像素,PayPal按钮将正确居中。但现在它是百分比,它们没有居中。为什么会发生这种情况,我该如何修复它?
英文:
Please have a look at this playground, which presents the following React code:
import React from "react";
import ReactDOM from "react-dom";
import {
PayPalScriptProvider,
PayPalButtons,
usePayPalScriptReducer
} from "@paypal/react-paypal-js";
import "./styles.css";
function App() {
return (
<div className="hv-center-container">
<div>
<div className="thank-you h-center-container">
<div>Make the viewport as wide as you can, and some more</div>
</div>
<div className="h-center-container">
<PayPalScriptProvider>
<div className="paypal">
<PayPalButtons
style={{
layout: "vertical",
shape: "pill",
tagline: false,
height: 55
}}
/>
</div>
</PayPalScriptProvider>
</div>
</div>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
and the styles:
.hv-center-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
width: 100%;
}
.h-center-container {
display: flex;
flex-direction: column;
align-items: center;
width: 100%;
}
.thank-you {
font-size: 3em;
margin-bottom: 1em;
}
.paypal {
width: 100%;
}
Had I specified the width
property of the .paypal
rule in pixels, the PayPal buttons would center properly. But now that it's in percentage, they don't.
Why is this happening and how can I fix it?
答案1
得分: 1
PayPal使用@media
查询来声明min-width
和max-width
。按钮的最大宽度为750px。当您的.paypal
容器宽度超过750px时,按钮会在更宽的.paypal
容器内左对齐。
要使.paypal
容器的宽度与<iframe>
内的按钮相匹配,您可以使用与PayPal相同的@media
查询。
@media only screen and (min-width: 75px) {
.paypal {
min-width: 75px;
max-width: 150px;
}
}
@media only screen and (min-width: 150px) {
.paypal {
min-width: 150px;
max-width: 200px;
}
}
@media only screen and (min-width: 200px) {
.paypal {
min-width: 200px;
max-width: 300px;
}
}
@media only screen and (min-width: 300px) {
.paypal {
min-width: 300px;
max-width: 500px;
}
}
@media only screen and (min-width: 500px) {
.paypal {
min-width: 500px;
max-width: 750px;
}
}
英文:
PayPal uses @media
queries to declare min-width
and max-width
. The widest the buttons can get is 750px. When your .paypal
container is wider than that, the buttons become left-aligned inside of the wider .paypal
container.
To have the .paypal
container match the width of the buttons inside the <iframe>
, you can use the same @media
queries that PayPal uses.
@media only screen and (min-width: 75px) {
.paypal {
min-width: 75px;
max-width: 150px;
}
}
@media only screen and (min-width: 150px) {
.paypal {
min-width: 150px;
max-width: 200px;
}
}
@media only screen and (min-width: 200px) {
.paypal {
min-width: 200px;
max-width: 300px;
}
}
@media only screen and (min-width: 300px) {
.paypal {
min-width: 300px;
max-width: 500px;
}
}
@media only screen and (min-width: 500px) {
.paypal {
min-width: 500px;
max-width: 750px;
}
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论