CSS 垂直置中,css垂直置中

來源:互聯網
上載者:User

CSS 垂直置中,css垂直置中

一、垂直置中:單行的行內元素解決方案

置中元素:單行的行內元素,即inline或者inline-*類型元素,如文字、連結等

解決方案:將該行內元素的height、inline-height設定為其父元素的高度

HTML

<div id="container">
    <a href="#">hello,gbtags.comhello,gbtags.comhello,gbtags.com</a>
</div>

CSS

#container {
    background: #222;
    height: 200px;
}
a {
    /*height: 200px;*/
    line-height: 200px;
    color: #fff;
}

 

二、垂直置中:多行的行內元素解決方案

置中元素:多行的行內元素

解決方案:組合使用display:table-cell和vertical-align:middle屬性來定義需要置中元素的父元素

HTML

<div id="container">
  <a href="#">  
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Reiciendis blanditiis optio accusamus quia sapiente at labore consectetur in quasi veritatis possimus quod nihil aliquam vero saepe rem quas. Ratione eligendi!
  </a>  
</div>

CSS 

#container {
    width: 300px;
    height: 300px;
    background: #222;
    /*以下屬性垂直置中*/
    display: table-cell;
    vertical-align: middle;
}
a {
    color: #fff;
}

 

三、垂直置中:已知高度的塊狀元素解決方案

置中元素:區塊層級元素,如div

解決方案:將待置中元素設定為絕對位置,並將其margin-top值設定為待置中元素高度的一半的負值。

HTML

<div class="item">
</div>

CSS

div {
    width: 100px;
    height: 100px;
    background: #222;
}
/*以下為置中代碼*/
.item {
    position: absolute;
    top: 50%;
    margin-top: -50px;
    padding: 0;   /*如果有padding設定,相對計算下margin-top的值*/
}

 

四、垂直置中:未知高度的塊狀元素解決方案

置中元素:區塊層級元素,如div,但不知其高度 

解決方案:使用CSS3的transform屬性

HTML

<div class="item">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Amet sint repellendus ab aut quisquam eligendi est in deleniti.
</div>

CSS 

div {
    width: 150px;
    background: #222;
    color: #fff;
}
/*以下為置中代碼*/
.item {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
}

 

五、水平垂直置中:已知寬度和高度的元素解決方案

置中類型:垂直和水平同時置中(前提是知道元素的高度和寬度)

解決方案:設定元素 絕對位置,並設定margin-top(高度/2)和margin-left值為(寬度/2)的負值

HTML

<div class="item">
</div>

CSS

div {
    width: 150px;
    height: 250px;
    background: #222;
    color: #fff;
}
/*以下為置中代碼*/
.item {
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -125px;
    margin-left: -75px;
}

 

六、水平垂直置中:未知元素高度和寬度的解決方案

置中類型:水平和垂直置中(前提是該元素的寬度和高度未知)

解決方案:設定該元素為絕對位置,並且使用CSS3的transform屬性 

HTML

<div class="item">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit. Cupiditate nostrum quaerat debitis.
</div>

CSS

div {
    background: #222;
    color: #fff;
}
/*以下為置中代碼*/
.item {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
}

 

七、水平垂直置中:使用flex布局實現

解決方案:設定flex布局,並設定置中元素父元素的justify-content和align-items屬性為center

HTML

<div class="parent">
    <div class="item"></div>
</div>

CSS

.item {
    width: 100px;
    height: 100px;
    background: #222;
}
/*以下為置中代碼*/
.parent {
    display: flex;
    justify-content: center;
    align-items: center;
    /*需要設定height查看垂直置中效果*/
    background: #ccc;
    height: 300px;
}

 


DIV+CSS怎讓文字垂直置中?

在說到這個問題的時候,也許有人會問CSS中不是有vertical-align屬性來設定垂直置中的嗎?即使是某些瀏覽器不支援我只需做少許的CSS Hack技術就可以啊!所以在這裡我還要囉嗦兩句,CSS中的確是有vertical-align屬性,但是它只對(X)HTML元素中擁有valign特性的元素才生效,例如表格元素中的<td>、<th>、<caption>等,而像<div>、<span>這樣的元素是沒有valign特性的,因此使用vertical-align對它們不起作用。

CSS網頁布局DIV水平置中的各種方法

一、單行垂直置中

如果一個容器中只有一行文字,對它實現置中相對比較簡單,我們只需要設定它的實際高度height和所在行的高度line-height相等即可。如:

imoker.cn(愛摩客)提供的程式碼片段:

div {
height:25px;
line-height:25px;
overflow:hidden;
}
這段代碼很簡單,後面使用overflow:hidden的設定是為了防止內容超出容器或者產生自動換行,這樣就達不到垂直置中效果了。

imoker.cn(愛摩客)提供的程式碼片段:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3.org/...al.dtd">
<html xmlns="www.w3.org/1999/xhtml">
<head>
<title> 單行文字實現垂直置中 </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body { font-size:12px;font-family:tahoma;}
div {
height:25px;
line-height:25px;
border:1px solid #FF0099;
background-color:#FFCCFF;
}
</style>
</head>
<body>
<d......餘下全文>>
 
css 裡面怎讓一個DIV置中 ?

你是要水平置中還是垂直置中?
水平置中:
<style>
.juzhong{margin:0px auto; width:500px;}
</style>
<div class=juzhong></div>

垂直置中:
<style>
#mid{
position:absolute;
top:50%;
left:50%;
margin:-150px 0 0 -150px;
width:300px;
height:300px;
border:1px solid red;
}

</style>
<div id=mid></div>

注意,其中margin:-150px 0 0 -150px;
第一個150是高度的一半,第四個150是寬度的一半
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.