简体   繁体   中英

CSS Arrow vertically-centered on the right side of a div

As the title says, I am trying to center vertically a CSS triangle/arrow onto the right side of a div, and of course, I am looking to do this using a pure CSS solution.

I would have included pictures, but because of my low-rep, I can't.

Note: My current code isn't working properly and is not cross-browser compatible (ie Chrome puts the arrow on the top right~ but on Firefox, it gets squished down outside the bounds)

My code

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 link: http://jsfiddle.net/LvG2C/

Try like this:

.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: "";
}

height triangle 14px, for vertical alignment to do it: 14/2 = 7, for this margin-top: -7px;

Example

By the way, the generator triangles

Alternative solution keeping float in the run Using line-height value + font-size to set size of arrow and margin. http://jsfiddle.net/LvG2C/3/

edit: what follows is not compatible with text-ellipsis if you turn <a> as one inline-block.

you use float. it is not compatible with vertical-align.

You can set as inline-box and the arrow too to vertical-align them mutually. give a width to a to push arrow all the way : 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;
}

An alternative would be to set a class on the elements that you wish to have an arrow on the left side centered vertically. In the class would look something like:

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

This would mean that any element with a class="left-arrow" attribute would have an arrow centered vertically on the left side. Then the nav code would look like:

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

Nice use of CSS3 there. If your client is okay with you using cSS3 then your method suffices (if it isn't the best option out there). However, I work for clients that require the use of legacy browsers and as a matter of fact my work still actively uses IE7/8.

This is the approach I would employ for a simple arrow styling solution. It is simple, robust, and compatible with IE5+. IN addition, this method will work beautifully on mobile devices and tablet browsers. (Know your customers/client needs and decide if you want to fall back on this method).

  1. Create the arrow in photoshop and save it as a .png . The file should only be 50bytes or something ridiculously small. (5px X 10px).

  2. Create the gradient in photoshop as well. It should be (1px wide) and the height should match be exactly two times that of an li element. Create a normal / hover state on the image. So the first y pixels should be normal gradient and the next y pixels should be hover over gradient. We will use css to change the positioning of the background. NO reason to create 2 gradient images.

  3. Deploy CSS2.

    li { background: url(gradient.png) repeat-x; height: 30px; line-height: 30px; text-align: center; color: #000; font-size: 14px; } li:hover { background-position: 0 -30px; } li a { background: url(arrow.png) no-repeat 100% center; }

This should get you started. And show you an alternative approach.

SO is bugging out and can't get the code to code block to format

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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