【问题标题】:How to put two button in the same line using only html and css?如何仅使用 html 和 css 将两个按钮放在同一行?
【发布时间】:2018-09-25 10:12:58
【问题描述】:

如何仅使用 css 和 html 正确地将两个按钮并排对齐,并在它们之间留出一些空间?

这是我的html 和css。两个按钮没有正确对齐。

* {
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
}

body {
  background-color: #0c0129;
  background-image: url(coding.jpg);
  background-position: center;
  background-size: cover;
  background-blend-mode: soft-light;
}

.heading {
  width: 600px;
  height: 300px;
  margin: auto;
  position: absolute;
  top: 0px;
  bottom: 0px;
  right: 0px;
  left: 0px;
  outline: none;
}

.title1 {
  display: block;
  text-align: center;
  color: white;
  font-size: 50pt;
  font-family: fantasy;
}

.title2 {
  display: block;
  margin-top: 16px;
  text-align: center;
  color: white;
  font-size: 15pt;
  font-family: sans-serif;
}

.register {
  margin: 50px auto;
  display: block;
  width: 180px;
  height: 50px;
  border: 3px solid white;
  background-color: rgba(255, 255, 255, 0);
  color: white;
  font-family: sans-serif;
  font-weight: bold;
  border-radius: 5px;
  float: left;
  transition: background-color 1000ms, color 1000ms;
}

.register:hover {
  background-color: rgba(255, 255, 255, 1);
  color: #222;
  cursor: pointer;
  transition: background-color 1000ms, color 1000ms;
}

.login {
  margin: 20px auto;
  display: block;
  width: 180px;
  height: 50px;
  border: 3px solid white;
  background-color: rgba(255, 255, 255, 0);
  color: white;
  font-family: sans-serif;
  font-weight: bold;
  border-radius: 5px;
  float: right;
  transition: background-color 1000ms, color 1000ms;
}

.login:hover {
  background-color: rgba(255, 255, 255, 1);
  color: #222;
  cursor: pointer;
  transition: background-color 1000ms, color 1000ms;
}

ul {
  list-style: none;
}
<div class="heading">
  <span class="title1">Banerjee Solutions</span>
  <span class="title2">Your one stop shop for everything tech.</span>
  <ul>
    <li><button class="register">REGISTER NOW</button></li>
    <li><button class="login">LOG IN</button></li>
  </ul>
</div>

我需要修改什么以获得我想要的结果?

【问题讨论】:

  • 需要对齐的是lis。 li { display: inline-block; vertical-align: middle; }
  • 给两个按钮相同的边距,实际上一个是 50px,另一个是 20px
  • 试试这个小提琴jsfiddle.net/varsha19/2a1pjqky

标签: html css


【解决方案1】:

这是我使用flex的解决方案

ul {
    list-style: none;
    display: flex;
    align-items: center;
    justify-content: center;
}

li {
    margin: auto;
}

【讨论】:

  • 感谢您的解决方案...但是当我单击任何一个按钮时都会出现边框,我该如何摆脱它?
  • 边框是什么颜色的?我看不到我的一个
  • 检查 dropbox.com/s/g72apjuxby8o69m/Untitled.png?dl=0 只需放大 275% 即可
  • @TamalBanerjee - 你用的是什么浏览器?
  • 我可以复制这个问题 - 我现在会尝试修复它 - 这个问题在 Firefox 上没有发生,但现在在 Chrome 上
【解决方案2】:

在您的 css 类中,您的注册类(使用第一个按钮)比登录类(使用第二个按钮)有更多的余量。它们必须具有相同的边距,以便它们排成一行。

比如把登录类和注册类放在一样,就像这样:

.login {
  margin: 50px auto;
  display: block;
  width: 180px;
  height: 50px;
  border: 3px solid white;
  background-color: rgba(255, 255, 255, 0);
  color: white;
  font-family: sans-serif;
  font-weight: bold;
  border-radius: 5px;
  float: right;
  transition: background-color 1000ms, color 1000ms;
}

【讨论】:

    【解决方案3】:

    列表中的 li 是块级元素 - 所以要让按钮位于同一行 - 只需将 li 的 display: inline.每个按钮的边距也不同 - 因此使它们相同可以使它们彼此一致。每个按钮都是浮动的 - 一个在左侧,一个在右侧,因此每个按钮都位于容器的一侧。

    * {
      margin: 0;
      padding: 0;
    }
    
    html,
    body {
      height: 100%;
    }
    
    body {
      background-color: #0c0129;
      background-image: url(coding.jpg);
      background-position: center;
      background-size: cover;
      background-blend-mode: soft-light;
    }
    
    .heading {
      width: 600px;
      height: 300px;
      margin: auto;
      position: absolute;
      top: 0px;
      bottom: 0px;
      right: 0px;
      left: 0px;
      outline: none;
    }
    
    .title1 {
      display: block;
      text-align: center;
      color: white;
      font-size: 50pt;
      font-family: fantasy;
    }
    
    .title2 {
      display: block;
      margin-top: 16px;
      text-align: center;
      color: white;
      font-size: 15pt;
      font-family: sans-serif;
    }
    
    .register {
      margin: 20px auto;
      display: block;
      width: 180px;
      height: 50px;
      border: 3px solid white;
      background-color: rgba(255, 255, 255, 0);
      color: white;
      font-family: sans-serif;
      font-weight: bold;
      border-radius: 5px;
      float: left;
      transition: background-color 1000ms, color 1000ms;
    }
    
    .register:hover {
      background-color: rgba(255, 255, 255, 1);
      color: #222;
      cursor: pointer;
      transition: background-color 1000ms, color 1000ms;
    }
    
    .login {
      margin: 20px auto;
      display: block;
      width: 180px;
      height: 50px;
      border: 3px solid white;
      background-color: rgba(255, 255, 255, 0);
      color: white;
      font-family: sans-serif;
      font-weight: bold;
      border-radius: 5px;
      float: right;
      transition: background-color 1000ms, color 1000ms;
    }
    
    .login:hover {
      background-color: rgba(255, 255, 255, 1);
      color: #222;
      cursor: pointer;
      transition: background-color 1000ms, color 1000ms;
    }
    
    ul {
      list-style: none;
    }
     ul li {display: inline}
    <div class="heading">
      <span class="title1">Banerjee Solutions</span>
      <span class="title2">Your one stop shop for everything tech.</span>
      <ul>
        <li><button class="register">REGISTER NOW</button></li>
        <li><button class="login">LOG IN</button></li>
      </ul>
    </div>

    【讨论】:

      【解决方案4】:

      检查以下代码

      • 更改了元素结构。
      • 移除 float 属性,并在登录和注册类中添加 display: inline-block
      • 添加了 btn 容器类

      .btn-container{
        margin-top: 30px;
        text-align: center;
      }
      
      * {
        margin: 0;
        padding: 0;
      }
      
      html,
      body {
        height: 100%;
      }
      
      body {
        background-color: #0c0129;
        background-image: url(coding.jpg);
        background-position: center;
        background-size: cover;
        background-blend-mode: soft-light;
      }
      
      .heading {
        width: 600px;
        height: 300px;
        margin: auto;
        position: absolute;
        top: 0px;
        bottom: 0px;
        right: 0px;
        left: 0px;
        outline: none;
      }
      
      .title1 {
        display: block;
        text-align: center;
        color: white;
        font-size: 50pt;
        font-family: fantasy;
      }
      
      .title2 {
        display: block;
        margin-top: 16px;
        text-align: center;
        color: white;
        font-size: 15pt;
        font-family: sans-serif;
      }
      
      .register {
          margin-right: 20px;
          display: block;
          width: 180px;
          height: 50px;
          border: 3px solid white;
          background-color: rgba(255, 255, 255, 0);
          color: white;
          font-family: sans-serif;
          font-weight: bold;
          border-radius: 5px;
          transition: background-color 1000ms, color 1000ms;
          display: inline-block;
          outline: none;
      }
      
      .register:hover {
        background-color: rgba(255, 255, 255, 1);
        color: #222;
        cursor: pointer;
        transition: background-color 1000ms, color 1000ms;
      }
      
      .login {
          width: 180px;
          height: 50px;
          border: 3px solid white;
          background-color: rgba(255, 255, 255, 0);
          color: white;
          font-family: sans-serif;
          font-weight: bold;
          border-radius: 5px;
          transition: background-color 1000ms, color 1000ms;
          display: inline-block;
          outline: none;
      }
      
      .login:hover {
        background-color: rgba(255, 255, 255, 1);
        color: #222;
        cursor: pointer;
        transition: background-color 1000ms, color 1000ms;
      }
      <div class="heading">
        <span class="title1">Banerjee Solutions</span>
        <span class="title2">Your one stop shop for everything tech.</span>
        <div class="btn-container">
          <button class="register">REGISTER NOW</button>
          <button class="login">LOG IN</button>
        </div>
      </div>

      【讨论】:

      • 当我点击任何一个按钮时都会出现一个边框,我该如何摆脱它?....如果你仔细观察,你可以看到它,但它并不容易看到..无论如何它不应该在那里
      • 可以设置大纲:无;我已经更新了代码,请检查。
      • 你能把截图发给我吗?
      【解决方案5】:

      这是一个更新的工作小提琴:https://jsfiddle.net/Ly06krt3/25/

      总而言之,您需要将LI's 向左浮动,并为它们提供 50% 的宽度,这将使它们并排移动。

      然后,您希望将auto 添加到水平边距以使按钮与LI's 的中心对齐。所以是这样的:margin: 50px auto。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-25
        • 2023-02-20
        • 2020-05-07
        • 2022-07-08
        • 2019-03-16
        • 2019-09-01
        • 2017-08-24
        相关资源
        最近更新 更多