当位置是绝对的时,div高度为0

时间:2018-05-08 03:15:42

标签: javascript html css

我不知道为什么第2行上面的第2行

我想在第1行之后添加第2行。我不想删除绝对位置,因为这与某些不同的网页有关。

Codepen Link of that Web page

 console.log('a', document.getElementById('a').getBoundingClientRect().height);
 console.log('b', document.getElementById('b').getBoundingClientRect().height);
 console.log('c', document.getElementById('c').getBoundingClientRect().height);
#a{
  position:realative;
  background:#ef0000;
}
#b{
  position:absolute;
}
#c{
  
}
<div id="a">
  <div id="b">
    <h2>Line 1 :</h2>
  </div>
  <div id="c">
    <h2>Line 2 :</h2>
  </div>
</div>

2 个答案:

答案 0 :(得分:0)

     <div id="a">
          <div id="b">
            <h2>Line 1 :</h2>
            <div id="c">
            <h2>Line 2 :</h2>
          </div>
          </div>

        </div>

    place div c inside b.

    Make id  'b' as ancestor to 'c'.so,since 'b'  is positioned absolute its child will be  positioned relative to 'b'. 

LINE1
LINE2

答案 1 :(得分:0)

此处为..您可以删除position: absolute并可以display使用fiddle

var current ;
(function(){
  const cTab = document.getElementById('tab-control').children;
  current = cTab[0].dataset.tab;
  for(var i =0;i<=cTab.length;i++){
    cTab[i].addEventListener('click',changeTab);
  }
})();
function changeTab(){
  var clickTab  = this.dataset.tab;
  document.querySelector('[data-tab='+current+']').classList.remove('active');
  document.querySelector('[data-tab='+clickTab+']').classList.add('active');
  var tab = document.getElementById(clickTab);
  document.getElementById(current).classList.remove('active');
  tab.classList.add('active');
  current =  clickTab;
}
body{
  background:#efefef;
  font-family:sans-serif;
  font-size:1.2em;
}
#tab{
  box-sizing:border-box;
  width:80%;
  margin:0 auto;
}
#tab ul#tab-control{
  border-bottom:1px solid #444;
  padding:0;
}
#tab #tab-control li{
  display:inline-flex;
  padding:10px 15px;
  cursor:pointer;
}
#tab #tab-control li.active{
  border:1px solid #444;
  border-bottom:0;
  border-top-left-radius:4px;
  border-top-right-radius:4px;
  position:relative;
}

#tab #tab-control li.active:after{
  position:absolute;
  content:'';
  bottom:-2px;
  left:0;
  width:100%;
  height:2px;
  background:#efefef;
}
#tab #tab-content{
  position:relative;
}

#tab #tab-content > *{
/*   display:none; */
  opacity:0;
  transition: opacity 0.4s ease-in; 
  display: none;
}
#tab #tab-content > .active {
  display: block;
}
#tab #tab-content .active{
/*   display:block; */
  opacity:1;
  z-index:2;
}
<div class="container">
  <div id="tab">
    <ul id="tab-control">
      <li data-tab="tab-1" class="active">Home</li>
      <li data-tab="tab-2">About</li>
      <li data-tab="tab-3">Contact</li>
    </ul>
    <div id="tab-content">
      <div id="tab-1" class="active">
        <h1>Tab 1</h1>
        <p>
          Raw denim you probably haven't heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache cliche tempor, williamsburg carles vegan helvetica. Reprehenderit butcher retro keffiyeh dreamcatcher synth. Cosby sweater eu banh mi, qui irure terry richardson ex squid. Aliquip placeat salvia cillum iphone. Seitan aliquip quis cardigan american apparel, butcher voluptate nisi qui.
        </p>
      </div>
      <div id="tab-2">
        <h1>Tab 2</h1>
        <p>
          Food truck fixie locavore, accusamus mcsweeney's marfa nulla single-origin coffee squid. Exercitation +1 labore velit, blog sartorial PBR leggings next level wes anderson artisan four loko farm-to-table craft beer twee. Qui photo booth letterpress, commodo enim craft beer mlkshk aliquip jean shorts ullamco ad vinyl cillum PBR. Homo nostrud organic, assumenda labore aesthetic magna delectus mollit. Keytar helvetica VHS salvia yr, vero magna velit sapiente labore stumptown. Vegan fanny pack odio cillum wes anderson 8-bit, sustainable jean shorts beard ut DIY ethical culpa terry richardson biodiesel. Art party scenester stumptown, tumblr butcher vero sint qui sapiente accusamus tattooed echo park.
        </p>
      </div>
      <div id="tab-3">
        <h1>Tab 3</h1>
        <p>
          Etsy mixtape wayfarers, ethical wes anderson tofu before they sold out mcsweeney's organic lomo retro fanny pack lo-fi farm-to-table readymade. Messenger bag gentrify pitchfork tattooed craft beer, iphone skateboard locavore carles etsy salvia banksy hoodie helvetica. DIY synth PBR banksy irony. Leggings gentrify squid 8-bit cred pitchfork. Williamsburg banh mi whatever gluten-free, carles pitchfork biodiesel fixie etsy retro mlkshk vice blog. Scenester cred you probably haven't heard of them, vinyl craft beer blog stumptown. Pitchfork sustainable tofu synth chambray yr.
        </p>
      </div>
    </div>
  </div>
  <div>
    <p>
      Something after Tab
    </p>
  </div>
</div>