3

I have a list which are displayed with arrows using :after. It works ok, but sometimes more than one line of text is dynamically added inside each <li> - how can I make the arrow stretch or just somehow follow the height of the <li>?

See fiddle here. The first step has more text and so the arrow no longer fits the height.

* {
    box-sizing: border-box;
}

#step {
    padding: 0;
    list-style-type: none;
    font-family: arial;
    font-size: 12px;
    clear: both;
    line-height: 1em;
    margin: 0;
    text-align: center;
    display:table;
    width: 100%;
}

#step li {
    float: left;
    padding: 10px 30px 10px 40px;
    background: #333;
    color: #fff;
    position: relative;
    width: 30%;
    margin: 0 10px;
}


#step li:after {
    content: '';
    border-left: 16px solid #333;
    border-top: 16px solid transparent;
    border-bottom: 16px solid transparent;
    position: absolute;
    top: 0;
    left: 100%;
    z-index: 20;
}
<ul id="step">
    <li>Step 1<br/>Step 1</li>
    <li>Step 2</li>
    <li>Step 3</li>
</ul>

Is there a dynamic solution to this issue?

Thanks in advance.

ketan
  • 19,129
  • 42
  • 60
  • 98
Meek
  • 3,086
  • 9
  • 38
  • 64

4 Answers4

1

Stretching the arrow - would look bad - just center the arrow:

add - top:50%; and add margin-top: - 16px (half of the arrow)

works great :)

#step li:after {
    content: '';
    border-left: 16px solid #333;
    border-top: 16px solid transparent;
    border-bottom: 16px solid transparent;
    position: absolute;
    top: 50%;
    left: 100%;
    z-index: 20;
    margin-top:-16px;
}
Daniel
  • 815
  • 1
  • 6
  • 13
1

I found an interesting solution here which seems to be able to handle dynamic height - it's using linear gradient backgrounds:

/* CSS: */

.box {
  width: 400px;
  background-color: #307084;
  color: #fff;
  margin-bottom: 20px;
  padding: 10px;
  position: relative;
}

.box:before,
.box:after {
  width: 20px;
  height: 50%;
  position: absolute;
  left: 100%;
  content: "";
}

.box:before {
  top: 0px;
  background: -webkit-linear-gradient(left bottom, #307084 50%, rgba(0, 0, 0, 0) 50%);
  background: linear-gradient(to right top, #307084 50%, rgba(0, 0, 0, 0) 50%);
}

.box:after {
  top: 50%;
  background: -webkit-linear-gradient(left top, #307084 50%, rgba(0, 0, 0, 0) 50%);
  background: linear-gradient(to right bottom, #307084 50%, rgba(0, 0, 0, 0) 50%);
}
<!-- HTML: -->

<div class="box" style="font-size: 20px">
  css triangle/arrow grow when its parent div is resized
</div>

<div class="box" style="font-size: 25px;">
  css triangle/arrow grow when its parent div is resized
</div>

<div class="box" style="font-size: 40px;">
  css triangle/arrow grow when its parent div is resized
</div>
yinsweet
  • 2,823
  • 1
  • 9
  • 21
Meek
  • 3,086
  • 9
  • 38
  • 64
0

a little more code, but dynamic solution:

* {
  box-sizing: border-box;
}

#step {
  padding: 0;
  list-style-type: none;
  font-family: arial;
  font-size: 12px;
  clear: both;
  line-height: 1em;
  margin: 0;
  text-align: center;
  display: table;
  width: 100%;
}

#step li {
  float: left;
  padding: 10px 30px 10px 40px;
  background: #333;
  color: #fff;
  position: relative;
  width: 30%;
  margin: 0 10px;
}

li:before,
li:after {
  width: 16px;
  height: 50%;
  position: absolute;
  left: 100%;
  content: '';
}

li:before {
  top: 0px;
  background: linear-gradient(to right top, #333 50%, transparent 50%)
}

li:after {
  top: 50%;
  background: linear-gradient(to right bottom, #333 50%, transparent 50%)
}

jsfiddle-link

Igor Ivancha
  • 3,413
  • 4
  • 30
  • 39
-1

You could do something like this with JS and with span instead of :after

$(window).on("resize", function () {
    $('li').each(function() {
      var width = $(this).width();
      var height = $(this).outerHeight();
      $(this).children('span').css({
        'border-top': height/2+'px'+' solid transparent',
        'border-left': height+'px'+' solid #333333',
        'border-bottom': (height/2+1)+'px'+' solid transparent',
        'right': '-'+(height-1)+'px'  
      }); 
    });
}).resize();
* {
    box-sizing: border-box;
}

#step {
    padding: 0;
    list-style-type: none;
    font-family: arial;
    font-size: 12px;
    clear: both;
    line-height: 1em;
    margin: 0;
    text-align: center;
    display:table;
    width: 100%;
}

#step li {
    float: left;
    padding: 10px 30px 10px 40px;
    background: #333;
    color: #fff;
    position: relative;
    width: 20%;
    margin: 0 30px;
    box-sizing: border-box;
}

li span {
  position: absolute;
  height: 0;
  width: 0;
  top: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="step">
    <li>Step 1<br/>Step 1 <br>Step 1 <span></span></li>
    <li>Step 2<br>Step 1 <span></span></li>
    <li>Step 3 <span></span></li>
</ul>
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176