skip to Main Content

In below code, I want "Search By" in 1 line, the 2 radio buttons in next line just below, "Search Text" & text Box in 3rd line and Submit button in last line. I’ve used flexbox. Currently everything is coming in 1 single line.

body {
  background: #ccc;
  font-family: 'Garamond', sans-serif;
  color: #000080;
}

h2 {
  width: 25%;
  text-align: center;
  margin: auto;
  padding: 15px;
}

.mytabs {
  display: flex;
  /*property of flex container to use flexbox*/
  flex-wrap: wrap;
  max-width: 1000px;
  margin: 25px auto;
}

.mytabs input[type="radio"] {
  display: none
}

.mytabs label {
  padding: 25px;
  font-weight: bold;
  background: #e2e2e2;
  font-size: 20px;
}

.mytabs .tab {
  width: 100%;
  background: #fff;
  order: 1;
  display: none;
}

.mytabs input[type='radio']:checked+label+.tab {
  display: block;
}

.mytabs input[type="radio"]:checked+label {
  background: #fff;
}

.form-group {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 40px 40px;
  font-size: 20px;
}

.form-group input[type="radio"] {
  display: block;
}

.form-group label {
  padding: 5px;
  margin: 5px;
  font-weight: normal;
  background: #fff;
  font-size: 20px;
}

input[type=text] {
  padding: 6px 8px;
  margin: 8px 0;
  box-sizing: border-box;
  border: 1px solid navy;
  border-radius: 4px;
}
<br><br>
<h2>Tool</h2>
<div class="mytabs">
  <input type="radio" id="tabfree" name="mytabs" checked="checked">
  <label for="tabfree">Tab1</label>
  <div class="tab">
    <form action="/submitu">
      <div class="form-group">
        <p>Search By :</p>
        <input type="radio" id="user" name="srchusr" value="UserID" checked/>
        <label for="user">Option1</label>
        <input type="radio" id="group" name="srchusr" value="Group" />
        <label for="group">Option2</label>
        <label for="srchtxt">Search Text :</label><br>
        <input type="text" id="srchtxt" name="srchtxt" value=""><br>
        <input type="submit" value="Submit">
      </div>
    </form>
  </div>
  <input type="radio" id="tabsilver" name="mytabs">
  <label for="tabsilver">Tab2</label>
  <div class="tab">
    <p>Tab2 Data</p>
  </div>
</div>

2

Answers


  1. Use flex-direction:column to have the elements vertically instead of horizontally, and wrap some elements in a div with display:flex, so that they are in 1 line.

    In the snippet below, i highlighted the changes I made to your code with "!!!" comments.

    body {
      background: #ccc;
      font-family: 'Garamond', sans-serif;
      color: #000080;
    }
    
    h2 {
      width: 25%;
      text-align: center;
      margin: auto;
      padding: 15px;
    }
    
    .mytabs {
      display: flex;
      /*property of flex container to use flexbox*/
      flex-wrap: wrap;
      max-width: 1000px;
      margin: 25px auto;
    }
    
    .mytabs input[type="radio"] {
      display: none
    }
    
    .mytabs label {
      padding: 25px;
      font-weight: bold;
      background: #e2e2e2;
      font-size: 20px;
    }
    
    .mytabs .tab {
      width: 100%;
      background: #fff;
      order: 1;
      display: none;
    }
    
    .mytabs input[type='radio']:checked+label+.tab {
      display: block;
    }
    
    .mytabs input[type="radio"]:checked+label {
      background: #fff;
    }
    
    .form-group {
      display: flex;
      align-items: center;
      justify-content: center;
      flex-direction: column; /* !!! */
      margin: 40px 40px;
      font-size: 20px;
    }
    
    .form-group input[type="radio"] {
      display: block;
    }
    
    .form-group label {
      padding: 5px;
      margin: 5px;
      font-weight: normal;
      background: #fff;
      font-size: 20px;
    }
    
    input[type=text] {
      padding: 6px 8px;
      margin: 8px 0;
      box-sizing: border-box;
      border: 1px solid navy;
      border-radius: 4px;
    }
    
    .flex-wrapper { /* !!! */
      display: flex; /* !!! */
    } /* !!! */
    <br><br>
    <h2>Tool</h2>
    <div class="mytabs">
      <input type="radio" id="tabfree" name="mytabs" checked="checked">
      <label for="tabfree">Tab1</label>
      <div class="tab">
        <form action="/submitu">
          <div class="form-group">
            <p>Search By :</p>
            <div class=flex-wrapper> <!-- !!! -->
              <input type="radio" id="user" name="srchusr" value="UserID" checked/>
              <label for="user">Option1</label>
              <input type="radio" id="group" name="srchusr" value="Group" />
              <label for="group">Option2</label>
            </div> <!-- !!! -->
            <div class=flex-wrapper> <!-- !!! -->
              <label for="srchtxt">Search Text :</label><br>
              <input type="text" id="srchtxt" name="srchtxt" value="">
              <br>
            </div> <!-- !!! -->
            <input type="submit" value="Submit">
          </div>
        </form>
      </div>
      <input type="radio" id="tabsilver" name="mytabs">
      <label for="tabsilver">Tab2</label>
      <div class="tab">
        <p>Tab2 Data</p>
      </div>
    </div>
    Login or Signup to reply.
  2. You can use grid. To structure a page, or a form, grid is really useful.

    body {
      background: #ccc;
      font-family: 'Garamond', sans-serif;
      color: #000080;
    }
    
    h2 {
      width: 25%;
      text-align: center;
      margin: auto;
      padding: 15px;
    }
    
    .mytabs {
      display: flex;
      /*property of flex container to use flexbox*/
      flex-wrap: wrap;
      max-width: 1000px;
      margin: 25px auto;
    }
    
    .mytabs input[type="radio"] {
      display: none
    }
    
    .mytabs label {
      padding: 25px;
      font-weight: bold;
      background: #e2e2e2;
      font-size: 20px;
    }
    
    .mytabs .tab {
      width: 100%;
      background: #fff;
      order: 1;
      display: none;
    }
    
    .mytabs input[type='radio']:checked+label+.tab {
      display: block;
    }
    
    .mytabs input[type="radio"]:checked+label {
      background: #fff;
    }
    
    .form-group {
      display: grid;
      grid-template-columns: 1fr 1fr;
      grid-template-rows: repeat(4, 1fr);
      gap: 10px;
      align-items: center;
      justify-content: center;
      margin: 40px 40px;
      font-size: 20px;
    }
    
    .form-group>div:nth-child(even) {
      text-align: right;
    }
    
    .form-group>div:nth-child(odd) {
      text-align: left;
    }
    
    .form-group>div:first-child,
    .form-group>div:last-child {
      grid-column: span 2;
      text-align: center;
    }
    
    .form-group input[type="radio"] {
      display: inline-block;
    }
    
    .form-group label {
      padding: 5px;
      margin: 5px;
      font-weight: normal;
      background: #fff;
      font-size: 20px;
    }
    
    input[type=text] {
      padding: 6px 8px;
      margin: 8px 0;
      box-sizing: border-box;
      border: 1px solid navy;
      border-radius: 4px;
    }
    <br><br>
    <h2>Tool</h2>
    <div class="mytabs">
      <input type="radio" id="tabfree" name="mytabs" checked="checked">
      <label for="tabfree">Tab1</label>
      <div class="tab">
        <form action="/submitu">
          <div class="form-group">
            <div>Search By :</div>
            <div>
              <input type="radio" id="user" name="srchusr" value="UserID" checked/>
              <label for="user">Option1</label>
            </div>
            <div>
              <input type="radio" id="group" name="srchusr" value="Group" />
              <label for="group">Option2</label>
            </div>
            <div>
              <label for="srchtxt">Search Text :</label>
            </div>
            <div>
              <input type="text" id="srchtxt" name="srchtxt" value="">
            </div>
            <div>
              <input type="submit" value="Submit">
            </div>
          </div>
        </form>
      </div>
      <input type="radio" id="tabsilver" name="mytabs">
      <label for="tabsilver">Tab2</label>
      <div class="tab">
        <p>Tab2 Data</p>
      </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search