前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HTML 水平居中 垂直居中 垂直水平居中的几种实现方式「建议收藏」

HTML 水平居中 垂直居中 垂直水平居中的几种实现方式「建议收藏」

作者头像
Java架构师必看
发布2022-05-22 09:12:52
4.8K0
发布2022-05-22 09:12:52
举报
文章被收录于专栏:Java架构师必看Java架构师必看

大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说HTML 水平居中 垂直居中 垂直水平居中的几种实现方式「建议收藏」,希望能够帮助大家进步!!!

文章目录

代码语言:txt
复制
- 水平居中
- 垂直居中垂直水平居中方式1:绝对定位方式二 定位+负margin方式3:使用translate实现平移方式4:通过设置bottom top left right margin来实现方式5:flex布局方式6:使用tablecell方式7:JS方式

水平居中

  1. 方法一:在父容器上定义固定宽度,margin值设成auto
代码语言:javascript
复制
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>helloworld</title>
	</head>
	<style type="text/css">
		.div1{
			background-color: blue;
			width: 200px;
			margin: auto;
		}
		.div2{
			background-color: orangered;
		}
	</style>
	<body>
		<div class="div1">
			<div class="div2">
				你好啊!!!!!!!!!
			</div>
		</div>
	</body>
	
</html>

只听到从架构师办公室传来架构君的声音:

相见无言还有恨,几回判却又思量,月窗香径梦悠飏。有谁来对上联或下联?

方法2:在子元素中将display设置为inline-block,父元素text-algin设置为center

代码语言:javascript
复制
此代码由Java架构师必看网-架构君整理
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>helloworld</title>
	</head>
	<style type="text/css">
		.div1{
			background-color: blue;
			text-align: center;
		}
		.div2{
			display: inline-block;
			background-color: orangered;
		}
	</style>
	<body>
		<div class="div1">
			<div class="div2">
				你好啊!!!!!!!!!
			</div>
		</div>
	</body>
	
</html>

垂直居中

代码语言:javascript
复制
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>helloworld</title>
	</head>
	<style type="text/css">
		.div1{
			width: 200px;
			height: 200px;
			display: table-cell;
    		vertical-align: middle;
    		text-align: center;     
		}
		.div2{
			/*display: inline-block;*/
			background-color: orangered;
		}
	</style>
	<body>
		<div class="div1">
			<div class="div2">
				你好啊!!!!!!!!!
			</div>
		</div>
	</body>
	
</html>
代码语言:javascript
复制
此代码由Java架构师必看网-架构君整理
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>helloworld</title>
	</head>
	<style type="text/css">
		.div1{
			width: 200px;
			height: 200px;
			display: flex;
    		justify-content:center;
    		align-items:Center;
		}
		.div2{
			/*display: inline-block;*/
			background-color: orangered;
		}
	</style>
	<body>
		<div class="div1">
			<div class="div2">
				你好啊!!!!!!!!!
			</div>
		</div>
	</body>
	
</html>

垂直水平居中

方式1:绝对定位

代码语言:javascript
复制
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>居中</title>
		<style>
			*{
				padding: 0;
				margin: 0;
			}
			.box{
				width: 500px;
				height: 400px;
				background: red;
				margin: 100px;
				//border: 2px;
			}
			.child{
				position: absolute;
				width: 200px;
				height: 150px;
				background: blue;
				margin: 125px 150px;
			}
		</style>
	</head>
	<body>
 
				<div class="box">
				    <div class="child"></div>
				</div>
		
	</body>
</html>

方式二 定位+负margin

代码语言:javascript
复制
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>居中</title>
		<style>
			.child{
				position: fixed;
				width: 200px;
				height: 150px;
				background: blue;
				top:50%;
				left: 50%;
				margin-top: -75px;
				margin-left:-100px ;
				
			}
		</style>
	</head>
	<body>
		 <div class="child"></div>
	</body>
</html>

方式3:使用translate实现平移

代码语言:javascript
复制
  下面的transform代码可以更换为transform: translate(-50%,-50%);
代码语言:javascript
复制
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>居中</title>
		<style>
			.child{
				position: fixed;
				width: 200px;
				height: 150px;
				background: blue;
				top:50%;
				left: 50%;
				transform: translate(-100px,-75px);
			}
		</style>
	</head>
	<body>
		 <div class="child"></div>
	</body>
</html>

效果如上图。

方式4:通过设置bottom top left right margin来实现

代码语言:javascript
复制
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>居中</title>
		<style>
			.father{
				width: 500px;
				height: 500px;
				background: black;
				position: relative;
			}
			.child{
				position: absolute;
				width: 200px;
				height: 150px;
				background: blue;
				top:0;
				left:0;
				right: 0;
				bottom: 0;
				margin: auto;
			}
		</style>
	</head>
	<body>
		<div class="father">
			<div class="child"></div>
		</div>
		 
	</body>
</html>

方式5:flex布局

代码语言:javascript
复制
最长使用,设置 display: flex;justify-content: center;align-items: center;三个属性;
代码语言:javascript
复制
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>居中</title>
		<style>
	.father { 
   
        display: flex;
        justify-content: center;
        align-items: center;
        width: 500px;
        height: 500px;
        background-color: rgb(197, 34, 34);    
    }
    .child { 
   
        width: 50px;
        height: 70px;
        background-color: rgb(36, 167, 64);
    } 
	</style>
	</head>
	<body>
		<div class="father">
			<div class="child"></div>
		</div>
		 
	</body>
</html>

方式6:使用tablecell

代码语言:javascript
复制
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>居中</title>
	<style>
	  .father { 
   

        display: table-cell;
    	vertical-align: middle;
    	text-align: center;    
        width: 500px;
        height: 500px;
        background-color: rgb(197, 34, 34);
        
    }

    .child { 
   
        display: inline-block;
        width: 50px;
        height: 70px;
        background-color: rgb(36, 167, 64);
    } 
	</style>
	</head>
	<body>
		<div class="father">
			<div class="child"></div>
		</div>
		 
	</body>
</html>

方式7:JS方式

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    
    .father { 
     
        width: 500px;
        height: 500px;
        background-color: rgb(197, 34, 34);
    }
    .child { 
   
        width: 50px;
        height: 70px;
        background-color: rgb(28, 150, 123);
    }
</style>

<body>
    <div class="father">
        <div class="child">
        </div>
    </div>
</body>
    <script>
        let father=document.getElementsByClassName('father')[0];
        let child=document.getElementsByClassName('child')[0];
        fatherH=father.clientHeight;
        fatherW=father.clientWidth;
        childW=child.offsetWidth;
        childH=child.offsetHeight;
        child.style.position="absolute";
        child.style.top=(fatherH-childH)/2+'px';
        child.style.left=(fatherW-childW)/2+'px';


    </script>
</html>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-05-222,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 水平居中
  • 垂直居中
  • 垂直水平居中
    • 方式1:绝对定位
      • 方式二 定位+负margin
        • 方式3:使用translate实现平移
          • 方式4:通过设置bottom top left right margin来实现
            • 方式5:flex布局
              • 方式6:使用tablecell
                • 方式7:JS方式
                相关产品与服务
                容器服务
                腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档