【问题标题】:CSS button will not align at center of divCSS 按钮不会在 div 的中心对齐
【发布时间】:2019-03-25 06:51:17
【问题描述】:

我有一个展示div 的网站,在div 内我有一个“开始”button。我能够将buttonmargin 属性垂直对齐。但是我不想将buttonmargin 水平对齐,因为这会在未来给我带来一些麻烦,我尝试过align: center;align="center"button 粘在左侧的陈列柜。如何在不使用 margin 属性的情况下取消粘贴此 button 并水平对齐它?

html,
body,
header {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  margin: 0;
  margin-top: 0;
  margin: auto;
  padding: 0;
}

h1 {
  display: inline-block;
  font-family: Trebuchet MS;
  font-size: 75px;
  letter-spacing: 3px;
  margin: 10px 0px 0px 20px;
}

h2 {
  display: inline-block;
  flex-direction: row;
  margin: 0px 0px 0px 50px;
  font-family: Georgia;
}

.highlight {
  color: #45d845;
}

.heading {
  background-color: #d84545;
  border-bottom: 5px solid black;
  padding-top: 20px;
  padding-bottom: 20px;
}

#comet {
  font-size: 65px;
  margin-left: 20px;
}

.showcase {
  background: url('background1.jpeg');
  border-bottom: 5px solid black;
  height: 1000px;
  width: 100%;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  color: #cccccc;
}

.showcase h2 {
  color: #fff;
  margin-top: 170px;
  font-size: 60px;
  font-family: Verdana;
  text-align: center;
  text-shadow: 1px 3px #000;
}

#start {
  align: center;
  margin-top: 130px;
  background-color: transparent;
  color: #fff;
  padding: 20px 30px 20px 30px;
  text-align: center;
  font-family: Helvetica;
  font-weight: bold;
  border-radius: 10px;
  -webkit-transition-duration: 0.4s;
  transition-duration: 0.4s;
}

#start:hover {
  cursor: pointer;
  background-color: #fff;
  color: #000;
}
<header>
  <div class="heading">
    <h1>ThumbTac <span id="comet">&#9732;</span> </h1>
  </div>
</header>
<div class="showcase">
  <h2>He moonlight difficult engrossed an it sportsmen. Interested has all devonshire difficulty jay assistance joy. Unaffected at ye </h2>
  <button id="start" align="center">Get Started</button>
</div>

【问题讨论】:

    标签: html css layout margin


    【解决方案1】:

    您可以考虑使用flexbox。 CSS 源代码中的文档。

    /* Global Sets */
    
    html,
    body,
    header {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      margin: 0;
      margin-top: 0;
      margin: auto;
      padding: 0;
    }
    
    
    /*Heading*/
    
    h1 {
      display: inline-block;
      font-family: Trebuchet MS;
      font-size: 75px;
      letter-spacing: 3px;
      margin: 10px 0px 0px 20px;
    }
    
    h2 {
      display: inline-block;
      flex-direction: row;
      margin: 0px 0px 0px 50px;
      font-family: Georgia;
    }
    
    .highlight {
      color: #45d845;
    }
    
    .heading {
      background-color: #d84545;
      border-bottom: 5px solid black;
      padding-top: 20px;
      padding-bottom: 20px;
    }
    
    #comet {
      font-size: 65px;
      margin-left: 20px;
    }
    
    
    /*Showcase*/
    
    .showcase {
      background: url('background1.jpeg');
      border-bottom: 5px solid black;
      height: 1000px;
      width: 100%;
      background-size: 100% 100%;
      background-repeat: no-repeat;
      color: #cccccc;
      display: flex; /* Added */
      flex-direction: column; /* Added */
      align-items: center; /* Added, horizonal alignment */
    }
    
    .showcase h2 {
      color: #fff;
      margin-top: 170px;
      font-size: 60px;
      font-family: Verndana;
      text-shadow: 1px 3px #000;
      text-align: center;
    }
    
    #start {
      /* align: center; Not valid CSS */
      margin-top: 130px;
      background-color: transparent;
      color: #fff;
      padding: 20px 30px 20px 30px;
      /* text-align: center; No longer required */
      font-family: Helvetica;
      font-weight: bold;
      border-radius: 10px;
      -webkit-transition-duration: 0.4s;
      /* Safari */
      transition-duration: 0.4s;
    }
    
    #start:hover {
      cursor: pointer;
      background-color: #fff;
      color: #000;
    }
    <header>
      <div class="heading">
        <h1>ThumbTac <span id="comet">&#9732;</span> </h1>
      </div>
    </header>
    <div class="showcase">
      <h2>He moonlight difficult engrossed an it sportsmen. Interested has all devonshire difficulty jay assistance joy. Unaffected at ye </h2>
      <button id="start" align="center">Get Started</button>
    </div>

    【讨论】:

      【解决方案2】:

      您可以将button 包裹在div 中,样式为text-align: center;,以水平居中button。示例:

      html, body, header{
          position:absolute;
              left:0;
              top:0;
              width: 100%;
          margin: 0;
          margin-top: 0;
              margin: auto;
          padding: 0;
      }
      
      
      /*Heading*/
      h1{
          display: inline-block;
          font-family: Trebuchet MS;
          font-size: 75px;
          letter-spacing: 3px;
          margin: 10px 0px 0px 20px;
      }
      
      h2{
      
          display: inline-block;
          flex-direction: row;
          margin: 0px 0px 0px 50px;
          font-family: Georgia;
      }
      
      
      .highlight{
          color: #45d845;
      }
      .heading{
          background-color: #d84545;
          border-bottom: 5px solid black;
          padding-top: 20px;
          padding-bottom: 20px;
      }
      
      #comet{
          font-size: 65px;
          margin-left: 20px;
      }
      
      /*Showcase*/
      
      .showcase{
          background: url('background1.jpeg');
          border-bottom: 5px solid black;
          height: 1000px;
          width: 100%;
              background-size: 100% 100%;
          background-repeat: no-repeat;
          color: #cccccc;
      }
      
      .showcase h2{
          color: #fff;
          margin-top: 170px;
          font-size: 60px;
          font-family: Verndana;
          text-align: center;
          text-shadow: 1px 3px #000;
      }
      
      
      
       #start{
          align: center;
          margin-top: 130px;
          background-color: transparent;
          color: #fff;
          padding: 20px 30px 20px 30px;
          text-align: center;
          font-family: Helvetica;
          font-weight: bold;
          border-radius: 10px;
      
          -webkit-transition-duration: 0.4s; /* Safari */
              transition-duration: 0.4s;
      
      }
      
      #start:hover{
          cursor: pointer;
          background-color: #fff;
          color: #000;
      }
      <header>
      <div class="heading">
          <h1>ThumbTac <span id="comet">&#9732;</span> </h1>
      
      
      
      </div>
      </header>
      
      <div class="showcase">
      <h2>He moonlight difficult engrossed an it sportsmen. 
      Interested has all devonshire difficulty jay 
      assistance joy. Unaffected at ye </h2>
      
      <div style="text-align: center;">
      <button id="start">Get Started</button>
      </div>
      
      </div>

      【讨论】:

      • 干净简洁,谢谢!
      • 如果我的回答解决了您的问题,请考虑点击灰色复选标记接受它,谢谢!
      猜你喜欢
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 2015-05-16
      • 2020-08-22
      • 2022-10-01
      • 2019-08-07
      • 2011-08-14
      相关资源
      最近更新 更多