当前位置:网站首页>Center an element horizontally and vertically
Center an element horizontally and vertically
2022-07-26 08:55:00 【Arrogance.miner】
Method 1 : We know the width and height of this element
.div1 {
background-color: blue;
width: 100px;
height: 100px;
/* <!-- Turn elements into positioning elements --> */
position: absolute;
/* <!-- Set the location of the element , Distance 、 Zuo Duwei 50%--> */
left: 50%;
top: 50%;
/* <!-- Set the left outer margin of the element 、 The upper and outer margins are negative for width and height 1/2--> */
margin-left: -50px;
margin-top: -50px;
}
Method 2 : Use transform
.div1 {
background-color: blue;
width: 100px;
height: 100px;
/* <!-- Turn elements into positioning elements --> */
position: absolute;
/* <!-- Set the location of the element , Distance 、 Zuo Duwei 50%--> */
left: 50%;
top: 50%;
/* <!-- Set the element's offset from itself to be negative 50%( That's half the size of the element itself )--> */
transform:translate(-50%,-50%);
}
shortcoming : Poor compatibility , Can only support IE9+ Browser
Method 3 : margin:auto
Parent element Settings position: relative;
Subelement :
.div1 {
background-color: blue;
width: 100px;
height: 100px;
/* <!-- Turn elements into positioning elements --> */
position: absolute;
/* <!-- Set the location of the element , Distance 、 The bottom left and right are 0--> */
left: 0;
right: 0;
top: 0;
bottom: 0;
/* <!-- Set the element's offset from itself to be negative 50%( That's half the size of the element itself )--> */
margin: auto;
}
shortcoming : I won't support it IE7 Next browser
Method four : Elastic layout flex
Add... To the parent element :
.div0 {
border: 1px solid #000;
width: 300px;
height: 230px;
/* position: relative; */
display: flex;
justify-content: center;
align-items: center;
}
边栏推荐
- MySQL 8.0 OCP 1z0-908 certification examination question bank 1
- Oracle 19C OCP 1z0-082 certification examination question bank (24-29)
- General file upload vulnerability getshell of a digital campus system (penetration test -0day)
- OA项目之我的会议(查询)
- Typescript encryption tool passwordencoder
- CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主?
- What are the contents of Oracle OCP and MySQL OCP certification exams?
- Pxe原理和概念
- Dynamic SQL and exceptions of pl/sql
- Pytoch realizes logistic regression
猜你喜欢

JS file import of node

Sklearn machine learning foundation (linear regression, under fitting, over fitting, ridge regression, model loading and saving)

JDBC数据库连接池(Druid技术)

2000年的教训。web3是否=第三次工业革命?

Regular expression: judge whether it conforms to USD format

Memory management - dynamic partition allocation simulation

Excel find duplicate lines

at、crontab

Study notes of automatic control principle -- dynamic model of feedback control system

Web overview and b/s architecture
随机推荐
Day06 homework - skill question 7
Learning notes of automatic control principle - Performance Analysis of continuous time system
MySQL 8.0 OCP 1z0-908 certification examination question bank 1
My meeting of OA project (query)
at、crontab
Self review ideas of probability theory
keepalived双机热备
合工大苍穹战队视觉组培训Day6——传统视觉,图像处理
IC's first global hacking bonus is up to US $6million, helping developers venture into web 3!
Pytoch learning - from tensor to LR
Uni app simple mall production
Human computer interaction software based on C language
The idea shortcut key ALT realizes the whole column operation
【加密周报】加密市场有所回温?寒冬仍未解冻 盘点上周加密市场发生的重大事件
TCP solves the problem of short write
Pytoch realizes logistic regression
Install HR schema, example, and Scott schema on Oracle and MySQL
sklearn 机器学习基础(线性回归、欠拟合、过拟合、岭回归、模型加载保存)
[search topics] flood coverage of search questions after reading the inevitable meeting
File management file system based on C #