html - 如何将模式中的列表元素居中对齐

标签 html css

我想为下面的代码对齐以 HTML 为中心的列表内容。当屏幕尺寸较小时它看起来不错,但当它很宽时它不居中。我试图通过使用视口(viewport)使其响应,但在它被触发之前出现了模态。感谢您的帮助。

/* Modal Window */
.modal {
  display: none;        /* Hidden by default */
  position: fixed;      /* Stay in place */
  z-index: 1;           /* Sit on top */
  padding-top: 100px;   /* Location of the box */
  left: 0;
  top: 0;
  width: 100%;          /* Full width */
  height: 100%;         /* Full height */
  overflow: auto;       /* Enable scroll if needed */
  background-color: rgb(0, 0, 0);         /* Fallback color */
  background-color: rgba(0, 0, 0, 0.4);   /* Black w/ opacity */
}

/* Modal Content */
.modal-content {
  position: relative;
  background-color: #fefefe;
  margin: auto;
  padding: 0;
  border: 1px solid #888;
  width: 50%;
  height: 50%;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  -webkit-animation-name: animatetop;
  -webkit-animation-duration: 0.4s;
  animation-name: animatetop;
  animation-duration: 0.4s
}

/* The Close Button */
.close {
  color: white;
  float: right;
  font-size: 28px;
  font-weight: bold;
}
.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}
.modal-header {
  padding: 2px 16px;
  background-color: #02ccba;
  color: white;
  text-align: center;
}
.modal-body {
  padding: 2px 10px;
  text-align: center;
  width: 50%;
  /*margin-right: 30px;*/
}
.modal-list {
  padding: 2px 16px;
  width: 200px;
}
#modal-body-list {
  list-style-type: none;
  text-align: left;
}
.list-item {
  font-weight: bold;
}
#list-star {
  text-align: center;
}
#playAgainButton {
  -webkit-border-radius: 34;
  -moz-border-radius: 34;
  border-radius: 34px;
  font-family: Arial;
  color: #000000;
  font-size: 14px;
  font-weight: bold;
  padding: 5px 10px;
  margin: 20px 0;
  border: solid #02ccbb 4px;
  text-decoration: none;
  cursor: pointer;
}
<!-- Modal Content -->
<div class="modal-content">
  <div class="modal-header">
    <span class="close">&times;</span>
    <h2>CONGRATULATIONS!</h2>
  </div>
  <div class="modal-body">
    <ul id="modal-body-list">
      <li class="modal-list">
        <span class="list-item">Moves:</span>
        <span id="list-moves"></span>
      </li>
      <li class="modal-list">
        <span class="list-item">Time:</span>
        <span id="list-time"></span>
      </li>
      <li class="modal-list">
        <span class="list-item">Star:</span>
        <span id="list-star"></span>
      </li>
      <li class="modal-list">
        <button id="playAgainButton">Play Again</button>
      </li>
    </ul>
  </div>
</div>

PS:以上代码来自w3schools。我刚刚修改了它。

最佳答案

尝试这个希望这个解决方案有所帮助。

 .modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

/* Modal Content */
.modal-content {
    position: relative;
    background-color: #fefefe;
    margin: auto;
    padding: 0;
    border: 1px solid #888;
    width: 50%;
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
    -webkit-animation-name: animatetop;
    -webkit-animation-duration: 0.4s;
    animation-name: animatetop;
    animation-duration: 0.4s
}

/* The Close Button */
.close {
    color: white;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}

.modal-header {
    padding: 2px 16px;
    background-color: #02ccba;
    color: white;
    text-align: center;
}

.modal-body {
    text-align: center;
    width:100%;
    /*margin-right: 30px;*/
}

.modal-list {
    width: 100%;
    text-align: center;

}

#modal-body-list {
    list-style-type: none;
    padding: 0;
}

.list-item {
    font-weight: bold;
}

#list-star {
  text-align: center;
}

#playAgainButton {
    -webkit-border-radius: 34;
    -moz-border-radius: 34;
    border-radius: 34px;
    font-family: Arial;
    color: #000000;
    font-size: 14px;
    font-weight: bold;
    padding: 5px 10px;
    margin: 20px 0;
    border: solid #02ccbb 4px;
    text-decoration: none;
    cursor: pointer;
}
<!-- Modal content -->
  <div class="modal-content">
    <div class="modal-header">
      <span class="close">&times;</span>
      <h2>CONGRATULATIONS!</h2>
    </div>
    <div class="modal-body">
      <ul id="modal-body-list">
        <li class="modal-list">
          <span class="list-item">Moves:</span>
          <span id="list-moves"></span>
        </li>
        <li class="modal-list">
          <span class="list-item">Time:</span>
          <span id="list-time"></span>
        </li>
        <li class="modal-list">
          <span class="list-item">Star:</span>
          <span id="list-star"></span>
        </li>
        <li class="modal-list">
          <button id="playAgainButton">Play Again</button>
        </li>
      </ul>

    </div>
  </div>
  

关于html - 如何将模式中的列表元素居中对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51051079/

相关文章:

javascript - 如何使用 jQuery 查找变量中无效的未封闭 XML 标签?

javascript - 由外部 DIV 控制的可滚动 DIV(用于触摸屏)。 jQuery HTML5

jquery - 如何在单击 div 后保持其悬停状态?

html - 审核复杂网页上未使用的 CSS

javascript - 如何 chop 非环绕 HTML 表格单元格中的多余文本并以 "..."结尾

javascript - 使用 JavaScript 读取元素的 CSS 属性

html - 垂直对齐中间带有固定标题和表格响应 Bootstrap

jquery - 如何使用CSS翻转div?

带有缓动和 stellar.js 的 javascript 水平视差

javascript - jQuery - 当滚动击中特定元素时自动输入焦点