首页 > 解决方案 > 将不同的类添加到标签和值并在同一行输出

问题描述

我正在尝试向标签和值添加不同的类,以便我可以对它们进行不同的样式设置,但需要做两件事。

  1. 添加一条在两者下方延伸的边框底部线
  2. 在同一行显示标签名称和值

这是我正在使用的 PHP

$output .= '<div class="property-details">';

foreach ( (array) $this->property_details as $label => $key ) {
$output .= sprintf( '<div class="label">%s</div><div class="value">%s</div>', $label, $value ) );
        }
$output .= '</div>';
return $output;

这给了我这个:

价格:

10,000,000

我需要的是这个

价格:10,000,000

名称:价值

所以每个标签和值都内联显示

标签: phphtmlcssforms

解决方案


尝试这个..

$output .= '<div class="property-details">';

foreach ( (array) $this->property_details as $label => $key ) {
$output .= sprintf( '<div class="label" style="display: inline-block;">%s</div><div class="value" style="display: inline-block;">%s</div>', $label, $value ) );
        }
$output .= '</div>';
return $output;

尽管除非您使用像 Bootstrap 这样的库,否则我鼓励您span改用:

$output .= '<div class="property-details">';

foreach ( (array) $this->property_details as $label => $key ) {
$output .= sprintf( '<span class="label" >%s</span ><span class="value">%s</span >', $label, $value ) );
        }
$output .= '</div>';
return $output;


对多行使用br标签:

$output .= '<div class="property-details">';

foreach ( (array) $this->property_details as $label => $key ) {
$output .= sprintf( '<span class="label" >%s</span ><span class="value">%s</span > <br>', $label, $value ) );
        }
$output = substr_replace($output , '', strrpos($output , '<br>'), 4);
$output .= '</div>';
return $output;


推荐阅读