css3 - css flex 的問題
問題描述
如圖,移動端導(dǎo)航用了flex均勻分布。但是視覺上不對。因為字?jǐn)?shù)不相同。導(dǎo)致間隔不整齊。現(xiàn)在想調(diào)整css 能讓字的間隔均勻分布。同時滿足
移動端同行100%
注意下面紅線
問題解答
回答1:如果只是改變css,我的認(rèn)知中好像并沒有適合你目前這種文字間距均勻的方法;不過可以通過一些樣式調(diào)整達(dá)到視覺上的舒適,如下圖:
添加一個淺色的背景;
給每個內(nèi)容之間加1像素間隔符…
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title>Super8_share</title> <meta name='viewport' content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no'> <meta name='apple-mobile-web-app-capable' content='yes'> <meta name='format-detection' content='telephone=no'> <meta content='telephone=no' name='format-detection'> <style> .list {display: flex;flex-flow: row nowrap;height: 50px; } .item {width: 20%;line-height: 50px;text-align: center;border-right: 1px solid #fff;background-color: #efefef;border-bottom: 2px solid #f00;overflow: hidden; } .item:last-child{border-right: none;} </style></head><body> <p class='list'><p name='item'>中 國</p><p name='item'>美 國</p><p name='item'>加拿大</p><p name='item'>澳大利亞</p><p name='item'>新西蘭</p> </p></body></html>
另外,還有下面這種兩邊間隔相同的方式:
只需改變一句代碼即可
flex-grow:1; // 替換 width: 20%;回答2:
美?????國 //用
overflow: hidden 隱藏超出的文字。
利用letter-spacing來解決!letter-spacing 屬性增加或減少字符間的空白(字符間距)。
類似于下面的效果://
CSS樣式:<style type='text/css'>.hotsearch dd{float: left;line-height: 24px;margin-right: 30px;overflow: hidden;text-align: center;width: 4em; /這個值是看最長能顯示幾個文字,如x,則為x em/}.hotsearch dd a{display:block;}.w2{letter-spacing:2em; /如果需要y個字兩端對齊,則為(x-y)/(y-1),這里是(4-2)/(2-1)=2em /...
回答3:貌似css還沒有這么強(qiáng)大的功能,而且每一個元素的字?jǐn)?shù)不一樣,計算出來的間距也會不一致。題主也可以試一試兩端對齊這個方式
.nav{ display:flex; width:100%; height:50px; line-height: 50px; border-bottom:1px solid #ccc; font-size: 12px; text-align: center;}a{ display:block; padding:0 10px; box-sizing: border-box; flex:1; width:1%; text-align:justify; color:#000; }a:after{ overflow:hidden; display: inline-block; height:0; content:'200B';//利用偽元素來產(chǎn)生一個換行符,不然text-align:justify;屬性不會生效 width: 100%;}.cur{ position: relative; color:#e22828;}.cur:before{ width: 100%; height:1px; content:''; position: absolute; left:0; bottom:-1px; border-bottom:1px solid #e22828; z-index: 100;}
有三個以上字體的分配的比較均勻
根據(jù)字?jǐn)?shù)設(shè)置相應(yīng)的flex-grow
<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <title></title> <style> .list {display: flex;flex-flow: row nowrap;height: 50px;background-color: #aaa; } .item {line-height: 50px;text-align: center;border: 1px solid #8cb;background-color: #ccc; } .item:nth-child(1) {flex-grow: 2; } .item:nth-child(2) {flex-grow: 2; } .item:nth-child(3) {flex-grow: 3; } .item:nth-child(4) {flex-grow: 4; } .item:nth-child(5) {flex-grow: 3; } </style></head><body> <p class='list'><p class='item'>中國</p><p class='item'>中國</p><p class='item'>大中國</p><p class='item'>大大中國</p><p class='item'>大中國</p> </p></body></html>
相關(guān)文章:
1. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””2. Mysql如何按照日期對比數(shù)據(jù),求SQL語句3. 為什么我輸入了refresh不會跳轉(zhuǎn)?請教大神支招!4. 我安裝了xampp集成環(huán)境,再安裝php工具箱提示80端口被占用5. java類型強(qiáng)轉(zhuǎn)為泛型V,竟然沒有報錯?6. phpStudy2017輕巧版mysql無法啟動7. mysql - 如何高效的查詢需要合并大數(shù)據(jù)表的操作8. javascript - 學(xué)習(xí)網(wǎng)頁開發(fā),關(guān)于head區(qū)域一段腳本的疑惑9. javascript - 移動端一個小效果10. 為什么我寫的PHP不行
