CSS四種方案實現 居中布局

2021-08-16 00:04:36 字數 721 閱讀 3314

不定寬元素的居中對齊

方案一: text-align: center + display: inline-block

**:

css:html:

demo

效果:

優點:1.相容性好,相容ie 6 7只需對元素 display 屬性修改為 inline 

缺點: child 會繼承 parent 的text-align center ,導致其內部也會句中顯示

方案二: display: table + margin

核心**:

.child
方案三: position:absolute + transform: translatex(-50%)

核心**:

.parent 

.child

注:transform : translate 以自身為參照

方案四: display: flex + justify-content: center

核心**:

.parent
或者去掉.parent justify屬性,為.child新增上 margin:0 auto 亦

css實現居中

要考慮布局場景,有些並不適合複雜布局,主要實現垂直居中,水平居中會帶一下 1 height配合line height 父標籤 parent 2 vertical align middle 父標籤 parent 子標籤 child 3 display table cell 父標籤 parent 4 d...

CSS實現居中的方式

class out class in div div 通過margin 0 auto 實現水平居中 out in通過position absolute margin left 實現水平居中 out in通過text align center 實現水平居中 out in通過隱藏節點 float的方法實...

利用CSS實現居中對齊

1.文字居中 首先編寫乙個簡單的html 設定乙個類名為parentdiv的div物件。html 如下 1 div class parentdiv 2這裡隨意填寫 3div 1.1 實現文字水平居中 使用text align 對div.parentdiv設定text align center 來實現...