当前位置:网站首页>8 vertical centering methods
8 vertical centering methods
2022-06-23 01:54:00 【Bejpse】
Eight vertical centering methods
Vertically centered requirements are often encountered , Eight methods of vertical centering are practiced through data .
The following methods revolve around this HTML an
HTML Code
<div class="wrap">
<div class="box"></div>
</div>
CSS
Method 1( Commonly used ):display:flex
.wrap{
width:300px;
height:300px;
border: 1px solid red;
display:flex;
justify-content:center;
align-items:center;
}
.box{
height:100px;
width:100px
boder:1px solid blue;
}
Method 2: table-cell
vertical-align Property to set the vertical alignment of an element .
This attribute defines the vertical alignment of the baseline of the element in the row with respect to the baseline of the element in the row . It is allowed to specify a negative length value and a percentage value . This will make the element lower rather than higher . In table cells in , This property sets the alignment of the cell contents in the cell box .
.wrap{
width: 300px;
height: 300px;
border: 1px solid red;
display: table-cell;
text-align: center;
vertical-align: middle;
}
.box{
width: 100px;
height: 100px;
border: 1px solid blue;
display: inline-block;
}
Method 3: margin,transform coordination
.wrap{
width: 300px;
height: 300px;
background-color: pink;
/* border: 1px solid red; */
/* Prevent collapse of outer margin . The method to solve the collapse of outer margin :
Parent element plus overflow:hidden Or add a border */
overflow: hidden;
}
.box{
width: 100px;
height: 100px;
background-color: plum;
margin:50% auto;
transform: translateY(-50%);
}
Method 4: inline-block+vertical-aligin
.wrap {
width: 300px;
height: 300px;
background-color: pink;
text-align: center;
line-height: 300px;
}
.box {
width: 100px;
height: 100px;
/* Reset the row height of child elements , Otherwise, the row height of the parent element will be inherited */
line-height: 100px;
background-color: plum;
display: inline-block;
/* middle Place this element in the middle of the parent element . */
vertical-align: middle;
}
Method 5:absolute+ negative margin
.wrap{
width: 300px;
height: 300px;
position: relative;
background-color: plum;
}
.box{
width: 100px;
height: 100px;
position: absolute;
left: 50%;
top: 50%;
/* Half the width and half the height */
margin-left: -50px;
margin-top: -50px;
background-color: powderblue;
}
Method 6 absolute+margin:auto
And methods 5 similar , When width and height are unknown
.wrap{
width: 300px;
height: 300px;
position: relative;
background-color: plum;
}
.box{
width: 100px;
height: 100px;
position: absolute;
left: 0;
top: 0;
bottom:0;
right:0;
margin:auto;
background-color: powderblue;
}
Method 7:absolute+transform
With the method 5 similar
.wrap {
width: 300px;
height: 300px;
position: relative;
background-color: plum;
}
.box {
width: 100px;
height: 100px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
background-color: powderblue;
}
Method 8 Powerful grid
Ruan Yifeng Grid Grid layout tutorial
.wrap {
width: 300px;
height: 300px;
display: grid;
background-color: plum;
}
.box {
width: 100px;
height: 100px;
align-self: center;
justify-self: center;
background-color: powderblue;
}
边栏推荐
- [cmake command notes]find_ path
- Centos7 installing postgresql12
- Pat class A - 1013 battle over cities
- Exercise analysis summary
- How are pub and sub connected in ros1?
- Install MySQL (5.7+8.0) through docker and configure master-slave replication (gtid+ enhanced semi synchronization)
- 4. functions and inline functions with default values for formal parameters
- How to download online printing on Web pages to local PDF format (manual personal test)
- [luogu] p1083 [noip2012 improvement group] borrow classroom (line segment tree)
- //1.9 char character variable operation
猜你喜欢

7.new, delete, OOP, this pointer

Browser independent way to detect when image has been loaded

1. Mx6u bare metal program (4) - GPIO module

Component development

1. Mx6u bare metal program (5) - external interrupt

Binary String

Three methods for solving Fibonacci sequence feibonacci (seeking rabbit) - program design

How to type Redux actions and Redux reducers in TypeScript?

1. Mx6u bare metal program (6) - timer

Error reported when compiling basalt
随机推荐
//1.17 printf function
Zabbix5 series - use temperature and humidity sensor to monitor the temperature and humidity of the machine room (XX)
//1.9 char character variable operation
Summary of the first week of winter vacation
Data skew analysis of redis slice cluster
On function overloading from several examples
Day500: keyboard line
SQL programming task03 job - more complex query
【CodeWars】 Pete, the baker
Garbled code of SecureCRT, double lines, double characters, unable to input (personal detection)
Openvino CPU acceleration survey
Pat a - 1010 radical (thinking + two points)
Analysis of current mainstream video coding technology | community essay solicitation
Dynamic address book in C language (add, delete, modify, check (duplicate), delete, sort and export)
Bc110 tic tac toe chess
Questions not written in the monthly contest
Pat class A - 1007 maximum subsequence sum
C language foundation ----- write a function to find the larger value of two unequal integers
[ZOJ] P3228 Searching the String
Module 8 job