本文收錄css設置樣式的一些小技巧css
水平居中html
方法一:使用text-alignspa
text-align:center
方法二:目標標籤的父級標籤設置position:relative,目標標籤設置margin:autocode
.parent { position: relative } .target { margin:auto }
垂直居中htm
設置line-height與父級元素height相同blog
div { width:200px; height:40px; line-height:40px }
方法:利用絕對定位get
步驟:(1)設置父級元素的position: relative(absolute也能夠)it
(2)設置目標元素的margin值爲autoio
(3)設置目標元素的left與right值相同,top與bottom值相同class
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>元素居中</title> <style> .box1 { width: 50%; height: 300px; position: relative; background-color: #71b639; } .box2 { width: 200px; height: 200px; background-color: #ed6962; /*只設置設置auto只能達到水平居中的效果*/ /*margin: auto;*/ position: absolute; margin: auto; /*這裏不必定要設置爲0,只要對應的值相同便可*/ left: 0; right: 0; top: 500px; bottom: 500px; } </style> </head> <body> <div class="box1"> <div class="box2"></div> </div> </body> </html>
效果
<style>
.class {
vetical-align: middle
}
</style>
補充:vertical-align定義行內元素的基線相對於該元素所在行的基線的垂直對齊方式
top: 頂部對齊
middle: 居中對齊
bottom: 底部對齊
持續更新中...