标签:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>不定宽度水平居中</title> <style> body{ background-color: #e5da31; } .container{ position: absolute; /*脱离文档流,其宽度将由子元素的宽度决定*/ left:50%; } .content{ position: absolute; left:-50%; background-color: #2ecc71; } </style> </head> <body> <div class="container"> <div class="content">标签嵌套</div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>不定宽度水平居中</title> <style> body { background-color: #e5da31; } .container { display: flex; /*定义一个flex容器*/ justify-content: center; /*定义容器的空间没有被全部占用时,内容的对其方式*/ } .content{ background-color: #2ecc71; } </style> </head> <body> <div class="container"> <div class="content">标签嵌套</div> </div> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>不定宽度水平居中</title> <style> body{ background-color: #e5da31; } .container{ text-align:center; } .content{ display: inline; background-color: #2ecc71; } </style> </head> <body> <div class="container"> <div class="content">标签嵌套</div> </div> </body> </html>
标签:
原文地址:http://www.cnblogs.com/shibazijiang/p/5624831.html