Search code examples
htmlcsscss-grid

How to prevent grid columns from changing size as text increases (html/css)?


In the following piece of code (found here), the size of each column changes as I increase the length of any of the numbers. How do I prevent that from happening?

I've already tried messing around with the word-break and overflow properties (in the style portion of the code), but neither worked.

<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
  display: grid;
  grid-template-columns: auto auto auto auto;
  grid-gap: 10px;
  background-color: #2196F3;
  padding: 10px;
}

.grid-container > div {
  background-color: rgba(255, 255, 255, 0.8);
  border: 1px solid black;
  text-align: center;
  font-size: 30px;
}
</style>
</head>
<body>

<h1>Grid Container</h1>

<p>A Grid Container consists of grid items arranged in columns and rows</p>

<div class="grid-container">
  <div>1</div>
  <div>2</div>
  <div>3</div>  
  <div>444</div>
  <div>5</div>
  <div>6</div>
  <div>7</div>
  <div>8</div>
</div>

<p>Direct child elements(s) of the grid container automatically becomes grid items.</p>

</body>
</html>

Thanks in advance for helping me out.


Solution

  • If I got your problem right, you can define a fixed width in percents, like:

    grid-template-columns: 24% 24% 24% 24%;
    

    it will prevent columns from changes the width, but you will need to manage the content with the overflow property.