在菜单中的图标下方添加文字
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在菜单中的图标下方添加文字相关的知识,希望对你有一定的参考价值。
我是CSS和html的新手。我创建了一个菜单栏,其中包含8个图标。我想在每个图标下面添加文本,但无论我在HTML中添加文本的哪个位置,都会让一切都变得混乱。我已经尝试了所有我能想到的东西。任何帮助将不胜感激。谢谢!
<div class="icon-bar">
<a href="#"><span id="surround">
<span id="initial"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/ria-
stroke.png"></span>
<span id="onhover"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/circle-
ria.png"></span>
</span></a>
<a href="#"><span id="surround">
<span id="initial"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/yoga-
stroke.png"></span>
<span id="onhover"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/circle-
yoga.png"></span>
</span></a>
<a href="#"><span id="surround">
<span id="initial"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/abit-
stroke.png"></span>
<span id="onhover"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/circle-
abit.png"></span>
</span></a>
<a href="#"><span id="surround">
<img class="esm-icon-active" src="http://treehouserecoverypdx.com/wp-
content/uploads/2017/05/circle-esm.png">
</span></a>
<a href="#"><span id="surround">
<span id="initial"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/pg-
stroke.png"></span>
<span id="onhover"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/circle-
pg.png"></span>
</span></a>
<a href="#"><span id="surround">
<span id="initial"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/lit-
stroke.png"></span>
<span id="onhover"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/circle-
lit.png"></span>
</span></a>
<a href="#"><span id="surround">
<span id="initial"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/1on1-
stroke.png"></span>
<span id="onhover"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/circle-
1on1.png"></span>
</span></a>
<a href="#"><span id="surround">
<span id="initial"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/education-
stroke.png"></span>
<span id="onhover"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/circle-
education.png"></span>
</span></a>
</div>
这是CSS:
#surround{
display:inline-block;
width:12.2%;
}
#onhover {
display: none;
}
#surround:hover span[id="initial"] {
display: none;
}
#surround:hover span[id="onhover"] {
display: block;
}
.icon-bar {
width: 100%;
overflow: hide;
padding-top: 7px;
background-color:black;
padding-bottom:100px;
}
.icon-bar a {
float: right;
width: 12.5%;
text-align: center;
transition: all 0.3s ease;
color: white;
font-size: 12px;
}
.esm-icon {
opacity:0.5!important;
min-width: 55px;
max-width: 55px;
display: block;
margin: auto;
border-radius: 100%;
border: 2px solid transparent;
}
.esm-icon-active {
opacity:1!important;
min-width: 55px;
max-width: 55px;
display: block;
margin: auto;
border-radius: 100%;
border: 2px solid transparent;
}
我相信我有一个解决方案。首先,我将附上一个截图,向您展示我的解决方案的结果,然后我会告诉您我做了什么。
这是截图:
我在每个图标下添加的文字只是“这是一个测试”。好的,现在让我解释一下我做了什么以及为什么。
真的,基本上我做了两件事。第一件事是在你的CSS代码中,我注释掉了1行。这里是:
.icon-bar a {
/*float: right;*/
width: 12.5%;
text-align: center;
transition: all 0.3s ease;
color: white;
font-size: 12px;
}
“浮动:正确”是主要问题。不幸的是,我无法准确解释为什么右边的浮动是问题,但几个月前我建立了一个网站,我在导航栏中放了一些东西,同样的事情发生在我身上。
我不知道你是不是把事情弄得正确,因为你想按特定的顺序排列图标但是如果是这样的话,那么我只想进入浮点数(总是一个坏主意)而不是试图获得你想要的订单。将图标按照您希望它们首先出现的顺序放入代码中。作为未来的练习,这将为您节省很多麻烦。
我说我做了两件事。这是第二个......
我将文本放在代码中的每个图标后面。这是我改变它的方式。仔细看看我如何将文本放置在具有“环绕”类的图像的跨度中。
HTML
<div class="icon-bar">
<a href="#"><span id="surround">
<span id="initial"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/ria-
stroke.png"><p>This is a test</p></span>
<span id="onhover"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/circle-
ria.png"></span>
</span></a>
<a href="#"><span id="surround">
<span id="initial"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/yoga-
stroke.png"><p>This is a test</p></span>
<span id="onhover"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/circle-
yoga.png"></span>
</span></a>
<a href="#"><span id="surround">
<span id="initial"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/abit-
stroke.png"><p>This is a test</p></span>
<span id="onhover"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/circle-
abit.png"></span>
</span></a>
<a href="#"><span id="surround">
<img class="esm-icon-active" src="http://treehouserecoverypdx.com/wp-
content/uploads/2017/05/circle-esm.png"><p>This is a test</p></span></a>
<a href="#"><span id="surround">
<span id="initial"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/pg-
stroke.png"><p>This is a test</p></span>
<span id="onhover"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/circle-
pg.png"></span>
</span></a>
<a href="#"><span id="surround">
<span id="initial"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/lit-
stroke.png"><p>This is a test</p></span>
<span id="onhover"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/circle-
lit.png"></span>
</span></a>
<a href="#"><span id="surround">
<span id="initial"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/1on1-
stroke.png"><p>This is a test</p></span>
<span id="onhover"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/circle-
1on1.png"></span>
</span></a>
<a href="#"><span id="surround">
<span id="initial"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/education-
stroke.png"><p>This is a test</p></span>
<span id="onhover"><img class="esm-icon"
src="http://treehouserecoverypdx.com/wp-content/uploads/2017/05/circle-
education.png"></span>
</span></a>
</div>
通过将文本放在图像代码之后,默认情况下文本将在图标下居中,因为您将“环绕”类显示为“内联块”。这是CSS代码...我确实在p标签中添加了“white”的字体颜色,这样你就可以看到文字了:
CSS
p {
color: white;
}
#surround{
display:inline-block;
width:12.2%;
}
#onhover {
display: none;
}
#surround:hover span[id="initial"] {
display: none;
}
#surround:hover span[id="onhover"] {
display: block;
}
.icon-bar {
width: 100%;
overflow: hide;
padding-top: 7px;
background-color:black;
padding-bottom:100px;
}
.icon-bar a {
/*float: right;*/
width: 12.5%;
text-align: center;
transition: all 0.3s ease;
color: white;
font-size: 12px;
}
.esm-icon {
opacity:0.5!important;
min-width: 55px;
max-width: 55px;
display: block;
margin: auto;
border-radius: 100%;
border: 2px solid transparent;
}
.esm-icon-active {
opacity:1!important;
min-width: 55px;
max-width: 55px;
display: block;
margin: auto;
border-radius: 100%;
border: 2px solid transparent;
}
我希望有所帮助!还有一件事。我注意到你在“.icon-bar”的CSS中有一个“padding-bottom:100px”。当然,如果你不喜欢在图标和文字下面有那么多空间,你可以给它一个20px或其他东西的填充。
让我知道这对你有用。
以上是关于在菜单中的图标下方添加文字的主要内容,如果未能解决你的问题,请参考以下文章