当前位置:网站首页>Seven Ways to Center a Box Horizontally and Vertically
Seven Ways to Center a Box Horizontally and Vertically
2022-08-05 06:22:00 【MoXinXueWEB】
方法一:定位+ 平移
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style> .parent {
width: 500px; height: 500px; border: 1px solid #000; position: relative; } .child {
width: 100px; height: 100px; border: 1px solid #999; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } </style>
</head>
<body>
<div class="parent">
<div class="child">我是子元素</div>
</div>
</body>
</html>
方法二:弹性布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style> .parent {
width: 500px; height: 500px; border: 1px solid #000; display: flex; justify-content: center; align-items: center; } .child {
width: 100px; height: 100px; border: 1px solid #999; } </style>
</head>
<body>
<div class="parent">
<div class="child">我是子元素</div>
</div>
</body>
</html>
方法三:网格 Grid
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style> .parent {
width: 500px; height: 500px; border: 1px solid #000; display: grid; place-items: center; } .child {
width: 100px; height: 100px; border: 1px solid #999; } </style>
</head>
<body>
<div class="parent">
<div class="child">我是子元素</div>
</div>
</body>
</html>
方法四:margin:auto
;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style> .parent {
width: 500px; height: 500px; border: 1px solid #000; position: relative; } .child {
width: 100px; height: 100px; border: 1px solid #999; position: absolute; margin: auto; top: 0; left: 0; right: 0; bottom: 0; } </style>
</head>
<body>
<div class="parent">
<div class="child">我是子元素</div>
</div>
</body>
</html>
方法五:表格布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style> .parent {
width: 500px; height: 500px; border: 1px solid #000; display: table-cell; vertical-align: middle; text-align: center; } .child {
width: 100px; height: 100px; border: 1px solid #999; display: inline-block; } </style>
</head>
<body>
<div class="parent">
<div class="child">我是子元素</div>
</div>
</body>
</html>
方法六:计算父盒子与子盒子的空间距离
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style> .parent {
width: 500px; height: 500px; border: 1px solid #000; } .child {
width: 100px; height: 100px; border: 1px solid #999; margin-top: 200px; margin-left: 200px; } </style>
</head>
<body>
<div class="parent">
<div class="child">我是子元素</div>
</div>
</body>
</html>
方法七:定位+负边距
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style> .parent {
width: 500px; height: 500px; border: 1px solid #000; position: relative; } .child {
width: 100px; height: 100px; border: 1px solid #999; position: absolute; top: 50%; left: 50%; margin-top: -50px; margin-left: -50px; } </style>
</head>
<body>
<div class="parent">
<div class="child">我是子元素</div>
</div>
</body>
</html>
边栏推荐
- VLAN is introduced with the experiment
- [issue resolved] - jenkins pipeline checkout timeout
- el-autocomplete使用
- Technology Sharing Miscellaneous Technologies
- 带你深入了解Cookie
- Quick question and quick answer - FAQ of Tencent Cloud Server
- 7步完成云上监控
- Unity realizes first-person roaming (nanny-level tutorial)
- 5分钟完成mysql离线安装
- The hook of the operation of the selenium module
猜你喜欢
By solving these three problems, the operation and maintenance efficiency will exceed 90% of the hospital
深度 Zabbix 使用指南——来自惨绿少年
ALC实验
King power volume LinkSLA, realize operations engineer is happy fishing
增长:IT运维发展趋势报告
LeetCode练习及自己理解记录(1)
spark operator - map vs mapPartitions operator
Vim tutorial: vimtutor
Switch principle
网络不通?服务丢包?看这篇就够了
随机推荐
The Servlet to jump to the JSP page, forwarding and redirection
The size of the screen adaptation
The method of using ROS1 bag under ROS2
Browser Storage for H5
The problem come from line screening process
One-arm routing experiment and three-layer switch experiment
What should I do if the SSL certificate prompts that it is expired or invalid?
The highlight moment of operation and maintenance starts with intelligence
用户和用户组管理、文件权限管理
input detailed file upload
markdown editor template
网络层协议介绍
Mina disconnects and reconnects
Switch principle
Tencent Cloud Message Queue CMQ
In-depth Zabbix user guide - from the green boy
This is indeed the best article on microservice architecture I have read!
Does flink cdc currently support Gauss database sources?
Disk management and file systems
DisabledDate date picker datePicker