0

I wish to style the <button> element using CSS grid.

I know the user agent stylesheet has some defaults, but I can't find the right one to override.

It should look like this - indeed, it does look like this if I style a div:

enter image description here

However styling a button looks like this:

enter image description here

Here's a demonstration using JSfiddle

Here's my CSS:

button, .button {
  display: grid;
  justify-content: left;
  align-items: center;
  grid-auto-flow: column;
  grid-template-columns: 80px 1fr;
}

And HTML:

How buttons look:

<button>
  <h1>
    hello
  </h1>
  <h2>
    world
  </h2>
</button>

How divs look, and how I want buttons to look:

<div class="button">
  <h1>
    hello
  </h1>
  <h2>
    world
  </h2>
</div>

My styles work on a div, but don't work on a button.

How can I style a button using CSS grid?

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
  • Seems to be some kind of bug https://stackoverflow.com/questions/51815477/is-a-button-allowed-to-have-displaygrid – Keith Aug 21 '19 at 14:58
  • From the duplicate: [The ` – Michael Benjamin Aug 21 '19 at 15:01
  • Acknowledged @Michael_B - a small note, there's no reference for the comment about display: grid not being designed to work on buttons, browsers just need to (and are) fixing it. Totally agree this is a dupe though. – mikemaccana Aug 21 '19 at 15:08
  • Actually Mike, the references are included in my answer. In the listed bug reports, you'll find: *"That is effectively what the HTML spec requires"* and *"Several HTML container-elements are special and effectively ignore their CSS display value"*. – Michael Benjamin Aug 21 '19 at 15:19
  • display can be applied to the children (so no grid nor flex). starting with a reset on margin,padding and box-sizing : https://jsfiddle.net/njx6721t/ go around the bug instead trying to fix it ;) It is usually a bad idea to try to restyle form elemts. they are supposed to be recognizable to avoid tricking the user .as part of the browser – G-Cyrillus Aug 21 '19 at 16:21

1 Answers1

1

It appears all you need is to add a font-size to overwrite browser defaults and to align your h1 and h2 left. I'd also recommend updating grid-template-columns: auto 1fr; This will allow the first element, in this case your h1 to be whatever size it needs to be.

<button>
  <div class="button">
    <h1>
      hello
    </h1>
    <h2>
      world
    </h2>
  </div>
</button>
button {
  margin: 12px;
  padding: 12px;
  font-family: "Helvetica";
  background-color: #999;
  width: 200px;
  border: 1px solid black;
  display: block;
  font-size: 1em;
}

.button {
  display: grid;
  justify-content: left;
  align-items: center;
  grid-template-columns: auto 1fr;
}

h1, h2 {
  text-align: left;
}

itsallgoodie
  • 418
  • 2
  • 7
  • 1
    This does not fix the issue: https://jsfiddle.net/oqn0x2wp/ – mikemaccana Aug 21 '19 at 15:10
  • Sorry, I was only looking in Firefox. Some browsers do weird things with buttons, one of those weird things is what Chrome is doing with yours. I've updated my answer to add `div.button` inside your `button` and resolved the issue. – itsallgoodie Aug 21 '19 at 15:16