Home  >  Q&A  >  body text

"Unrendered margin-top is on fieldset's legend"

I have some HTML code equivalent to:

<div style="border: 1px solid black">
  <fieldset style="margin: 0">
    <legend style="margin-top: 20px;">testing....</legend>
  </fieldset>
</div>

My question is why the margin-top on the legend is not rendered? I want to solve this problem using only CSS. Thanks!

P粉659516906P粉659516906156 days ago3477

reply all(2)I'll reply

  • P粉521013123

    P粉5210131232024-04-05 12:44:25

    If you really want to do this, then set legend { float: left; }:

    legend {
      float: left;
    }
    <div style="border: 1px solid black">
      <fieldset style="margin: 0">
        <legend style="margin-top: 20px;">testing....</legend>
      </fieldset>
    </div>

    reply
    0
  • P粉593118425

    P粉5931184252024-04-05 11:17:11

    Try using padding instead. This will work.

    The reason why margin doesn't work is because the distance between the border and the text content is called padding instead of margin.

    <div style="border: 1px solid black">
      <fieldset style="margin: 0">
        <legend style="padding-top: 20px;">testing....</legend>
      </fieldset>
    </div>

    reply
    0
  • Cancelreply