Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="w-100 bg-dark d-flex justify-content-between align-items-center p-2">
  <button class="btn bg-transparent text-white"><i class="fa fa-bars" aria-hidden="true"></i></button>
  <h1 class="text-white">LOGO</h1>
  <div id="search-wrap" class="d-flex search-wrap position-relative">
    <form action="#" id="form" class="position-absolute d-flex form h-100">
      <button id="search-btn" class="btn search-btn bg-transparent border-0 text-white"><i class="fa fa-search" aria-hidden="true"></i></button>
      <input placeholder="Search..." id="search-text" class="search-text bg-transparent">
      <span id="text-under-line" class="text-under-line position-absolute"></span>
    </form>
    <button id="close-btn" class="btn btn-danger close-btn bg-transparent border-0"></button>
  </div>
</div>
              
            
!

CSS

              
                button {
  cursor: pointer;
}

input,
button {
  outline: none;
}

.form {
  overflow: hidden;//.formの領域からはみ出た部分を隠す
  top: 0;
  right: 50%; //虫眼鏡が現在地からそのまま移動するように調節
  //visibility: hidden;
  transition: all 0.1s linear;
  width: 0;
  &.is-open {
    //width:auto;
    //visibility:visible;
  }
}

.close-btn {
  z-index: 2;
}

.search-text {
  border: none;
  border-bottom: 1px solid white;
  //width: 100%; //absoluteで左右ぴったりに配置するために100%にしている※なくても正常に動作した
  //100%にしないとラインが引かれない
  &:focus ~ .text-under-line {//フォーカスされるとラインが広がるようにする
    transform: scaleX(1);
  }
}

.text-under-line {
  border-bottom: 2px solid white;
  height: 2px;
  bottom: 0;
  transition: all 0.1s linear;
  transform: scaleX(0);//始めはラインを隠す
}

/*
.ov-v {
  overflow: visible;
}

.ov-h {
  overflow: hidden;
}
*/

              
            
!

JS

              
                (function() {
  const searchBtn = document.getElementById("search-btn");
  const searchText = document.getElementById("search-text");
  const closeBtn = document.getElementById("close-btn");
  const form = document.getElementById("form");
  const textUnderLine = document.getElementById("text-under-line");

  closeBtn.innerHTML = '<i class="fa fa-search" aria-hidden="true"></i>';

  function getPropertyValue(elem, property) {
    //ブラウザが示す要素の厳密なサイズを取得
    const computedStyle =
      elem.currentStyle || document.defaultView.getComputedStyle(elem, ""); //currentStyleはIE,getComputedStyleはそれ以外
    const propertyValue = computedStyle.getPropertyValue(property);
    return propertyValue;
  }

  closeBtn.addEventListener(
    "click",
    e => {
      e.preventDefault();
      form.classList.toggle("is-open");
      if (form.classList.contains("is-open")) {
        closeBtn.innerHTML = '<i class="fa fa-times" aria-hidden="true"></i>';
        searchText.style.width = "auto";//#search-textのブラウザが示す小数点を含む幅を取得するためにwidthをautoにする
        //searchText.style.width = searchText.offsetWidth + 'px';
        const searchTextWidth = getPropertyValue(searchText, "width");//#search-textのブラウザが示す小数点を含む幅を取得する
        const searchBtnWidth = getPropertyValue(searchBtn, "width");//#search-btnのブラウザが示す小数点を含む幅を取得する
        //console.log(parseFloat(searchTextWidth) + parseFloat(searchBtnWidth));
        form.style.width =
          parseFloat(searchTextWidth) + parseFloat(searchBtnWidth) + "px";//#search-textの幅+#search-btnの幅を#formの幅にしてtransitionが効くようにする。width:auto;では効かないためこのようなことをしている。
        textUnderLine.style.width = searchTextWidth;//#text-under-lineの幅を#search-textの幅にする
        //leftとrightで左右ぴったりに配置
        textUnderLine.style.left = searchText.getBoundingClientRect().left - searchBtn.getBoundingClientRect().left + "px"; //#search-btnの座標から数えた#search-textの相対座標※追記:rightのみでも良かった
        textUnderLine.style.right = 0 + "px";
        form.style.right = 0; //#formの右端を×アイコンの右端に揃える
        searchText.focus();//自動でフォーカスする
      } else {
        closeBtn.innerHTML = '<i class="fa fa-search" aria-hidden="true"></i>';
        searchText.style.width = ""; //position:absolute;で配置しているため、閉じる直前に指定しないとぴったりと収まっていないため、吸収場所がボタン領域内ではなく画面外となってしまう。
        textUnderLine.style.width = "";
        textUnderLine.style.left = "";//追記:書かなくても動作した
        textUnderLine.style.right = "";//追記:書かなくても動作した
        form.style.width = "";
        form.style.right = "";
        searchText.blur();//フォーカスを解除する
      }
    },
    false
  );
})();

              
            
!
999px

Console