简体   繁体   English

无法对齐CSS圆圈的导航列表元素的基线

[英]Having trouble aligning baselines of nav list elements that are css circles

I have made a nav out of css drawn circles as anchor elements. 我已经用css绘制的圆圈作为锚元素导航。 They all have varying amounts of text in them causing them to spill out of the circles. 它们中包含的文字数量各不相同,导致它们从圈子中溢出。 So I have used a JS solution to horizontally align the text to the middle of the circle. 因此,我使用了JS解决方案将文本水平对齐到圆的中间。 The problem I now have is that the circles baselines are unequal depending on how many lines of text are in them. 我现在遇到的问题是,圆基线不相等,具体取决于其中的文本行数。 Is there an easy css solution to this. 是否有一个简单的CSS解决方案。 Or will I have to calculate and amend the heights of each list item with javascript as well? 还是我也必须使用javascript计算并修改每个列表项的高度?

.html .html

<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 .css

.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 .js

$("a.stylish").contents().wrap("<span>");
$("a.stylish span").css({
    position : "relative",
    "top" : function() {
        return ($(this).parent().height() - $(this).height()) / 2
    }
});

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

.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%
}

If its ok i will explain everything :D 如果可以的话,我会解释一切:D

I found a simple css solution. 我找到了一个简单的CSS解决方案。 Adding vertical-align:text-top; 添加vertical-align:text-top; to the class of .list-inline li solved the issue .list-inline li类的问题解决了

well, not to be rude here, but i've written a markup of my own to cater to you needs, disregarding what you've given in your question. 好吧,在这里不要失礼,但是我已经写了一个自己的标记来满足您的需求,而忽略了您在问题中给出的内容。 Hope you find it usefull: 希望对您有用:

HTML 的HTML

<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 的CSS

  *{ 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; } 


and some minor js 和一些小js

 $('li').click(function(){ $(this).child('a').click(); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM