Utilize display grid to showcase just three columns on the screen

I am looking to only display 3 columns on my webpage and hide the rest of the content. I believe that adjusting the height of the container dynamically to fit just 3 columns may be the solution, but I am unsure how to achieve this or whether it is the correct approach. Any guidance on this matter would be greatly appreciated 🙏

Currently, I am utilizing scss for styling, however, I am open to examples in css as well.

.contentContainer {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  grid-gap: 5px;
  place-items: center;

  height: 330px; // I speculate this might need to be adjusted dynamically

  overflow: hidden;
  
  .imageContainer {
    width: 100%;
    height: 100%;
    
    img {
      width: 100%;
      height: 100%;
      aspect-ratio: 1/1;
      border-radius: 5px;
    }
  }
}

Edit: I have no issues incorporating JavaScript if needed.

Answer №1

Facing the XY Dilemma!

The challenge does not lie within the grid itself. To achieve the task of hiding all elements except for the first 3, utilize the :not(:nth-child(-n+3)) selector to conceal all elements inside the grid except for the initial 3:

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}

.container > div:not(:nth-child(-n+3)) {
  display: none;
}


/* for visualization purpose only */
.container > div {
  border: 2px dashed red;
  min-height: 30vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
<div class="container">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
  <div>5</div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ensure you have the right zoom in and out percentages to properly test how your website responds on various browsers across different

Currently, I am using Google Chrome to test the responsiveness of a web application that I am developing. As of today, my browser versions are as follows: Google Chrome - Version 88.0.4324.96 (Official Build) (64-bit) Mozilla Firefox - Version 84.0.2 (64- ...

Easiest method to globally disable form autocompletion in an application without manually setting autocomplete off for every individual form

Is there a quick way to disable autocomplete in all forms within the application without individually adding "autocomplete=off" to each form? ...

What is the best way to space out words in a navigation bar?

Hello, I'm new to programming and have a question: How can I increase the spacing between words in this navigation bar? ul{ list-style-type: none; margin: 0px; padding: 0px; width: 100%; overflow: hidde ...

Switching Column Content with jQuery on Mobile Devices

In my design, I have a dynamic two-column layout created using Twitter Bootstrap 3. The layout switches between image-text and text-image alignment for each row. The goal is to adjust it for smaller screens (less than 768px) so that rows with images on th ...

The height property in the CSS is causing the parent element to break and cannot be confined

Here is the HTML code that I am currently working with: <div id="div1"> <p id = "a"> <! -- content --> </p> <p id='b'> <! -- content --> </p> </div> Additionally, th ...

Support for numeric font weights in web browsers

Which browser versions have full support for using numeric values for the font-weight property? This includes Internet Explorer, Firefox, Chrome, Safari, iOS Safari, and Android Browser. While most browsers support the keyword values 'bold' and ...

Step by step guide on incorporating two background images in a single header

I'm looking to create a header with a background image that appears twice, like this: To achieve the effect, I added opacity of 0.5 to one of the images, but it is affecting everything in my header including text and logo. Is there a way to prevent t ...

Get rid of the "clear field" X button for IE11 on Windows 8

After trying out the suggestions provided in Remove IE10's "clear field" X button on certain inputs? .someinput::-ms-clear { display: none; } I successfully removed the X button on IE 11 running on Windows 7, but unfortunately it didn&a ...

Is there a glitch or a misinterpretation of the specifications concerning the use of relative padding

VIEW DEMO At times, I like to create a square (or any rectangle) that maintains its ratio regardless of size, using a technique similar to this method. My Objective: To prevent the square from extending beyond the viewport on devices with limited heigh ...

Arranging media elements in Bootstrap to interchange text with an image

Utilizing the Twitter Bootstrap example, I attempted to have the text of the media object wrap around an image. However, it seems that this feature is not functioning correctly, as illustrated in the image provided below. You can visit the site at Is the ...

"Upon clicking, toggle the addition or removal of the overflow hidden

I'm currently working on a function to add and remove the CSS property .css('overflow-y','hidden') using an onclick event, which I have successfully implemented. However, I am encountering an issue when attempting to remove this CS ...

How can I create spacing between squares in an HTML div element?

Currently, I am working on my project using Laravel 5. I retrieve some integer values from a database and use them to create square divs in HTML. Below is the current output of my work. https://i.stack.imgur.com/sy2ru.png You may notice that there is spa ...

Instructions on adjusting the height of a flexbox container to utilize the available space

I am facing a challenge in grasping the interaction between flexbox containers and other elements on a webpage. When I have only a flexbox on my page, everything works smoothly. However, when I introduce other elements, things start acting strangely. It se ...

The swiper slider loop malfunctions when the number of slides is less than the double-value

When using **6 slides** with slidesPerView: 3, everything works fine. However, if I use 5 slides and set slidesPerView: 3, the loop stops after the last slide. var swiper = new Swiper('.swiper-container', { direction: 'ver ...

Setting line height as a pixel value does not yield the desired result

$(element).css(options.css).attr(options.attr).addClass(options.class) //ensure line-height is correctly assigned if($(element).is('p') && _.isString(options.css['line-height'])){ console.log('line-height assigned: &apos ...

Ways to troubleshoot an unusual margin issue in a static column of an HTML table

I have been exploring various resources to create an HTML table with a fixed first column using CSS only. Despite my efforts, the fixed column is consistently appearing lower than the rest of the content, and I'm unsure why. Check out the code I&apos ...

Place the background image of the parent element relative to the child element

I am struggling to position the background image of <body> relative to one specific child <div>. <body> <div>content</div> <-- place background image of body relative to this div <div>other content</ ...

Blue outlined React Select dropdown with search functionality

When the dropdown is searchable, there seems to be a blue outline around the cursor: Link to image To remove the cursor, you can use this CSS: .Select-input > input { color: transparent; } Is there a way to also eliminate the blue outline on f ...

Create a div in HTML with a customized style to resemble a label with rounded edges

I'm struggling with CSS styling and trying to achieve a specific look for a div, as shown in the picture. Can someone provide guidance on how to achieve this style or share the necessary CSS code? I have come across similar buttons and div designs but ...

Choosing between JavaScript and Handlebars.js depends on the specific needs

Can anyone explain the advantages of utilizing Handlebars.js or similar libraries over solely relying on JavaScript? Thus far, I haven't come across any functionality in Handlebars that cannot be achieved with just pure JavaScript. ...