CSS 常用布局

一、Flex 布局的自适应子项内容过长导致其被撑大问题

效果图:
请添加图片描述
参考:https://blog.csdn.net/u012557814/article/details/114102731
实现:
问题还原:
flex 布局中,把其中一个子项设置 flex-grow: 1,可以使它自动占据所有剩余空间,但是当该子项内容过长时,子项长度会被长内容给撑开而出现滚动条。

<div class="box">
	<div class="item-1">xxxx</div>
	<div class="item-2">
    <div class="item-3">
          long content - long content - long content 
          - long content - long content
          long content - long content - long content 
          - long content - long content
          long content - long content - long content 
          - long content - long content
      </div>
  </div>
</div>
.box {
	display: flex;
}
.item-2 {
	flex: 1;
}
.item-3 {
  white-space: nowrap;
  width: 100%;
  overflow: hidden;
}

解决
给自适应的子项加一个 width: 0 可以完美解决该问题。

.box {
	display: flex;
}
.item-2 {
	flex: 1;
  width: 0;
}
.item-3 {
  white-space: nowrap;
  width: 100%;
  overflow: hidden;
}

二、css实现填充剩余高度

参考: https://blog.csdn.net/u012557814/article/details/120709653
自动填充高度.html

<div class="box">
  <div class="header">头部</div>
  <div class="auto-fill">自动填充</div>
</div>

使用 flex(推荐)

  • 兼容性好;
  • 只需关注自动填充的 div,无需考虑其他 div 的高度
.box {
  display: flex;
  flex-flow: column;
  height: 100%;
}

.header {
  background: dodgerblue;
  height: 30px;
}

.auto-fill {
  background: wheat;
  flex: 1
}

使用绝对定位(一般)

  • 无兼容问题;
  • 但需要知道其他 div 已占据的高度
.box {
  height: 100%;
}

.header {
  background: dodgerblue;
  height: 30px;
}

.auto-fill {
  background: wheat;
  position: absolute;
  top: 30px;
  bottom: 0px;
}

使用 calc 函数(一般)

  • 无兼容问题;
  • 但需要知道其他 div 已占据的高度;
.box {
  height: 100%;
}

.header {
  background: dodgerblue;
  height: 30px;
}

.auto-fill {
  background: wheat;
  height: calc(100% - 30px);
}

三、不固定宽高的元素水平垂直居中

参考:https://blog.csdn.net/w390058785/article/details/77992457

四、两边固定,中间自适应

参考:https://blog.csdn.net/qq_43368682/article/details/108507746

五、CSS图片背景

在这里插入图片描述

<div class="empty-search">
	<div class="img-box"></div>
	<!-- <img :src="searchNodataImg" alt="" /> -->
	<div class="text">未搜索到相关内容</div>
</div>
.empty-search {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  img {
    width: 180px;
    height: 135px;
  }
  .img-box {
    width: 180px;
    height: 135px;
    background-image: url("~@/assets/images/common/empty_dark.png");
    background-size: cover;
    /* background-image: url("~@/assets/images/common/empty_dark.png"); */ 
    /* background-repeat: no-repeat; */ 
    /* background-size: 100% 100%; */ 
  }
  .text {
    font-size: 14px;
    font-family: Microsoft YaHei-Regular, Microsoft YaHei;
    font-weight: 400;
    color: #c0c2c4;
    line-height: 18px;
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Windyluna

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

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

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

打赏作者

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

抵扣说明:

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

余额充值