skip to Main Content

I have a sample code that is displaying a search query with css animation.

I would like to know, how can I hide the ‘title’ class when the search query is expanded, and show it again when the search query is minimized?

Here is a sample image of the two states (as you can see ‘title’ is viewable behind the search text, which I dont want):

enter image description here

I know that within the jquery I can hide it like so (showing it again I don’t know how the logic would go):

$("p").hide();

but how can I do it with just CSS?

The snippet is working, but its not displaying correctly, though I dont know why.

$('.opener').click(function() {
  $(this).parent('.search').toggleClass('inactive').find('input').focus();
});
.row {
  border: 1px solid red;
}

.title {
  font-size: 16px;
  font-weight: bold;
  position: absolute;
  margin: 6px 0;
}

.input-group {
  display: flex;
  height: 36px;
  width: 100%;
  overflow: hidden;
  padding: 0;
  /*background-color: rgba(100, 100, 100, .1);*/
  cursor: pointer;
  /*border-radius: 2px;*/
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
  /*box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);*/
}

.input-group:active,
.input-group:hover {
  /*background-color: rgba(100, 100, 100, .1);*/
}

.input-group.centered {
  margin: 0 auto;
}

.input-group .input-group-addon {
  justify-content: center;
  width: 36px;
  background: transparent;
  border: none;
  display: flex;
  text-align: center;
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
}

.input-group .input-group-addon i {
  height: 24px;
  width: 24px;
  align-self: center;
  color: #777;
}

.input-group input {
  font-family: "Roboto", sans-serif;
  background: transparent;
  border: none;
  box-shadow: none !important;
  font-size: 16px;
  padding: 0;
}

.inactive {
  width: 36px;
  border-radius: 18px;
  background: transparent;
  box-shadow: none;
}

.inactive .input-group-addon:not(:first-child) {
  opacity: 0;
}

.row {
  margin-top: 1em;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link href='https://fonts.googleapis.com/css?family=Roboto:400,300|Material+Icons' rel='stylesheet' type='text/css'>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<div class="container">
    <div class="row text-center">
        <h1>Search Inputs</h1>
    </div>
    <div class="row">
        <div class="title">Title</div>
        <div class="input-group search inactive pull-right">
            <span class="input-group-addon opener">
              <i class="material-icons">search</i>
            </span>
            <input type="text" class="form-control" placeholder="Search">
            <span class="input-group-addon opener">
              <i class="material-icons">clear</i>
            </span>
        </div>
    </div>
</div>

2

Answers


  1. You can use siblings to select the div.title.
    Add another class for with display:none property and toggle that class

    $("p").hide();
    $('.opener').click(function() {
      $(this).parent('.search').toggleClass('inactive').find('input').focus();
      $(this).parent('.search').siblings('div.title').toggleClass('hideTitle')
    });
    .row {
      border: 1px solid red;
    }
    
    .title {
      font-size: 16px;
      font-weight: bold;
      position: absolute;
      margin: 6px 0;
    }
    
    .input-group {
      display: flex;
      height: 36px;
      width: 100%;
      overflow: hidden;
      padding: 0;
      /*background-color: rgba(100, 100, 100, .1);*/
      cursor: pointer;
      /*border-radius: 2px;*/
      transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
      /*box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);*/
    }
    
    .input-group:active,
    .input-group:hover {
      /*background-color: rgba(100, 100, 100, .1);*/
    }
    
    .input-group.centered {
      margin: 0 auto;
    }
    
    .input-group .input-group-addon {
      justify-content: center;
      width: 36px;
      background: transparent;
      border: none;
      display: flex;
      text-align: center;
      transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    }
    
    .input-group .input-group-addon i {
      height: 24px;
      width: 24px;
      align-self: center;
      color: #777;
    }
    
    .input-group input {
      font-family: "Roboto", sans-serif;
      background: transparent;
      border: none;
      box-shadow: none !important;
      font-size: 16px;
      padding: 0;
    }
    
    .inactive {
      width: 36px;
      border-radius: 18px;
      background: transparent;
      box-shadow: none;
    }
    
    .inactive .input-group-addon:not(:first-child) {
      opacity: 0;
    }
    
    .row {
      margin-top: 1em;
    }
    .hideTitle{
     display:none;
    
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
    
    <link href='https://fonts.googleapis.com/css?family=Roboto:400,300|Material+Icons' rel='stylesheet' type='text/css'>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    
    <div class="container">
      <div class="row text-center">
        <h1>Search Inputs</h1>
      </div>
    
      <div class="row">
        <div class="title titleDiv">Title</div>
        <div class="input-group search inactive pull-right">
          <span class="input-group-addon opener">
    				<i class="material-icons">search</i>
    			</span>
          <input type="text" class="form-control" placeholder="Search">
    
          <span class="input-group-addon opener">
    				<i class="material-icons">clear</i>
    			</span>
        </div>
    
      </div>
    </div>
    Login or Signup to reply.
  2. Wrap label and input in one div with position relative and used siblings jquery to hide the title

    $('.opener').click(function() {
      $(this).parent('.search').toggleClass('inactive').find('input').focus();
      $(this).parent('.search').siblings('label').toggleClass('hide');
    });
    .row {
      border: 1px solid red;
    }
    .search-form {
      position:relative;
    }
    
    .search-form .title {
      font-size: 16px;
      font-weight: bold;
      position: absolute;
      margin: 6px 0;
    }
    
    .input-group {
      display: flex;
      height: 36px;
      width: 100%;
      overflow: hidden;
      padding: 0;
      /*background-color: rgba(100, 100, 100, .1);*/
      cursor: pointer;
      /*border-radius: 2px;*/
      transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
      /*box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24);*/
    }
    
    .input-group:active,
    .input-group:hover {
      /*background-color: rgba(100, 100, 100, .1);*/
    }
    
    .input-group.centered {
      margin: 0 auto;
    }
    
    .input-group .input-group-addon {
      justify-content: center;
      width: 36px;
      background: transparent;
      border: none;
      display: flex;
      text-align: center;
      transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    }
    
    .input-group .input-group-addon i {
      height: 24px;
      width: 24px;
      align-self: center;
      color: #777;
    }
    
    .input-group input {
      font-family: "Roboto", sans-serif;
      background: transparent;
      border: none;
      box-shadow: none !important;
      font-size: 16px;
      padding: 0;
    }
    
    .inactive {
      width: 36px;
      border-radius: 18px;
      background: transparent;
      box-shadow: none;
    }
    
    .inactive .input-group-addon:not(:first-child) {
      opacity: 0;
    }
    
    .row {
      margin-top: 1em;
    }
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
    <link href='https://fonts.googleapis.com/css?family=Roboto:400,300|Material+Icons' rel='stylesheet' type='text/css'>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <div class="container">
        <div class="row text-center">
            <h1>Search Inputs</h1>
        </div>
        <div class="row">
            <div class="search-form">
              <label class="title">Title</label>
              <div class="input-group search inactive pull-right">
                  <span class="input-group-addon opener">
                    <i class="material-icons">search</i>
                  </span>
                  <input type="text" class="form-control" placeholder="Search">
                  <span class="input-group-addon opener">
                    <i class="material-icons">clear</i>
                  </span>
              </div>
             </div>
        </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search