android studio table居中代码_CSS 之 居中
生活随笔
收集整理的這篇文章主要介紹了
android studio table居中代码_CSS 之 居中
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
閱讀本文約需要8分鐘
大家好,我是你們的導(dǎo)師,經(jīng)??次遗笥讶Φ耐瑢W(xué)應(yīng)該知道,我每天會(huì)在微信上給大家免費(fèi)提供以下服務(wù)!
1、長(zhǎng)期為你提供最優(yōu)質(zhì)的學(xué)習(xí)資源!
2、給你解決技術(shù)問(wèn)題!
3、每天在朋友圈里分享優(yōu)質(zhì)的技術(shù)文章!
4、每周1、3、5送紙質(zhì)書(shū)籍免費(fèi)送給大家,每年至少送書(shū)800本書(shū)!
5、為大家推薦靠譜的就業(yè)單位!
請(qǐng)注意!我上面說(shuō)的5點(diǎn)全部都是免費(fèi)的!全網(wǎng)你應(yīng)該找不到第二家吧!
當(dāng)然,大家在我私人微信上問(wèn)我問(wèn)題,僅限回答web前端、java相關(guān)的。
---------------------------
好了,接下來(lái)開(kāi)始今天的技術(shù)分享!上次老師跟大家分享了CSS 之 margin的知識(shí),今天跟大家分享下CSS 之 居中的知識(shí)。
0?前言CSS居中是前端工程師經(jīng)常要面對(duì)的問(wèn)題,也是基本技能之一。今天有時(shí)間把CSS居中的方案匯編整理了一下,目前包括水平居中,垂直居中及水平垂直居中方案共15種。如有漏掉的,還會(huì)陸續(xù)的補(bǔ)充進(jìn)來(lái),算做是一個(gè)備忘錄吧。1?水平居中01、 內(nèi)聯(lián)元素水平居中利用 text-align: center 可以實(shí)現(xiàn)在塊級(jí)元素內(nèi)部的內(nèi)聯(lián)元素水平居中。此方法對(duì)內(nèi)聯(lián)元素(inline), 內(nèi)聯(lián)塊(inline-block), 內(nèi)聯(lián)表(inline-table), inline-flex元素水平居中都有效。 Demo代碼:<html><head> <meta charset="utf-8"> <title>42度空間-內(nèi)聯(lián)元素水平居中-測(cè)試1title> <style> div { height:60px; border: 2px dashed #f69c55; } .center-text { text-align: center; }style>head><body><div class="center-text"> 簡(jiǎn)單是穩(wěn)定的前提。div>body>html>02、 塊級(jí)元素水平居中通過(guò)把固定寬度塊級(jí)元素的margin-left和margin-right設(shè)成auto,就可以使塊級(jí)元素水平居中。 Demo代碼:<html><head> <meta charset="utf-8"> <title>42度空間-塊級(jí)元素水平居中title> <style> div { height:100px; border: 2px dashed #f69c55; } .center-block { margin: 0 auto; width: 8rem; padding:1rem; color:#fff; background:#000; }style>head><body><div> <p class="center-block"> 簡(jiǎn)單不先于復(fù)雜,而是在復(fù)雜之后。 p>div>body>html>03、多塊級(jí)元素水平居中,利用inline-block如果一行中有兩個(gè)或兩個(gè)以上的塊級(jí)元素,通過(guò)設(shè)置塊級(jí)元素的顯示類型為inline-block和父容器的text-align屬性從而使多塊級(jí)元素水平居中。Demo代碼:<html><head> <meta charset="utf-8"> <title>42度空間-多塊級(jí)元素水平居中-inline-blocktitle> <style> .container { height:100px; padding: 8px; text-align: center; border: 2px dashed #f69c55; } .inline-block { padding: 8px; width: 4rem; margin: 0 8px; color: #fff; background: #000; display: inline-block; }style>head><body><div class="container"> <div class="inline-block"> 簡(jiǎn)單不先于復(fù)雜 div> <div class="inline-block"> 而是在復(fù)雜之后 div> <div class="inline-block"> 簡(jiǎn)單不先于復(fù)雜,而是在復(fù)雜之后。 div>div>body>html>04、多塊級(jí)元素水平居中,利用display: flex利用彈性布局(flex),實(shí)現(xiàn)水平居中,其中justify-content 用于設(shè)置彈性盒子元素在主軸(橫軸)方向上的對(duì)齊方式,本例中設(shè)置子元素水平居中顯示。Demo代碼:<html><head> <meta charset="utf-8"> <title>42度空間-多塊級(jí)元素水平居中-彈性布局title> <style> .flex-center { padding: 8px; display: flex; justify-content: center; border: 2px dashed #f69c55; } .flex-center >div { padding: 8px; width: 4rem; margin: 0 8px; color: #fff; background: #000; }style>head><body><div class="flex-center"> <div> 簡(jiǎn)單不先于復(fù)雜。 div> <div> 簡(jiǎn)單不先于復(fù)雜,而是在復(fù)雜之后。 div> <div> 而是在復(fù)雜之后。 div>div>body>html>2?垂直居中05、單行內(nèi)聯(lián)(inline-)元素垂直居中通過(guò)設(shè)置內(nèi)聯(lián)元素的高度(height)和行高(line-height)相等,從而使元素垂直居中。 Demo代碼:<html><head> <meta charset="utf-8"> <title>42度空間-單行內(nèi)聯(lián)元素垂直居中-line-heighttitle> <style> #box { height: 120px; line-height: 120px; border: 2px dashed #f69c55; }style>head><body><div id="box"> 軟件在能夠復(fù)用前必須先能用。div>body>html>06、多行元素垂直居中, 利用表布局(table)利用表布局的vertical-align: middle可以實(shí)現(xiàn)子元素的垂直居中。 Demo代碼:<html><head> <meta charset="utf-8"> <title>42度空間-多行內(nèi)聯(lián)元素垂直居中-tabletitle> <style> .center-table { display: table; height: 140px; border: 2px dashed #f69c55; } .v-cell { display: table-cell; vertical-align: middle; }style>head><body><div class="center-table"> <p class="v-cell">The more technology you learn, the more you realize how little you know.p>div>body>html>07、多行元素垂直居中,利用flex布局(flex)利用flex布局實(shí)現(xiàn)垂直居中,其中flex-direction: column定義主軸方向?yàn)榭v向。因?yàn)閒lex布局是CSS3中定義,在較老的瀏覽器存在兼容性問(wèn)題。Demo代碼:<html><head> <meta charset="utf-8"> <title>42度空間-多行內(nèi)聯(lián)元素垂直居中-flextitle> <style> .center-flex { height: 140px; display: flex; flex-direction: column; justify-content: center; border: 2px dashed #f69c55; }style>head><body><div class="center-flex"> <p>Dance like nobody is watching, code like everybody is.p>div>body>html>08、多行元素垂直居中, 利用“精靈元素”利用“精靈元素”(ghost element)技術(shù)實(shí)現(xiàn)垂直居中,即在父容器內(nèi)放一個(gè)100%高度的偽元素,讓文本和偽元素垂直對(duì)齊,從而達(dá)到垂直居中的目的。Demo代碼:<html><head> <meta charset="utf-8"> <title>42度空間-多行內(nèi)聯(lián)元素垂直居中-偽元素title> <style> .ghost-center { position: relative; border: 2px dashed #f69c55; padding: 10px 0; } .ghost-center::before { content: " "; display: inline-block; height: 100%; width: 1%; vertical-align: middle; } .ghost-center p { display: inline-block; vertical-align: middle; width: 12rem; padding:1rem; color:#fff; background:#000; }style>head><body><div class="ghost-center"> <p>“你畢業(yè)才兩年,這三年工作經(jīng)驗(yàn)是怎么來(lái)的?”程序員答:“加班?!眕>div>body>html>09、塊級(jí)元素垂直居中,固定高度的塊級(jí)元素我們知道居中元素的高度和寬度,垂直居中問(wèn)題就很簡(jiǎn)單。通過(guò)絕對(duì)定位元素距離頂部50%,并設(shè)置margin-top向上偏移元素高度的一半,就可以實(shí)現(xiàn)垂直居中了。 Demo代碼:<html><head> <meta charset="utf-8"> <title>42度空間-固定高度的塊元素垂直居中title> <style> .parent { height: 140px; position: relative; border: 2px dashed #f69c55; } .child { position: absolute; top: 50%; height: 100px; margin-top: -50px; color:#fff; background: #000; }style>head><body><div class="parent"> <div class="child">控制復(fù)雜性是計(jì)算機(jī)編程的本質(zhì)。div>div>body>html>10、塊級(jí)元素垂直居中, 未知高度的塊級(jí)元素當(dāng)垂直居中的元素的高度和寬度未知時(shí),我們可以借助CSS3中的transform屬性向Y軸反向偏移50%的方法實(shí)現(xiàn)垂直居中。但是部分瀏覽器存在兼容性的問(wèn)題。Demo代碼:<html><head> <meta charset="utf-8"> <title>42度空間-未知高度的塊元素垂直居中title> <style> .parent { height: 140px; position: relative; border: 2px dashed #f69c55; } .child { position: absolute; top: 50%; transform: translateY(-50%); background: black; color: #fff; padding: 1rem; width: 12rem; }style>head><body><div class="parent"> <div class="child">世界上有 10 種人,懂二進(jìn)制的和不懂二進(jìn)制的。div>div>body>html>3?水平垂直居中11、固定寬高元素水平垂直居中通過(guò)margin平移元素整體寬度的一半,使元素水平垂直居中。Demo代碼:<html><head> <meta charset="utf-8"> <title>42度空間-固定寬高元素水平垂直居中title> <style> .parent { height: 140px; position: relative; border: 2px dashed #f69c55; } .child { width: 200px; height: 80px; padding: 10px; position: absolute; top: 50%; left: 50%; margin: -50px 0 0 -110px; background: black; color: #fff; }style>head><body><div class="parent"> <div class="child">控制復(fù)雜性是計(jì)算機(jī)編程的本質(zhì)。div>div>body>html>12、未知寬高元素水平垂直居中利用2D變換,在水平和垂直兩個(gè)方向都向反向平移寬高的一半,從而使元素水平垂直居中。Demo代碼:<html><head> <meta charset="utf-8"> <title>42度空間-未知寬高元素水平垂直居中title> <style> .parent { height: 140px; position: relative; border: 2px dashed #f69c55; } .child { padding: 10px; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); color: #fff; background: black; }style>head><body><div class="parent"> <div class="child">當(dāng)你試圖解決一個(gè)你不理解的問(wèn)題時(shí),復(fù)雜化就產(chǎn)成了。div>div>body>html>13、 利用flex布局利用flex布局,其中justify-content 用于設(shè)置或檢索彈性盒子元素在主軸(橫軸)方向上的對(duì)齊方式;而align-items屬性定義flex子項(xiàng)在flex容器的當(dāng)前行的側(cè)軸(縱軸)方向上的對(duì)齊方式。 Demo代碼:<html><head> <meta charset="utf-8"> <title>42度空間-利用flex布局實(shí)現(xiàn)元素水平垂直居中title> <style> .parent { height: 140px; display: flex; justify-content: center; align-items: center; border: 2px dashed #f69c55; } .child { padding: 20px; background: black; color: #fff; }style>head><body><div class="parent"> <div class="child">Facebook wasn't built in a day.div>div>body>html>14、 利用grid布局利用grid實(shí)現(xiàn)水平垂直居中。 Demo代碼:<html><head> <meta charset="utf-8"> <title>42度空間-利用grid布局實(shí)現(xiàn)元素水平垂直居中title> <style> .parent { height: 140px; display: grid; align-items:center; border: 2px dashed #f69c55; } .child { margin:auto; padding: 20px; width:10rem; color: #fff; background: black; }style>head><body><div class="parent"> <div class="child">好的程序員能寫(xiě)出人能讀懂的代碼。div>div>body>html>15、 屏幕上水平垂直居中屏幕上水平垂直居中十分常用,常規(guī)的登錄及注冊(cè)頁(yè)面都需要用到。要保證較好的兼容性,還需要用到表布局。Demo代碼:DOCTYPE HTML><html><head> <meta charset="utf-8"> <title>42度空間-如何使DIV在屏幕上水平垂直居中顯示?兼容性要好title> <style> .outer { display: table; position: absolute; height: 100%; width: 100%; } .middle { display: table-cell; vertical-align: middle; } .inner { margin-left: auto; margin-right: auto; background: #2b2b2b; color:#fff; padding: 2rem; max-width: 320px; } style>head><body><div class="outer"> <div class="middle"> <div class="inner"> <p>一個(gè)好的程序員應(yīng)該是那種過(guò)單行線都要往兩邊看的人。p> <button value="add" id="add">增加內(nèi)容button> div> div>div><script type="text/javascript" src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js">script><script type="text/javascript"> $(document).ready(function () { $("#add").click(function () { $("p").after("解決問(wèn)題大多數(shù)都很容易;找到問(wèn)題出在哪里卻很難。
"); }); });script>body>html> width: 400px;}4?說(shuō)明文中所述文字及代碼部分匯編于網(wǎng)絡(luò)。因時(shí)間不足,能力有限等原因,存在文字闡述不準(zhǔn)及代碼測(cè)試不足等諸多問(wèn)題。因此只限于學(xué)習(xí)交流范圍,如果需要進(jìn)行實(shí)際應(yīng)用的話,請(qǐng)自行把握。參考文獻(xiàn):https://cloud.tencent.com/developer/article/1115615今天就分享這么多,關(guān)于CSS 之 居中,你學(xué)會(huì)了多少?歡迎在留言區(qū)評(píng)論,對(duì)于有價(jià)值的留言,我們都會(huì)一一回復(fù)的。如果覺(jué)得文章對(duì)你有一丟丟幫助,請(qǐng)點(diǎn)右下角【在看】,讓更多人看到該文章。總結(jié)
以上是生活随笔為你收集整理的android studio table居中代码_CSS 之 居中的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 鸣潮如何滑翔 鸣潮滑翔伞技能介绍
- 下一篇: 华为watchgt4和华为gt3哪款好