如何使侧边栏菜单元素居中

Posted

技术标签:

【中文标题】如何使侧边栏菜单元素居中【英文标题】:How to center a sidebar menu elements 【发布时间】:2021-11-17 10:56:26 【问题描述】:

我在link 中找到了响应式侧边栏菜单的代码示例。

这个例子非常棒,因为它完全响应,但 菜单栏元素(主页、新闻、联系人、关于)始终位于菜单的左侧,我正在寻找将这些元素放置在页面大屏幕的顶部中心,就像这个例子一样

但是我没有成功,我是 css 的初学者,所以我在这里寻求帮助,感谢任何帮助,非常感谢,这是代码:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body 
  margin: 0;
  font-family: "Lato", sans-serif;


.sidebar 
  margin: 0;
  padding: 0;
  width: 200px;
  background-color: #f1f1f1;
  position: fixed;
  height: 100%;
  overflow: auto;


.sidebar a 
  display: block;
  color: black;
  padding: 16px;
  text-decoration: none;

 
.sidebar a.active 
  background-color: #04AA6D;
  color: white;


.sidebar a:hover:not(.active) 
  background-color: #555;
  color: white;


div.content 
  margin-left: 200px;
  padding: 1px 16px;
  height: 1000px;


@media screen and (max-width: 700px) 
  .sidebar 
    width: 100%;
    height: auto;
    position: relative;
  
  .sidebar a float: left;
  div.content margin-left: 0;


@media screen and (max-width: 400px) 
  .sidebar a 
    text-align: center;
    float: none;
  

</style>
</head>
<body>

<div class="sidebar">
  <a class="active" href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
</div>

<div class="content">
  <h2>Responsive Sidebar Example</h2>
  <p>This example use media queries to transform the sidebar to a top navigation bar when the screen size is 700px or less.</p>
  <p>We have also added a media query for screens that are 400px or less, which will vertically stack and center the navigation links.</p>
  <h3>Resize the browser window to see the effect.</h3>
</div>

</body>
</html>

【问题讨论】:

【参考方案1】:

首先,你必须改变 html "sidebar" div,添加另一个 div "a-holder"

<div class="sidebar">
  <div class="a-holder">
  <a class="active" href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
  </div>
</div>

然后在 css 中,添加带有如下参数的 a-holder:

@media screen and (max-width: 700px) 
  .sidebar 
    width: 100%;
    height: auto;
    position: relative;
  
  .sidebar a float: left;
  div.content margin-left: 0;
  .a-holder 
    margin: auto;
    align-self: center;
    width: 80%;
  

那么完整的代码就是

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body 
  margin: 0;
  font-family: "Lato", sans-serif;


.sidebar 
  margin: 0;
  padding: 0;
  width: 200px;
  background-color: #f1f1f1;
  position: fixed;
  height: 100%;
  overflow: auto;


.sidebar a 
  display: block;
  color: black;
  padding: 16px;
  text-decoration: none;

 
.sidebar a.active 
  background-color: #04AA6D;
  color: white;


.sidebar a:hover:not(.active) 
  background-color: #555;
  color: white;


div.content 
  margin-left: 200px;
  padding: 1px 16px;
  height: 1000px;


@media screen and (max-width: 700px) 
  .sidebar 
    width: 100%;
    height: auto;
    position: relative;
  
  .sidebar a float: left;
  div.content margin-left: 0;
  .a-holder 
    margin: auto;
    align-self: center;
    width: 80%;
  


@media screen and (max-width: 400px) 
  .sidebar a 
    text-align: center;
    float: none;
  


</style>
</head>
<body>

<div class="sidebar">
  <div class="a-holder">
  <a class="active" href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
  </div>
</div>

<div class="content">
  <h2>Responsive Sidebar Example</h2>
  <p>This example use media queries to transform the sidebar to a top navigation bar when the screen size is 700px or less.</p>
  <p>We have also added a media query for screens that are 400px or less, which will vertically stack and center the navigation links.</p>
  <h3>Resize the browser window to see the effect.</h3>
</div>

</body>
</html>

【讨论】:

我使用了这个选项,它可以工作,只需要在 .a-holder width:50% 内进行更改,非常感谢您的帮助 我给出了 80% 的宽度,因为 50% 的宽度会导致 .a-holder 在宽度非常低时分成两行。无论如何,如果它适合你。 如果我把 80% 放在宽度上它不会移动到中心,只会向右移动,这是什么?一种边距? 行 align-self: center 不是必需的,margin: auto 完成所有工作。是的。【参考方案2】:

从问题中提供的图片和提供给模板的链接开始。我相信您必须在侧面导航的顶部添加图像。作为问题中提供的链接的解决方案。

@media screen and (min-width: 767) 
    .sidebar
    text-align:center;
  margin: 0;
  padding: 0;
  background-color: #f1f1f1;
  height: 100%;
  overflow: auto;
  
  .sidebar a 
        display: inline-block;
  color: black;
  padding: 16px;
  text-decoration: none;
  

【讨论】:

【参考方案3】:

只需将侧边栏的属性从@media screen and (max-width: 700px) 交换为常规属性

这是一个示例

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body 
  margin: 0;
  font-family: "Lato", sans-serif;


.sidebar 
  margin: 0;
  padding: 0;
  background-color: #f1f1f1;
  overflow: auto;
  width: 100%;
  height: auto;
  position: relative;

  *box-sizing: border-box;
  .sidebar::after content: ''; clear: both; display: table;
  .sidebar .logo float: left; width: 120px; padding-left: 10px; padding-top: 15px;
  .sidebar .social_linksfloat: right; width: 100px;
  .sidebar .social_links ul lilist-style:none; display: inline-block;
  .sidebar .social_links ulmargin: 0;
  .sidebar .social_links ul li a padding: 15px 5px
  .sidebar .menu float: left; width: calc(100% - 220px); text-align:center;

.sidebar a 
  display: inline-block;
  color: black;
  padding: 16px;
  text-decoration: none;

 
.sidebar a.active 
  background-color: #04AA6D;
  color: white;


.sidebar a:hover:not(.active) 
  background-color: #555;
  color: white;


div.content 
  padding: 1px 16px;


@media screen and (max-width: 700px) 
  .sidebar 
      width: 200px;
      position: fixed;
      height: 100%;
  
    .sidebar a float: none;
  .sidebar .logo float: none; display: block; width: 100%; text-align: center; margin-bottom: 20px;
  .sidebar .menu, .sidebar .social_links float: none; width: 100%;
  .sidebar .menu adisplay: block; 
  .sidebar .social_links li float: none; text-align:center;
  .sidebar .social_links ulpadding-left: 0; width: 100%; text-align:center;
  div.content margin-left: 0; height: 1000px; margin-left: 200px;


@media screen and (max-width: 400px) 
  .sidebar a 
    text-align: center;
    float: none;
  

</style>
</head>
<body>

<div class="sidebar">
  <div class="logo">LOGO HERE</div>
  <div class="menu">
   <a class="active" href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
  </div>
  <div class="social_links">
    <ul>
      <li><a href="#">fb</a></li>
      <li><a href="#">in</a></li>
    </ul>
  </div>
</div>

<div class="content">
  <h2>Responsive Sidebar Example</h2>
  <p>This example use media queries to transform the sidebar to a top navigation bar when the screen size is 700px or less.</p>
  <p>We have also added a media query for screens that are 400px or less, which will vertically stack and center the navigation links.</p>
  <h3>Resize the browser window to see the effect.</h3>
</div>

</body>
</html>

当屏幕尺寸缩小到700px以下时,此代码也会响应

PS:在整页中运行代码,看看它在桌面视图中的样子

【讨论】:

【参考方案4】:

body 
  margin: 0;
  font-family: "Lato", sans-serif;


.sidebar 
  margin: 0;
  padding: 0;
  width: 200px;
  background-color: #f1f1f1;
  position: fixed;
  height: 100%;
  overflow: auto;
  display: flex;
   justify-content: center;


.sidebar a 
  display: block;
  color: black;
  padding: 16px;
  text-decoration: none;

 
.sidebar a.active 
  background-color: #04AA6D;
  color: white;


.sidebar a:hover:not(.active) 
  background-color: #555;
  color: white;


div.content 
  margin-left: 200px;
  padding: 1px 16px;
  height: 1000px;


@media screen and (max-width: 700px) 
  .sidebar 
    width: 100%;
    height: auto;
    position: relative;
  
  .sidebar a float: left;
  div.content margin-left: 0;


@media screen and (max-width: 400px) 
  .sidebar a 
    text-align: center;
    float: none;
  
<div class="sidebar">
  <a class="active" href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
</div>

<div class="content">
  <h2>Responsive Sidebar Example</h2>
  <p>This example use media queries to transform the sidebar to a top navigation bar when the screen size is 700px or less.</p>
  <p>We have also added a media query for screens that are 400px or less, which will vertically stack and center the navigation links.</p>
  <h3>Resize the browser window to see the effect.</h3>
</div>

【讨论】:

以上是关于如何使侧边栏菜单元素居中的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 CSS 或 HTML 居中导航栏?

以子菜单项相对于父菜单项将块居中

如何使用 flexbox 使菜单居中 [关闭]

如何使按钮框居中?

我怎样才能使我的菜单栏居中?

如何垂直居中导航栏元素(Twitter Bootstrap)?