水平和垂直居中方法总结

本文详细介绍了多种在HTML和CSS中实现元素水平和垂直居中的方法,包括文本居中、行内元素、块级元素、单行文本和多行文本的垂直居中,以及不同场景下的块元素垂直居中策略,如已知高度和未知高度的父元素情况。同时,还涵盖了使用Flexbox布局进行居中的简便方法。
摘要由CSDN通过智能技术生成


前面或多或少讲过一些水平垂直居中的方法,这里总结一下常用的几种。

水平居中

文本水平居中

多行水平居中意义不大,这里只讲单行文本。设置text-align:center即可。

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title>文本水平居中</title>
  <style type="text/css">
	div{
		width:300px;
		height:40px;
		background-color: #1E90FF;
		text-align:center;
	}
  </style>
 </head>
 <body>
	<div>单行文本水平居中</div>
 </body>
</html>

在这里插入图片描述

元素水平居中

行内元素水平居中

和单行文本水平居中方法一致。

块元素水平居中

方法一:将自身的margin-left和margin-right设置成auto;

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <style type="text/css">
	.father{
		width:300px;
		height:80px;
		background-color: yellowgreen;
	}
	.son{
		width:200px;
		height:70px;
		margin:0 auto;
		background-color:lightgoldenrodyellow;
		text-align:center;
	}
  </style>
 </head>
 <body>
	<div class="father">
		<div class="son">块元素水平居中</div>
	</div>
 </body>
</html>

方法二:将要水平居中的元素转化为行内元素或行内块元素,父元素将text-align设置成center。

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <style type="text/css">
	.father{
		width:300px;
		height:80px;
		background-color: yellowgreen;
		text-align:center;
	}
	.son{
		width:200px;
		height:70px;
		background-color:lightgoldenrodyellow;
		display:inline-block;
	}
  </style>
 </head>
 <body>
	<div class="father">
		<div class="son">块元素水平居中</div>
	</div>
 </body>
</html>

方法三:利用flex布局:

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <style type="text/css">
	.father{
		width:300px;
		height:100px;
		background-color: yellowgreen;
		display:flex;
		justify-content: center;
	}
	.son{
		background-color: antiquewhite;
		width:200px;
		height:60px;
	}
  </style>
 </head>
 <body>
	<div class="father">
		<div class="son">k块元素水平居中</div>
	</div>
	</div>
 </body>
</html>

三者方法的运行结果皆如下:
在这里插入图片描述

垂直居中

文本垂直居中

单行文本垂直居中

设置height和line-height相等即可。

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <style type="text/css">
	.father{
		width:240px;
		height:80px;
		line-height: 80px;;
		background-color: yellowgreen;
	}
  </style>
 </head>
 <body>
	<div class="father">单行文本垂直居中</div>
	</div>
 </body>
</html>

在这里插入图片描述

多行文本垂直居中

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <style type="text/css">
	.father{
		width:120px;
		height:90px;
		background-color: yellowgreen;
		display:table-cell;
		vertical-align: middle;
	}
	.son{
		display:inline-block;
	}
  </style>
 </head>
 <body>
	<div class="father">
		<span class="son">多行文本垂直居中
		</span>
	</div>
	</div>
 </body>
</html>

在这里插入图片描述

元素垂直居中

块元素垂直居中

父元素已知高度

父元素{
position:relative;
}
子元素{
position:absolute;
top:50%;
left:50%;
margin-top:负子元素高度的一半;
margin-left:负子元素宽度的一半;
}
例子如下:

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <style type="text/css">
	.father{
		position:relative;
		width:300px;
		height:100px;
		background-color: yellowgreen;	
	}
	.son{
		background-color: antiquewhite;
		width:200px;
		height:60px;
		position:absolute;
		top:50%;
		left:50%;
		margin-top:-30px;
		margin-left:-100px;
	}
  </style>
 </head>
 <body>
	<div class="father">
		<div class="son"></div>
	</div>
	</div>
 </body>
</html>
父元素未知高度

利用flex布局

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title></title>
  <style type="text/css">
	.father{
		width:300px;
		height:100px;
		background-color: yellowgreen;
		display:flex;
		align-items: center;
	}
	.son{
		background-color: antiquewhite;
		width:200px;
		height:60px;
	}
  </style>
 </head>
 <body>
	<div class="father">
		<div class="son"></div>
	</div>
	</div>
 </body>
</html>

运行结果都如下:
在这里插入图片描述

行内块元素垂直居中

和多行文本垂直居中原理一样!就不再赘述了 。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 使用 Flex 布局可以使元素水平垂直居中。首先,将父元素的 `display` 设置为 `flex`,然后使用 `align-items` 和 `justify-content` 属性设置垂直和水平对齐方式。 具体来说,要使元素垂直居中,可以将 `align-items` 设置为 `center`。要使元素水平居中,可以将 `justify-content` 设置为 `center`。 例如,以下代码将一个按钮元素水平垂直居中: ```css .parent { display: flex; align-items: center; justify-content: center; } .button { /* 其他样式 */ } ``` ```html <div class="parent"> <button class="button">按钮</button> </div> ``` ### 回答2: flex布局是一种现代的、灵活的布局方式,可以轻松实现元素的水平垂直居中。 我们可以通过以下步骤来实现flex元素的水平垂直居中: 1. 设置父容器的display属性为flex,这样父容器就变成了flex容器。 2. 定义flex容器的justify-content属性为center,这样子元素就会在水平方向上居中对齐。 3. 定义flex容器的align-items属性为center,这样子元素就会在垂直方向上居中对齐。 使用上述步骤,我们可以很方便地实现元素的水平垂直居中。下面是一个示例代码: ```css .parent { display: flex; justify-content: center; align-items: center; } .child { /* 子元素的样式 */ } ``` 在上面的代码中,父容器的display属性设置为flex,使其变成了一个flex容器。通过设置justify-content属性为center,子元素会在水平方向上居中对齐。同时,通过设置align-items属性为center,子元素会在垂直方向上居中对齐。 我们可以在.child的样式中添加一些具体的样式,以满足我们的需求。 总结起来,通过使用flex布局的display、justify-content和align-items属性,我们可以轻松实现元素的水平垂直居中。 ### 回答3: Flexbox是一种CSS布局模型,可以帮助我们轻松地实现元素的水平垂直居中。在Flexbox中,我们可以通过设置flex容器和子元素的属性来实现水平垂直居中。 首先,我们需要将要居中的元素包裹在一个flex容器中,设置容器的display属性为flex。然后,我们可以使用align-items属性来实现垂直居中。在align-items属性中,可以设置的值包括flex-start(顶部对齐)、flex-end(底部对齐)、center(居中对齐)、baseline(基线对齐)和stretch(拉伸对齐)。通过将align-items属性设置为center,我们可以实现元素的垂直居中。 另外,我们还可以使用justify-content属性来实现元素的水平居中。在justify-content属性中,可以设置的值包括flex-start(左对齐)、flex-end(右对齐)、center(居中对齐)、space-between(两端对齐)和space-around(每个元素周围空间相等)。通过将justify-content属性设置为center,我们可以实现元素的水平居中。 综上所述,在Flexbox中,可以通过设置flex容器的align-items属性为center实现垂直居中,通过设置justify-content属性为center实现水平居中。使用这两个属性的组合,我们可以轻松地实现元素的水平垂直居中
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值