I want to add a block with pictures to the page. This block should occupy the entire height and width of the page. The number of pictures in the block is unknown in advance. Pictures are always all the same size and should be aligned relative to each other vertically and horizontally. Pictures should not go beyond the block. Is it possible to implement this without using js, only with CSS? If so, please kick in the right direction how to do it correctly.
3 answers
This is done by writing the css for each case using the number of requests method. Only if the number of elements is within the limits of reasonable value.
Parent unit set display: flex; flex-wrap: wrap;
display: flex; flex-wrap: wrap;
. In this way, it will contain all the elements, narrowing them. To place them in several rows - you need flex-wrap
, and for elements, respectively, size and margin
. But it may be that some one element simply will not fit on the right, so it is better to set flex-grow: 1;
pictures flex-grow: 1;
that will make them all stretch to the required size. Also, for everyone, add max-width
, but this is optional.
To scale images make size in percent and height: auto; max-height: ΡΠ°Π·ΠΌΠ΅Ρ;
height: auto; max-height: ΡΠ°Π·ΠΌΠ΅Ρ;
.
- I also tried to do C flex, but the problem is that if the number of elements starts to increase, then they will come out of the screen and a scroll will appear. those. Not all elements fit in the screen. codepen.io/anon/pen/vbqZKJ Is it possible to somehow restrict them to decrease, but continue to fit in only one screen? - se0ga
- @ se0ga Use js? - Vitaliy Shebanits February
- It was interesting to do without using js just. Made by option @ 4e4e4i. The user has thrown off a very interesting article. - se0ga 8:08 pm
A variant that uses CSS Grid Layout and Jquery.
In the snippet code comments are used, I think it will be clear.
function CreatGrid() { let parent = $('.content'), // Π‘Π°ΠΌ ΡΠΎΠ΄ΠΈΡΠ΅Π΅Π»Ρ child = parent.find('.item'), // ΠΠ»Π΅ΠΌΠ΅Π½ΡΡ "ΡΠ΅ΡΠΊΠΈ" lenChild = child.length, // ΠΠΎΠ»-Π²ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² rowLen = 3; // ΠΠ°ΡΠ°Π»ΡΠ½ΠΎΠ΅\ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»-Π²ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² ΠΏΠΎ Π³ΠΎΡΠΈΠ·ΠΎΠ½ΡΠ°Π»ΠΈ if(lenChild > 0 && lenChild <= 10) rowLen = 3; else if(lenChild > 10 && lenChild <= 15) rowLen = 4; else if(lenChild > 15 && lenChild <= 20) rowLen = 5; else if(lenChild > 20 && lenChild <= 25) rowLen = 6; else if(lenChild > 25 && lenChild <= 50) rowLen = 7; else if(lenChild > 50 && lenChild <= 75) rowLen = 8; else if(lenChild > 75 && lenChild <= 100) rowLen = 9; else if(lenChild > 100) rowLen = 10; // ΠΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»-Π²ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² ΠΏΠΎ Π³ΠΎΡΠΈΠ·ΠΎΠ½ΡΠ°Π»ΠΈ parent.css('grid-template-columns', 'repeat('+rowLen+', 1fr)'); // ΠΡΠΎ ΠΊΠΎΠ»-Π²ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² ΠΏΠΎ Π³ΠΎΡΠΈΠ·ΠΎΠ½ΡΠ°Π»ΠΈ, ΠΏΡΠΈ ΠΏΠΎΠΌΠΎΡΠΈ grid-block child.height(child.width()); // ΠΠ°ΡΠΌ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ°ΠΌ ΡΠ°ΠΊΡΡ ΠΆΠ΅ Π²ΡΡΠΎΡΠΊΡ ΠΊΠ°ΠΊ ΠΈ ΡΠΈΡΠΈΠ½Ρ } CreatGrid(); // ΠΠ°ΠΏΡΡΠΊΠ°Π΅ΠΌ ΠΏΡΠΈ ΠΎΡΡΠΈΡΠΎΠ²ΠΊΠ΅ ΡΡΡΠ°Π½ΠΈΡΡ // Π‘ΠΎΠ²Π΅ΡΡΡ Π΅Π³ΠΎ Π·Π°ΡΡΠ½ΡΡΡ Π΅ΡΡ ΠΈ Π² $(window).resize(); // ΠΡΡ ΡΡΠΎ Π½ΠΈΠΆΠ΅ ΠΏΡΠΎΡΡΠΎ Π΄Π»Ρ Π²ΠΈΠ·ΡΠ°Π»ΠΈΠ·Π°ΡΠΈΠΈ $('.button').on('click', 'input', function(){ console.clear(); if($(this).hasClass('add')) { $('.content').append('<div class="item"></div>'); console.info('elem .item +1'); } else if($(this).hasClass('rmv')) { $('.content .item:last-child').remove(); console.info('last elem .item remove'); } else if($(this).hasClass('rnm')) { let len = Math.floor(Math.random() * 100); $('.content').html(''); for(let i=0; i<len; i++) $('.content').append('<div class="item"></div>'); console.info('creat elem .item '+(len+1)); } else if($(this).hasClass('rma')) { $('.content').html(''); console.info('all elem .item'); } CreatGrid(); // $('.content').find('.item').each(function(){ let arr = ['https://image.flaticon.com/icons/svg/1146/1146298.svg','https://image.flaticon.com/icons/svg/1146/1146338.svg','https://image.flaticon.com/icons/svg/1146/1146322.svg','https://image.flaticon.com/icons/svg/1146/1146314.svg','https://image.flaticon.com/icons/svg/1146/1146320.svg','https://image.flaticon.com/icons/svg/1146/1146329.svg','https://image.flaticon.com/icons/svg/1146/1146325.svg','https://image.flaticon.com/icons/svg/1146/1146339.svg','https://image.flaticon.com/icons/svg/1146/1146342.svg','https://image.flaticon.com/icons/svg/1146/1146343.svg','https://image.flaticon.com/icons/svg/1146/1146309.svg'], rnm = Math.floor(Math.random() * 11); $(this).css('background-image', 'url("'+arr[rnm]+'")'); }); });
@import url('https://use.fontawesome.com/releases/v5.7.2/css/all.css'); .button {margin-bottom: 10px;} .button .add {color: green;} .button .rmv {color: red;} .content { display: grid; grid-template-columns: repeat(3, 1fr); grid-gap: 10px; min-height: 100px; background: #999; border-radius: 5px; padding: 10px; box-sizing: border-box; } .content .item { display: block; border-radius: 5px; max-width: 80px; max-height: 80px; background: #ddd no-repeat center center / 90%; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <!-- --> <div class="button"> <input class="add" type="button" value="add"> <input class="rmv" type="button" value="remove"> <input class="rnm" type="button" value="random"> <input class="rma" type="button" value="remove all"> </div> <div class="content"> </div>
On pure CSS, it is unlikely to be able to do this simply (that is, using the minimum amount of code), as an option to pervert on the proposal @ 4e4e4i