Custom fieldset Legend css

326 Views Asked by At

How can i recreate the classic behavior of a form with fieldset and legend

<form>
  <fieldset>
    <legend>
      Text
    </legend>
  </fieldset>
</form>

using a div and a label?

<div>
  <label>
    Text
  </label>
</div>

I need the label to have a transparent background.

How can I recreate it in a custom way? Thanks

1

There are 1 best solutions below

3
Chris Barr On

Not a transparent background, most elements already do that by default. It needs a white background and then just move it up to be on top of the line.

div {
  border: 1px solid #000;
  padding: 10px;
}

label {
  background: #FFF;
  padding: 5px;
  position: relative;
  top: -1rem;
}
<div>
  <label>
    Text
  </label>
</div>