Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically add <span> in div set every span tag to equal height

Tags:

html

jquery

css

I try to create this but not set, div height is fixed 50px and maximum span add in 5 enter image description here

$('section.group').each(function() {
        //alert(($(this).find('.item')).length);
        var hig =50;
        
        var total =($(this).find('.item')).length;
        if(total !== 0){
          //alert(hig/total);
          //$('.item').height(hig/total);
          $(this).each('.item').height(hig/total);
          
        }
      }
    );
section.group{
  height:50px;
  margin-bottom:10px;
  overflow:hidden;
  border:1px solid;
}
.item{
  display:block;
}
.item:nth-child(1) {
    background: #ff0000;
}
.item:nth-child(2) {
    background: #00ff00;
}
.item:nth-child(3) {
    background: #0000ff;
}
.item:nth-child(4) {
    background: #000;
}
.item:nth-child(5) {
    background: #f0f000;
}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<section class="group">
  <span class="item"></span>
  <span class="item"></span>
  <span class="item"></span>

</section>

<section class="group">
  <span class="item"></span>
  <span class="item"></span>
  <span class="item"></span>
  <span class="item"></span>
  <span class="item"></span>

</section>  
  <section class="group">
  <span class="item"></span>
    <span class="item"></span>
</section> 
  <section class="group">
</section>
like image 650
Kapil Avatar asked Feb 29 '16 09:02

Kapil


People also ask

How do I make one div the same height as another?

Answer: Use the CSS3 flexbox With CSS3 flex layout model you can very easily create the equal height columns or <div> elements that are aligned side by side. Just apply the display property with the value flex on the container element and the flex property with the value 1 on child elements.

Does span have height?

The <span> tag is a inline element, it fits into the flow of the content and can be distributed over multiple lines. We can not specify a height or width or surround it with a margin. It can be placed inside a paragraph to define a part of it without affecting the appearance of the text.

Can we give height and width to span?

Span is an inline element. It has no width or height.


3 Answers

Please try This Code :-

$(this).each('.item').height(hig/total); to replce with $(this).find('.item').height(hig/total);


    $('section.group').each(function() {       
            var hig =50;        
            var listitem =($(this).find('.item')).length;
            if(listitem !== 0){         
              $(this).find('.item').height(hig/listitem);          
            }
          }
        );
like image 99
Mitul Patel Avatar answered Oct 14 '22 03:10

Mitul Patel


Please have a look at updated code:

I changed this:

$(this).each('.item').height(hig/total);

to:

$(this).find('.item').height(hig/total);

$('section.group').each(function() {
        //alert(($(this).find('.item')).length);
        var hig =50;
        
        var total =($(this).find('.item')).length;
        if(total !== 0){
          //alert(hig/total);
          //$('.item').height(hig/total);
          $(this).find('.item').height(hig/total);
          
        }
      }
    );
section.group{
  height:50px;
  margin-bottom:10px;
  overflow:hidden;
  border:1px solid;
}
.item{
  display:block;
}
.item:nth-child(1) {
    background: #ff0000;
}
.item:nth-child(2) {
    background: #00ff00;
}
.item:nth-child(3) {
    background: #0000ff;
}
.item:nth-child(4) {
    background: #000;
}
.item:nth-child(5) {
    background: #f0f000;
}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<section class="group">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>

</section>

<section class="group">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>

</section>  
  <section class="group">
  <div class="item"></div>
  <div class="item"></div>
</section> 
  <section class="group">
</section>
like image 27
vijayP Avatar answered Oct 14 '22 04:10

vijayP


Please find the demo here. Code is as illustrated below:

HTML:

<section class="group">
    <span class="item"></span>
    <span class="item"></span>
    <span class="item"></span>

</section>

<section class="group">
    <span class="item"></span>
    <span class="item"></span>
    <span class="item"></span>
    <span class="item"></span>
    <span class="item"></span>

</section>
<section class="group">
    <span class="item"></span>
    <span class="item"></span>
</section>
<section class="group"></section>

JS:

$(function () {
    $('section.group').each(function (k, v) {
        var hig = 50;
        var total = ($(this).find('.item')).length;
        if (total != 0) {
            var equalHeight = (hig / total);
            $(this).find('span.item').css({
                'height': (equalHeight + 'px')
            });
        }
    });
});

CSS:

section.group {
  height: 50px;
  margin-bottom: 10px;
  overflow: hidden;
  border: 1px solid;
}

.item {
  display: block;
}

.item:nth-child(1) {
  background: #ff0000;
}

.item:nth-child(2) {
  background: #00ff00;
}

.item:nth-child(3) {
  background: #0000ff;
}

.item:nth-child(4) {
  background: #000;
}

.item:nth-child(5) {
  background: #f0f000;
}
like image 33
Shashank Avatar answered Oct 14 '22 04:10

Shashank