最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • 【CSS三种居中方案全解】CSS水平居中常用方法集结

    正文概述 掘金(soloplayer)   2020-12-09   430

    目录

    • 前言
    • CSS水平居中
    • 参考资料

    CSS 居中系列文章

    CSS 垂直居中

    CSS 水平垂直居中

    一、前言

    二、CSS 水平居中

    1、行内元素水平居中text-align:center,一个块级的容器包裹一个行内元素,行内元素水平居中,不需要知道该元素的宽高

    /* HTML */
    <div class='father'>
      <span class='son'></span>
    </div>
    <style>
      .father {
    	text-align: center;
    	width: 300px;
    	height: 300px;
    	border: 3px solid red;
      }
      .son {
    	width: 50px;
    	height: 50px;
    	background-color: aqua;
    	display: inline-block;
      }
    </style>
    
    • 效果展示

    【CSS三种居中方案全解】CSS水平居中常用方法集结

    2、margin-left:auto,margin-right:auto。,块级元素水平居中,加上 text-align:center 让里面的文字水平居中。不需要知道元素的宽高,与父元素无关,只是显示出现水平居中了。

    /* HTML */
    <div class='father'>
      <div class='son'></div>
    </div>
    <style>
      .father {
    	width: 300px;
    	height: 300px;
    	border: 1px solid red;
      }
      .son {
    	width: 100px;
    	height: 100px;
    	background-color: aqua;
    	/* 下面一行代码让文本水平居中 */
    	text-align: center;
    	margin-left: auto;
    	margin-right: auto;
      }
    </style>
    
    • 效果展示

    【CSS三种居中方案全解】CSS水平居中常用方法集结

    3、父元素display:table-cell;text-align:center,里面的子元素就会实现水平居中,不需要知道子元素的宽高

    /* HTML */
    <div class='father'>
      <span class='son'></span>
    </div>
    <style>
      .father {
    	display: table-cell;
    	text-align: center;
    	width: 300px;
    	height: 300px;
    	border: 3px solid red;
      }
      .son {
    	display: inline-block;
    	width: 100px;
    	height: 100px;
    	background-color: aqua;
      }
    </style>
    
    • 效果展示

    【CSS三种居中方案全解】CSS水平居中常用方法集结

    4、absolute+margin:auto,定位为 absolute 的元素水平居中,不需要知道该元素的宽高

    /* HTML */
    <div class='father'>
      <div class='son'></div>
    </div>
    <style>
      .father {
    	position: relative;
    	width: 300px;
    	height: 300px;
    	border: 3px solid red;
      }
      .son {
    	width: 100px;
    	height: 100px;
    	background-color: aqua;
    	position: absolute;
    	left: 0;
    	right: 0;
    	margin: auto;
      }
    </style>
    
    • 效果展示

    【CSS三种居中方案全解】CSS水平居中常用方法集结

    5、absolute+负margin,定位为 absolute 的元素水平居中,需要知道该元素的宽高

    <!-- HTMl -->
    <div class="father">
    	<div class="son"></div>
    </div>
    <style>
      .father {
    	position: relative;
    	width: 300px;
    	height: 300px;
    	border: 3px solid red;
      }
      .son {
    	position: absolute;
    	width: 100px;
    	height: 100px;
    	background-color: aqua;
    	left: 50%;
    	/* 负margin须是宽度的一半 */
    	margin-left: -50px;
      }
    </style>
    
    • 效果展示

    【CSS三种居中方案全解】CSS水平居中常用方法集结

    6、absolute+calc(css3计算属性),定位为 absolute 的元素水平居中,需要知道该元素的宽高

    <!-- HTMl -->
    <div class="father">
    	<div class="son"></div>
    </div>
    <style>
      .father {
    	position: relative;
    	width: 300px;
    	height: 300px;
    	border: 3px solid red;
      }
      .son {
    	position: absolute;
    	width: 100px;
    	height: 100px;
    	background-color: aqua;
    	/* 注意"-"两边要隔开 减去的须是宽度或高度的一半*/
    	left: calc(50% - 50px);
      }
    </style>
    
    • 效果展示

    【CSS三种居中方案全解】CSS水平居中常用方法集结

    7、absolute+transform,定位为 absolute 的元素水平居中,不需要知道元素的宽高

    <!-- HTMl -->
    <div class="father">
    	<div class="son"></div>
    </div>
    <style>
      .father {
    	position: relative;
    	width: 300px;
    	height: 300px;
    	border: 3px solid red;
      }
      .son {
    	position: absolute;
    	width: 100px;
    	height: 100px;
    	background-color: aqua;
    	left: 50%;
    	transform: translateX(-50%);
      }
    </style>
    
    • 效果展示

    【CSS三种居中方案全解】CSS水平居中常用方法集结

    8、flex,目前主流的布局方案,父元素为 flex 容器且添加 justify-content: center;,控制子元素的布局。不需要知道子元素的宽高

    <!-- HTMl -->
    <div class="father">
    	<div class="son"></div>
    </div>
    <style>
      .father {
    	width: 300px;
    	height: 300px;
    	border: 3px solid red;
    	display: flex;
    	justify-content: center;
      }
      .son {
    	background-color: aqua;
    	width: 100px;
    	height: 100px;
      }
    </style>
    
    • 效果展示

    【CSS三种居中方案全解】CSS水平居中常用方法集结

    9、grid ,目前最强大的布局方案,使用还尚未流行。父元素为 grid,子元素添加 justify-self: center;。不需要知道子元素的宽高

    <!-- HTMl -->
    <div class="father">
    	<div class="son"></div>
    </div>
    <style>
      .father {
    	width: 300px;
    	height: 300px;
    	border: 3px solid red;
    	display: grid;
      }
      .son {
    	background-color: aqua;
    	width: 100px;
    	height: 100px;
    	justify-self: center;
      }
    </style>
    
    • 效果展示

    【CSS三种居中方案全解】CSS水平居中常用方法集结

    10、隐藏节点(盒子)实现 该原理就是使用盒子占位置,但不显示出该盒子。另外的盒子水平居中,子盒子的宽高需由实际计算时确定

    <!-- HTML -->
    <div class="father">
    	<div class="hide"></div>
    	<div class="son"></div>
    </div>
    <style>
      .father {
    	width: 300px;
    	height: 300px;
    	border: 3px solid red;
      }
      .son {
    	display: inline-block;
    	background-color: aqua;
    	width: 50%;
    	height: 50%;
      }
      .hide {
    	display: inline-block;
    	width: 25%;
    	height: 50px;
      }
    </style>
    
    • 效果展示

    【CSS三种居中方案全解】CSS水平居中常用方法集结

    三、参考资料

    张鑫旭 CSS 大佬

    百度经验


    起源地下载网 » 【CSS三种居中方案全解】CSS水平居中常用方法集结

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元