为什么我的按钮在页面上向下移动?

时间:2017-12-21 03:38:54

标签: html css

我正在尝试在我的网站上制作导航栏。我试过更改按住按钮的元素的显示类型,但没有任何反应。我正在做的主要是将这些按钮集中在导航栏的中间。我该怎么做才能让它看起来更自然?提前致谢。

显然我必须有更多的句子所以这是一个句子。这是另一句话。这是另一个。

/* import fonts */
@import url('https://fonts.googleapis.com/css?family=Open+Sans|Roboto:300');

html, body {
  margin: 0;
  background-color: rgb(236, 240, 241);
}

button:focus {
  outline:0;
}

.mainBlock {
  background-color: rgb(29,49,49);
}

.mainBlock h1 {
  text-align: center;
  font-family: 'Open Sans', sans-serif;
  font-size: 48pt;
  padding-top: 20px;
  margin-bottom: 0;
  padding-bottom: 0;
  color: rgb(236, 240, 241);
  margin-top: 0;
}

.mainBlock h3 {
  text-align: center;
  font-family: 'Roboto', sans-serif;
  font-size: 18pt;
  color: rgb(236, 240, 241);
  margin-bottom: 50px;
}
/* big button styles */
.mainButtons {
  text-align: center;
}

.mainButtons input[type=submit] {
  font-family: 'Open Sans', sans-serif;
  font-size: 15pt;
  text-align: center;
  border: none;
  width: 200px;
  height: 40px;
  margin: 10px;
  border-radius: 3px;
  color: rgba(255, 255, 255, 1);
  position: static;
  cursor: pointer;
  transition: all 0.1s ease-out;
}

#browse {
  background-color: rgb(41, 128, 185);
  box-shadow: 0 10px 0 0 rgb(53, 93, 158);
}

#browse:hover {
  box-shadow: 0 7px 0 0 rgb(53, 93, 158);
}

#create {
  background-color: rgb(231, 76, 60);
  box-shadow: 0 10px 0 0 rgb(192, 57, 43);
  margin-bottom: 42px;
}

#create:hover {
  box-shadow: 0 7px 0 0 rgb(192, 57, 43);
}

button:focus, input[type="submit"]:focus {
  outline:0;
}

#navlistcontainer {
  float: right;
  padding-right: 10px;
}

#nav {
  background-color: rgb(26, 188, 156);
  font-family: 'Open Sans', sans-serif;
  padding-top: 20px;
  padding-bottom: 20px;
}

#homelink {
  text-decoration: none;
  color: white;
  background-color: rgb(22, 160, 133);
  margin-left: 10px;
  padding: 10px;
}

#login {
  margin-left: 10px;
}

.navbaritem {
  border-radius: 100px;
  border: none;
  height: 40px;
  width: 100px;
  box-shadow: 0 3px 7px 3px rgba(0, 0, 0, 0.2);
  cursor: pointer;
  color: white;
  background-color: rgb(52, 73, 94);
  transition: all 0.1s ease-out;
}

.navbaritem:hover {
    box-shadow: 0 5px 10px 4px rgba(0, 0, 0, 0.2);
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Debater</title>
    <link href="css/styles.css" rel="stylesheet" type="text/css" />
    <link rel="shortcut icon" type="image/x-icon" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  </head>
  <body>
    <!-- Include NavBar -->
<link href="components/navbar.css" rel="stylesheet" type="text/css" />
<link type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<nav id="nav">
  <a href="index.html" id="homelink"><i class="fa fa-home fa-lg" aria-hidden="true"></i> Home</a>
  <div id="navlistcontainer">
    <a href="signup.php"><button class="navbaritem" id="signup">SIGNUP</button></a>
    <a href="login.php"><button class="navbaritem" id="login">LOGIN</button></a>
  </div>
</nav>

    <!-- Big Main Page Block -->
    <div class="mainBlock">
      <h1>Welcome to site</h1>
      <h3>siteinfo<br>
          moreinfo</h3>
      <div class="buttonsWrapper">
        <form class="mainButtons">
          <input type="submit" value="Basdasds" id="browse">
          <input type="submit" value="Cs" id="create" formaction="debate.php">
        </form> <!-- Ends the buttons form -->
      </div> <!-- Ends buttons wrapper -->
    </div>
  </body>
</html>

2 个答案:

答案 0 :(得分:1)

我认为您遇到的部分问题是您的导航栏不包含其子项的高度,因为右侧的按钮会浮动。为了解决这个问题,您可以clearfix it

导航清晰,20px顶部和底部填充实际上均匀地围绕按钮。然后,您可以通过将其更改为from django.db.models.signals import post_save def delete_ipv4manage(sender, instance, using): print('{instance} was deleted'.format(instance=str(instance))) post_delete.connect(delete_ipv4manage, sender=IPv4Manage) 来修复左侧主页链接的错位。

&#13;
&#13;
display: inline-block
&#13;
/* import fonts */

@import url('https://fonts.googleapis.com/css?family=Open+Sans|Roboto:300');
html,
body {
  margin: 0;
  background-color: rgb(236, 240, 241);
}

.clearfix::after { /* THIS IS NEW */
  content: '';
  display: table;
  clear: both;
}

button:focus {
  outline: 0;
}

.mainBlock {
  background-color: rgb(29, 49, 49);
}

.mainBlock h1 {
  text-align: center;
  font-family: 'Open Sans', sans-serif;
  font-size: 48pt;
  padding-top: 20px;
  margin-bottom: 0;
  padding-bottom: 0;
  color: rgb(236, 240, 241);
  margin-top: 0;
}

.mainBlock h3 {
  text-align: center;
  font-family: 'Roboto', sans-serif;
  font-size: 18pt;
  color: rgb(236, 240, 241);
  margin-bottom: 50px;
}


/* big button styles */

.mainButtons {
  text-align: center;
}

.mainButtons input[type=submit] {
  font-family: 'Open Sans', sans-serif;
  font-size: 15pt;
  text-align: center;
  border: none;
  width: 200px;
  height: 40px;
  margin: 10px;
  border-radius: 3px;
  color: rgba(255, 255, 255, 1);
  position: static;
  cursor: pointer;
  transition: all 0.1s ease-out;
}

#browse {
  background-color: rgb(41, 128, 185);
  box-shadow: 0 10px 0 0 rgb(53, 93, 158);
}

#browse:hover {
  box-shadow: 0 7px 0 0 rgb(53, 93, 158);
}

#create {
  background-color: rgb(231, 76, 60);
  box-shadow: 0 10px 0 0 rgb(192, 57, 43);
  margin-bottom: 42px;
}

#create:hover {
  box-shadow: 0 7px 0 0 rgb(192, 57, 43);
}

button:focus,
input[type="submit"]:focus {
  outline: 0;
}

#navlistcontainer {
  float: right;
  padding-right: 10px;
}

#nav {
  background-color: rgb(26, 188, 156);
  font-family: 'Open Sans', sans-serif;
  padding-top: 20px;
  padding-bottom: 20px;
}

#homelink {
  text-decoration: none;
  color: white;
  background-color: rgb(22, 160, 133);
  margin-left: 10px;
  padding: 10px;
  display: inline-block; /* THIS IS NEW */
}

#login {
  margin-left: 10px;
}

.navbaritem {
  border-radius: 100px;
  border: none;
  height: 40px;
  width: 100px;
  box-shadow: 0 3px 7px 3px rgba(0, 0, 0, 0.2);
  cursor: pointer;
  color: white;
  background-color: rgb(52, 73, 94);
  transition: all 0.1s ease-out;
}

.navbaritem:hover {
  box-shadow: 0 5px 10px 4px rgba(0, 0, 0, 0.2);
}
&#13;
&#13;
&#13;

另一种选择是您可以使用flex display。这样可以确保您的导航始终包含其子项,但这是一种不同的做事方式。使用柔性显示器,浮动不会做任何事情,因此您必须找到一种不同的方式将您的东西对齐到页面的右边缘和左边缘。 <link type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" /> <nav id="nav" class="clearfix"> <!-- CLEARFIX CLASS ADDED TO #NAV --> <a href="index.html" id="homelink"><i class="fa fa-home fa-lg" aria-hidden="true"></i> Home</a> <div id="navlistcontainer"> <a href="signup.php"><button class="navbaritem" id="signup">SIGNUP</button></a> <a href="login.php"><button class="navbaritem" id="login">LOGIN</button></a> </div> </nav> <!-- Big Main Page Block --> <div class="mainBlock"> <h1>Welcome to site</h1> <h3>siteinfo<br> moreinfo </h3> <div class="buttonsWrapper"> <form class="mainButtons"> <input type="submit" value="Basdasds" id="browse"> <input type="submit" value="Cs" id="create" formaction="debate.php"> </form> <!-- Ends the buttons form --> </div> <!-- Ends buttons wrapper --> </div>会很好地帮助你。

&#13;
&#13;
justify-content: space-between
&#13;
/* import fonts */

@import url('https://fonts.googleapis.com/css?family=Open+Sans|Roboto:300');
html,
body {
  margin: 0;
  background-color: rgb(236, 240, 241);
}

button:focus {
  outline: 0;
}

.mainBlock {
  background-color: rgb(29, 49, 49);
}

.mainBlock h1 {
  text-align: center;
  font-family: 'Open Sans', sans-serif;
  font-size: 48pt;
  padding-top: 20px;
  margin-bottom: 0;
  padding-bottom: 0;
  color: rgb(236, 240, 241);
  margin-top: 0;
}

.mainBlock h3 {
  text-align: center;
  font-family: 'Roboto', sans-serif;
  font-size: 18pt;
  color: rgb(236, 240, 241);
  margin-bottom: 50px;
}


/* big button styles */

.mainButtons {
  text-align: center;
}

.mainButtons input[type=submit] {
  font-family: 'Open Sans', sans-serif;
  font-size: 15pt;
  text-align: center;
  border: none;
  width: 200px;
  height: 40px;
  margin: 10px;
  border-radius: 3px;
  color: rgba(255, 255, 255, 1);
  position: static;
  cursor: pointer;
  transition: all 0.1s ease-out;
}

#browse {
  background-color: rgb(41, 128, 185);
  box-shadow: 0 10px 0 0 rgb(53, 93, 158);
}

#browse:hover {
  box-shadow: 0 7px 0 0 rgb(53, 93, 158);
}

#create {
  background-color: rgb(231, 76, 60);
  box-shadow: 0 10px 0 0 rgb(192, 57, 43);
  margin-bottom: 42px;
}

#create:hover {
  box-shadow: 0 7px 0 0 rgb(192, 57, 43);
}

button:focus,
input[type="submit"]:focus {
  outline: 0;
}

#navlistcontainer {
  /*float: right;*/ /* WE DON'T NEED THIS ANY MORE */
  padding-right: 10px;
}

#nav {
  background-color: rgb(26, 188, 156);
  font-family: 'Open Sans', sans-serif;
  padding-top: 20px;
  padding-bottom: 20px;
  display: flex; /* NEW */
  justify-content: space-between; /* NEW */
}

#homelink {
  text-decoration: none;
  color: white;
  background-color: rgb(22, 160, 133);
  margin-left: 10px;
  padding: 10px;
}

#login {
  margin-left: 10px;
}

.navbaritem {
  border-radius: 100px;
  border: none;
  height: 40px;
  width: 100px;
  box-shadow: 0 3px 7px 3px rgba(0, 0, 0, 0.2);
  cursor: pointer;
  color: white;
  background-color: rgb(52, 73, 94);
  transition: all 0.1s ease-out;
}

.navbaritem:hover {
  box-shadow: 0 5px 10px 4px rgba(0, 0, 0, 0.2);
}
&#13;
&#13;
&#13;

如果你去弹性路线,你也可以杀死导航的顶部和底部填充,只需指定它的高度。然后,使用flex属性<link type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" /> <nav id="nav" class="clearfix"> <a href="index.html" id="homelink"><i class="fa fa-home fa-lg" aria-hidden="true"></i> Home</a> <div id="navlistcontainer"> <a href="signup.php"><button class="navbaritem" id="signup">SIGNUP</button></a> <a href="login.php"><button class="navbaritem" id="login">LOGIN</button></a> </div> </nav> <!-- Big Main Page Block --> <div class="mainBlock"> <h1>Welcome to site</h1> <h3>siteinfo<br> moreinfo </h3> <div class="buttonsWrapper"> <form class="mainButtons"> <input type="submit" value="Basdasds" id="browse"> <input type="submit" value="Cs" id="create" formaction="debate.php"> </form> <!-- Ends the buttons form --> </div> <!-- Ends buttons wrapper --> </div>,您的按钮将自动垂直居中在导航栏中。

&#13;
&#13;
align-items: center
&#13;
/* import fonts */

@import url('https://fonts.googleapis.com/css?family=Open+Sans|Roboto:300');
html,
body {
  margin: 0;
  background-color: rgb(236, 240, 241);
}

button:focus {
  outline: 0;
}

.mainBlock {
  background-color: rgb(29, 49, 49);
}

.mainBlock h1 {
  text-align: center;
  font-family: 'Open Sans', sans-serif;
  font-size: 48pt;
  padding-top: 20px;
  margin-bottom: 0;
  padding-bottom: 0;
  color: rgb(236, 240, 241);
  margin-top: 0;
}

.mainBlock h3 {
  text-align: center;
  font-family: 'Roboto', sans-serif;
  font-size: 18pt;
  color: rgb(236, 240, 241);
  margin-bottom: 50px;
}


/* big button styles */

.mainButtons {
  text-align: center;
}

.mainButtons input[type=submit] {
  font-family: 'Open Sans', sans-serif;
  font-size: 15pt;
  text-align: center;
  border: none;
  width: 200px;
  height: 40px;
  margin: 10px;
  border-radius: 3px;
  color: rgba(255, 255, 255, 1);
  position: static;
  cursor: pointer;
  transition: all 0.1s ease-out;
}

#browse {
  background-color: rgb(41, 128, 185);
  box-shadow: 0 10px 0 0 rgb(53, 93, 158);
}

#browse:hover {
  box-shadow: 0 7px 0 0 rgb(53, 93, 158);
}

#create {
  background-color: rgb(231, 76, 60);
  box-shadow: 0 10px 0 0 rgb(192, 57, 43);
  margin-bottom: 42px;
}

#create:hover {
  box-shadow: 0 7px 0 0 rgb(192, 57, 43);
}

button:focus,
input[type="submit"]:focus {
  outline: 0;
}

#navlistcontainer {
  /*float: right;*/ /* WE DON'T NEED THIS ANY MORE */
  padding-right: 10px;
}

#nav {
  background-color: rgb(26, 188, 156);
  font-family: 'Open Sans', sans-serif;
  /*padding-top: 20px;*/ /* WE DON'T NEED THIS ANY MORE */
  /*padding-bottom: 20px;*/ /* WE DON'T NEED THIS ANY MORE */
  height: 80px; /* NEW */
  display: flex; /* NEW */
  justify-content: space-between; /* NEW */
  align-items: center; /* NEW*/
}

#homelink {
  text-decoration: none;
  color: white;
  background-color: rgb(22, 160, 133);
  margin-left: 10px;
  padding: 10px;
}

#login {
  margin-left: 10px;
}

.navbaritem {
  border-radius: 100px;
  border: none;
  height: 40px;
  width: 100px;
  box-shadow: 0 3px 7px 3px rgba(0, 0, 0, 0.2);
  cursor: pointer;
  color: white;
  background-color: rgb(52, 73, 94);
  transition: all 0.1s ease-out;
}

.navbaritem:hover {
  box-shadow: 0 5px 10px 4px rgba(0, 0, 0, 0.2);
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

删除padding: 25px上的#nav并将其替换为line-height: 50px,有望解决您的问题。看起来你认为按钮会按照文本而不是按钮本身对齐。填充将两个按钮按下25px,而不仅仅是文本。