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
? ?
? ?
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
? ?
? ?
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
? ?
? ?
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
? ?
? ?
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
? ?
? ?
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
? ?
? ?
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
? ?
? ?
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:文本在中间不对齐的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html 子框架刷新,webpack 热
- 下一篇: html金额输入框转大写,纯CSS实现输