1

正如标题所说,我试图将 CSS 三角形/箭头垂直居中到 div 的右侧,当然,我希望使用纯 CSS 解决方案来做到这一点。

我本来会包括图片,但由于我的低代表,我不能。

注意:我当前的代码不能正常工作并且不兼容跨浏览器(即 Chrome 将箭头放在右上角〜但在 Firefox 上,它会被挤压到边界之外)

我的代码

HTML

<div class="main-panel">
    <nav class="left-panel">
        <ul class="page-nav">
            <li class="page-tab active"><a class="page-nav-link">Dashboard</a></li>
            <li class="page-tab"><a class="page-nav-link">Page 1</a></li>
            <li class="page-tab"><a class="page-nav-link">Page 2</a></li>
            <li class="page-tab"><a class="page-nav-link">Page 3</a></li>
            <li class="page-tab"><a class="page-nav-link">Somewhat long</a></li>
            <li class="page-tab"><a class="page-nav-link">Somewhat long abc</a></li>
            <li class="page-tab"><a class="page-nav-link">Somewhat long abcdef</a></li>
            <li class="page-tab"><a class="page-nav-link">Somewhat long abcdefghi</a></li>
            <li class="page-tab"><a class="page-nav-link">Somewhat long abcdefghijkl</a></li>
            <li class="page-tab"><a class="page-nav-link">Very long element abcdefghijklmno</a></li>
        </ul>
    </nav>
    <div class="right-panel">
        <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam pellentesque nisl vitae nulla dignissim, at rutrum nunc interdum. Mauris et facilisis orci. Phasellus ac libero id nisl malesuada ultricies. Aenean vitae cursus velit. Nulla sit amet leo eu enim scelerisque volutpat. Vivamus gravida felis et pulvinar mollis. Sed bibendum consectetur nisi, sit amet tincidunt enim interdum a. Duis sed gravida felis. Fusce mauris est, bibendum a neque et, fringilla placerat magna. Vestibulum pellentesque massa quis bibendum dapibus. Praesent pharetra ipsum id libero dapibus rutrum. Quisque accumsan dictum lacinia.
            Mauris ut mi nec orci accumsan consequat. Donec blandit augue eget nulla fermentum, quis porttitor lacus feugiat. Proin vehicula dolor id lorem egestas elementum. Nullam vitae rutrum tortor. Proin varius adipiscing nibh. Vivamus ultrices nibh nec varius bibendum. Integer quis nisi sed metus adipiscing sagittis. Curabitur congue, nisi nec pretium suscipit, mi leo posuere dui, pellentesque dapibus ligula urna a ligula. Quisque mauris nisl, interdum eu est in, elementum cursus arcu. Sed suscipit vel erat vitae viverra. Mauris neque ipsum, bibendum eget hendrerit ac, vulputate in eros. Ut tincidunt, urna ac auctor porttitor, mauris purus varius erat, ut pellentesque urna turpis non nibh. Aenean sed neque a velit tristique tristique.
            Praesent id cursus libero. Vestibulum pulvinar feugiat neque pretium suscipit. Nam vitae magna eget elit aliquam accumsan. Duis et eleifend enim. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec nunc est, commodo quis pretium vitae, lacinia ac lectus. Cras mi tellus, suscipit non leo eget, auctor feugiat mauris. Praesent vitae vehicula ipsum, nec ultrices arcu. Proin mollis leo sit amet erat scelerisque feugiat. Nullam eget varius ante. Praesent a laoreet libero.
            Suspendisse ut eros et odio rhoncus malesuada. Integer laoreet, leo at fringilla volutpat, augue lorem tincidunt augue, sit amet feugiat erat neque in est. Phasellus imperdiet, nisi vel faucibus auctor, sapien orci mollis est, tincidunt vehicula nisi nunc sit amet purus. Sed cursus ipsum nisi, id elementum leo feugiat in. Phasellus vitae arcu ac ligula pellentesque malesuada. Maecenas vulputate nunc et consequat pellentesque. Nulla semper quam a orci eleifend, vel gravida est euismod. Nam fermentum, nisi ullamcorper congue convallis, turpis purus lacinia ligula, ac posuere urna est non felis. Cras lobortis ligula neque, vel fermentum magna facilisis sit amet. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
            Quisque a quam nec sem vestibulum dapibus quis non lacus. Integer eget ligula eu turpis pretium iaculis. Cras gravida ligula in mauris bibendum placerat. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Proin ac tristique risus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Quisque diam nisi, sodales vel nisi a, sollicitudin commodo ipsum. Fusce elementum nisi ac semper dignissim. Etiam nec felis dapibus lacus faucibus vehicula eget eu odio. Sed id risus tellus. Curabitur in luctus orci.
        </p>
    </div>
</div>

CSS

* {
    margin: 0;
    padding: 0;
    font-family: "Helvetica Neue",Helvetica,sans-serif;
    line-height: 20px;
}

.main-panel {
    min-width: 700px;
    width: 100%;
    min-height: 500px;
    max-height: 700px;
    display: -webkit-flex;
    display: flex;
    background: rgba(64, 64, 64, 0.4);
}

.left-panel {
    border-width: 2px 0 0 0 0;
    background: rgba(245, 245, 245, 0.4); /* fallback color if gradients are not supported */
    background: -webkit-linear-gradient(top, rgba(245, 245, 245, 0.4), rgba(221, 221, 221, 0.4)); /* For Chrome and Safari */
    background:    -moz-linear-gradient(top, rgba(245, 245, 245, 0.4), rgba(221, 221, 221, 0.4)); /*     For old Fx (3.6 to 15) */
    background:     -ms-linear-gradient(top, rgba(245, 245, 245, 0.4), rgba(221, 221, 221, 0.4)); /* For pre-releases of IE 10*/
    background:      -o-linear-gradient(top, rgba(245, 245, 245, 0.4), rgba(221, 221, 221, 0.4)); /* For old Opera (11.1 to 12.0) */ 
    background:         linear-gradient(to center bottom, rgba(245, 245, 245, 0.4), rgba(221, 221, 221, 0.4)); /* Standard syntax; must be last */
    box-shadow:         0px 0px 7px white inset;
    -moz-box-shadow:    0px 0px 7px white inset;
    -webkit-box-shadow: 0px 0px 7px white inset;

    overflow: hidden;
    min-width: 180px;
    max-width: 280px;
    padding: 5px 0 5px 7px;
}

.right-panel {
    background-color: white;
    overflow: hidden;
    padding: 5px 5px 5px 5px;
    -webkit-flex: 1;
    flex: 1;
    flex-basis: auto;
}

.left-panel > .page-nav > .page-tab {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    border: solid 1px rgba(245, 245, 245, 0.4);
    box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.3) inset;
    border-top-width: 1px;
    border-right-width: 0;
    padding: 0 5px;
    margin-bottom: 0px;
    cursor: pointer;
    height: 25px;
    text-transform: uppercase;
    font-weight: bold;
    font-size: 11px;
    list-style: none;
    text-align: center;
}

.left-panel > .page-nav > .page-tab.active {
    box-shadow: 2px 2px 2px rgba(245, 245, 245, 0.6) inset;
    background: rgba(245, 245, 245, 0.4); /* fallback color if gradients are not supported */
    background: -webkit-linear-gradient(top, rgba(68, 68, 68, 0.4), rgba(59, 59, 59, 0.4)); /* For Chrome and Safari */
    background:    -moz-linear-gradient(top, rgba(68, 68, 68, 0.4), rgba(59, 59, 59, 0.4)); /*   For old Fx (3.6 to 15) */
    background:     -ms-linear-gradient(top, rgba(68, 68, 68, 0.4), rgba(59, 59, 59, 0.4)); /* For pre-releases of IE 10*/
    background:      -o-linear-gradient(top, rgba(68, 68, 68, 0.4), rgba(59, 59, 59, 0.4)); /* For old Opera (11.1 to 12.0) */ 
    background:         linear-gradient(to center bottom, rgba(68, 68, 68, 0.4), rgba(59, 59, 59, 0.4)); /* Standard syntax; must be last */
}

.left-panel > .page-nav > .page-tab.active:after {
    content: " ";
    height: 0;
    width: 0;

    border-top: 7px solid transparent;
    border-bottom: 7px solid transparent; 
    border-right: 7px solid white;
    margin-right: -5px;
    display: inline-block;
    float: right;
}

.left-panel > .page-nav > .page-tab:hover {
    box-shadow: 2px 2px 2px rgba(245, 245, 245, 0.6) inset;
}

JSFiddle 链接:http: //jsfiddle.net/LvG2C/

4

4 回答 4

2

试试这样:

.left-panel > .page-nav > .page-tab {
    position: relative;
}

.left-panel > .page-nav > .page-tab.active:after {
    width: 0px;
    height: 0px;
    border-style: solid;
    border-width: 7px 7px 7px 0;
    border-color: transparent #ffffff transparent transparent;
    position: absolute;
    top: 50%;
    right: 0;
    margin-top: -7px;
    content: "";
}

高度三角形 14px,用于垂直对齐:14/2 = 7,对于此边距顶部:-7px;

例子

顺便说一下,发电机三角形

于 2013-06-17T00:10:41.970 回答
0

在运行中保持浮动的替代解决方案 使用 line-height value + font-size 设置箭头和边距的大小。 http://jsfiddle.net/LvG2C/3/

编辑:如果您将其作为一个内联块, 则以下内容与文本省略号不兼容。<a>

你使用浮动。它与垂直对齐不兼容。

您也可以将它们设置为内联框和箭头,以使它们相互垂直对齐。给 a 一个宽度以一直推动箭头:http: //jsfiddle.net/LvG2C/1/

.left-panel > .page-nav > .page-tab.active:after {
    content:" ";
    height: 0;
    width: 0;
    border-top: 7px solid transparent;
    border-bottom: 7px solid transparent;
    border-right: 7px solid white;
    vertical-align:middle;
    display: inline-block;
}
.left-panel > .page-nav > .page-tab:hover {
    box-shadow: 2px 2px 2px rgba(245, 245, 245, 0.6) inset;
}
a {
    display:inline-block;
    width:100%;
    vertical-align:middle;
    margin-right:-3px;
}
于 2013-06-17T00:14:40.400 回答
0

另一种方法是在您希望左侧垂直居中箭头的元素上设置一个类。在课堂上看起来像:

.left-arrow {
    background: url('/images/arrow.png') 0% 50% no-repeat;
    padding-left: 20px; // Number of pixels you need to offset the content to make the content come after the arrow the way you want it to.
}

这意味着任何具有 class="left-arrow" 属性的元素都会在左侧有一个垂直居中的箭头。然后导航代码将如下所示:

<nav class="left-panel">
    <ul class="page-nav">
        <li class="page-tab active"><a class="page-nav-link">Dashboard</a></li>
        <li class="page-tab"><a class="page-nav-link">Page 1</a></li>
        <li class="page-tab"><a class="page-nav-link">Page 2</a></li>
        <li class="page-tab"><a class="page-nav-link">Page 3</a></li>
        <li class="page-tab"><a class="page-nav-link left-arrow">Somewhat long</a></li>
        <li class="page-tab"><a class="page-nav-link">Somewhat long abc</a></li>
        <li class="page-tab"><a class="page-nav-link">Somewhat long abcdef</a></li>
        <li class="page-tab"><a class="page-nav-link">Somewhat long abcdefghi</a></li>
        <li class="page-tab"><a class="page-nav-link">Somewhat long abcdefghijkl</a></li>
        <li class="page-tab"><a class="page-nav-link">Very long element abcdefghijklmno</a></li>
    </ul>
</nav>
于 2013-06-17T01:19:50.547 回答
-1

在那里很好地使用了 CSS3。如果您的客户同意您使用 cSS3,那么您的方法就足够了(如果它不是最好的选择)。但是,我为需要使用旧版浏览器的客户工作,事实上我的工作仍然积极使用 IE7/8。

这是我将采用的简单箭头样式解决方案的方法。它简单、健壮且与 IE5+ 兼容。此外,这种方法在移动设备和平板浏览器上也能很好地工作。(了解您的客户/客户需求并决定是否要使用此方法)。

  1. 在 Photoshop 中创建箭头并将其保存为 .png 。该文件应该只有 50 字节或小得离谱的东西。(5 像素 X 10 像素)。

  2. 也可以在 Photoshop 中创建渐变。它应该是(1px 宽)并且高度应该正好是 li 元素的两倍。在图像上创建正常/悬停状态。所以第一个 y 像素应该是正常的渐变,接下来的 y 像素应该悬停在渐变上。我们将使用 css 来改变背景的位置。没有理由创建 2 个渐变图像。

  3. 部署 CSS2。

    li { 背景: url(gradient.png) 重复-x;高度:30px;行高:30px;文本对齐:居中;颜色:#000;字体大小:14px;} li:hover { 背景位置:0 -30px; } li a { background: url(arrow.png) no-repeat 100% center; }

这应该让你开始。并向您展示另一种方法。

SO 出现问题,无法将代码转换为代码块以格式化

于 2013-06-17T00:25:23.430 回答