html - 如何从 PHP 对齐同一行上的两个元素

标签 html css

截图: Inspect element shows: The "7 in stock" i want to align with the "10" bellow

我想将两个 $elements 对齐显示在同一行上。

<span class="max">
    <?php echo $tickets_sold, $max_tickets; ?>
</span>

如果不包含 $tickets_sold,则 $max_tickets 位于顶部。但是当它被包含时,它会立即进入下一行。

我尝试过许多不同的选项,如 &&, and,但它们从未出现在同一行。我确信解决方案很简单,但我是初学者。

$max_tickets = $product->get_max_tickets();
$tickets_sold = wc_get_stock_html( $product );

PHP代码

 <div class="wcl-progress-meter <?php if($product->is_max_tickets_met()) echo 'full' ?>"> 
    <progress  max="<?php echo $max_tickets ?>" value="<?php echo $lottery_participants_count ?>"  low="<?php echo $min_tickets ?>"></progress></br> 
    <span class="zero">0 </span>
    <span class="max">  <?php echo $tickets_sold, $max_tickets?></div></span>
</div>

CSS文件

.wcl-progress-meter meter::-webkit-meter-suboptimum-value {
  box-shadow: 0 5px 5px -5px #999 inset;
  background: #cb132b;
  display:inline-block;

}

.wcl-progress-meter .zero {
  display: inline-block;
  position: absolute;
  top: -100%;
}

.wcl-progress-meter .min {
  display: inline-block;
  position: absolute;
  top: -100%;
}

.wcl-progress-meter .max {
  display: inline-block;
  position: absolute;
  top: -100%;
  right: 0;
  vertical-align: middle;
}

最佳答案

只需稍微调整一下您的 HTML,一切都会如您所愿:

<style>
    .flex-parent{display:flex;}
    .flex-child{flex-basis:content;margin-right:10px;}
</style>
<div class="max flex-parent">
    <div class="flex-child"><?php echo $tickets_sold;?></div>
    <div class="flex-child"><?php echo $max_tickets;?></div>
</div>

<style>
    .flex-parent{display:flex;}
    .flex-child{flex-basis:content;margin-right:10px;}
</style>
<div class="max flex-parent">
    <div class="flex-child">tickets_sold</div>
    <div class="flex-child">max_tickets</div>
</div>


或者,您也可以只使用 display:inline:

<style>
    .inline-block{display:inline-block;margin-right:10px;}
</style>
<div class="max">
    <div class="inline-block"><?php echo $tickets_sold;?></div>
    <div class="inline-block"><?php echo $max_tickets;?></div>
</div>

<style>
    .inline-block{display:inline-block;margin-right:10px;}
</style>
<div class="max">
    <div class="inline-block">tickets_sold</div>
    <div class="inline-block">max_tickets</div>
</div>

更新:

请注意第一个示例代码的更改 - 我忽略了将 flex-parent(我发明的 className,不是约定) 添加到 .max 格。

要使元素在父级中居中,请将 justify-content:center; 添加到父级:

<style>
    .flex-parent{display:flex;}
    .flex-child{flex-basis:content;margin-right:10px;justify-content:center;}
</style>
<div class="max flex-parent">
    <div class="flex-child"><?php echo $tickets_sold;?></div>
    <div class="flex-child"><?php echo $max_tickets;?></div>
</div>

使用 display:inline-block 方法:

<style>
    .inline-parent{text-align:center;}
    .inline-block{display:inline-block;margin-right:10px;}
</style>
<div class="max inline-parent">
    <div class="inline-block"><?php echo $tickets_sold;?></div>
    <div class="inline-block"><?php echo $max_tickets;?></div>
</div>

    <style>
        .inline-parent{text-align:center;}
        .inline-block{display:inline-block;margin-right:10px;}
    </style>
    <div class="max inline-parent">
        <div class="inline-block">tickets_sold</div>
        <div class="inline-block">max_tickets</div>
    </div>

关于html - 如何从 PHP 对齐同一行上的两个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56345384/

相关文章:

html - 并排 table 不能正常工作

javascript - 在剪切路径中使用时重新计算滚动 div 位置

css - 创建 css 按钮的当前状态

html - 元素在悬停之前正在转换 (translateZ)

html - 无法调整 <td> 内按钮的宽度

css - 如何在 Bootstrap 中为 jumbotron 添加背景图像?

html - 如何在我的 CSS 样式定义中排除没有子菜单的元素?

css - 在 Internet Explorer 上固定位置

html - bootstrap 折叠 div 显示 1 隐藏 1

CSS百分比宽度和高度以及分辨率问题