大家精心準(zhǔn)備了工作中常用的CSS樣式,都是自己工作中常用到的記錄發(fā),分享給大家,如果覺得可以希望點(diǎn)贊關(guān)注和評(píng)論。
最后把源碼奉獻(xiàn)給大家
SS 是前端里面的基礎(chǔ)之一,也是非常重要的一部分,它往往決定了你所做出來的網(wǎng)頁頁面是否美觀。在設(shè)計(jì)網(wǎng)頁頁面的過程中,總會(huì)有將元素或者文字進(jìn)行水平垂直居中的要求。下面w3cschool編程獅就為大家介紹 CSS 中幾種常用到的水平垂直居中的方法。
當(dāng)元素有給定的高度以及寬度的時(shí)候,使用 margin: auto; 元素僅會(huì)水平居中,并不會(huì)進(jìn)行垂直居中。此時(shí)就需要設(shè)置元素的 position 為 absolute,父級(jí)元素的 position 為 relative,同時(shí)元素的上下左右都需要設(shè)置為 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;
}
效果展示:
當(dāng)已經(jīng)知道了要進(jìn)行水平垂直居中的元素的寬高時(shí),就可以通過設(shè)置 position: absolute 來實(shí)現(xiàn)。但是,使用的同時(shí)還需要結(jié)合其他屬性才完整實(shí)現(xiàn)。因?yàn)椋瑔问窃O(shè)置 absolute,上左距離均為一半,就會(huì)出現(xiàn)下面這種情況。很顯然可以看到,元素并不是完全居中,僅只有左上角的位置在中心點(diǎn)
概念圖:
因此想要實(shí)現(xiàn)元素完全水平垂直居中,在設(shè)置了 absolute 定位后,可以設(shè)置 margin 值為負(fù),或者使用 calc 來計(jì)算,上左距離在 50% 的基礎(chǔ)上還要減去元素本身一半的寬高。
margin 值為負(fù)或者 calc 計(jì)算均是在已知元素寬高的情況下,假設(shè)不知道元素的寬高,那么怎么實(shí)現(xiàn)水平垂直居中呢?這里就可以使用 transform 屬性,通過坐標(biāo)位移來實(shí)現(xiàn)居中。
CSS 代碼
/* 結(jié)合 margin */
.center2{
width: 50px;
height: 50px;
background-color: #7FFFD4;
position: absolute;
left: 50%;
top: 50%;
margin-left: -25px;
margin-top: -25px;
}
/* 結(jié)合 calc 計(jì)算*/
.center2{
width: 50px;
height: 50px;
background-color: #7FFFD4;
position: absolute;
left: calc(50% - 25px)
top: calc(50% - 25px);
}
/* 結(jié)合 transform */
.center2{
width: 50px;
height: 50px;
background-color: #7FFFD4;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
效果展示
03
PART
可以通過彈性布局來設(shè)置水平垂直居中,這里需要設(shè)置父級(jí)元素 display:flex; 還需要設(shè)置兩個(gè)屬性,水平布局 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;
}
效果展示:
前面介紹的是元素如何實(shí)現(xiàn)水平垂直居中,下面介紹的是如何將文字進(jìn)行水平垂直居中。這第一個(gè)方法也是最經(jīng)常用的,使用文本水平對(duì)齊 text-align 和行高 line-height 來實(shí)現(xiàn)的。
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
第二個(gè)方法可以通過網(wǎng)格布局 grid 來實(shí)現(xiàn)。而這里通過 grid 有兩種方式實(shí)現(xiàn),一種對(duì)元素本身屬性進(jìn)行設(shè)置,另一種在元素的父級(jí)元素中設(shè)置。兩者看上去內(nèi)容似乎差不多,不同的是在元素中設(shè)置的是 align-self 還要多了一個(gè) margin,父級(jí)元素中是 align-items。
相關(guān)代碼:
/* grid 元素中設(shè)置 */
.box4{
background-color: #eee;
width: 200px;
height: 200px;
margin-top: 20px;
display: grid;
}
.center6{
align-self: center;
justify-content: center;
margin: auto;
}
/* grid 父級(jí)元素中設(shè)置 */
.box5{
background-color: #eee;
width: 200px;
height: 200px;
margin-top: 20px;
display: grid;
align-items: center;
justify-content: center;
}
效果展示:
以上就是關(guān)于 CSS 如何將元素或者文字進(jìn)行水平垂直居中的幾種常用方法,大家還其他關(guān)于 CSS 實(shí)現(xiàn)水平垂直居中的方法嗎?請(qǐng)?jiān)谠u(píng)論區(qū)留下你的想法。
關(guān)注w3cschool編程獅訂閱更多IT資訊、技術(shù)干貨~
天學(xué)習(xí)下css文本幾個(gè)常用相關(guān)屬性,包括文本顏色(color)這個(gè)是非常常見的、文本間距(這里主要指letter-spacing和word-spacing)、文本對(duì)齊(text-align)以及文本修飾(text-decoration),以下是相應(yīng)的CSS和HTML代碼示例。
使用color屬性可以設(shè)置文本的顏色。顏色值可以是顏色名(如red)、十六進(jìn)制(如#FF0000)、RGB(如rgb(255, 0, 0))、RGBA(如rgba(255, 0, 0, 0.5))、HSL或HSLA等。
HTML:
<p class="text-color">這是一段紅色文本。</p>
CSS:
.text-color {
color: red; /* 或者使用其他顏色值,如 #FF0000, rgb(255, 0, 0) 等 */
}
HTML:
<p class="letter-spacing">這是一個(gè)字母間距示例。</p>
<p class="word-spacing">這是 一個(gè)單詞間距 示例。</p>
CSS:
.letter-spacing {
letter-spacing: 2px; /* 字母間距增加2px */
}
.word-spacing {
word-spacing: 5px; /* 單詞間距增加5px */
}
使用text-align屬性可以設(shè)置文本的對(duì)齊方式,如左對(duì)齊(left)、右對(duì)齊(right)、居中對(duì)齊(center)或兩端對(duì)齊(justify)。
HTML:
<p class="text-left">左對(duì)齊文本。</p>
<p class="text-center">居中對(duì)齊文本。</p>
<p class="text-right">右對(duì)齊文本。</p>
<p class="text-justify">兩端對(duì)齊文本。這是一個(gè)較長的句子,用于展示兩端對(duì)齊的效果。</p>
CSS:
.text-left {
text-align: left;
}
.text-center {
text-align: center;
}
.text-right {
text-align: right;
}
.text-justify {
text-align: justify;
}
text-decoration屬性用于設(shè)置文本的裝飾線,如下劃線(underline)、上劃線(overline)、刪除線(line-through)或無裝飾線(none)。
HTML:
<p class="underline">下劃線文本。</p>
<p class="overline">上劃線文本。</p>
<p class="line-through">刪除線文本。</p>
<p class="no-decoration">無裝飾線文本。</p>
CSS:
.underline {
text-decoration: underline;
}
.overline {
text-decoration: overline;
}
.line-through {
text-decoration: line-through;
}
.no-decoration {
text-decoration: none;
}
以上就是關(guān)于CSS中文本相關(guān)屬性的詳細(xì)解釋及代碼示例。
演示效果
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。