当前位置:网站首页>Box horizontal vertical center layout (summary)
Box horizontal vertical center layout (summary)
2022-07-29 04:44:00 【Blue lover】
Catalog
2. inline-block( Only approximate when vertically centered , Imprecise !!!)
3. Absolute positioning + 2D transformation
4. Absolute positioning + margin: auto
1.flex Layout
.box {
margin: 100px auto;
width: 300px;
height: 300px;
border: 1px solid black;
display: flex;
justify-content: center;
align-items: center;
}
.child {
width: 50px;
height: 50px;
background-color: pink;
} <div class="box">
<div class="child"></div>
</div>2. inline-block( Only approximate when vertically centered , Imprecise !!!)
.box {
margin: 100px auto;
width: 300px;
height: 300px;
border: 1px solid black;
text-align: center;
line-height: 300px;
}
.child {
display: inline-block;
width: 50px;
height: 50px;
background-color: pink;
vertical-align: middle;
} <!--
to child Set up display: inline-block; hold child Become a box in the line , It can be regarded as img Label settings text-align: center; Horizontal center
Then increase the height of the parent box line-height: 300px; Sub box baseline alignment
-->
<div class="box">
<div class="child"></div>
</div>3. Absolute positioning + 2D transformation
.box {
position: relative;
margin: 100px auto;
width: 300px;
height: 300px;
border: 1px solid black;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 50px;
height: 50px;
background-color: pink;
} <div class="box">
<div class="child"></div>
</div>4. Absolute positioning + margin: auto
Just want to center horizontally right:0; left: 0;
.box {
position: relative;
margin: 100px auto;
width: 300px;
height: 300px;
border: 1px solid black;
}
.child {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto;
width: 50px;
height: 50px;
background-color: pink;
} <div class="box">
<div class="child"></div>
</div>5. table
Just want to center horizontally child Inside margin: 0 auto; Get rid of
.box {
margin: 100px auto;
width: 300px;
height: 300px;
border: 1px solid black;
display: table-cell; // The small square in the table
vertical-align: middle; // Cannot be used for block level elements , It can only act on inline elements and inline blocks . It's special here .
}
.child {
margin: 0 auto;
width: 50px;
height: 50px;
background-color: pink;
} <!--
Why does the parent box run ?
display: table-cell; After setting, the parent box is no longer a block element , therefore margin: 100px auto; It doesn't work
-->
<div class="box">
<div class="child"></div>
</div>边栏推荐
- [C language] PTA 7-52 finding the sum of the first n terms of a simple interleaved sequence
- 在线教育的推荐系统
- Basic grammar of C language
- Pyscript cannot import package
- EF core: one to one, many to many configuration
- EMI interference troubleshooting with near-field probe and current probe
- Actual combat of flutter - DIO of request encapsulation (II)
- Sign the college entrance examination
- def fasterrcnn_ resnet50_ FPN () instance test
- Leetcode (Sword finger offer) - 53 - I. find the number I in the sorted array
猜你喜欢

Flutter 手势监听和画板实现

SGuard64.exe ACE-Guard Client EXE:造成磁盘经常读写,游戏卡顿,及解决方案

What is the difference between field, variable and property

img 响应式图片的实现(含srcset属性、sizes属性的使用方法,设备像素比详解)

STL source code analysis (Hou Jie) notes -- Classification and testing of stl containers

Use of construction methods

MySQL - 聚簇索引和辅助索引

Classes and objects (III)

The most complete NLP Chinese and English stop words list in the whole station (including punctuation marks, which can be copied directly)

Ethernet of network
随机推荐
Idea small settings
UE plays video in scene or UMG
Simply change the picture color
Webrtc realizes simple audio and video call function
Flutter实战-请求封装(二)之dio
The most complete NLP Chinese and English stop words list in the whole station (including punctuation marks, which can be copied directly)
Implementation of img responsive pictures (including the usage of srcset attribute and sizes attribute, and detailed explanation of device pixel ratio)
Basic grammar of C language
Command line interactive tools (latest version) inquirer practical tutorial
Record the Niua packaging deployment project
Reveal installation configuration debugging
What is the use of meta-info?
Redux quick start
GCC Basics
def fasterrcnn_resnet50_fpn()实例测试
JVM (heap and stack) memory allocation
[C language] PTA 7-52 finding the sum of the first n terms of a simple interleaved sequence
Hengxing Ketong invites you to the 24th China expressway informatization conference and technical product exhibition in Hunan
TypeError: Cannot read properties of undefined (reading ‘then‘)
Dasctf2022.07 empowerment competition