Search code examples
htmlcssformsyui-pure-css

How to combine grouped inputs with aligned forms?


From Pure CSS I want to combine aligned forms:

<link href="https://cdnjs.cloudflare.com/ajax/libs/pure/0.6.0/forms-min.css" rel="stylesheet" />
<form class="pure-form pure-form-aligned">
  <fieldset>
    <div class="pure-control-group">
      <label for="name">Username</label>
      <input id="name" type="text" placeholder="Username">
    </div>

    <div class="pure-control-group">
      <label for="password">Password</label>
      <input id="password" type="password" placeholder="Password">
    </div>

    <div class="pure-control-group">
      <label for="email">Email</label>
      <input id="email" type="email" placeholder="Email">
    </div>
  </fieldset>
</form>

with grouped inputs:

<link href="https://cdnjs.cloudflare.com/ajax/libs/pure/0.6.0/forms-min.css" rel="stylesheet" />
<form class="pure-form">
  <fieldset class="pure-group">
    <input type="text" placeholder="Username">
    <input type="text" placeholder="Password">
    <input type="email" placeholder="Email">
  </fieldset>
</form>

so that the inputs are stacked while still allowing horizontally aligned labels to be placed to the left. How can I achieve this effect?


Solution

  • I came to a solution by applying the grouped input styling to a modified aligned form layout:

    .pure-form-aligned .pure-group .pure-control-group {
      margin: 0;
    }
    .pure-form.pure-form-aligned .pure-group .pure-control-group input {
      display: inline-block;
      position: relative;
      margin: 0 0 -1px;
      border-radius: 0;
      top: -1px;
    }
    .pure-form-aligned .pure-group .pure-control-group:first-child input {
      top: 1px;
      border-radius: 4px 4px 0 0;
      margin: 0;
    }
    .pure-form-aligned .pure-group .pure-control-group:last-child input {
      top: -2px;
      border-radius: 0 0 4px 4px;
      margin: 0;
    }
    <link href="https://cdnjs.cloudflare.com/ajax/libs/pure/0.6.0/forms-min.css" rel="stylesheet" />
    <form class="pure-form pure-form-aligned">
      <fieldset class="pure-group">
        <div class="pure-control-group">
          <label for="name">Username</label>
          <input id="name" type="text" placeholder="Username">
        </div>
    
        <div class="pure-control-group">
          <label for="password">Password</label>
          <input id="password" type="password" placeholder="Password">
        </div>
    
        <div class="pure-control-group">
          <label for="email">Email</label>
          <input id="email" type="email" placeholder="Email">
        </div>
      </fieldset>
    </form>