css 超实用的:empty —— 隐藏空元素、缺失字段智能提示

应用1:隐藏空元素

有时空元素会影响页面布局,通过:empty可以很方便地将它们隐藏。
在这里插入图片描述

<template>
  <ol>
    <li></li>
    <li></li>
    <li></li>
  </ol>
</template>

<style scoped>
li:empty {
  display: none;
}
</style>

在这里插入图片描述
注意事项:若是v-for循环,很多格式化插件在内容为空时,默认也会生成空格,则无法匹配:empty,解决方案是多加一层div

<template>
  <div class="bigBox">
    <div class="smallBox">1</div>
    <div class="smallBox">2</div>
    <div class="smallBox"></div>
    <div class="smallBox">4</div>
  </div>
</template>
<style scoped>
.smallBox {
  height: 60px;
  width: 100px;
  background: red;
  color: white;
  text-align: center;
  line-height: 60px;
}
.smallBox:empty {
  display: none;
}
.bigBox {
  margin: 30px;
  width: 60%;
  display: flex;
  justify-content: space-around;
}
</style>

应用2:缺失字段智能提示

在这里插入图片描述

<template>
  <div class="bigBox">
    <div class="smallBox" v-for="(item, index) in list" :key="index">
      <span class="content">{{ item }}</span>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      list: [1, 2, , 4],
    };
  },
};
</script>

<style scoped>
.bigBox {
  width: 60%;
  margin: 20px;
  display: flex;
  justify-content: space-around;
}
.smallBox {
  height: 60px;
  width: 100px;
  background: red;
  color: white;
  text-align: center;
  line-height: 60px;
}
.content:empty::before {
  content: "暂无编号";
}
</style>

在这里插入图片描述

<template>
  <table border>
    <tr>
      <td>1</td>
      <td></td>
      <td>3</td>
    </tr>
  </table>
</template>


<style scoped>
td {
  min-width: 100px;
  text-align: center;
}
td:empty::before {
  content: "暂无数据";
}
</style>

类似的常用场景有:

.searchResult:empty::before {
   content: '没找到相关内容';
   display: block;
   line-height: 300px;
   text-align: center;
   color: gray;
}
.articleList:empty::before {
   content: '还没有发表任何文章';
   display: block;
   line-height: 300px;
   text-align: center;
   color: gray;
}

全局空元素样式的css

.empty:empty::before {
   content: '暂无数据';
   display: block;
   line-height: 300px;
   text-align: center;
   color: gray;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朝阳39

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值