Centering PayPal buttons horizontally with a width specified as a percentage

Take a look at this interactive coding playground showcasing 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>Adjust the viewport width and observe</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);

Along with the specified 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%;
}

If the width property in the .paypal style rule is specified in pixels, the alignment of the PayPal buttons would be correct. However, when set in percentage, an issue arises. Why does this occur and what are the possible solutions?

Answer №1

When designing buttons for PayPal, they utilize @media queries to set the parameters for min-width and max-width. The maximum width allowed for these buttons is 750px. If the containing element with class .paypal exceeds this limit, the buttons will automatically align to the left within the wider container.

If you want the .paypal container to match the width of the buttons inside an <iframe>, you can apply the same @media queries used by PayPal.

@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;
  }
}

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

Avoid displaying duplicate items from the array

Utilizing react js, I am attempting to iterate through an array of objects and display the name of each object in the array: const arr = [ { name:"Bill", age:88 }, { name:"Bill", age:18 }, { name:"Jack", age:55 }, ] ...

Adjust the header width and column alignment when freezing the header in a gridview interface

Looking to implement a gridview with a fixed header? I've tried various solutions including this link and this one. While I've managed to get it working, the main issue lies in the alignment of header width and column width. The scroll and freez ...

Unresponsive JavaScript on specific element IDs

I'm experimenting with making three lines perform different animations: line 1 rotating 45deg and translating, line 2 becoming opaque, and line 3 rotating -45deg and translating. Check out my JS Fiddle here <a href="#"><div id="menu" onclic ...

Enhancing Build Speed with Webpack Externals in NextJS

In a unique situation, I find myself needing to construct and export my website at runtime with speed being essential. This requires a rapid deployment of the site and its components. My plan involves utilizing Webpack externals to pre-bundle all componen ...

Utilize functional JS code within a TypeScript environment

Attempting to integrate this code into a TypeScript project, it is a modified version of the react-custom-scrollbars. I am struggling with TypeScript in regards to declaring types for style and props. In this particular case, I prefer to bypass type check ...

Multiple dropdowns/popovers causing trouble in NextUI interface

I am encountering a problem with NextUI where I have to double-click to open a new popover or dropdown because the previous one needs to be closed first. Is there a way to resolve this issue? Here is the code snippet: <Popover className="bg-white ...

Troubleshooting challenges with a React web application and resolving the issues

Recently, I stumbled upon a React web application from GitHub that caught my interest. I followed the instructions and ran the code using "npm start" in the terminal before accessing it at http://localhost:8080/webpack-dev-server/. However, I encountered a ...

Serving static files in Next.js with specific extensions without triggering a download

I'm facing an issue where I need to serve the stellar.toml file in domain/.well-known/stellar.toml with the content type as text/plain. The current configuration works only when the stellar file is saved without an extension. It is essential for me t ...

Encountering a glitch in Jest unit testing post updating to npm version 3.3.12

After updating my project to node 5.1.0, npm 3.3.12, and updating all dependencies, I am now facing a plethora of errors! Originally, I encountered the same error as discussed in this GitHub issue: https://github.com/facebook/jest/issues/554, which was re ...

Is it possible to manipulate the content inside a div tag using JavaScript in HTML?

What is the most efficient way to dynamically adjust a data offset value within an HTML tag using JavaScript? For example: <div class="box" data-offset="2s"></div> I am looking to update the value within the data-offset attribute based on s ...

Animate a dotted border with CSS

How can I make a text block with a dotted style border move like a gif image using CSS at runtime? ...

ReactJS/NextJS - How using useState can cause the tab to crash

When running the code setOpenAddFolders(true) at line #27, the entire tab freezes. I've tried various solutions and searched extensively, but nothing has worked. I have used similar code (setting a useState variable) in another file and it works fine ...

CSS: Dynamically adjusting height based on parent element's current height

I'm seeking clarification on the topic at hand. Why am I unable to simply use the following code: <div style="min-height: 100vh"> <div style="height: 100%"> </div> </div> When I implement this code, ...

What are the steps to recursively transform a JavaScript object?

I am currently working on recursively transforming a JavaScript object, but I have encountered an issue. The problem is: if any object key has exactly two properties - 'label' and 'value', then the value should change to the label only ...

Troubleshooting Bootstrap 4 Content Alignment Issue: Centering Horizontally and Vertically

Having trouble centering my content both vertically and horizontally within the body of the webpage using Bootstrap 4. Despite trying various solutions from the documentation and StackOverflow, I can't seem to get it to work. Would greatly appreciate ...

Obtaining the value from a checked checkbox in a react form with mixed fields

I am trying to streamline my form by using a single function called handleChanges to update the state for each field. However, I am facing an issue with checkboxes as they need to return either true or false, or even "yes" and "no" when toggled. I am unsur ...

React does not automatically re-render components created with the built-in function

I'm facing some confusion with the behavior in my code: I've created a component that should function as a menu using MaterialUI. The idea is that when a button in the menu is clicked, it becomes "active" and visually reflects this change by set ...

How do you merge a JSX element with a JavaScript command in React?

My code is ready, but I'm stuck on how to combine the two parts together. Any suggestions? <ul> {suggestionList.length ? <li className="header">Suggestions</li> // <--- compiler says error from here suggestionList.ma ...

update the componentWillMount() function

I am currently exploring the code found at https://github.com/Spyna/react-store/blob/master/src/createStore.js What adjustments do I need to make in order to align with the deprecated componentWillMount lifecycle method? CreateStore const createStore = ...