Brian Bier Brian Bier - 3 months ago 15
Less Question

CSS: Can You positiion a legend inside a fieldset on the left border centered?

I am trying to take a legend and attach it to the left border. I want to display it center vertically. Is this Possible?
See the example below

Left border should look like this:

__________________________________
|
|
|
Some Text here
|
|
|
|__________________________________

Answer

You can absolutely position the legend, and then use a transition to correct slightly. The CSS looks like this (left padding added to the fieldset so the content doesn't overlap the legend):

CSS

fieldset {
  position: relative;
  padding-left: 50px;
}

legend {
  position: absolute;
  display: block;
  top: 50%;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
}

HTML

<form action="test.php" method="post">
  <fieldset>
    <legend>Title</legend>
    <input type="radio" id="radio"> <label for="radio">Click me</label>
    <div>
         <label for="name">Text Input:</label>
         <input type="text" name="name" id="name" value="" tabindex="1" />
    </div>
  </fieldset>
</form>