【问题标题】:What is creating the gap between these two html elements?是什么造成了这两个 html 元素之间的差距?
【发布时间】:2017-02-08 23:47:13
【问题描述】:

在添加标签的 JavaScript 之前,li 很好地排列在下面的 div 之上,没有间隙。据我所知,现在它们之间存在的差距在添加 JavaScript 时出现了。检查dev site(在本例中为h1)选项卡下方的第一个文本项显示用户代理样式表已应用-webkit-margin-before: 0.83em。将margin: 0 !important; 放在h1 上没有任何作用。

编辑:我不知道这是否会对任何人有用,但整个问题是因为我更改了 jekyll 使用的 _config.yml 文件,之后没有停止并重新启动服务器,所以它没有更新。事实上,另一个错误是我从编译到 _site 文件夹的目录中排除了 sass 文件夹,这导致 sass 中的更改不会反映在 jekyll 虚拟服务器上。

function openContent (evt, contentType) {
  var i, tabcontent, tabheads;
  tabcontent =
document.getElementsByClassName('tabcontent');
  for (i = 0; i < tabcontent.length; i++) {
      tabcontent[i].style.display = 'none';
  }
  tabheads =
document.getElementsByClassName('tabheads');
  for (i = 0; i < tabheads.length; i++) {
      tabheads[i].className =
      tabheads[i].className.replace(' active', '')
  }
  document.getElementById(contentType).style.display =
'block';
  evt.currentTarget.className += ' active';
}
document.getElementById('defaultOpen').click();
.outerDiv {
  background-color: rgba(50, 50, 0, 0.6);
  border-radius: 2rem; }

.innerDiv {
  background-color: rgba(10, 10, 10, 0.6);
  border-radius: 1.5rem;
  display: flex;
  flex-flow: column nowrap;
  align-items: flex-end;
  margin: 15px; }
  .innerDiv p {
    padding: 0.5rem 1.2rem; }

ul.tabs {
  display: flex;
  align-items: center;
  margin: 1rem 0 0.3rem 0;
  list-style-type: none;
  height: 2rem; }

.tabs li a {
  padding: 0.5rem 1.2rem 0.5rem 1.2rem;
  background-color: rgba(15, 10, 0, 0.4);
  color: #96912d;
  border-radius: 1.2rem 1.2rem 0 0;
  margin: 0.3rem 0.3rem 0.5rem 0.3rem;
  letter-spacing: 2px; }
  .tabs li a:hover {
    background-color: black;
    color: yellow; }
  .tabs li a:focus, .tabs li a .active {
    background-color: rgba(10, 10, 10, 0.6);
    color: #fac819; }

.under-tabs {
  margin-top: 0; }

.tabcontent {
  display: none; }
<div class="outerDiv">
 <ul class="tabs">
    <li><a href="javascript:void(0)" class="tabheads"
         onclick="openContent(event,'intro')"
         id="defaultOpen">Intro</a></li>
     <li><a href="javascript:void(0)" class="tabheads" onclick="openContent(event,'blog')">Blog</a></li>
     <li><a href="javascript:void(0)" class="tabheads"
         onclick="openContent(event,'comments')">Comments</a></li>
 </ul>
 <div class="innerDiv under-tabs tabcontent"
     id="intro">
	
	<p>
	This project is building a series of virtual colonies on the Moon. 
	</p>
 </div>
</div>

【问题讨论】:

  • "把边距:0 !important; 在 h1 上什么都不做。" - 它在你的开发网站上对我有用。您的示例中的差距是由&lt;ul&gt;&lt;p&gt; 上的边距引起的
  • @Turnip 也许它没有从 sass 正确编译...我已经尝试将它放入其中一段时间​​了。
  • @Turnip gahd - 似乎这一切都归结为 _config.yml 文件更新,之后没有重启 Jekyll 服务器...感谢您的检查。

标签: javascript html css sass jekyll


【解决方案1】:

如评论所述,您正在处理您的 sn-p 中的默认边距 &lt;p&gt;

请参阅https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/Mastering_margin_collapsing 以了解它如何干扰您的布局、它是关于什么以及当它发生时如何处理它。

这里的基本示例是将 p 的 margin-top 重置为零以消除间隙

function openContent (evt, contentType) {
  var i, tabcontent, tabheads;
  tabcontent =
document.getElementsByClassName('tabcontent');
  for (i = 0; i < tabcontent.length; i++) {
      tabcontent[i].style.display = 'none';
  }
  tabheads =
document.getElementsByClassName('tabheads');
  for (i = 0; i < tabheads.length; i++) {
      tabheads[i].className =
      tabheads[i].className.replace(' active', '')
  }
  document.getElementById(contentType).style.display =
'block';
  evt.currentTarget.className += ' active';
}
document.getElementById('defaultOpen').click();
.outerDiv {
  background-color: rgba(50, 50, 0, 0.6);
  border-radius: 2rem; }

.innerDiv {
  background-color: rgba(10, 10, 10, 0.6);
  border-radius: 1.5rem;
  display: flex;
  flex-flow: column nowrap;
  align-items: flex-end;
  margin: 15px; }
  .innerDiv p {
    padding: 0.5rem 1.2rem; }

ul.tabs {
  display: flex;
  align-items: center;
  margin: 1rem 0 0 0;
  list-style-type: none;
  height: 2rem; }

.tabs li a {
  padding: 0.5rem 1.2rem 0.4em 1.2rem;
  background-color: rgba(15, 10, 0, 0.4);
  color: #96912d;
  border-radius: 1.2rem 1.2rem 0 0;
  margin: 0.3rem 0.3rem 0.5rem 0.3rem;
  letter-spacing: 2px; }
  .tabs li a:hover {
    background-color: black;
    color: yellow; }
  .tabs li a:focus, .tabs li a .active {
    background-color: rgba(10, 10, 10, 0.6);
    color: #fac819; }

.under-tabs {
  margin-top: 0; }

.tabcontent {
  display: none; }
p {margin-top:0;
<div class="outerDiv">
 <ul class="tabs">
    <li><a href="javascript:void(0)" class="tabheads"
         onclick="openContent(event,'intro')"
         id="defaultOpen">Intro</a></li>
     <li><a href="javascript:void(0)" class="tabheads" onclick="openContent(event,'blog')">Blog</a></li>
     <li><a href="javascript:void(0)" class="tabheads"
         onclick="openContent(event,'comments')">Comments</a></li>
 </ul>
 <div class="innerDiv under-tabs tabcontent"
     id="intro">
	
	<p>
	This project is building a series of virtual colonies on the Moon. 
	</p>
 </div>
</div>

【讨论】:

  • 虽然最后这不是我的问题,但我接受了答案,因为这就是我所问的。我尝试将边距设置为 0 很多次,但从未意识到我把 Jekyll 搞砸了,因此没有合并更改。
猜你喜欢
  • 2018-12-20
  • 2018-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多