html - DIV 未正确对齐
问题描述
如何正确并排对齐 3 个 div?
下面是外观:
我尝试使用左侧的浮动属性使所有 div 具有相同的宽度,但它没有帮助;我也使用inline-block
了 float 属性,但这也无济于事。
.payment_box .row .col-md-4 {
position: relative;
}
@media (min-width: 992px) {
.payment_box .row .col-md-4 {
float: left;
display: inline-block;
}
.col-md-4 {
width: 33.33333333%;
}
}
<fieldset id="wc-<?php echo esc_attr( $this->id ); ?>-cc-form" class="wc-credit-card-form wc-payment-form row">
<?php do_action( 'woocommerce_credit_card_form_start', $this->id
); ?>
<?php if ( $this->inline_cc_form ) { ?>
<label for="card-element">
<?php esc_html_e( 'Credit or debit card', 'woocommerce-
gateway-stripe' ); ?>
</label>
<div id="stripe-card-element" class="wc-stripe-elements-
field">
<!-- a Stripe Element will be inserted here. -->
</div>
<?php } else { ?>
<div class="col-md-4 ">
<!--<label for="stripe-card-element"><?php esc_html_e(
'Card Number', 'woocommerce-gateway-stripe' ); ?>
<span class="required">*</span></label>-->
<div class="stripe-card-group">
<i class="stripe-credit-card-brand stripe-card-
brand" alt="Credit Card"></i>
<div id="stripe-card-element" class="wc-stripe-
elements-field">
<!-- a Stripe Element will be inserted here. -->
</div>
</div>
</div>
<div class="col-md-4">
<!--<label for="stripe-exp-element"><?php esc_html_e(
'Expiry Date', 'woocommerce-gateway-stripe' ); ?>
<span class="required">*</span></label>-->
<div id="stripe-exp-element" class="wc-stripe-elements-
field">
<!-- a Stripe Element will be inserted here. -->
</div>
</div>
<div class="col-md-4">
<!--<label for="stripe-cvc-element"><?php esc_html_e(
'Card Code (CVC)', 'woocommerce-gateway-stripe' ); ?>
<span class="required">*</span></label>-->
<div id="stripe-cvc-element" class="wc-stripe-elements-
field">
<!-- a Stripe Element will be inserted here. -->
</div>
</div>
<div class="clear"></div>
<?php } ?>
<!-- Used to display form errors -->
<div class="stripe-source-errors" role="alert"></div>
<br />
<?php do_action( 'woocommerce_credit_card_form_end', $this->id );
?>
<div class="clear"></div>
</fieldset>
解决方案
您还应该为您的问题提供 HTMl,以便人们知道您在尝试什么。我将使用col 1
,来提供我的答案col 2
,col 3
因此您可以了解它们的工作原理。
为您的列创建一个初始包装器,然后为它们创建一个 DIV 是一个好主意。
我在下面提供了一个使用三个列名的示例,以便您可以修改百分比和宽度以在移动设备上显示不同的数量。
.columnswrapper {
min-width: 100%;
max-width: 100%;
overflow: hidden;
}
@media only screen and (max-width: 500px) {
.columnswrapper {
min-width: 100%;
max-width: 100%;
overflow: hidden;
}
}
.columns {
min-width: 100%;
text-align: center;
}
.col1 {
min-width: 33.3%;
float: left;
}
@media only screen and (max-width: 500px) {
.col1 {
min-width: 100%;
float: left;
}
}
.col2 {
min-width: 33.3%;
float: left;
}
@media only screen and (max-width: 500px) {
.col1 {
min-width: 100%;
float: left;
}
}
.col3 {
min-width: 33.3%;
float: left;
}
@media only screen and (max-width: 500px) {
.col3 {
min-width: 100%;
float: left;
}
}
<div class="columnswraper">
<div class="columns">
<div class="col1">
<p>Content Goes Here</p>
</div>
<div class="col2">
<p>Content Goes Here</p>
</div>
<div class="col3">
<p>Content Goes Here</p>
</div>
</div>
</div>
如果您希望它们都具有相同的大小,则可以像这样修剪代码;
.columnswrapper {
min-width: 100%;
max-width: 100%;
overflow: hidden;
}
@media only screen and (max-width: 500px) {
.columnswrapper {
min-width: 100%;
max-width: 100%;
overflow: hidden;
}
}
.columns {
min-width: 100%;
text-align: center;
}
.col {
min-width: 33.3%;
float: left;
}
@media only screen and (max-width: 500px) {
.col {
min-width: 100%;
float: left;
}
}
<div class="columnswraper">
<div class="columns">
<div class="col">
<p>Content Goes Here</p>
</div>
<div class="col">
<p>Content Goes Here</p>
</div>
<div class="col">
<p>Content Goes Here</p>
</div>
</div>
</div>
或者说我们希望两列占 40%,一列占 80,但为了移动设备而堆叠它们,我们可以这样做;
.columnswrapper {
min-width: 100%;
max-width: 100%;
overflow: hidden;
}
@media only screen and (max-width: 500px) {
.columnswrapper {
min-width: 100%;
max-width: 100%;
overflow: hidden;
}
}
.columns {
min-width: 100%;
text-align: center;
}
.col1 {
min-width: 40%;
float: left;
}
@media only screen and (max-width: 500px) {
.col1 {
min-width: 100%;
float: left;
}
}
.col2 {
min-width: 40%;
float: left;
}
@media only screen and (max-width: 500px) {
.col1 {
min-width: 100%;
float: left;
}
}
.col3 {
min-width: 20%;
float: left;
}
@media only screen and (max-width: 500px) {
.col3 {
min-width: 100%;
float: left;
}
}
<div class="columnswraper">
<div class="columns">
<div class="col1">
<p>Content Goes Here</p>
</div>
<div class="col2">
<p>Content Goes Here</p>
</div>
<div class="col3">
<p>Content Goes Here</p>
</div>
</div>
</div>
保持100%
您的宽度将确保一列在移动设备上变为一行。您还可以使用该float
属性并将max-width
列设置为与 相同,min-with
以确保它们始终具有相同的大小。
推荐阅读
- c - 汇编代码转储中 ECX 堆栈寄存器的功能
- r - 如何规范化特定列的值
- react-native - React Native + react-native-router-flux: hideNavBar 让所有组件消失
- mysql - 如果这等于 mysql 表中的 x,如何仅选择最后一个匹配结果
- node.js - 使用 Sequelize 分组
- python - 如何找到波兰语 Unicode 字符的 ASCII 等价物?
- delphi - TListView Livebinding 搜索功能仅在 Firemonkey Delphi Rio 10.3 中限制前 20 名搜索
- python - 更改定义模式的字符串(Python)
- javascript - 尝试添加返回功能。(上一个)使用 Javascript 的多步骤表单按钮
- c++ - 为什么我的代码偶尔会在自由存储(堆)上显示内存并向上和向下增长?(C++)