link1781 link1782 link1783 link1784 link1785 link1786 link1787 link1788 link1789 link1790 link1791 link1792 link1793 link1794 link1795 link1796 link1797 link1798 link1799 link1800 link1801 link1802 link1803 link1804 link1805 link1806 link1807 link1808 link1809 link1810 link1811 link1812 link1813 link1814 link1815 link1816 link1817 link1818 link1819 link1820 link1821 link1822 link1823 link1824 link1825 link1826 link1827 link1828 link1829 link1830 link1831 link1832 link1833 link1834 link1835 link1836 link1837 link1838 link1839 link1840 link1841 link1842 link1843 link1844 link1845 link1846 link1847 link1848 link1849 link1850 link1851 link1852 link1853 link1854 link1855 link1856 link1857 link1858 link1859 link1860 link1861 link1862 link1863 link1864 link1865 link1866 link1867 link1868 link1869 link1870 link1871 link1872 link1873 link1874 link1875 link1876 link1877 link1878 link1879 link1880 link1881 link1882 link1883 link1884 link1885 link1886 link1887 link1888 link1889 link1890 link1891 link1892 link1893 link1894 link1895 link1896 link1897 link1898 link1899 link1900 link1901 link1902 link1903 link1904 link1905 link1906 link1907 link1908 link1909 link1910 link1911 link1912 link1913 link1914 link1915 link1916 link1917

[Vue.js] CSS grid - count number of columns (javascript) Subscribe to RSS

there is a CSS grid

.wrapper {
display: grid;
grid-template-columns: repeat(auto-fill, 12.5vw );
grid-template-rows: repeat(auto-fill, 12.5vw );
background-color: black;
grid-auto-flow: row dense;
color: #444;

<div class=”wrapper”>
<div class=”box wide tall a”>
<div class=”box b”>
<div class=”box c”>
<div class=”box c”>
<div class=”box d”>
<div class=”box e”>
<div class=”box f”>
<div class=”box g”>
<div class=”box h”>

And I would like to know the number of columns in my javascript code. is it possible?

EDIT: the reason this is that if there is less than the good amount of items so that it makes a square, these extra items not to be displayed at all.

Thanks a lot!

Solution :

You can use getElementsByClassName to retrieve all the box divs and get the array length:

var columns = document.getElementsByClassName(‘box’).length

Solution 2:

I think you are missing the minmax part of the grid-template-columns:
eg grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));

with out it, you will always have 7 columns, and the grid columns wont be responsive and change as page width shrinks/expands.

can see Jen Simmons example page here : Spice Gallery Layout

A Solution that doesnt require any javascript.. but a few media queries..
is to wrap the .wrapper div with a container. and set a max height. per screen break point when every there would be un even images on the last row.
then set the .container overflow-y:hidden;

link to pen here : Hide last row of grid with css

Doing this with javascript you would need to know how many images you have, and what the minmax column widths are. vw units are basicaly width in %. so you would need to do some maths. with a mod function to know how many items are on the last row. and then need a way to hide them.