bootstrap4内容/ div对齐

时间:2019-05-06 02:24:45

标签: html bootstrap-4

请为此查看代码笔: codepen

 <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="container position-relative">
   <div class="pillwrapper">
      <div class="rightpart d-flex justify-content-end">
         <ul class="nav nav-pills" id="mypill">
            <li class="nav-item"><a href="#content-1" class="nav-link active" data-toggle="tab">button</a></li>
            <li class="nav-item"><a href="#content-2" class="nav-link" data-toggle="tab">button</a></li>
            <li class="nav-item"><a href="#content-3" class="nav-link" data-toggle="tab">button</a></li>
         </ul>
         <div class="tab-content" id="mypillcontent">
            <div class="tab-pane fade show active" id="content-1">
               <p>content here</p>
               <img src="https://images.pexels.com/photos/162140/duckling-birds-yellow-fluffy-162140.jpeg" alt="" class="content-photo position-absolute" />
            </div>
            <div class="tab-pane fade" id="content-2">
              <p>content here</p>
              <img src="https://images.pexels.com/photos/162140/duckling-birds-yellow-fluffy-162140.jpeg" alt="" class="content-photo position-absolute" />
            </div>
            <div class="tab-pane fade" id="content-3">
              <p>content here</p>
              <img src="https://images.pexels.com/photos/162140/duckling-birds-yellow-fluffy-162140.jpeg" alt="" class="content-photo position-absolute" />
            </div>
         </div>
      </div>
   </div>
</div> 

我正在学习bootstrap4。我正在尝试使用引导NAV PILL实现一个结构:一个标签系统(但看起来像按钮),您可以切换标签,在标签下有一个文本内容和一张照片,只有这张照片会显示在左侧,就像标签系统在右边,照片在左边。问题是:1,第一个选项卡处于活动状态,当我单击任何其他选项卡时,什么也没有发生。 2,为什么绝对位置不起作用,照片仍然正确? 3,如何将文本内容放在标签下,并使标签和内容水平居中对齐?

.rightpart{
  border:2px solid red;
  min-height:100vh;
}
.pillwrapper{
  border:1px solid green;
  min-height:100vh;
}
.container{
  border:1px solid yellow;
  min-height:100vh;
}
.content-photo{
  width:200px;
  left:0;
  top:0;
}

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

对于Bootstrap 4,“ tabs”实际上是不可标签的,除非您包含JavaScript插件以使Tabs起作用。 Bootstrap的文档指出:

https://getbootstrap.com/docs/4.3/components/navs/#tabs

要查看选项卡的工作原理,请参见Bootstrap文档中的以下链接:

https://getbootstrap.com/docs/4.3/components/navs/#javascript-behavior

以上链接显示了使用与每个选项卡相关的内容的选项卡。要使每个标签下都有内容,每个标签的内容必须具有标签窗格。请参阅Bootstrap文档中的示例。

<ul class="nav nav-tabs" id="myTab" role="tablist">
  <li class="nav-item">
    <a class="nav-link active" id="home-tab" data-toggle="tab" href="#home" role="tab" aria-controls="home" aria-selected="true">Home</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" id="profile-tab" data-toggle="tab" href="#profile" role="tab" aria-controls="profile" aria-selected="false">Profile</a>
  </li>
  <li class="nav-item">
    <a class="nav-link" id="contact-tab" data-toggle="tab" href="#contact" role="tab" aria-controls="contact" aria-selected="false">Contact</a>
  </li>
</ul>
<div class="tab-content" id="myTabContent">
  <div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">...</div>
  <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">...</div>
  <div class="tab-pane fade" id="contact" role="tabpanel" aria-labelledby="contact-tab">...</div>
</div>

要使标签导航和每个标签下的内容居中,可以使用如下所示的CSS进行标签导航(来自上面的示例HTML)。

.nav {
    display: block;
    margin: 0 auto;
}

有了这些信息,您的HTML应该看起来像这样:

<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet" />

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.css" rel="stylesheet" />


<div class="container">

        <ul class="nav">
            <li class="nav-item">
                <a href="#content-1" class="nav-link active" data-toggle="tab">Button 1</a>
            </li>
            <li class="nav-item">
                <a href="#content-2" class="nav-link active" data-toggle="tab">Button 2</a>
            </li>
            <li class="nav-item">
                <a href="#content-3" class="nav-link active" data-toggle="tab">Button 3</a>
            </li>
        </ul>

        <div class="tab-content" id="mypillcontent">

            <div class="tab-pane fade" id="content-1">
                <p>Content Here</p>
                <img src="https://images.pexels.com/photos/162140/duckling-birds-yellow-fluffy-162140.jpeg" alt="" class="content-photo position-absolute" />
            </div>

            <div class="tab-pane fade" id="content-2">
            </div>

            <div class="tab-pane fade" id="content-3">
            </div>

        </div>


    </div>
</div>


<!--
Add Bootstrap JavaScript
-->

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>