将宽度以百分比指定的PayPal按钮水平居中。

huangapple go评论84阅读模式
英文:

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 &quot;react&quot;;
import ReactDOM from &quot;react-dom&quot;;
import {
  PayPalScriptProvider,
  PayPalButtons,
  usePayPalScriptReducer
} from &quot;@paypal/react-paypal-js&quot;;
import &quot;./styles.css&quot;;

function App() {
  return (
    &lt;div className=&quot;hv-center-container&quot;&gt;
      &lt;div&gt;
        &lt;div className=&quot;thank-you h-center-container&quot;&gt;
          &lt;div&gt;Make the viewport as wide as you can, and some more&lt;/div&gt;
        &lt;/div&gt;
        &lt;div className=&quot;h-center-container&quot;&gt;
          &lt;PayPalScriptProvider&gt;
            &lt;div className=&quot;paypal&quot;&gt;
              &lt;PayPalButtons
                style={{
                  layout: &quot;vertical&quot;,
                  shape: &quot;pill&quot;,
                  tagline: false,
                  height: 55
                }}
              /&gt;
            &lt;/div&gt;
          &lt;/PayPalScriptProvider&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  );
}

const rootElement = document.getElementById(&quot;root&quot;);
ReactDOM.render(&lt;App /&gt;, 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-widthmax-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 &lt;iframe&gt;, 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>



huangapple
  • 本文由 发表于 2023年8月4日 06:54:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76832037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定