Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get number of columns created by css column-width [duplicate]

Possible Duplicate:
How to get css3 multi-column count in Javascript

I have a long dynamic text which is going to be split into columns by using CSS

div
{
    -moz-column-width: 500px;
    -moz-column-gap: 20px;
}

Is it possible to get how many columns are created? I'm using jQuery in the client side.

like image 706
Iswanto Arif Avatar asked Apr 23 '12 07:04

Iswanto Arif


People also ask

Is used to create number of columns in CSS?

The columns CSS shorthand property sets the number of columns to use when drawing an element's contents, as well as those columns' widths.

What is column-count CSS?

The column-count property in CSS is used to divide a portion of content inside any HTML element into a given number of columns.

How many columns will container have?

You can use a maximum of 12 columns in a Bootstrap grid system. Bootstrap's grid system uses a series of containers, rows, and columns to layout and align content. It's built with flexbox and is fully responsive.

What is column width in CSS?

The column-width property specifies the column width. The number of columns will be the minimum number of columns needed to show all the content across the element. column-width is a flexible property. Think of column-width as a minimum width suggestion for the browser.


1 Answers

Your questions and fiddle ignore vendor prefixes; larssin's answer is a good direction but isn't complete.

I created a small helper function for you, which goes over currently implemented prefixes for these values, fetches the actual values with .css() and returns the correct number. Works cross-browser, including non-supporting browsers which always return 1.

I chose to ignore the iframe case, which just makes things more complicated but as I understand isn't related directly to your question.

Update: Now accounts for border and padding.

Code on jsFiddle and here:

function getColumnsNumber(el){
    var $el = $(el),
        width = $el.innerWidth(),
        paddingLeft, paddingRight, columnWidth, columnGap, columnsNumber;

    paddingLeft = parseInt( $el.css('padding-left'), 10 );
    paddingRight = parseInt( $el.css('padding-right'), 10 );

    $.each(['-webkit-','-moz-',''], function(i, prefix){
        var _width = parseInt( $el.css(prefix+'column-width'), 10 );
        var _gap =   parseInt( $el.css(prefix+'column-gap'), 10 );
        if (!isNaN(_width)) columnWidth = _width;
        if (!isNaN(_gap))   columnGap = _gap;
    });

    columnsNumber = Math.floor( (width - paddingLeft - paddingRight) / (columnWidth + columnGap) );
    if (isNaN(columnsNumber) || columnsNumber < 1) columnsNumber = 1;
    return columnsNumber;
}
like image 142
4 revs Avatar answered Sep 21 '22 16:09

4 revs