CSS实现水平居中的四种方法

1.inline-block + text-align

DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>水平居中title>
<link rel="stylesheet" type="text/css" href="demo.css">
<style type="text/css">
	body{margin:20px;}
	
	.parent{
		text-align: center;
	}
	.child{
		display: inline-block;
	}
style>
head>
<body>
<div class="parent">
	<div class="child">DEMOdiv>
div>
body>
html>

2.table + margin

DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>水平居中title>
<link rel="stylesheet" type="text/css" href="demo.css">
<style type="text/css">
	body{margin:20px;}

	.child{
		display: table;
		margin: 0 auto;
	}
style>
head>
<body>
<div class="parent">
	<div class="child">DEMOdiv>
div>
body>
html>

3.absolute + transform

CSS实现水平居中的四种方法_第1张图片

4.flex + justify-content

CSS实现水平居中的四种方法_第2张图片

你可能感兴趣的:(CSS,css,水平居中)