为了账号安全,请及时绑定邮箱和手机立即绑定

html侧边栏居中的图像页脚

html侧边栏居中的图像页脚

德玛西亚99 2022-11-11 16:13:49
我正在为我的网站制作一个新的侧边栏,如果您在完整窗口中查看下面的代码片段,您可以看到“sideBarFooter”元素(其中包含图像和其下方的一些文本)。我试图让这个页脚始终位于侧边栏的底部,我尝试添加 margin-top 和 margin-bottom 样式但没有运气。如何将我的 sideBarFooter 对齐到我的侧边栏元素的底部?$(document).ready(function() {  $('#sidebarCollapse').on('click', function() {    $('#sidebar').toggleClass('active');  });});
查看完整描述

4 回答

?
Qyouu

TA贡献1786条经验 获得超11个赞

使用您的示例作为基础,您只需要添加一些样式就可以了。

我做了什么:

  1. body并且htmlheight: 100%;

  2. .wrapperheight: 100%

  3. #sidebardisplay: flex;flex-direction: column;这使它垂直。

  4. #sideBarFootermargin-top: auto

$(document).ready(function() {

  $('#sidebarCollapse').on('click', function() {

    $('#sidebar').toggleClass('active');

  });

});

@import "https://fonts.googleapis.com/css?family=Poppins:300,400,500,600,700";

body {

  font-family: 'Poppins', sans-serif;

  background: #ffffff;

}


p {

  font-family: 'Poppins', sans-serif;

  font-size: 1.1em;

  font-weight: 300;

  line-height: 1.7em;

  color: #999;

}


.btn-info {

  color: #fff;

  background-color: #b81717;

  border-color: #52b817;

}


a,

a:hover,

a:focus {

  color: inherit;

  text-decoration: none;

  transition: all 0.3s;

}


.navbar {

  padding: 15px 10px;

  background: #fff;

  border: none;

  border-radius: 0;

  margin-bottom: 40px;

  box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.1);

}


.navbar-btn {

  box-shadow: none;

  outline: none !important;

  border: none;

}


.line {

  width: 100%;

  height: 1px;

  border-bottom: 1px dashed #ddd;

  margin: 40px 0;

}


body, 

html {

  height: 100%;

}



/* ---------------------------------------------------

        SIDEBAR STYLE

    ----------------------------------------------------- */


.wrapper {

  display: flex;

  width: 100%;

  align-items: stretch;

  height: 100%;

}


#sidebar {

  min-width: 250px;

  max-width: 250px;

  background: #f7f9fb;

  /* NAVBAR BACKGROUND */

  color: rgb(0, 0, 0);

  transition: all 0.3s;

  

  display: flex;

  flex-direction: column;

  

}


#sidebar.active {

  margin-left: -250px;

}


#sidebar .sidebar-header {

  padding: 20px;

  /* background: #6d7fcc; */

}


#sidebar ul.components {

  /* padding: 20px 0; */

  /* border-bottom: 1px solid #47748b; */

}


#sidebar ul p {

  color: #fff;

  padding: 10px;

}


#sidebar ul li a {

  padding: 10px;

  font-size: 1.1em;

  display: block;

}


.currentPage,

.currentPage * {

  background: #bed6b8 !important;

}


#sidebar ul li a:hover {

  color: #000;

  background: #8FC1E3;

  /* hover tab color */

}


#sidebar ul li.active>a,

a[aria-expanded="true"] {

  /* color: #fff; */

  background: #f7f9fb;

  /* active tab */

}


a[data-toggle="collapse"] {

  position: relative;

}


.dropdown-toggle::after {

  display: block;

  position: absolute;

  top: 50%;

  right: 20px;

  transform: translateY(-50%);

}


ul ul a {

  font-size: 0.9em !important;

  padding-left: 30px !important;

  background: #f7f9fb;

  /* tab list color */

}


ul.CTAs {

  padding: 20px;

}


ul.CTAs a {

  text-align: center;

  font-size: 0.9em !important;

  display: block;

  border-radius: 5px;

  margin-bottom: 5px;

}



/* ---------------------------------------------------

        CONTENT STYLE

    ----------------------------------------------------- */


#content {

  width: 100%;

  padding: 20px;

  min-height: 100vh;

  transition: all 0.3s;

}



/* ---------------------------------------------------

        MEDIAQUERIES

    ----------------------------------------------------- */


@media (max-width: 768px) {

  #sidebar {

    margin-left: -250px;

  }

  #sidebar.active {

    margin-left: 0;

  }

  #sidebarCollapse span {

    color: pink display: inline;

  }

}



/* Sidebar expand/collapse button */


#sidebutton {

  margin-left: 100%;

  box-shadow: 0px 1px 4px 1px rgba(0, 0, 0, .3);

  border: none;

  height: 40px;

  width: 40px;

  border-radius: 50%;

  cursor: pointer;

}


#sideBarFooter{

  margin-top: auto;

}

<!-- jQuery CDN - Slim version (=without AJAX) -->

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>


<!-- Popper.JS -->

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"></script>


<!-- Bootstrap JS -->

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script>


<!-- bootstrap css-->

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">


<!-- jQuery -->

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>


<!-- Page Content -->

<div class="wrapper" class="toggled">

  <!-- Sidebar  -->

  <nav id="sidebar">


    <div class="sidebar-header">

      <h3>Website Title</h3> <button id='sidebutton'>M</button>

    </div>


    <ul class="list-unstyled components">

      <li class="currentPage">

        <a href="#">Home</a>

      </li>

      <li>

        <a href="#">About</a>

      </li>

      <li>

        <a href="#contactSubmenu" data-toggle="collapse" aria-expanded="true" class="dropdown-toggle">Contact</a>

        <ul class="collapse list-unstyled" id="contactSubmenu">

          <li>

            <a href="#">Email</a>

          </li>

          <li>

            <a href="#">Github</a>

          </li>

          <li>

            <a href="#">LinkedIn</a>

          </li>

          <li>

            <a href="#">Resume</a>

          </li>

        </ul>

      </li>

    </ul>


    <!-- Footer -->

    <div id='sideBarFooter' class="sidebar-footer">

      <img src="https://www.w3schools.com/images/w3schools_green.jpg" alt="W3Schools.com">

      <p>Color 1: #657856</p>

      <p>Color 2: #657856</p>

      <p>Color 3: #657856</p>

    </div>


  </nav>

</div>


查看完整回答
反对 回复 2022-11-11
?
慕后森

TA贡献1802条经验 获得超5个赞

几件事:

height: 100vh

首先,元素的父级(在这种情况下#sidebar)应该有 100% 的视口高度。你vh问什么?更多关于这里

position: absolute,bottom: 0

现在您可以使用position: absoluteand bottom: 0。这会将所述页脚带到页面底部。

为什么要迈出第一步?

实际上,这无需第一步就可以工作,但是#sidebar不会从上到下,因此会影响灰色背景。

#sidebar {  
/* etc... */
  height: 100vh
  }
#sideBarFooter{ 
 position: absolute; 
  bottom: 0}

工作示例

快乐的黑客,Bobbbay

PS 不要将ids 与 CSS 一起使用。这是一个不好的做法。


查看完整回答
反对 回复 2022-11-11
?
Helenr

TA贡献1780条经验 获得超4个赞

您可以使用 id 侧边栏向导航元素添加display: flexflex-direction: column设置高度。height: 100vh然后你只需要给出页脚margin-top: auto,它就会在社交上与其他元素保持距离。

快速提示:不要按他们的 ID 设置样式,这是不好的做法。这是MDN 用于他们的 CSS的一些其他规则,我发现它很有帮助。


查看完整回答
反对 回复 2022-11-11
?
慕姐4208626

TA贡献1852条经验 获得超7个赞

您应该尝试将侧边栏页脚定位到绝对位置。尝试将此添加到您的 css :


#sideBarFooter{

  position:absolute;

  bottom : 0;

}


查看完整回答
反对 回复 2022-11-11
  • 4 回答
  • 0 关注
  • 272 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号