CSS/CSS3 如何实现元素水平居中

3 篇文章 0 订阅

更新:可直接访问 [CSS/CSS3 如何实现元素水平居中] 查看效果,右键查看源代码

-------------------------------------------------分割线---------------------------------------------

先上效果图


图中链接:inline-block元素之间存在空白间距


图中链接:Horizontally Centered Menus with no CSS hacks



实现代码:

<span style="font-family:Microsoft YaHei;font-size:14px;"><span style="font-family:Microsoft YaHei;font-size:14px;"><!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8"/>
	<title>Horizontal</title>
</head>
<body>
<h3>CSS 如何实现元素水平居中</h3>
<!-- margin:auto实现水平居中 -->
<p>1.margin:auto实现水平居中</p>
<p>缺点:必须设置固定宽度</p>
<style type="text/css">
	.margin-auto-parent{width: 300px; height: 300px; background: #60bcd3; }
	.margin-auto-content{width: 200px; height: 200px; margin:0 auto; background: #ff6700;}	
</style>
<div class="margin-auto-parent">
	<div class="margin-auto-content"></div>
</div>
<!-- text-align+inline-block实现水平居中 -->
<p>2.text-align+inline-block实现水平居中</p>
<p>缺点:<a href="http://www.w3cplus.com/css/fighting-the-space-between-inline-block-elements" target="_blank">inline-block元素之间存在空白间距</a></p>
<style type="text/css">
	.inline-block-parent{width: 300px; height: 300px; background: #60bcd3; text-align: center;}
	.inline-block-content{width: 200px; height: 200px; display: inline-block; background: #ff6700;}	
	.inline-block-content span{display: inline-block;text-align: left;}
</style>
<div class="inline-block-parent">
	<div class="inline-block-content">
		<!-- <span>换行后右边会产生空白间距</span> -->
	</div>
</div>
<!-- 通过设置float + text-align + display:inline;实现水平居中 -->
<p>3.通过设置float + text-align + display:inline;实现水平居中</p>
<p>缺点:原理稍复杂,需额外增加一个元素做嵌套</p>
<p>参考:<a href="http://matthewjamestaylor.com/blog/beautiful-css-centered-menus-no-hacks-full-cross-browser-support" target="_blank">Horizontally Centered Menus with no CSS hacks</a></p>
<style type="text/css">
	.float-center-parent{width: 300px; height: 300px; background: #60bcd3; position: relative;}
	.float-center-content-wrap{position: relative;left: 50%; text-align: center;}
	.float-center-content{display: inline; position: relative; right: 50%; background: #ff6700;}	
</style>
<div class="float-center-parent">
	<div class="float-center-content-wrap">
		<div class="float-center-content">float...自适应宽度</div>
	</div>
</div>
<!-- 绝对定位实现水平居中 -->
<p>4.绝对定位实现水平居中</p>
<p>缺点:必须设置固定宽度</p>
<style type="text/css">
	.position-absolute-parent{width: 300px; height: 300px; background: #60bcd3; position: relative;}
	.position-absolute-content{width: 200px; height: 200px; position: absolute; left: 50%; margin-left: -100px; background: #ff6700;}	
</style>
<div class="position-absolute-parent">
	<div class="position-absolute-content"></div>
</div>
<p>解决之道:结合方法3实现自适应宽度</p>
<style type="text/css">
	.position-float-parent{width: 300px; height: 300px; background: #60bcd3; position: relative;}
	.position-float-content-wrap{position: absolute; left: 50%;}
	.position-float-content{position: relative; right: 50%; float: left; display: inline; background: #ff6700;}	
</style>
<div class="position-float-parent">
	<div class="position-float-content-wrap">
		<div class="position-float-content">绝对定位自适应宽度</div>
	</div>
</div>

<h3>CSS3 如何实现元素水平居中</h3>
<!-- flex实现水平居中 -->
<p>5.flex实现水平居中</p>
<p>缺点:兼容性差</p>
<style type="text/css">
	.flex-parent{width: 300px; height: 300px; background: #60bcd3; display: flex; justify-content:center;}
	.flex-content{ background: #ff6700;}	
</style>
<div class="flex-parent">
	<div class="flex-content">自适应宽度</div>
</div>
<!-- width:fit-content; + margin:0 auto;实现水平居中 -->
<p>6.width:fit-content; + margin:0 auto;实现水平居中</p>
<p>缺点:兼容性差</p>
<style type="text/css">
	.flex-parent{width: 300px; height: 300px; background: #60bcd3; }
	.flex-content{ background: #ff6700; width: fit-content; margin: 0 auto;}	
</style>
<div class="flex-parent">
	<div class="flex-content">fit-content自适应宽度</div>
</div>
</body>
</html></span></span>

---------------------------------------------------------参考资料------------------------------------------------------------------

如何只用CSS做到完全居中

六种实现元素水平居中

CSS制作水平垂直居中对齐


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值