0

There is a page with a list of product entries which have different statuses. I want to be able to use little ribbons per each product, which would indicate what is the current status of the product.

An example of such a list with the ribbon is located here: http://jsfiddle.net/19ren646/

HTML:

<div class="list-group-item">
    <h4>Short item</h4>
    <p>I don't have that much content really</p>

    <div class="ribbon">
        <span>Ribbon</span>
    </div>
</div>
<div class="list-group-item">
    <h4>A much longer item</h4>
    <p>I have a lot of content, because I have a Lorem ipsum in me</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dignissimos amet, minus maiores adipisci. Neque possimus, iusto odio explicabo. Libero, fugiat.</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsam, aliquid.</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dignissimos amet, minus maiores adipisci. Neque possimus, iusto odio explicabo. Libero, fugiat.</p>

    <div class="ribbon">
        <span>Ribbon</span>
    </div>
</div>

CSS:

.list-group-item {
    position:relative;
    padding:20px;
    padding-left:60px;
    border:1px solid #ccc;
}
.ribbon {
    position:absolute;
    top:0;
    left:0;
    width:40px;
    height:100%;
    background:blue;
    color:white;
}
.ribbon span {
    display:block;
    transform:rotate(90deg);
}

To save horizontal space, the ribbon is rotated 90 deg. But my question is, can I get the text of the ribbon vertically in the middle?

I know I can push the text down by user position:relative or margins for example, but that would only work well if the heights of all entry items were constant, which they aren't. So can I push the text to the middle, in a way that would work for any height of the .list-group-item element?

Digital Ninja
  • 3,415
  • 5
  • 26
  • 51

3 Answers3

1

You can use absolute positioning + translate function in css:

http://jsfiddle.net/19ren646/1/

.ribbon span {
    position: absolute;
    top:50%;
    display:block;
    transform:rotate(90deg) translate(-50%);
}

Edit

To compatibilize with safari it's fine to define with webkit prefix too

.ribbon span {
    position: absolute;
    top:50%;
    display:block;
    -webkit-transform: rotate(90deg) translate(-50%);
    transform: rotate(90deg) translate(-50%);
}
Marcos Pérez Gude
  • 21,869
  • 4
  • 38
  • 69
0

try this:

.ribbon span {
    display:block;
    position: relative;
    top:50%;
    transform:rotate(90deg) translateX(-50%);
    text-align:center;
}

fiddle

this should be quite accurate in positioning_

Community
  • 1
  • 1
maioman
  • 18,154
  • 4
  • 36
  • 42
0

hi you can achieve by simply this.

.ribbon span {
    display:block;
    transform:rotate(90deg);
    top: calc(50% - 1.5em);
    position: relative;
}

Even you can do like this for better look

.ribbon span {
    display:block;
    top:20%;
    left:0.6em;
    position: relative;
    -ms-writing-mode: tb-rl; /* old syntax. IE */
    -webkit-writing-mode: vertical-rl;
    -moz-writing-mode: vertical-rl;
    -ms-writing-mode: vertical-rl;
    writing-mode: vertical-rl; /* new syntax */
    -webkit-text-orientation: upright;
    -moz-text-orientation: upright;
    -ms-text-orientation: upright;
    text-orientation: upright;
}

here is the working demo for this last one

Demo code you can Try like this

Himesh Aadeshara
  • 2,114
  • 16
  • 26