doumi1884 2019-03-06 22:31
浏览 107
已采纳

当php中没有内容时更改背景颜色

I am working on a php/wordpress website in which I want to hide the background-color when there is no content.

The php code which I am using is:

<div class="featured-block__content">
    <h1 class="featured-block__tag"><?php echo esc_html( explode('|',get_the_title())[1]); ?></h1>
</div>

The css code for the above html/php code is:

.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;
}

Problem Statement:

I am wondering what changes I need to make in the php code above so that where there is no content inside h1 tag, the background-color should be background-color: rgba(0, 0, 0, 0);

At this moment where there is content inside h1 tag, the background-color is background-color: rgb(255, 255, 255);

  • 写回答

3条回答 默认 最新

  • du2986 2019-03-06 22:37
    关注

    You can use the following:

    <?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>
    

    With the following additional CSS:

    .featured-block__content.no-h1 {
        background-color: rgba(0, 0, 0, 0);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法