<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>行內塊元素水平居中</title> <style> .father{ text-align: center; width: 200px; height: 200px; border: 1px solid black; } .son{ display: inline-block; width: 50px; height: 50px; background: red; } </style> </head> <body> <div class="father"> <div class="son"></div> </div> </body> </html>
<style> .father{ width: 200px; height: 200px; border: 1px solid black; } .son{ margin: 0 auto; width: 50px; height: 50px; background: red; } </style>
<style> .father{ width: 200px; height: 200px; border: 1px solid black; position: relative; } .son{ width: 50px; height: 50px; background: red; position: absolute; left: 50%; transform: translateX(-50%); } </style>
<style> .father{ width: 200px; height: 200px; border: 1px solid black; display: flex; justify-content: center; } .son{ width: 50px; height: 50px; background: red; } </style>
<style> .father{ width: 200px; height: 200px; border: 1px solid black; position: relative; } .son{ width: 50px; height: 50px; background: red; position: absolute; top: 50%; left: 50%; margin: -25px 0 0 -25px; } </style>
<style> .father{ width: 200px; height: 200px; border: 1px solid black; position: relative; } .son{ width: 50px; height: 50px; background: red; position: absolute; top: 0; left: 0; right: 0; bottom: 0; margin: auto; } </style>
<style> .father{ width: 200px; height: 200px; border: 1px solid black; position: relative; } .son{ width: 50px; height: 50px; background: red; position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); } </style>
<style> .father{ width: 200px; height: 200px; border: 1px solid black; display: flex; justify-content: center; align-items: center; } .son{ width: 50px; height: 50px; background: red; } </style>