我有多个水平显示的菜单项。在每个菜单项中,我都有一个跨度图标(数据图标)和一个文本。它们以 text-align:center 居中。一些菜单项文本很长。一切正常,直到我进入较小的屏幕。
问题是,当文本中断到下一行时,它会在菜单项容器内居中并位于图标下方。
有没有一种方法可以防止元素文本本身居中,同时让图标和文本在菜单容器内居中?
.mycontainer {
width: 50%;
background: green;
text-align: center;
float: left;
}
.big-icon {
font-size: 40px;
}
.small-text {
font-size: 12px;
position: relative;
top: -15px;
}
<div class="menu">
<div class="mycontainer">
<span class="big-icon">Big Icon</span>
<span class="small-text">This is very very very very very very very very very long text</span>
</div>
<div class="mycontainer" style="background: yellow;">
<span class="big-icon">Big Icon</span>
<span class="small-text">This is short text</span>
</div>
</div>
最佳答案
尝试使用 Flexbox
堆栈片段
.menu {
display: flex;
flex-wrap: wrap;
}
.mycontainer {
padding: 20px;
box-sizing: border-box;
width: 50%;
background: green;
text-align: center;
display: flex;
align-items: center;
flex-direction: row;
justify-content: center;
}
.big-icon {
font-size: 40px;
flex-shrink: 0;
}
.small-text {
font-size: 12px;
position: relative;
flex: 0 1 40%;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<div class="menu">
<div class="mycontainer">
<span class="fa fa-address-book-o fa-4x"></span>
<span class="small-text">This is very very very very veryveryveryveryverylongtext</span>
</div>
<div class="mycontainer" style="background: yellow;">
<span class="fa fa-address-book-o fa-4x"></span>
<span class="small-text">This is short text</span>
</div>
</div>
关于html - 响应式 - 将图标和文本对齐到菜单项的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48495729/