当前位置:网站首页>常见布局效果实现方案
常见布局效果实现方案
2022-08-11 03:41:00 【bug丸】
常见布局效果实现方案
盒子居中
<body>
<div class="father">
<div class="child"></div>
</div>
</body>
方案1:flex布局
需不需要垂直居中就自己看需求而定,这里主要是水平居中
.father {
width: 100%;
// height: 600px;
background-color: #f5f5f5;
display: flex;
// align-items: center; 垂直(侧轴)方向的居中
justify-content: center;
}
.child {
height: 200px;
width: 200px;
background-color: skyblue;
}
方案2:定位 + tramsform
子绝父相:子盒子相对父盒子向下和向右走父盒子宽度的50%;然后在通过translate返回走自身的50%
.father {
width: 100%;
height: 600px;
background-color: #f5f5f5;
position: relative;
}
.child {
height: 200px;
width: 200px;
background-color: skyblue;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
方案3:定位 + margin
子绝父相:但是top/bottom/left/right都为0;通过margin:auto使其居中
.father {
width: 100%;
height: 600px;
background-color: #f5f5f5;
position: relative;
}
.child {
height: 200px;
width: 200px;
background-color: skyblue;
margin: 0;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
}
左侧固定宽度,右侧自适应
<div class="content">
<div class="left">左边</div>
<div class="right">右边</div>
</div>
方法1:float+overflow
.content {
height: 300px;
width: 100%;
}
.left {
background-color: pink;
height: 100%;
width: 200px;
float: left;
}
.right {
background-color: skyblue;
height: 100%;
overflow: hidden;
}
方法2:float+calc
宽度是通过calc来定,位置通过浮动的特点
.content {
height: 300px;
width: 100%;
}
.left {
background-color: pink;
height: 100%;
width: 200px;
float: left;
}
.right {
background-color: skyblue;
height: 100%;
width: calc(100% - 200px);
float: left;
}
方法3:inline-block + calc
inline-block,两个盒子的宽度刚好为100%,右边calc动态变化宽度
.content {
height: 300px;
width: 100%;
font-size: 0;
}
.left {
background-color: pink;
height: 100%;
width: 200px;
display: inline-block;
vertical-align: top;
}
.right {
background-color: skyblue;
height: 100%;
width: calc(100% - 200px);
display: inline-block;
vertical-align: top;
}
方法4:flex
flex:1 剩下宽度的都分给右边的盒子
.content {
display: flex;
height: 300px;
width: 100%;
}
.left {
height: 100%;
width: 200px;
background-color: pink;
}
.right {
flex: 1;
background-color: skyblue;
}
方法5:position+margin
左边脱离标准流不占位,右边宽度正好利用块级盒子的特点,margin不仅让盒子流出左边空白,宽度页刚好减去了margin值
.content {
height: 300px;
width: 100%;
position: relative;
}
.left {
background-color: pink;
height: 100%;
width: 200px;
position: absolute;
}
.right {
background-color: skyblue;
height: 100%;
margin-left: 200px;
}
边栏推荐
- Summary of debugging skills
- Idea (preferred) cherry-pick operation
- CSDN 博客更换皮肤
- Paper Accuracy - 2017 CVPR "High-Resolution Image Inpainting using Multi-Scale Neural Patch Synthesis"
- C language recv() function, recvfrom() function, recvmsg() function
- Talk about the understanding of RPC
- Get the length of the linked list
- AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis
- 树莓派入门(5)系统备份
- DOM-DOM tree, a DOM tree has three types of nodes
猜你喜欢

STC8H开发(十五): GPIO驱动Ci24R1无线模块

大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,如何分配?

How does MSP430 download programs to the board?(IAR MSPFET CCS)

Qnet弱网测试工具操作指南

【FPGA】day22-SPI协议回环

Interchangeable Measurement Techniques - Geometric Errors

A simple JVM tuning, learn to write it on your resume

C语言之自定义类型------结构体
![[BX] and loop](/img/3c/1be08db6898613c3a1c0bcd39e73be.png)
[BX] and loop

Multi-merchant mall system function disassembly 26 lectures - platform-side distribution settings
随机推荐
[BX] and loop
Binary tree related code questions [more complete] C language
The development of the massage chair control panel makes the massage chair simple and intelligent
Multi-serial port RS485 industrial gateway BL110
Build Zabbix Kubernetes cluster monitoring platform
MySQL数据库存储引擎以及数据库的创建、修改与删除
Summary of debugging skills
音视频开发,为什么要学习FFmpeg?应该怎么入手FFmpeg学习?
Get the length of the linked list
A brief analysis of whether programmatic futures trading or manual order is better?
Uni - app - access to Chinese characters, pinyin initials (according to the Chinese get pinyin initials)
机器学习中什么是集成学习?
Leetcode 450. 删除二叉搜索树中的节点
元素的BFC属性
MongoDB 基础了解(二)
电商项目——商城限时秒杀功能系统
Idea (preferred) cherry-pick operation
分布式和集群的区别和联系
QueryDet:级联稀疏query加速高分辨率下的小目标检测
C语言之自定义类型------结构体