Home > database >  How to center the content of (centered) pure css columns
How to center the content of (centered) pure css columns

Time:02-04

The snippet presents a series of columns containing a single letter. I'd like to have the center of the glyph in the center of each, but as you can see, they not quite centered -- appearing just off to the right. (I suspect by half the glyph width, but I'm not sure).

I've tried justify-content: center, margin: auto to no avail. Inspecting the pure, I don't see anything that should prevent what I'm aiming for. Can you help me fix it?

.centered-cols {
  justify-content: center;
}

.key {
  text-align: center;
  min-width: 1.9em;
  margin: 0.2em;
  border-radius: 0.4em;
  background-color: lightgrey;
}
<link href="https://unpkg.com/[email protected]/build/pure-min.css" rel="stylesheet" />

<div >
  <div id="q" ><p>A</p></div>
  <div id="w" ><p>B</p></div>
  <div id="e" ><p>C</p></div>
  <div id="r" ><p>D</p></div>
  <div id="t" ><p>E</p></div>
  <div id="y" ><p>F</p></div>
  <div id="u" ><p>G</p></div>
  <div id="i" ><p>H</p></div>
  <div id="o" ><p>I</p></div>
  <div id="p" ><p>J</p></div>
</div>

CodePudding user response:

Add letter-spacing: 0 in your .key css class:

.key {
  letter-spacing: 0;
  text-align: center;
  min-width: 1.9em;
  margin: 0.2em;
  border-radius: 0.4em;
  background-color: lightgrey;
}
<link href="https://unpkg.com/[email protected]/build/pure-min.css" rel="stylesheet" />

<div >
  <div id="q" >
    <p>A</p>
  </div>
  <div id="w" >
    <p>B</p>
  </div>
  <div id="e" >
    <p>C</p>
  </div>
  <div id="r" >
    <p>D</p>
  </div>
  <div id="t" >
    <p>E</p>
  </div>
  <div id="y" >
    <p>F</p>
  </div>
  <div id="u" >
    <p>G</p>
  </div>
  <div id="i" >
    <p>H</p>
  </div>
  <div id="o" >
    <p>I</p>
  </div>
  <div id="p" >
    <p>J</p>
  </div>
</div>

CodePudding user response:

Pure is creating this problem by setting a letter-spacing: -.31em in the pure-g CSS class.

Just set letter-spacing: normal !important; to overwrite it:

.centered-cols {
  justify-content: center;
  letter-spacing: normal !important;
}

.key {
  text-align: center;
  min-width: 2.9em;
  margin: 0.2em;
  border-radius: 0.4em;
  background-color: lightgrey;
}
<link href="https://unpkg.com/[email protected]/build/pure-min.css" rel="stylesheet" />

<div >
  <div id="q" ><p>A</p></div>
  <div id="w" ><p>B</p></div>
  <div id="e" ><p>C</p></div>
  <div id="r" ><p>D</p></div>
  <div id="t" ><p>E</p></div>
  <div id="y" ><p>F</p></div>
  <div id="u" ><p>G</p></div>
  <div id="i" ><p>H</p></div>
  <div id="o" ><p>I</p></div>
  <div id="p" ><p>J</p></div>
</div>

CodePudding user response:

just remove letter-spacing: -.31em; in pure-g class

CodePudding user response:

Use CSS grid if you want equal width item and you can easily center them:

.centered-cols {
  display:grid;
  width:max-content;
  margin:auto;
  grid-auto-flow:column;
  grid-auto-columns:1fr;
}

.key {
  text-align:center;
  padding:0 .8em;
  margin: 0.2em;
  border-radius: 0.4em;
  background-color: lightgrey;
}
<link href="https://unpkg.com/[email protected]/build/pure-min.css" rel="stylesheet" />

<div >
  <div id="q" ><p>A</p></div>
  <div id="w" ><p>B</p></div>
  <div id="e" ><p>C</p></div>
  <div id="r" ><p>D</p></div>
  <div id="t" ><p>E</p></div>
  <div id="y" ><p>F</p></div>
  <div id="u" ><p>G</p></div>
  <div id="i" ><p>H</p></div>
  <div id="o" ><p>I</p></div>
  <div id="p" ><p>J</p></div>
</div>

CodePudding user response:

Since we are already in the realms of flexbox, something like this could also work, without the need for !important

.centered-cols {
  justify-content: center;
}

.key {
  display: flex;
  justify-content: center;
  letter-spacing: normal;
  min-width: 1.9em;
  margin: 0.2em;
  border-radius: 0.4em;
  background-color: lightgrey;
}
<link href="https://unpkg.com/[email protected]/build/pure-min.css" rel="stylesheet" />

<div >
  <div id="q" ><p>A</p></div>
  <div id="w" ><p>B</p></div>
  <div id="e" ><p>C</p></div>
  <div id="r" ><p>D</p></div>
  <div id="t" ><p>E</p></div>
  <div id="y" ><p>F</p></div>
  <div id="u" ><p>G</p></div>
  <div id="i" ><p>H</p></div>
  <div id="o" ><p>I</p></div>
  <div id="p" ><p>J</p></div>
</div>

  •  Tags:  
  • Related