在 <a> 内水平居中图像,为什么 margin:0 自动;不工作?
Horizontally centering image inside <a>, why is margin:0 auto; NOT working?
或
#left-control {
float: left;
height: 300px;
width:300px;
background-color:crimson;
}
#left-control:before {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
#left-control img {
vertical-align: middle;
z-index: 1;
margin: 0 auto;
}
<a id="left-control">
<img src="https://events.columbia.edu/3.10/calrsrc.MainCampus/themes/columbiaTheme/resourcesColumbia/FacebookIcon.png" />
</a>
我采用了 here 中的以下技巧,使 img
在 a
标签中垂直居中。
问题是我习惯用margin:0 auto;
来居中。但问题是它不能与这种垂直居中的技术一起工作。
这是为什么?我该怎么做才能解决这个问题?
使用以下 css:
#left-control::before {
height: 100%;
content: "";
}
#left-control {
background-color: crimson;
display: table;
height: 300px;
line-height: 300px;
text-align: center;
width: 300px;
}
从 #left-control
中删除 float: left;
。并使用 display: table;
line-height: 300px;
text-align: center;
并从 #left-control::before
中删除 display: inline-block;
vertical-align: middle;
图像宽度不是 100%,因此 margin: 0 auto;
无法使图像居中对齐。最好的办法是在 ID 为 left-control
的 <a>
标签上执行 text-align: center;
以制作图标 center-aligned。
添加文字对齐#left-control
text-align:center
#left-control {
float: left;
height: 300px;
width:300px;
text-align:center;
background-color:crimson;
}
#left-control:before {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
#left-control img {
vertical-align: middle;
z-index: 1;
margin: 0 auto;
}
<a id="left-control">
<img src="https://events.columbia.edu/3.10/calrsrc.MainCampus/themes/columbiaTheme/resourcesColumbia/FacebookIcon.png" />
</a>
或
#left-control {
float: left;
height: 300px;
width:300px;
background-color:crimson;
}
#left-control:before {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
#left-control img {
vertical-align: middle;
z-index: 1;
margin: 0 auto;
}
<a id="left-control">
<img src="https://events.columbia.edu/3.10/calrsrc.MainCampus/themes/columbiaTheme/resourcesColumbia/FacebookIcon.png" />
</a>
我采用了 here 中的以下技巧,使 img
在 a
标签中垂直居中。
问题是我习惯用margin:0 auto;
来居中。但问题是它不能与这种垂直居中的技术一起工作。
这是为什么?我该怎么做才能解决这个问题?
使用以下 css:
#left-control::before {
height: 100%;
content: "";
}
#left-control {
background-color: crimson;
display: table;
height: 300px;
line-height: 300px;
text-align: center;
width: 300px;
}
从 #left-control
中删除 float: left;
。并使用 display: table;
line-height: 300px;
text-align: center;
并从 #left-control::before
display: inline-block;
vertical-align: middle;
图像宽度不是 100%,因此 margin: 0 auto;
无法使图像居中对齐。最好的办法是在 ID 为 left-control
的 <a>
标签上执行 text-align: center;
以制作图标 center-aligned。
添加文字对齐#left-control
text-align:center
#left-control {
float: left;
height: 300px;
width:300px;
text-align:center;
background-color:crimson;
}
#left-control:before {
content: "";
display: inline-block;
vertical-align: middle;
height: 100%;
}
#left-control img {
vertical-align: middle;
z-index: 1;
margin: 0 auto;
}
<a id="left-control">
<img src="https://events.columbia.edu/3.10/calrsrc.MainCampus/themes/columbiaTheme/resourcesColumbia/FacebookIcon.png" />
</a>