我正在尝试按照下图创建导航,文本居中以及图标,但文本与图标保持对齐。链接需要是其容器的宽度,这也需要响应:
你是怎么做到的?
HTML
<ul class="mobile-home-section">
<li><a href="#">PROPERTY</a></li>
<li><a href="#">FUTURE PLANNING</a></li>
<li><a href="#">COMMERICAL</a></li>
</ul>
CSS
.mobile-home-section {
margin: 0;
padding: 0;
list-style-type: none;
width: 100%;
background: #163158
}
.mobile-home-section li {
background-image: url('http://s18.postimg.org/m26o71ohx/icon_future_hover.png');
background-repeat: no-repeat;
background-position: 0 10px;
padding-left: 50px;
}
.mobile-home-section li a {
color: white;
text-decoration: none;
padding: 20px 0;
display: block;
border-bottom: 1px solid white;
}
答案 0 :(得分:1)
编辑 更改了代码以使其响应
如果我是你,我不会将链接的图标作为背景图像。我会将图标作为图像放在链接标记内。
<强> HTML 强>
<div class="container">
<ul class="mobile-home-section">
<li>
<a href="#">
<span><img src="img2.jpg">PROPERTY</span>
</a>
</li>
<li>
<a href="#">
<span><img src="img2.jpg">FUTURE PLANNING</span>
</a>
</li>
<li>
<a href="#">
<span><img src="img3.jpg">COMMERICAL</span>
</a>
</li>
</ul>
</div>
<强> CSS 强>
.container {
background: #273F87;
}
ul.mobile-home-section {
display: block;
list-style-type: none;
padding: 0px;
}
ul.mobile-home-section li:nth-child(2) {
border-top: 2px solid #fff;
border-bottom: 2px solid #fff;
}
ul.mobile-home-section li a {
display: block;
color: #fff;
padding: 0px;
font-family: serif;
margin: 0px auto;
text-decoration: none;
}
ul.mobile-home-section li a span img {
width: 50px;
height: 50px;
padding-right:20px;
display: inline-block;
vertical-align: middle;
}
ul.mobile-home-section li a span {
padding: 10px 0px;
width: 230px;
display:block;
margin: auto;
}
答案 1 :(得分:1)
正如其他人所说,使用图像会更容易。最重要的是要知道并非所有display:
值都支持垂直对齐 - 我的首选是display: inline-block
,但如果您使用的是flexbox,则使用display: flex
可能会更方便
根据具体情况,我通常使用以下三种解决方案之一:
以下是每个例子。
注意:
<li>
丢失其子弹并获得自动宽度(与内容一样宽) - 根据您的上下文,您可能希望在width: 100%
上添加{ {1}} #or-another li
background: no-repeat center center; -webkit-background-size: cover; background-size: cover; background-image:url(...);
而不是#or-another span
,并且可以完全放弃#or-another li > *
#one-more li span {...}
#one-way li > * {
display: inline-block;
vertical-align: middle;
}
#or-another li {
display: table;
}
#or-another li > * {
display: table-cell;
vertical-align: middle;
}
#one-more li span {
display: inline-block;
vertical-align: middle;
}
#one-more li:before {
content:'';
width: 200px;
height: 200px;
display: inline-block;
vertical-align: middle;
background:url('https://placehold.it/200x200');
}
答案 2 :(得分:0)
在text-align: center;
课程中添加.mobile-home-section
,使其与中心对齐,然后删除padding-left: 50px;
课程下的.mobile-home-section li
。这两个CSS将成为:
.mobile-home-section {
margin: 0;
padding: 0;
list-style-type: none;
width: 100%;
background: #163158;
text-align: center;
}
.mobile-home-section li {
background-image: url('http://s18.postimg.org/m26o71ohx/icon_future_hover.png');
background-repeat: no-repeat;
background-position: 0 10px;
}
您可以看到demo here。
答案 3 :(得分:0)
尝试编辑填充和背景位置。
.container {
width: 600px;
}
.mobile-home-section {
margin: 0;
padding: 0;
list-style-type: none;
width: 100%;
background: #163158
}
.mobile-home-section li {
background-image: url('http://s18.postimg.org/m26o71ohx/icon_future_hover.png');
background-repeat: no-repeat;
background-position:50% 15px;
padding-left: 50px;
}
.mobile-home-section li a {
color: white;
text-decoration: none;
padding: 20px 50%;
display: block;
border-bottom: 1px solid white;
text-align:left;
}
更新试试这个
.mobile-home-section li a {
color: white;
text-decoration: none;
padding: 20px 0 20px 50%;
display: block;
border-bottom: 1px solid white;
text-align: left;
}
更新.mobile-home-section li a
的填充答案 4 :(得分:0)
选择者可以使用一些爱,但这是我的代码:
<div class="container">
<ul class="mobile-home-section">
<li><a href="#"><span><img src="http://s18.postimg.org/m26o71ohx/icon_future_hover.png" /><span>PROPERTY</span></span></a>
</li>
<li><a href="#"><span><img src="http://s18.postimg.org/m26o71ohx/icon_future_hover.png" /><span>FUTURE PLANNING</span></span></a>
</li>
<li><a href="#"><span><img src="http://s18.postimg.org/m26o71ohx/icon_future_hover.png" /><span>COMMERICAL</span></span></a>
</li>
</ul>
.container {
width: 600px;
}
.mobile-home-section {
margin: 0;
padding: 0;
list-style-type: none;
width: 100%;
background: #163158
}
.mobile-home-section li {
margin: 0;
padding: 0;
}
.mobile-home-section li a {
display: block;
height: 3em;
}
.mobile-home-section li a > span {
display: block;
width: 55%;
padding: 0;
margin: 0 auto;
text-align: left;
}
.mobile-home-section li a > span > span {
line-height: 2em;
font-size: 1.4em;
position: relative;
top: -0.4em;
}
.mobile-home-section li span img {
margin-right: 0.75em;
height: 2.6em;
position: relative;
top: 0.2em;
}
.mobile-home-section li a {
border-bottom: 1px solid white;
color: white;
display: block;
font-family: 'Times New Roman';
font-size: 1.2em;
padding: 0;
text-align: center;
text-decoration: none;
}