-3

is it possible to get the number of child elements in css? For example I want the last element to be 100% - (number of childs * 50px)

If there's no possibility in CSS3, can I do it with LESS or do I need JS

John Smith
  • 615
  • 4
  • 15
  • 1
    You need to use js in order to do that. – Nicolae Olariu Jan 30 '14 at 12:24
  • are you talking about `height` or `width` ? (*and what style do you want to apply because it might lead to different solutions*) – Gabriele Petrioli Jan 30 '14 at 12:26
  • 1
    I have a table with a total width of 1000px. Every header should be 50px wide and the last one should fill the rest. If I have 1 header I want the header to be 1000px wide cause it's the last one. If I have 2 I want the first one to be 50px and the last one 950 px and so on – John Smith Jan 30 '14 at 12:28
  • You mean an actual `table` tag ? – Gabriele Petrioli Jan 30 '14 at 12:37
  • I talked about a selfmade table with div's to get a static header and vertical scrolling for headers, the JS way was helpful, I'll also try out the auto width on the last element, thx for the help – John Smith Jan 30 '14 at 15:48

4 Answers4

3

Update answer after comment clarifying its a table

You could use an actual table tag and make the last header th have width:auto while the rest have a fixed width. This is the normal way tables work and it will make it expand to fill the rest of the table..

If you have to use other tags, you could simulate a table by using the display:table, display:table-row and display:table-cell values..

<div class="table">
    <div class="headers">
        <div>1</div>
        <div>2</div>
        <div>3</div>
        <div>4</div>
        <div>5</div>
        <div>6</div>
        <div>7</div>
        <div>8</div>
    </div>
</div>

with css

.table{
    display:table;
    width:100%;
}
.headers{
    display:table-row;
}
.headers div{
    width:50px;
    display:table-cell;
}
.headers div:last-child{
    width:auto;
}

Demo at http://jsfiddle.net/gaby/KSbXM/


Original Answer

You could use the :last-child selector..

For example

<div class="container">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>

and

.container div{
   height:50px;
}

.container div:last-child{
   height:100%;
}

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
2

Unfortunately, there is no way in both CSS or LESS to do that. You can go with a JavaScript solution, which is fairly simple to implement.

Rough idea:

var allChildren = document.querySelectorAll('#myContainer > *');
var count = allChildren.length;

allChildren[count-1].style.width = 'calc(100% - '+ count * 50 +'px)';

Your specific problem (always better to post a specific problem!) can be solved easily with a HTML table:

HTML:

  <table>
    <tr>
      <td>1</td>
      <td>2</td>      
      <td>3</td>
      <td>last</td>
    </tr>
  </table>

CSS:

table {
  width:300px;
  border:1px solid red;
}

td {
  width:50px; 
  border: 1px solid blue;
}

td:last-child {
  width:auto;
}

DEMO: http://jsbin.com/eTeBudiR/1/edit

If the data you want to display is not tabular in nature and you just want the layout to look like that, you can use CSS (display:table, display:table-cell) to achieve the same result.

HTML:

  <div id="myContainer">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>last</div>
  </div>

CSS:

#myContainer {
  display:table;
  width:300px;
  border:1px solid red;
}

#myContainer > div {
  display:table-cell;
  border:1px solid blue;
  width:50px;
}

#myContainer > div:last-child {
  width:auto;
}

DEMO: http://jsbin.com/eTeBudiR/2/edit

Tibos
  • 27,507
  • 4
  • 50
  • 64
  • Better do not mix javascript and css in this case. – Alex Jan 30 '14 at 12:31
  • @Pinal is there a reason for that? Other than browser support for the calc function, i see no problem with the solution. A JS-only solution that would be responsive is not that elegant and a CSS-only solution doesn't exist as far as i know. – Tibos Jan 30 '14 at 12:34
  • 1
    I just want to say that `'calc(100% - '+ count * 50 +'px)'` make the same, that javascript do by `myContainer.offsetHeight - count*50`, but more slower. – Alex Jan 30 '14 at 12:40
  • Yes, but it is not responsive. Whenever i see percentage values i feel that the author designed that element to be responsive and as i said, a JS responsive solution would be messier to implement. – Tibos Jan 30 '14 at 12:47
2

You do need JavaScript to get the number of elements or any other information about the displayed html. There is currently no way to do this with css and due its design there might never be .

last:child is an option, as others mentioned. You could somehow combine it with setting a relation to the parent-height (or width?). But there would be more information needed.

EDIT:

table {
   width: 1000px; /* or whatever you want it to be */
}
td{
   width: 50px;
}
td:last-child{
  width: auto;
}

http://codepen.io/johannesjo/pen/KLcrA

hugo der hungrige
  • 12,382
  • 9
  • 57
  • 84
0

You may try something from this.

.t1{
    counter-increment : test 1;
    background-color:red;

}
.t1:after{    
    content : counter(test);
    width : calc(counter(test)*50px);
}

working demo here

Mithlesh Kumar
  • 748
  • 7
  • 16