html - 如何在使用 CSS 创建响应式按钮时使按钮居中

标签 html css button textbox center

我使用基本的 HTML、CSS 和 Javascript 来创建一个简单的 Web 应用程序。当我尝试使用 text-align: center 和其他 CSS 元素将按钮居中时,我仍然遇到麻烦。我还尝试将文本居中在框中心内,但由于未知原因不起作用。到目前为止,我已经使用了 margin-left 和 margin-right 来调整按钮,但愿意知道如何将其居中,而不需要经历所有这些麻烦。

enter image description here

.image {
  display: grid;
  grid-template-columns: 50% 50%;
  grid-template-rows: 50% 50%;
  margin-top: 40px;
  margin-left: 50px;
}

.image img {
  cursor: pointer;
}

.image img:hover {
  border: solid 2px #1e88e5;
  border-radius: 2px;
}

.image #mother img:hover {
  border: solid 2px #1e88e5;
  border-radius: 2px;
}

.image img {
  width: 100px;
  height: 100px;
  text-align: center;
  padding: 10px;
  margin-left: 30%;
}

.button{
  width: 80px;
  height: 40px;
  text-align: center;
  margin: auto;
  display: flex;
  padding-top: 8px;
  padding-bottom: 8px;
  background-color: rgb(43, 43, 219);  
  cursor: pointer;
  border-radius: 2px;
}

.button:hover{
   background-color: #242424;
}
.button:active{
   background-color: #121212;
}

.button > span{
  color: #eeeeee;
  text-align: center;
}
<!doctype html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- Bootstrap CSS -->
  <link href="https://cdn.jsdelivr.net/npm/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="13717c7c67606761726353263d233d233e7176677221" rel="noreferrer noopener nofollow">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"
    integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">

  <title>Good Cop, Bad Cop</title>
  <style>
    .view-image img {
      margin-left: 90px;
    }
  </style>
  <link rel="stylesheet" href="../CSS/landing.css" />
  <link rel="stylesheet" href="../CSS/coach_selection.css" />
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <link type="text/css" rel="stylesheet" href="https://www.gstatic.com/firebasejs/ui/4.6.1/firebase-ui-auth.css" />

</head>

<body>
  <nav class="navbar navbar-custom">
    <div class="container-fluid">
      <a class="navbar-brand" href="">
        <img src="../IMAGES/Hamburger_icon.svg" alt="Character" height="35vh" />
      </a>

      <a href="settings.html">
        <ul class="navbar-nav">
          <img src="../IMAGES/bootstrap-icons-1.3.0/person-circle.svg" alt="Profile" height="45vh" />
        </ul>
      </a>
    </div>
  </nav>
  <div class="image">
    <div>
      <button type="button" id="dog-button" class="btn btn-light">
        <img src="../images/dog.png" id="dog" alt="dog">
      </button>
    </div>
    <div>
      <button type="button" id="mother-button" class="btn btn-light">
        <img src="../images/mother.png" id="mother" alt="mother">
      </button>
    </div>
    <div>
      <button type="button" id="soldier-button" class="btn btn-light">
        <img src="../images/military.png" id="soldier" alt="soldier">
      </button>
    </div>
    <div>
      <button type="button" id="teacher-button" class="btn btn-light">
        <img src="../images/teacher.png" id="teacher" alt="teacher">
      </button>
    </div>
    <div class="button" id="button-confirm">
      <span>Confirm</span>
    </div>
    <div class="view-image" style="display:none;">
      <img src="" width="150" height="150" />
      <div class="button" id="button-other"><span>Other</span></div>
    </div>
  </div>
  <div class="fixed-bottom">
    <div class="navbar navbar-custom">
      <div class="container-fluid">
        <div id="grid">
          <div class="bot-nav-img">
            <a href="Journal/journal_main.html">
              <img src="../IMAGES/bootstrap-icons-1.3.0/book-fill.svg" alt="Journal" height="40vh" />
            </a>
          </div>
          <div class="bot-nav-img">
            <a href="landing.html">
              <img src="../IMAGES/bootstrap-icons-1.3.0/house-door-fill.svg" alt="Home" height="40vh" />
            </a>
          </div>
          <div class="bot-nav-img">
            <a href="goals.html">
              <img src="../IMAGES/bootstrap-icons-1.3.0/table.svg" alt="Goals" height="40vh" />
            </a>
          </div>
        </div>
      </div>
    </div>
</body>

</html>

最佳答案

在 css 中创建完美居中对齐的最快、最简单的方法是使用 Flexbox。

一般模式是,为需要其子元素居中的父元素提供一个 display: flex;、一个 justify-content: center; 和一个 对齐元素:中心;

对于你的代码,去掉所有的text-align。然后在 .button 类(已经是 display: flex)中添加 justify-content: center;align-items:中心;

包含文本的 span 元素是按钮的子元素,因此它现在将居中。 span 元素本身不需要任何 CSS 属性来帮助其居中。

.button {
  width: 80px;
  height: 40px;
  margin: auto;
  display: flex;
  justify-content: center;
  align-items: center;
  padding-top: 8px;
  padding-bottom: 8px;
  background-color: rgb(43, 43, 219);  
  cursor: pointer;
  border-radius: 2px;
}

关于html - 如何在使用 CSS 创建响应式按钮时使按钮居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66998073/

相关文章:

每行的 HTML 悬停样式

css - 给 div 上色而不给填充区域上色

javascript - 在javascript中使用for循环将多个元素绑定(bind)到事件处理程序

html - 多级下拉导航菜单问题

javascript - 更改 JavaScript 警报按钮

java - 如何将按钮添加到 libgdx 中的 ApplicationAdapter?

python - ttk按钮跨越多列

javascript - 自动悬停效果

javascript - 一次只下拉一个菜单

javascript - 切换类动画 Jquery UI