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

How to center a sidebar menu elements

我在这个 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>

根据问题中提供的图像和提供给模板的 link。我相信你必须在侧面导航的顶部添加一个图像。至于问题中提供的 link 的解决方案。

@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;
  }
}

首先,您必须更改html“侧边栏”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 中添加一个带有如下参数的支架:

@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>

只需将侧边栏的属性从 @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_links{float: right; width: 100px;}
  .sidebar .social_links ul li{list-style:none; display: inline-block;}
  .sidebar .social_links ul{margin: 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 a{display: block; }
  .sidebar .social_links li {float: none; text-align:center;}
  .sidebar .social_links ul{padding-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: 运行 完整页面中的代码,以查看其在桌面视图中的外观

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>