当前位置:网站首页>常见布局效果实现方案
常见布局效果实现方案
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;
}
边栏推荐
- 浮点数在内存中的存储方式
- [BX] and loop
- Graphical LeetCode - 640. Solving Equations (Difficulty: Moderate)
- 【ADI低功耗2k代码】基于ADuCM4050的ADXL363、TMP75的加速度、温度检测及串口打印、蜂鸣器播放音乐(孤勇者)
- Leetcode 450. 删除二叉搜索树中的节点
- AI + medical: for medical image recognition using neural network analysis
- watch监听
- LeetCode Hot Questions (12. The Best Time to Buy and Sell Stocks)
- QueryDet: Cascading Sparse Query Accelerates Small Object Detection at High Resolution
- uni-app - 城市选择索引列表 / 通过 A-Z 排序的城市列表(uview 组件库 IndexList 索引列表)
猜你喜欢
How does MSP430 download programs to the board?(IAR MSPFET CCS)
多串口RS485工业网关BL110
What should I do if the channel ServerID is incorrect when EasyCVR is connected to a Hikvision Dahua device and selects another cluster server?
Detailed explanation of VIT source code
Description of ESB product development steps under cloud platform
互换性与测量技术——表面粗糙度选取和标注方法
A large horse carries 2 stone of grain, a middle horse carries 1 stone of grain, and two ponies carry one stone of grain. It takes 100 horses to carry 100 stone of grain. How to distribute it?
[yu gong series] Go program 035-08 2022 interfaces and inheritance and transformation and empty interface
索引的创建、查看、删除
Qnet弱网测试工具操作指南
随机推荐
我的 archinstall 使用手册
How does MSP430 download programs to the board?(IAR MSPFET CCS)
Redis老了吗?Redis与Dragonfly性能比较
【FPGA】day22-SPI协议回环
【FPGA】day19-二进制转换为十进制(BCD码)
Rotary array problem: how to realize the array "overall reverse, internal orderly"?"Three-step conversion method" wonderful array
uni-app - 获取汉字拼音首字母(根据中文获取拼音首字母)
索引的创建、查看、删除
The "top pillar" slides, and new growth is extremely difficult to shoulder the heavy responsibility. Is Ali "squatting" to jump higher?
用户如何克服程序化交易中的情绪问题?
图解LeetCode——640. 求解方程(难度:中等)
什么是三方支付?
Enter the starting position, the ending position intercepts the linked list
A large horse carries 2 stone of grain, a middle horse carries 1 stone of grain, and two ponies carry one stone of grain. It takes 100 horses to carry 100 stone of grain. How to distribute it?
【FPGA】day21-移动平均滤波器
Interchangeability Measurements and Techniques - Calculation of Deviations and Tolerances, Drawing of Tolerance Charts, Selection of Fits and Tolerance Classes
机器学习中什么是集成学习?
云平台下ESB产品开发步骤说明
轮转数组问题:如何实现数组“整体逆序,内部有序”?“三步转换法”妙转数组
构建程序化交易系统需要注意什么问题?