[精讚] [會員登入]
9861

CSS 垂直中文字

要把文字垂直顯示的處理中文字正轉或逆轉90度的css設定

分享此文連結 //n.sfs.tw/13102

分享連結 CSS 垂直中文字@新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2019-10-25 11:43:34 最後編修
2019-02-12 16:02:35 By 張○○
 

自動目錄

CSS中如果想要用直列的文字,只要把文字選擇直式顯示即可,這時可使用 writing-mode屬性。

這個屬性預設是水平顯示,也就是

writing-mode: horizontal-tb;

可以使用由右到左和由左到右的垂直顯示方式

writing-mode: vertical-rl;

writing-mode: vertical-lr;

來看看範例

由右至左垂直文字

HTML

<div>ABC 123 中文字<br/>DEF 667 第二行</div>

CSS

writing-mode: vertical-rl;

 

由左至右垂直文字

HTML

<div>ABC 123 中文字<br/>DEF 667 第二行</div>

CSS

writing-mode: vertical-lr;

上面的設定中,英文的方向都是由右到左,如果想要讓他轉個180度,突然想到原本CSS有個由右至左有CSS,不知管不管用:

direction: rtl;

上面這個只對水平顯示有用,對垂直的是完全沒作用。

 

所以如果要讓文字轉180度,就得使用 text-orientation 這個屬性質,他有幾個參數可以設定[1]

text-orientation: mixed;  預設值,就是上面看到的那樣

text-orientation: upright;

text-orientation: sideways-right;

text-orientation: sideways;

text-orientation: use-glyph-orientation;

text-orientation: inherit;

text-orientation: initial;

text-orientation: unset;

CSS

writing-mode: vertical-lr;
text-orientation: upright;

CSS

writing-mode: vertical-lr;
text-orientation: sideways-right;

原本轉不過去的中文字也橫轉

 

CSS

writing-mode: vertical-lr;
text-orientation: sideways;

sidewayssideways-right 看不出差異,這兩個是相同的。

text-orientation: use-glyph-orientation; 這個是給svg圖型用的,搭配作廢的什麼svg屬性,有興趣的人再去研究

到目前為只,都沒有辦法把文字轉向逆時針90度方向,無論中文或英數,都只能垂直或是順轉90度。

要逆轉90度或更多,得使出殺手鐧了。

 

中文字逆轉90度

使用transform的rotate函數

writing-mode: vertical-rl;
text-orientation: sideways;
transform: rotate(-180deg);

writing-mode: vertical-rl;
text-orientation: upright;
transform: rotate(-90deg);

如果把upright的轉90度,就會變成這樣,字掉到畫面外,因為字的align是中央,旋轉後以中央為軸心

你得設定區塊的寬度讓他可以完全顯示,加上以下:

width:200px;

或是修改旋轉的軸心,這部分可以參考[3] +transform-origin(0,0)。這裡我沒有試,他的範例還做了一個時鐘,超酷的。

 

參考資料

[1] https://developer.mozilla.org/en-US/docs/Web/CSS/text-orientation

[2] 改变CSS世界纵横规则的writing-mode属性 https://www.zhangxinxu.com/wordpress/2016/04/css-writing-mode/

[3] CSS沒有極限 - CSS transform-origin https://wcc723.github.io/css/2013/10/10/css-transform-origin/

END

你可能感興趣的文章

textarea計算字數和行數 textarea可以大量的放入文字,但要如何計算字數和行數呢?

[HTLM5] html元件上的 data-* 屬性 html5 中元件中多了一個 data-*的屬性,這個屬性超級好用

[HTML5] script 的新增屬性 defer, async script 的新增屬性 defer, async

[HTLM5] 表單color、email、url、search、tel輸入類型 介紹HTML5好用的輸入類型:color、email、url,以及兩個形同雞肋的類型search、tel..

[HTML] CSS中的折字換行或不換行(防止容器破壞) 使用者留言時故意來用一串長長的且沒有任何空白的長字串,把你的容器破壞得很難看...

讓radio box 或checkbox 好按 在行動裝置下,radio box及checkbox變得很難按得到

隨機好文

[jQuery] select 元件的取值及給值 html中的元件select,在jquery中要如何使用?

SELinux 常用指令和檔案 在Redhat系列中,Centos5以後加入了selinux,他並沒有這麼可怕,不必每次看到Selinux ,就想把他

[札記] 2016.7~12月札記 札記,只是札記

[Win7] 燒錄 iso 檔 在Windows7 中內建燒錄程式,可以直接把檔案拉到光碟機裡,再執行燒錄。

詭異的創業思維 創業的思維中,有多少銀彈,有多少技術,有多少人脈,有多少時間等等,每個都要考慮進去,以熱忱建立的關係脆弱的像蘇打餅乾一樣..