admin管理员组文章数量:1404330
<div>
<ul>
<li class="dropdown">
<a href="link" class="col-xs-12">
<span class="dropdown-toggle" data-toggle="dropdown">
<span id="image" class="col-xs-3"><img src="/home" width="100" height="100" /></span>
<span id="display_name" class="col-xs-6"> @Name </span>
<span class="caret"></span>
</span>
</a>
</li>
</ul>
</div>
I need center span element which contains name is always align at middle of anchor tag. I don't want to set height and line height of the a tag. Because the name length may vary. so a tag height also will differs. Whatever the name entered I need to align it to center. How to achieve this ?
<div>
<ul>
<li class="dropdown">
<a href="link" class="col-xs-12">
<span class="dropdown-toggle" data-toggle="dropdown">
<span id="image" class="col-xs-3"><img src="/home" width="100" height="100" /></span>
<span id="display_name" class="col-xs-6"> @Name </span>
<span class="caret"></span>
</span>
</a>
</li>
</ul>
</div>
I need center span element which contains name is always align at middle of anchor tag. I don't want to set height and line height of the a tag. Because the name length may vary. so a tag height also will differs. Whatever the name entered I need to align it to center. How to achieve this ?
Share Improve this question edited Jun 13, 2016 at 11:14 Shesha asked Jun 13, 2016 at 11:09 SheshaShesha 2,0077 gold badges23 silver badges29 bronze badges 8- center vertically or horizontally?? and where is the anchor tag in you code?? – Gaurav Aggarwal Commented Jun 13, 2016 at 11:10
-
Have you tried
margin: auto
? – William Brochmann Commented Jun 13, 2016 at 11:10 - Yes. margin: is not working – Shesha Commented Jun 13, 2016 at 11:11
- have you tried text-align:center ? – Ripun Commented Jun 13, 2016 at 11:12
- Can u paste your css or using code snapper? – MrDevel Commented Jun 13, 2016 at 11:13
2 Answers
Reset to default 5Use this display:table
and display:table-cell
structure for vertically align it to center.
a {
display: table;
}
a span {
display: table-cell;
vertical-align: middle;
}
<div>
<ul>
<li class="dropdown">
<a href="link" class="col-xs-12">
<span class="dropdown-toggle" data-toggle="dropdown">
<span id="image" class="col-xs-3"><img src="/home" width="100" height="100" /></span>
<span id="display_name" class="col-xs-6"> @Name<br>@Name<br>@Name </span>
<span class="caret"></span>
</span>
</a>
</li>
</ul>
</div>
In above snippet no matter how long is the name it will automatically align it to center
CSS-tricks has a guide on how to center things, vertically or horizontally.
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
transform: translateY(-50%);
}
This means that, implemented in your code, it will look like this:
<div>
<ul>
<li class="dropdown">
<a href="link" class="col-xs-12">
<span class="dropdown-toggle" data-toggle="dropdown">
<span id="image" class="col-xs-3"><img src="/home" width="100" height="100" /></span>
<span id="display_name" class="col-xs-6"> @Name </span>
<span class="caret"></span>
</span>
</a>
</li>
</ul>
</div>
CSS:
.col-xs-12{
position: relative;
}
.col-xs-6{
position: absolute;
top:50%;
transform: translateY(-50%);
}
This might need a little tweaking, since the positioning is absolute and you have two other elements, but it should work just fine.
本文标签: javascriptCenter align span inside anchor tagStack Overflow
版权声明:本文标题:javascript - Center align span inside anchor tag - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744799337a2625768.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论