Gibson Gibson - 7 months ago 17
HTML Question

Cross-browser legend centering

I'm getting different results on my legend centering across browsers:

Should be like this:

Chrome

But instead I get different margins on other browsers:

Firefox

HTML

<div class="teaser-header">
<fieldset class="teaser-fieldset">
<legend class="teaser-legend">We are a very passionate team</legend>
<h1>Who we are</h1>
</fieldset>
</div>


CSS

.teaser-header {
padding-top: 70px;
}
.teaser-fieldset {
border: 1px solid white;
color: white;
text-align: center;
width: 400px;
margin: auto;
}

.teaser-fieldset h1 {
color: white;
text-align: center;
margin: 0;
padding-bottom: 20px;
font-family: "Montserrat";
}

.teaser-legend {
padding: 0 10px;
width: 80%;
margin-left: auto;
margin-right: auto;
color: white;
font-size: 1em;
text-transform: uppercase;
margin-bottom: 0;
}


Any ideas? Thanks!

Answer

Setting the margin-left and margin-right explicitly as 10% seems to fix the centering issue. The 10% value is nothing but half of (100% - width).

.teaser-header {
  padding-top: 70px;
}
.teaser-fieldset {
  border: 1px solid white;
  color: white;
  text-align: center;
  width: 400px;
  margin: auto;
}
.teaser-fieldset h1 {
  color: white;
  text-align: center;
  margin: 0;
  padding-bottom: 20px;
  font-family: "Montserrat";
}
.teaser-legend {
  padding: 0 10px;
  width: 80%;
  margin-left: 10%;
  margin-right: 10%;
  color: white;
  font-size: 1em;
  text-transform: uppercase;
  margin-bottom: 0;
}
body {
  background: black;
}
<div class="teaser-header">
  <fieldset class="teaser-fieldset">
    <legend class="teaser-legend">We are a very passionate team</legend>
    <h1>Who we are</h1>
  </fieldset>
</div>