简体   繁体   English

html 段内文本的条件 alignment

[英]Conditional alignment of text inside html paragraph

Here is table data that sometimes needs to be aligned right, and other times needs to be aligned center:这是有时需要右对齐,有时需要居中对齐的表格数据:

<td colspan="5"><p align="right"><?php echo number_format($total_adjusted_taxes,2) ?></p></td>

What determines whether to align right or centered is this variable:决定是右对齐还是居中的是这个变量:

<?php $r_or_c = (count($categories)==1?"right":"left");    
?>

What is the best way to use this variable $r_or_c inside the p tag?在 p 标签中使用这个变量$r_or_c的最佳方法是什么?

I'll write something like this.我会写这样的东西。 You're going to echo in place the correct value:您将在适当的位置回显正确的值:

<p align="<?php echo count($categories)=='1' ? 'right' : 'left'; ?>">....</p>

I prefer using CSS to style elements, though, so you can use the same method to assign a different class, or just style="text-align:<?php echo....;?>"> .不过,我更喜欢使用 CSS 来设置元素样式,因此您可以使用相同的方法来分配不同的 class,或者只是style="text-align:<?php echo....;?>"> Using class is better because you won't need to make changes in every single paragraph you write your inline style.使用 class 更好,因为您不需要在编写内联样式的每个段落中进行更改。

If you define your variable somewhere else, and just want to echo it (like your doing, it seems):如果您在其他地方定义变量,并且只想回显它(就像您所做的那样,似乎):

$r_or_c = count($categories)=='1' ? "right" : "left";

<p align="<?php echo $r_or_c;?>">....</p>

As someone else said, it's better not to use this inline styling, or you'll make your code more difficult to maintain in the future.正如其他人所说,最好不要使用这种内联样式,否则将来您的代码将更难以维护。 You could style the <td> instead of your paragraph also:您也可以设置<td>的样式而不是您的段落:

css: css:

  .left { text-align:left;}
  .right { text-align:right;}
 /* OR : */
 td.left {}
 td.right{}

html: html:

<td class="<?php echo $r_or_c;?>">......</td>

Also, you said "center" but wrote "left", don't know what you really need, so in case just subsitute 'left' with 'center', obviusly.另外,您说“中心”但写了“左”,不知道您真正需要什么,所以以防万一,显然用“中心”代替“左”。

<td colspan="5"><p align="<?php echo (count($categories)==1?"right":"left");?>"><?php echo number_format($total_adjusted_taxes,2) ?></p></td>

Is that what you asked?那是你问的吗?

Just shove it in there.塞进去就好了Not much complication to it.没有太大的复杂性。

<td colspan="5"><p align="<?=$r_or_c?>"><?php echo number_format($total_adjusted_taxes,2) ?></p></td>

On a side note, you should start using CSS for styling:)附带说明一下,您应该开始使用 CSS 进行样式设置:)

.align-right { text-align: right; }
.align-left { text-align: left; }

- -

<td colspan="5"><p class="align-<?=$r_or_c?>"><?php echo number_format($total_adjusted_taxes,2) ?></p></td>

As my comment to the question, as responded to by the OP:作为我对这个问题的评论,正如 OP 所回应的那样:

...was indeed [what] [he] was looking for. ......确实是[他]在寻找的东西。 Worked perfect[ly].工作完美[ly]。

I've opted to post that comment as an answer:我选择发布该评论作为答案:

I'd suggest removing the p tag entirely, and using <td style="text-align: <?php echo $r_or_c; ?>;"> .我建议完全删除p标签,并使用<td style="text-align: <?php echo $r_or_c; ?>;"> That, or assign a class-name to the td ('right' or 'center'), and use that to style the text-alignment .那,或者为td ('right' 或 'center')分配一个类名,并使用它来设置text-alignment的样式。

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

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