简体   繁体   English

CSS 图像未在中心对齐

[英]CSS Image is not aligning in the center

I was trying to make a paypal button shown in this link: https://paypal.com/buttons/smart I tried in different ways but I could not put it in center properly.我正在尝试制作此链接中显示的 paypal 按钮: https://paypal.com/buttons/smart我尝试了不同的方式,但我无法将其正确放置在中心。 I just want that button to look like paypal button as if it was smart button.我只是希望该按钮看起来像 paypal 按钮,就好像它是智能按钮一样。

<center><div class="paypal-button"><a href="www.google.com" class="pp"><img src="https://cdn3.iconfinder.com/data/icons/payment-method-1/64/_Paypal-39-512.png" width="64px"></a></div>
.paypal-button
{
text-align: center;
white-space: nowrap;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
text-transform: none;
font-weight: 500;
font-size: 11px;
user-select: none;
transition: opacity 0.25s ease-in-out;
position: relative;
box-sizing: border-box;
border: none;
vertical-align: top;
cursor: pointer;
outline: none;
overflow: hidden;
background: #ffc439;
color: #111;
height: 35px;
min-height: 35px;
max-height: 55px;

 .paypal-button { text-align: center; white-space: nowrap; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; text-transform: none; font-weight: 500; font-size: 11px; user-select: none; transition: opacity 0.25s ease-in-out; position: relative; box-sizing: border-box; border: none; vertical-align: top; cursor: pointer; outline: none; overflow: hidden; background: #ffc439; color: #111; height: 35px; min-height: 35px; max-height: 55px; border-radius: 4px; max-width: 400px; height: 55px; }
 <center><div class="paypal-button"><a href="www.google.com" class="pp"><img src="https://cdn3.iconfinder.com/data/icons/payment-method-1/64/_Paypal-39-512.png" width="64px"></a></div>

border-radius: 4px;
max-width: 400px;
height: 55px;
}

The issue is in the fact that the image you are trying to use has extra padding around the paypal text, so it was causing the text to look vertically uncentered.问题在于您尝试使用的图像在 paypal 文本周围有额外的填充,因此它导致文本看起来垂直不居中。 I uploaded the image to an online image editor and autocropped the extra transparent spacing away.我将图像上传到在线图像编辑器并自动裁剪掉额外的透明间距。

However, the content of your button still wasn't vertically centered with your default code so I changed your button to use display flex.但是,您的按钮的内容仍未与您的默认代码垂直居中,因此我将您的按钮更改为使用 display flex。 This makes vertically and horizontally centering the image much easier with justify-content and align-items.这使得使用 justify-content 和 align-items 使图像垂直和水平居中变得更加容易。

New image: https://i.imgur.com/509VUan.png新图片: https://i.imgur.com/509VUan.png

 .paypal-button { display: flex; justify-content: center; align-items: center; white-space: nowrap; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; text-transform: none; font-weight: 500; font-size: 11px; user-select: none; transition: opacity 0.25s ease-in-out; position: relative; box-sizing: border-box; border: none; vertical-align: top; cursor: pointer; outline: none; overflow: hidden; background: #ffc439; color: #111; height: 35px; min-height: 35px; max-height: 55px; border-radius: 4px; max-width: 400px; height: 55px; }
 <center><div class="paypal-button"><a href="www.google.com" class="pp"><img src="https://i.imgur.com/509VUan.png" width="64px"></a></div>

Just add display: flex, justify-content: center, align-items: center.只需添加 display: flex, justify-content: center, align-items: center。 to make this image vertically and horizontally centered.使此图像垂直和水平居中。

 .paypal-button { display: flex; justify-content: center; align-items: center; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM