Tips for expanding text within a grid

While working on an auto-fill grid that moves elements to the next line when the width reaches 800px, I encountered an issue with my text inside the grid not stretching.

https://i.sstatic.net/OeJ3I.png

Below is my CSS code:

   ul {
       display: grid;
       grid-template-columns: repeat(auto-fill, minmax(60px, 1fr));
       grid-template-rows: auto;
       justify-content: center;
       align-items: center;
       text-align: center;
                
        li {
            margin: 0 10px;
            list-style-type: none;
            cursor: pointer;
            border: 1px solid black;
            border-radius: 10px;
             adding: 5px;
            }
         }

After making some updates:

I discovered that using auto-fit could partially solve my problem,

yet, I am interested in having only [New one] and [Test two] stretch to fit one line. This way, these buttons can expand to multiple lines if there are too many of them.

https://i.sstatic.net/E0Jqz.png

Answer №1

Swap out auto-fit with this alternative.

ul {
  display: grid;
  max-width: 800px;
  grid-template-columns: repeat(auto-fit, minmax(60px, 1fr));
  grid-template-rows: auto;
  justify-content: center;
  align-items: center;
  text-align: center;
  background-color: #aaa;
  padding: 0;
}

li {
  margin: 0 10px;
  list-style-type: none;
  cursor: pointer;
  border: 1px solid black;
  border-radius: 10px;
  padding: 5px;
}
<ul>
  <li>ALL</li>
  <li>New One</li>
  <li>777</li>
  <li>Test two</li>
  <li>Test</li>
</ul>

Alternatively, consider using float instead of grid.

ul {
  max-width: 800px;
  text-align: center;
  background-color: #aaa;
  padding: 0;
}

li {
  float: left;
  margin: 0 10px;
  list-style-type: none;
  cursor: pointer;
  border: 1px solid black;
  border-radius: 10px;
  padding: 5px;
  width: fit-content;
}
<ul>
  <li>ALL</li>
  <li>New One</li>
  <li>777</li>
  <li>Test two</li>
  <li>Test</li>
  <li>ALL</li>
  <li>New One</li>
  <li>777</li>
  <li>Test two</li>
  <li>Test</li>
  <li>ALL</li>
  <li>New One</li>
  <li>777</li>
  <li>Test two</li>
  <li>Test</li>
  <li>ALL</li>
  <li>New One</li>
  <li>777</li>
  <li>Test two</li>
  <li>Test</li>
</ul>

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

Positioning Arrows Adjacent to Definitions

I have a PHP snippet that adds arrow indicators next to previous and next sections on a page. This allows users to easily navigate between sections by clicking on the arrows. The desired output format is: (arrow) Previous Section Next Section ...

Placing an icon and a title on the same baseline

I'm working on a new website design and I really like the layout shown in this image: https://i.stack.imgur.com/QuVRZ.png I've been struggling to align the icon with the title in a responsive manner. Here are some of the approaches I've att ...

What is the best way to align navigation arrows vertically?

I am working on a modal album that displays images of various sizes. My issue is with styling the navigation arrows to appear in the center of the page, regardless of the image size. I have included my code below: <div class="row img-box"> <d ...

Slippery carousel: Showcase all items in center alignment mode

Currently, I am implementing Slick Carousel with centerMode. Is there a method to ensure that all items are displayed without being cut off on the screen? The setup I have is quite simple: $('.your-class').slick({ centerMode: true, slidesTo ...

Is Webkit's Overflow Scrolling feature hiding the divs on your website?

I recently implemented a design for my website where the content is contained within an absolute positioned div that takes up the entire screen. However, I noticed that the scrolling on this div felt clunky and not as smooth as I would like it to be. After ...

I have implemented an email validation form in Angular, however, if the validation is not properly handled, the data will still be stored. How

When I enter an email address, for example: if I enter "abc" it shows an alert saying "please enter a valid email". If I leave it blank and try to submit, it shows an alert saying "email required". But when I click register, the data is saved regardless of ...

What techniques can I use to merge alpha channels with compositing in images?

Utilizing an HTML5 Canvas along with the KineticJS(KonvaJS) canvas library, I have successfully incorporated an Image element. My current objective is to erase specific pixels while maintaining a certain level of transparency. The red dot erases pixels at ...

How to perfectly center a dropdown menu using CSS across different web browsers

I am dealing with a dropdown menu that is auto-generated by WordPress, so altering the HTML structure is not an option. However, I have the flexibility to adjust the CSS styling to correct an alignment issue in my dropdown menu. Currently, I'm facing ...

I am facing an issue where my simple three.js code is not displaying properly

I'm relatively new to three.js and HTML, but I have experience creating other JS files that import into HTML pages without any issues. However, I am confused as to why my basic box code is not showing up on the page when I import the JS file in the sa ...

Children can easily access multiple items within a list by using the ul tag

I am struggling with changing the class of ul inside the ul.navigator. I want to change it only when I click on a particular li, but my current approach doesn't seem to be working. Can anyone provide some guidance or assistance? $('.navigator& ...

Awesome C# PDF Printing Solution [closed]

This question does not comply with our Q&A standards and guidelines. To maintain the integrity of our platform, we require answers to be factual, supported by references or expertise. However, this question may provoke debates, arguments, polls, or pro ...

Adjust the background color of the dropdown when toggled

I want to add a background color to my dropdown menu when it's activated. In the demo, the dropdown content overlaps with the text behind it. Currently, I have a scrollspy feature that applies a background color to the nav bar and dropdown menu when s ...

Show two <p>'s next to each other

I am trying to arrange 2 paragraphs side by side. <p class = "firstClass">This is the first paragraph.</p> <p class = "secondClass">This is the second paragraph.</p> Result: This is the first paragraph. This is the second paragra ...

Tips for opening local HTML files on iPhones and Android devices

Is there a way to access local files on my PC from my iPhone and Android devices without having to upload them to an FTP server each time? I currently use Windows 7 with an iPhone 4 and an Android device. When I need to view a file on my iPhone's brow ...

Tips for extracting an XML value from an API

I'm attempting to showcase a value retrieved from an API (specifically used by PRTG software to extract information). Here is the link to the API: The API provides an XML file structured like this: <?xml version="1.0" encoding="UTF-8"?> <ch ...

Issue with continuous loader malfunction

I integrated a 3-second mini-loading animation on my website. It shows up every time I refresh the site or navigate to another page. The issue I'm facing is that once I add the loading section, it never stops (it should only last for 3 seconds). Usua ...

Can someone explain why the min-width property is not being respected by the <input> element?

Visit this link for the code The text field in the provided link should only be 10px wide but it is currently displaying a width of 152px. Here is the code snippet: .input { width: 100%; box-sizing: border-box; } .cont { padding: 2px; } .main ...

AngularJS is used to vertically collapse three different div elements. This feature

I am facing an issue with the design of my page that contains three panels. Two of these panels have tables, and when viewing the page on a smaller display, it disrupts the layout. I am wondering if there is a way to add collapsible buttons to the panels s ...

When working with NodeJS and an HTML form, I encountered an issue where the 'Endpoint'

Having trouble sending input data from a form to my nodejs endpoint. When I try printing the req.body, it shows up as undefined and I can't figure out why. Here is the relevant API code snippet: var bodyParser = require('body-parser') var e ...

Passing references using a personalized component

So, I've encountered this issue with my code: import MuiDialog from "@mui/material/Dialog"; import { styled } from "@mui/material/styles"; const TheDialog = styled((DialogProps) => ( <MuiDialog {...DialogProps} /> ))(( ...