首頁 web前端 css教學 css絕對定位居中技術優缺點圖表詳解

css絕對定位居中技術優缺點圖表詳解

Jul 20, 2017 am 09:23 AM
css 優缺點

絕對定位居中(Absolute Centering)技術

我們經常用margin:0 auto來實現水平居中,而一直認為margin:auto不能實現垂直居中…實際上,實作垂直居中只需要宣告元素高度和下面的CSS:

.Absolute-Center {
  margin: auto;
  position: absolute;
  top: 0; left: 0; bottom: 0; right: 0;
}
登入後複製

優點:

1.支援跨瀏覽器,包括IE8-IE10.

2.無需其他特殊標記,CSS代碼量少

3.支援百分比%屬性值和min-/max-屬性

4.只用這一類可實現任何內容塊居中

5.不論是否設定padding都可居中(在不使用box-sizing屬性的前提下)

6.內容塊可以被重繪。

7.完美支持圖片居中。

缺點:

1.必須宣告高度(查看可變高度Variable Height)。

2.建議設定overflow:auto來防止內容越界溢出。 (查看溢出Overflow)。

3.在Windows Phone裝置上不起作用。

瀏覽器相容性:

Chrome,Firefox, Safari, Mobile Safari, IE8-10.

絕對定位方法在最新版的Chrome,Firefox, Safari, Mobile Safari, IE8-10.上均測試通過。

比較表:

絕對居中法並不是唯一的實作方法,實現垂直居中還有些其他的方法,並各有各的優勢。採用哪種技術取決於你的瀏覽器是否支援和你使用的語言標記。這個對照表有助於你根據自己的需求做出正確的選擇。

##Major CaveatsYes*not perfect cross-browserYes

Technique

Browser Support

#Responsive

Overflow

resize:both

#Variable Height

#Variable Height

##Absolute Centering

#Modern & IE8+

#Yes

#Scroll, can overflow container Yes

Variable Height  

#Negative Margins

All

#No

Scroll

Resizes but doesn't stay centeredNo

Not responsive, margins must be calculated manually

Transforms

#Modern & IE9+

################################################################################################################################### #######Yes##################Scroll, can overflow container#################Yes ##################Yes######

Blurry rendering

Table-Cell

Modern & IE8+

Yes

Expands container

#No

Extra markup

##Inline-Block

#Modern, IE8+ & IE7*

#Yes

#Expands container

##No

Yes

#Requires container, hacky styles

#Flexbox

Modern & IE10+

#Yes

Scroll, can overflow container

Yes

Yes


###################################################################################################################################################### # ########Requires container, vendor prefixes#################### 解釋:######透過上述描述,絕對居中(AbsoluteCentering )的工作機制可以闡述如下:######1、在普通內容流(normal content flow)中,margin:auto的效果等同於margin-top:0;margin-bottom:0。 ######W3C中寫道If 'margin-top', or'margin-bottom' are 'auto', their used value is 0.######2、position:absolute使絕對定位塊跳出了內容流,內容流中的其餘部分渲染時絕對定位部分不進行渲染。 ######Developer.mozilla.org:...an element that is positioned absolutely is taken out of the flow and thustakes up no space#########3、為區塊區域設定top: 0; left: 0; bottom: 0; right: 0;將給瀏覽器重新分配一個邊界框,此時該區塊block將填充其父元素的所有可用空間,父元素一般為body或宣告為position:relative;的容器。 ######Developer.mozilla.org:For absolutely positioned elements, the top, right, bottom, and left propertiesspecify offsets from the edge of the element's containing block (what theelement is positioned relative to).######4、  給內容區塊設定一個高度height或寬度width,能夠防止內容區塊佔據所有的可用空間,促使瀏覽器根據新的邊界框重新計算margin:auto######Developer.mozilla.org: The margin of the[absolutely positioned] element is then positioned inside these offsets.######5、由於內容區塊被絕對定位,脫離了正常的內容流,瀏覽器會給margin-top,margin-bottom相同的值,使元素塊在先前定義的邊界內居中。 ######W3.org: I​​f none of the three [top, bottom,height] are 'auto': If both 'margin-top' and 'margin-bottom' are 'auto', solvethe equation under the extra constraint that the two margins get equal values.AKA: center the block vertically######這麼看來, margin:auto似乎生來就是為絕對居中(Absolute Centering)設計的,所以絕對居中(Absolute Centering)應該都相容於符合標準的現代瀏覽器。 ######簡而言之(TL;DR):絕對定位元素不在普通內容流中渲染,因此margin:auto可以使內容在通過top: 0; left: 0; bottom: 0;right: 0 ;設定的邊界內垂直居中。 ###

以上是css絕對定位居中技術優缺點圖表詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

vue中怎麼用bootstrap vue中怎麼用bootstrap Apr 07, 2025 pm 11:33 PM

在 Vue.js 中使用 Bootstrap 分為五個步驟:安裝 Bootstrap。在 main.js 中導入 Bootstrap。直接在模板中使用 Bootstrap 組件。可選:自定義樣式。可選:使用插件。

HTML,CSS和JavaScript的角色:核心職責 HTML,CSS和JavaScript的角色:核心職責 Apr 08, 2025 pm 07:05 PM

HTML定義網頁結構,CSS負責樣式和佈局,JavaScript賦予動態交互。三者在網頁開發中各司其職,共同構建豐富多彩的網站。

了解HTML,CSS和JavaScript:初學者指南 了解HTML,CSS和JavaScript:初學者指南 Apr 12, 2025 am 12:02 AM

WebDevelovermentReliesonHtml,CSS和JavaScript:1)HTMLStructuresContent,2)CSSStyleSIT和3)JavaScriptAddSstractivity,形成thebasisofmodernWebemodernWebExexperiences。

bootstrap怎麼設置框架 bootstrap怎麼設置框架 Apr 07, 2025 pm 03:27 PM

要設置 Bootstrap 框架,需要按照以下步驟:1. 通過 CDN 引用 Bootstrap 文件;2. 下載文件並將其託管在自己的服務器上;3. 在 HTML 中包含 Bootstrap 文件;4. 根據需要編譯 Sass/Less;5. 導入定製文件(可選)。設置完成後,即可使用 Bootstrap 的網格系統、組件和样式創建響應式網站和應用程序。

bootstrap怎麼寫分割線 bootstrap怎麼寫分割線 Apr 07, 2025 pm 03:12 PM

創建 Bootstrap 分割線有兩種方法:使用 標籤,可創建水平分割線。使用 CSS border 屬性,可創建自定義樣式的分割線。

bootstrap怎麼插入圖片 bootstrap怎麼插入圖片 Apr 07, 2025 pm 03:30 PM

在 Bootstrap 中插入圖片有以下幾種方法:直接插入圖片,使用 HTML 的 img 標籤。使用 Bootstrap 圖像組件,可以提供響應式圖片和更多樣式。設置圖片大小,使用 img-fluid 類可以使圖片自適應。設置邊框,使用 img-bordered 類。設置圓角,使用 img-rounded 類。設置陰影,使用 shadow 類。調整圖片大小和位置,使用 CSS 樣式。使用背景圖片,使用 background-image CSS 屬性。

bootstrap按鈕怎麼用 bootstrap按鈕怎麼用 Apr 07, 2025 pm 03:09 PM

如何使用 Bootstrap 按鈕?引入 Bootstrap CSS創建按鈕元素並添加 Bootstrap 按鈕類添加按鈕文本

bootstrap怎麼調整大小 bootstrap怎麼調整大小 Apr 07, 2025 pm 03:18 PM

要調整 Bootstrap 中元素大小,可以使用尺寸類,具體包括:調整寬度:.col-、.w-、.mw-調整高度:.h-、.min-h-、.max-h-

See all articles