如何在不使用Flexbox的情况下水平对齐元素?
有没有一种方法可以在不使用Flexbox
的情况下将Web组件彼此对齐。我知道它是一个很棒的工具,但不幸的是它不能在IE9或IE10上运行。我希望链接中的文本出现在图像的旁边。JSFdle显示了工作代码,但是使用FlexBox,我如何在不使用Flexbox的情况下实现这一点?
编码:
<display:setProperty name="paging.banner.full" value='<span class="pagelinks"> <a href="{1}"> <img src="../images/integration/FastLeft.jpg"/> </a> <a href="{2}"> <img src="../images/integration/SlowLeft.jpg"/> </a> | Page {5} of {6} | <a href="{3}"> <img src="../images/integration/SlowRight.jpg"/> </a> <a href="{4}"> <img src="../images/integration/FastRight.jpg"/> </a></span>'/>
<display:setProperty name="paging.banner.first" value='<span class="pagelinks"> <img src="../images/integration/FastLeft.jpg"/> <img src="../images/integration/SlowLeft.jpg"/> | Page {5} of {6} | <a href="{3}"> <img src="../images/integration/SlowRight.jpg"/> </a> <a href="{4}"> <img src="../images/integration/FastRight.jpg"/> </a></span>'/>
<display:setProperty name="paging.banner.last" value='<span class="pagelinks"> <a href="{1}"> <img src="../images/integration/FastLeft.jpg"/> </a> <a href="{2}"> <img src="../images/integration/SlowLeft.jpg"/> </a> | Page {5} of {6} | <img src="../images/integration/SlowRight.jpg"/> <img src="../images/integration/FastRight.jpg"/> </span>'/>
css
.pagelinks {
float: right;
margin-right: 48%;
color: #828282;
display: table-cell;
vertical-align: middle;
display: flex;
align-items: center;
justify-content: center;
}
.pagelinks a {
text-decoration: none;
}
.pagelinks img {
border: 1px solid transparent;
}
.pagelinks img:hover {
border-radius: 3px;
border: 1px solid #828282;
}
JSFiddle
解决方案
IE10及更低版本(可能到IE7/8),
您有两个解决方案:
内联块
您可以在img
中使用display:inline-block
和vertical-align:middle
,并用div
用一些width
和margin:auto
将其水平居中
div {
width: 50%;
/* change the value as you prefer, even in px */
margin: auto;
text-align: center;
/*demo*/
border: 1px solid red;
}
.pagelinks {
color: #828282;
}
.pagelinks a {
text-decoration: none;
}
.pagelinks img {
border: 1px solid transparent;
display: inline-block;
vertical-align: middle;
}
.pagelinks img:hover {
border-radius: 3px;
border: 1px solid #828282;
}
<div>
<span class="pagelinks"> <a href="{1}"> <img src="../images/integration/FastLeft.jpg"/> </a> <a href="{2}"> <img src="../images/integration/SlowLeft.jpg"/> </a> | Page {5} of {6} | <a href="{3}"> <img src="../images/integration/SlowRight.jpg"/> </a> <a href="{4}"> <img src="../images/integration/FastRight.jpg"/> </a></span>
</div>
表格/表格单元格
将display:table
应用于.page-links
和vertical-align:middle
应用于img
,并再次将其置于div
居中。
div {
width: 50%;
margin: auto;
}
.pagelinks {
color: #828282;
display: table;
width: 100%;
text-align: center;
/*demo*/
border: 1px solid red;
}
.pagelinks a {
text-decoration: none;
}
.pagelinks img {
border: 1px solid transparent;
vertical-align: middle;
}
.pagelinks img:hover {
border-radius: 3px;
border: 1px solid #828282;
}
<div>
<span class="pagelinks"> <a href="{1}"> <img src="../images/integration/FastLeft.jpg"/> </a> <a href="{2}"> <img src="../images/integration/SlowLeft.jpg"/> </a> | Page {5} of {6} | <a href="{3}"> <img src="../images/integration/SlowRight.jpg"/> </a> <a href="{4}"> <img src="../images/integration/FastRight.jpg"/> </a></span>
</div>
相关文章