php - 当php中没有内容时更改背景颜色
问题描述
我正在一个 php/wordpress 网站上工作,我想在没有内容时隐藏背景颜色。
我正在使用的 php 代码是:
<div class="featured-block__content">
<h1 class="featured-block__tag"><?php echo esc_html( explode('|',get_the_title())[1]); ?></h1>
</div>
上述html/php代码的css代码为:
.featured-block__content {
display: flex;
justify-content: center;
align-items: center;
height: 85px;
background-color: rgb(255, 255, 255);
opacity: 0.831;
z-index: 3;
position: relative;
bottom: 85px;
}
问题陈述:
我想知道我需要在上面的 php 代码中进行哪些更改,以便在 h1 标记内没有内容的地方,背景颜色应该是background-color:
rgba(0, 0, 0, 0);
此时h1标签内有内容,背景色为background-color: rgb(255, 255, 255);
解决方案
您可以使用以下内容:
<?php
$h1_content = esc_html(explode('|', get_the_title())[1]);
?>
<div class="featured-block__content <?= trim($h1_content) === '' ? 'no-h1' : ''; ?>">
<h1 class="featured-block__tag"><?= $h1_content; ?></h1>
</div>
使用以下附加 CSS:
.featured-block__content.no-h1 {
background-color: rgba(0, 0, 0, 0);
}
推荐阅读
- excel - Microsoft Excel 使用 MDX 连接到 HANA
- python - 如何获取所有不包含数字的特定长度的单词?
- php - 通过地址栏注销后保留各个警卫登录的路线
- angular - 将两个单独的 Angular 项目合并到 apache 服务器上的一个 URL
- javascript - Javascript:为单页应用程序存储一个全局变量
- c# - 导入到 SQL Server 后在数据库中显示额外的行
- flutter - 如何正确阅读 ModalRoute.of(context).settings.arguments?
- laravel - Laravel 上传多个文件
- html - 防止文本破坏 flexbox 容器
- javascript - 使用reactjs在Safari中滚动页面后单击事件不起作用