許多方法可以將HTML元素與CSS對齊,但是一起使用或單獨使用它們并不是那么容易。開發人員所面臨的困難之一就是試圖將元素集中在頁面中間。
因此,在本文中,我將展示一些最常用的方法,即通過使用不同的CSS屬性在水平和垂直方向上居中圖像。
讓我們開始使用3個不同的CSS屬性將圖像水平居中。
使圖像水平居中的第一種方法是使用text-align屬性。但是,僅當圖像位于塊級容器(例如<div>)內時,此方法才有效:
使圖像居中的另一種方法是使用margin:auto屬性(用于左邊距和右邊距)。但是,單獨使用margin:Auto將不適用于圖像。如果需要使用margin:auto,則還必須使用2個其他屬性。
margin-auto屬性對內聯級別的元素沒有任何影響。由于<img>標簽是一個內聯元素,因此我們需要先將其轉換為塊級元素:
其次,我們還需要定義寬度。因此,左右邊緣可以占用其余的空白空間并自動對齊,可以這樣解決問題(除非我們給出100%的寬度):
將圖像水平居中的第三種方法是使用display:flex。同樣,我們對容器使用text-align屬性,它也會使用display:flex。但是,僅使用display:flex是不夠的。容器還必須具有一個稱為justify-content的附加屬性。
justify-content屬性與display:flex一起使用,我們可以使用它水平放置圖像的中心。最后,圖像的寬度必須小于容器的寬度,否則,它會占用100%的空間,然后我們就無法對其進行集中化。
1、Display: Flex
對于垂直對齊,使用display:flex確實很有幫助。考慮到我們的容器的高度為800px,但圖像的高度僅為500px:
現在,在這種情況下,向容器中添加一行代碼(align-items:center)就可以了:
如果將align-items屬性與display:flex一起使用,就會將元素垂直放置。
2、位置:絕對和變換屬性
垂直對齊的另一種方法是一起使用position和transform屬性。這個有點復雜,所以讓我們一步一步地做。
步驟1:定義絕對位置
首先,我們將圖像的定位行為從靜態更改為絕對:
同樣,它應該位于相對放置的容器內,因此我們添加一個位置:相對于其容器的div。
步驟2:定義頂部和左側屬性
其次,我們定義圖像的頂部和左側屬性,并設置為50%。這會將圖像的起點(左上角)移到容器的中心:
步驟3:定義變換屬性
在第二步的時候已經將圖像的一部分移出容器。因此,我們需要將其取回內部。在圖像上定義轉換屬性,并在其X和Y軸上添加負50%可以達到目的:
還可以使用其他方法進行水平和垂直居中,我這里只嘗試了最常用的方法。希望這篇文章可以幫助你了解如何在頁面中心對齊圖像。
、頁面使用CSS的好處:
有效的傳遞頁面信息。
使用CSS美化過的頁面文本,使頁面漂亮、美觀,吸引用戶。
可以很好的突出頁面的主題內容,使用戶第一眼可以看到頁面主要內容。
具有良好的用戶體驗。
2、美化網頁的一些基本樣式
A:字體樣式:
font-family設置字體類型font-family:"隸書";
font-size設置字體大小font-size:12px;
font-style設置字體風格font-style:italic;
font-weight設置字體的粗細font-weight:bold;
font在一個聲明中設置所有字體屬性font:italic bold 36px "宋體";
舉例:
body{font-family: Times,"Times New Roman", "楷體";}
h1{font-size:24px;} 單位->px:像素
h2{font-style:oblique;}
p{font-weight:bold;}
p span{font:oblique bold 12px "楷體";}
B:文本屬性:
color設置文本顏色color:#00C;
text-align設置元素水平對齊方式text-align:right;
text-indent設置首行文本的縮進text-indent:20px;
line-height設置文本的行高line-height:25px;
text-decoration設置文本的裝飾text-decoration:underline;
C:排版文本段落:
水平對齊方式:text-align:left|right|center|justify;
left把文本排列到左邊。默認值:由瀏覽器決定
right把文本排列到右邊
center把文本排列到中間
justify實現兩端對齊文本效果
首行縮進:text-indent:em或px
行高:line-height:px
文本修飾:text-decoration:none|underline|overline|line_through
值說明
none默認值,定義的標準文本
underline設置文本的下劃線
overline設置文本的上劃線
line-through設置文本的刪除線
垂直對齊方式:vertical-align屬性:middle、top、bottom
文本陰影:text-shadow : color x-offset y-offset blur-radius;
D:背景顏色和背景圖片:
background-color:"red";
background-image:url(圖片路徑);
background-repeat:
沿水平和垂直兩個方向平鋪
no-repeat:不平鋪,即只顯示一次
repeat-x:只沿水平方向平鋪
repeat-y:只沿垂直方向平鋪
background-position:X、Y方向關鍵詞 水平方向的關鍵詞:left、center、right
垂直方向的關鍵詞:top、center、bottom
舉例:
.title {
font-size:18px;
font-weight:bold;
color:#FFF;
text-indent:1em;
line-height:35px;
background:#C00 url(../image/arrow-down.gif) 205px 10px no-repeat;
}
線性漸變:linear-gradient ( position, color1, color2,…)
3、舉例:
<head>
<title>家用電器</title>
</head>
<link rel="stylesheet" href="jiayongdianqi.css">
<body>
<div class="container">
<h2>家用電器</h2>
<div class="type">
<h6><a href="#">大家電</a></h6>
<ul>
<li> <a href="#">平板電視</a> <a href="#">洗衣機</a> <a href="#">冰箱</a></li>
<li> <a href="#">空調</a> <a href="#">煙機/灶具</a> <a href="#"> 熱水器</a></li>
<li> <a href="#">冷柜/酒柜</a> <a href="#">消毒柜</a> <a href="#">家庭影院</a></li>
</ul>
<h6><a href="#">生活電器</a></h6>
<ul>
<li> <a href="#">電風扇</a> <a href="#">凈化器</a> <a href="#">吸塵器</a></li>
<li> <a href="#">凈水設備</a> <a href="#">掛燙機</a> <a href="#"> 電話機</a></li>
</ul>
<h6><a href="#">廚房電器</a></h6>
<ul>
<li> <a href="#">榨汁機</a> <a href="#">電壓力鍋</a> <a href="#">電飯煲</a></li>
<li> <a href="#">豆漿機</a> <a href="#">微波爐</a> <a href="#"> 電磁爐</a></li>
</ul>
<h6><a href="#">五金家裝</a></h6>
<ul>
<li> <a href="#">淋浴/水槽</a> <a href="#">電動工具</a> <a href="#">手動工具</a></li>
<li> <a href="#">儀器儀表 </a> <a href="#">浴霸/排氣 </a> <a href="#"> 燈具</a></li>
</ul>
</div>
</div>
</body>
</html>
li{
/* 去圓點 */
list-style: none;
}
/* 分類頁面總寬度 */
.container{
width: 500px;
}
a{
/* 去超鏈接下劃線 */
text-decoration: none;
}
/* 大標題字體 */
.container>h2{
font-size: 18px;
color: white;
font-weight: bolder;
line-height: 50px;
text-indent: 2px;
/*從上到下,0%開始顏色。50%顏色。70%顏色 */
background: linear-gradient(to bottom,#0467ac,#63a7d6,#b6dbf5);
}
css代碼:
/* 所有電器分類內容 */
.type{
/* 首行縮進 */
text-indent: 4.5em;
/* 左外邊距40px */
margin-left: 40px;
}
/* 電器分類 */
.type>h6{
font-size: 14px;
font-weight: bolder;
line-height: 30px;
background: linear-gradient(to bottom,#e4f1fa,#bddff7,#e4f1fa);
}
/* 電器分類中的A標簽 */
.type>h6>a{
color:#0467ac;
}
/* 電器分類中的A標簽鼠標懸浮效果 */
.type>h6>a:hover{
text-decoration: underline;
}
/* 分類內容字體 */
.type>ul>li{
font-size: 12px;
line-height: 26px;
}
/* 分類內容字體里面的A標簽 */
.type>ul>li>a{
color: #969696;
}
/* 分類內容字體里面的A標簽懸浮效果 */
.type>ul>li>a:hover{
color: #804000;
text-decoration: underline;
}
4、效果圖鏈接:file:///D:/ruanjian/VS/wenjianxiangmu/htmlFive/jiayongdianqi.html
SS 是前端里面的基礎之一,也是非常重要的一部分,它往往決定了你所做出來的網頁頁面是否美觀。在設計網頁頁面的過程中,總會有將元素或者文字進行水平垂直居中的要求。下面w3cschool編程獅就為大家介紹 CSS 中幾種常用到的水平垂直居中的方法。
當元素有給定的高度以及寬度的時候,使用 margin: auto; 元素僅會水平居中,并不會進行垂直居中。此時就需要設置元素的 position 為 absolute,父級元素的 position 為 relative,同時元素的上下左右都需要設置為 0。
HTML 代碼
<div class="box">
<div class="center1"></div>
</div>
CSS 代碼
.box{
width: 200px;
height: 200px;
background-color: #eee;
position: relative;
margin-top: 20px;
}
.center1{
width: 50px;
height: 50px;
background-color: #00ACED;
margin: auto;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
效果展示:
當已經知道了要進行水平垂直居中的元素的寬高時,就可以通過設置 position: absolute 來實現。但是,使用的同時還需要結合其他屬性才完整實現。因為,單是設置 absolute,上左距離均為一半,就會出現下面這種情況。很顯然可以看到,元素并不是完全居中,僅只有左上角的位置在中心點
概念圖:
因此想要實現元素完全水平垂直居中,在設置了 absolute 定位后,可以設置 margin 值為負,或者使用 calc 來計算,上左距離在 50% 的基礎上還要減去元素本身一半的寬高。
margin 值為負或者 calc 計算均是在已知元素寬高的情況下,假設不知道元素的寬高,那么怎么實現水平垂直居中呢?這里就可以使用 transform 屬性,通過坐標位移來實現居中。
CSS 代碼
/* 結合 margin */
.center2{
width: 50px;
height: 50px;
background-color: #7FFFD4;
position: absolute;
left: 50%;
top: 50%;
margin-left: -25px;
margin-top: -25px;
}
/* 結合 calc 計算*/
.center2{
width: 50px;
height: 50px;
background-color: #7FFFD4;
position: absolute;
left: calc(50% - 25px)
top: calc(50% - 25px);
}
/* 結合 transform */
.center2{
width: 50px;
height: 50px;
background-color: #7FFFD4;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
效果展示
03
PART
可以通過彈性布局來設置水平垂直居中,這里需要設置父級元素 display:flex; 還需要設置兩個屬性,水平布局 justify-content 以及垂直布局 align-items。
HTML代碼
<div class="box2">
<div class="center4"></div>
</div>
CSS代碼:
.box2{
background-color: #eee;
width: 200px;
height: 200px;
position: relative;
margin-top: 20px ;
display: flex;
justify-content: center;
align-items: center;
}
.center4{
width: 50px;
height: 50px;
background-color: #B39873;
}
效果展示:
前面介紹的是元素如何實現水平垂直居中,下面介紹的是如何將文字進行水平垂直居中。這第一個方法也是最經常用的,使用文本水平對齊 text-align 和行高 line-height 來實現的。
HTML 代碼
<div class="box3">
<div class="center5">文字居中</div>
</div>
CSS 代碼
.box3{
background-color: #eee;
width: 200px;
height: 200px;
margin-top: 20px;
}
.center5{
text-align: center;
line-height: 200px;
}
效果展示
05
PART
第二個方法可以通過網格布局 grid 來實現。而這里通過 grid 有兩種方式實現,一種對元素本身屬性進行設置,另一種在元素的父級元素中設置。兩者看上去內容似乎差不多,不同的是在元素中設置的是 align-self 還要多了一個 margin,父級元素中是 align-items。
相關代碼:
/* grid 元素中設置 */
.box4{
background-color: #eee;
width: 200px;
height: 200px;
margin-top: 20px;
display: grid;
}
.center6{
align-self: center;
justify-content: center;
margin: auto;
}
/* grid 父級元素中設置 */
.box5{
background-color: #eee;
width: 200px;
height: 200px;
margin-top: 20px;
display: grid;
align-items: center;
justify-content: center;
}
效果展示:
以上就是關于 CSS 如何將元素或者文字進行水平垂直居中的幾種常用方法,大家還其他關于 CSS 實現水平垂直居中的方法嗎?請在評論區留下你的想法。
關注w3cschool編程獅訂閱更多IT資訊、技術干貨~
*請認真填寫需求信息,我們會在24小時內與您取得聯系。