日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

html文本长度不一样的对齐,关于html:文本在中间不对齐

發布時間:2025/3/21 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 html文本长度不一样的对齐,关于html:文本在中间不对齐 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本問題已經有最佳答案,請猛點這里訪問。

我想重新創建這樣的東西:

然后我會將它用于社交媒體和外部網站:)這是我能想到的:

如您所見,這里有兩個問題:

圖片不適合div。

文字不在中間。

我想如果我可以將文本對齊到中心,圖片也會自動適合,但我似乎無法做到。

這是我寫的代碼:

.child {

height: 250px;

margin: 20px;

}

.external-links {

display: table-cell;

background: #ccc;

vertical-align: middle;

text-align: center;

border-radius: 32px;

color: black;

text-decoration: none;

padding: 1px;

}

github

? ?

twitter

? ?

stackoverflow

我認為vertical-align: middle;&text-align: center;這兩行會使文本居中,但事實并非如此。

.child {

height: 250px;

margin: 20px;

}

.external-links {

display: inline-flex;

align-items: center;

background: #ccc;

vertical-align: middle;

text-align: center;

border-radius: 32px;

color: black;

text-decoration: none;

padding-right: 0.7em;

}

.external-links img {

width: 2em;

height: 2em;

}

.external-links span {

padding-left: 0.5em;

}

github

? ?

twitter

? ?

stackoverflow

你可以嘗試這個,我添加了新的css到.external-links和.external-links span來滿足你的要求

要垂直對齊需要將vertical-align: middle;放在img標記上的文本。

關于不適合div的圖像,您還需要將border-radius設置為圖像。

a img {

vertical-align: middle;

border-radius: 32px;

}

.child {

height: 250px;

margin: 20px;

}

.external-links {

display: inline-block;

background: #ccc;

text-align: center;

border-radius: 32px;

color: black;

text-decoration: none;

padding: 1px;

margin-right: 4px;

}

.external-links img {

vertical-align: middle;

border-radius: 32px;

margin-left: -2px;

}

.external-links span {

margin-right: 5px;

}

github

? ?

twitter

? ?

stackoverflow

除了上述內容之外,我在片段中進行了一些小的更改(比如將顯示更改為inline-block并更改了一些邊距)以使結果看起來更好一些。

使用display:inline-flex;使用HTML代碼制作圖像中心

.external-links {

display: inline-flex;

}

.external-links {

display: inline-block;

background: #ccc;

border-radius: 32px;

color: black;

text-decoration: none;

padding: 1px;

}

.external-links > *{

vertical-align: middle;

text-align: center;

height: 100%;

}

vertical-align Inherited為no,所以你應該設置child。 如果你沒有設置子對齊類型,圖像html沒有基線,所以圖像的底部是新的基線,在圖像之后,文本將按圖像的底部對齊,如果你設置了子對齊類型,它們將起作用。 我認為。

將display: flex;添加到.child和.external-links

.external-links{justify-content: center;

padding: 10px;

align-items: center;

margin: 5px;}

.child {

margin: 20px;

display: flex;

}

.external-links {

display: flex;

background: #ccc;

border-radius: 32px;

color: black;

text-decoration: none;

justify-content: center;

padding: 10px;

align-items: center;

margin: 5px;

}

github

? ?

twitter

? ?

stackoverflow

只需將.external-links中的顯示更改為display:inline-flex;

.child {

height: 250px;

margin: 20px;

}

.external-links {

display: inline-flex;

background: #ccc;

vertical-align: middle;

text-align: center;

border-radius: 32px;

color: black;

text-decoration: none;

padding: 5px;

}

github

? ?

twitter

? ?

stackoverflow

.child {

height: 250px;

margin: 20px;

}

.external-links {

display: inline-flex;

background: #ccc;

align-items: center;

text-align: center;

border-radius: 32px;

color: black;

text-decoration: none;

padding: 10px;

}

github

? ?

twitter

? ?

stackoverflow

刪除display:table-cell和vertical-align:middle并添加display:inline-flex和align-items:center in .external-links類。另外,我添加了填充:10px以便更好地理解。

.external-links {

display: inline-flex;

align-items: center;

}

如果您不想添加flex概念,可以嘗試以下結果。

.child {

height: 250px;

margin: 20px;

}

.external-links {

display: table-cell;

background: #ccc;

vertical-align: middle;

text-align: center;

border-radius: 32px;

color: black;

text-decoration: none;

padding: 10px;

}

img {

vertical-align: middle;

}

span {

display: inline-block;

vertical-align: middle

}

github

? ?

twitter

? ?

stackoverflow

將以下內容添加到您的CSS中:

.external-links img {

vertical-align: middle;

}

.external-links span {

vertical-align: middle;

}

您正在尋找line-height屬性,如果您給它與height屬性相同的值,您將獲得垂直居中的文本。

試試這個:

.child {

height: 250px;

margin: 20px

}

.external-links {

padding: 1px;

display: table-cell;

background: #CCC;

vertical-align: middle;

line-height: 250px;

text-align: center;

border-radius: 32px;

color: black;

text-decoration: none;

}

您還可以使用顯示屬性的inline-block值并排對齊按鈕。

.child {

height: 250px;

margin: 20px

}

.external-links {

padding: 1px;

display: inline-block;

background: #CCC;

vertical-align: middle;

line-height: 250px;

text-align: center;

border-radius: 32px;

color: black;

text-decoration: none;

}

總結

以上是生活随笔為你收集整理的html文本长度不一样的对齐,关于html:文本在中间不对齐的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。