首页 > 解决方案 > html页面上有sidenav时使文本居中

问题描述

我正在创建一个网站(我 13 岁并没有期望太多:D),我决定在我的设计中使用 sidenav。它是响应式的,我的登陆页面(在移动设备上查看时)的文本完全居中。但是,当在桌面上正常查看时,它会稍微偏离中心。这不是一个主要问题,但是它给我带来了不好的审美感觉(有点烦人)。总之,我将非常感谢你们的任何帮助!提前致谢!

登陆页面代码:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel='stylesheet' href='style.css'>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel='shorcut icon' type='image/x-icon' href='Icon.ico' />
</head>
<body>

<title>Home</title>

<style>
body {
  background-image: url('l.jpg');
  background-repeat: no-repeat;
}

.index-landing {
  text-align: center;
  height: 200px;
  width: 400px;
  position: fixed;
  top: 50%;
  left: 50%;
  margin-top: -100px;
  margin-left: -200px;
}
</style>

<div class='index-landing'>
  <h1>I am zS5hR3#oY3fV2</h1>
  <h3>And I'm a Programmer</h3>
</div>

<div class="sidebar">
  <a class="active" href="index.html">Home</a>
  <a href="python.html">Python</a>
  <a href="aboutme.html">About</a>
</div>

</body>
</html>

style.css 的代码:

body {
  margin: 0;
  font-family: Calibri;
}

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

.sidebar a {
  display: block;
  color: #000;
  padding: 16px;
  text-decoration: none;
  font-size: 130%
}

.sidebar a.active {
  background-color: #000000;
  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;
  }
}

.hero-text {
  text-align: center;
  position: absolute;
  top: 50%;
  left: 55%;
  transform: translate(-50%, -50%);
  color: black;
}


.title {
  color: grey;
  font-size: 18px;
}

button {
  border: none;
  outline: 0;
  display: inline-block;
  padding: 8px;
  color: white;
  background-color: #000;
  text-align: center;
  cursor: pointer;
  width: 100%;
  font-size: 18px;
}

a {
  text-decoration: none;
  font-size: 22px;
  color: black;
}

button:hover, a:hover {
  opacity: 0.7;
}

div.centered {
  text-align: center;
}

移动设备视角(应该居中):

在此处输入图像描述

桌面设备透视图(略居中): - 我还注释了我的问题,以便更清楚

在此处输入图像描述

谢谢你的阅读:D

标签: htmlcss

解决方案


不是最整洁的解决方案,但我认为以下会做

.index-landing {
        text-align: center;
        height: 200px;
        width: 100%;
        position: fixed;
        top: 50%;
        left: 100px;
        margin-top: -100px;
    }

    @media screen and (max-width: 700px) {
        .index-landing {
            left: 0px !important;
        }
    }

推荐阅读