首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法对齐css圆的导航列表元素的基线

无法对齐css圆的导航列表元素的基线
EN

Stack Overflow用户
提问于 2014-11-24 01:37:43
回答 3查看 46关注 0票数 0

我已经做了一个导航的css绘制的圆作为锚点元素。它们都有不同数量的文本,导致它们溢出到圆圈之外。因此,我使用了JS解决方案将文本水平对齐到圆圈的中间。我现在的问题是,圆圈基线是不相等的,这取决于它们有多少行文本。有没有一个简单的css解决方案。或者我也需要用javascript来计算和修改每个列表项的高度?

.html

代码语言:javascript
复制
<ul class="list-inline nav-horizontal">
    <li role="presentation" class="active">
        <a href="#1" data-toggle="tab" class="stylish">#1</a>
    </li>
    <li role="presentation">
        <a href="#2" data-toggle="tab" class="stylish">#2</a>
    </li>
    <li role="presentation">
        <a href="#3" data-toggle="tab" class="stylish">#3</a>
    </li>
    <li role="presentation">
        <a href="#4" data-toggle="tab" class="stylish">#4</a>
    </li>
    <li role="presentation">
        <a href="#5" data-toggle="tab" class="stylish">#5</a>
    </li>
</ul>

.css

代码语言:javascript
复制
.list-inline {
    display: inline-block;
    padding: 6px; 
}

.stylish {
    display: block;
    width: 140px;
    height: 140px;
    border-radius: 50%;
    border: 8px solid #ED1B24;
    font-size: 20px;
    color: #BBAE92;
    text-align: center;
    text-decoration: none;
    background: none;
    line-height: 1.1em;
}

.stylish:hover, li.active .stylish {
    color: #fff;
    text-decoration: none;
    background: #ED1B24;
}

.js

代码语言:javascript
复制
$("a.stylish").contents().wrap("<span>");
$("a.stylish span").css({
    position : "relative",
    "top" : function() {
        return ($(this).parent().height() - $(this).height()) / 2
    }
});
EN

回答 3

Stack Overflow用户

发布于 2014-11-24 01:55:18

这个怎么样?http://jsfiddle.net/hd8donbn/3/

代码语言:javascript
复制
.nav-horizontal li:before {
    content:'';
    display: inline-block;
    vertical-align: middle;
    height: 100%
}

.stylish {
    display: inline-block;
    text-decoration: none;
    color: #ED1B24;
    vertical-align: middle;
    max-width: 90%
}

如果可以的话,我会解释一切的:D

票数 0
EN

Stack Overflow用户

发布于 2014-11-24 02:12:16

我找到了一个简单的css解决方案。在.list-inline li类中添加vertical-align:text-top;解决了这个问题

票数 0
EN

Stack Overflow用户

发布于 2014-11-24 03:22:22

好吧,无意冒犯,但我已经写了一个我自己的标记来满足您的需求,不管您在您的问题中给出了什么。希望你会发现它很有用:

HTML

代码语言:javascript
复制
<ul>
    <li class="active"><a href="#">small</a></li>
    <li><a href="#">medium content</a></li>
    <li><a href="#">a bit larger content</a><`enter code here`/li>
    <li><a href="#">really large hunk of content</a></li>
    <li><a href="#">this is a rrrrreeeeaaaalllllllyyyyy big chunk, with a long word too</a></li>
</ul>

css

代码语言:javascript
复制
    *{
        margin: 0;
        padding: 0;
    }
    ul {
        display: inline-block;
        list-style: none; /*remove markers for `li`*/
    }

    li {
        border-radius: 50%; /*border-radius 70px;*/
        overflow: hidden; /*hide overflowing text, if any*/
        float: left; /*puts things in line, with their tops aligned. */
        /*If `display:inline-block` is used, it will match the bottoms at same level. try it*/

        border: 8px solid #ED1B24;
        display: block;
        height: 140px;
        text-align: center;
        width: 140px;
    }
    li a{
        padding: 0 5px; /*prevent border cutting*/
        word-wrap: break-word; /*breaks long words*/
        text-decoration: none;
        color: #BBAE92;

        /*the next 4 allow a child to be centered vertically*/
        display: block;
        position: relative;
        top: 50%;
        transform: translateY(-50%);

    }
    li.active,
    li:hover{
        cursor: pointer; /*makes it look like a link*/
        background: #ED1B24;
    }
    li.active a,
    li:hover a{
        color: #fff;
    }

和一些次要的js

代码语言:javascript
复制
$('li').click(function(){
    $(this).child('a').click();
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27092078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档