首页 > 解决方案 > CSS 按钮不会在 div 的中心对齐

问题描述

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

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>

标签: htmlcsslayoutmargin

解决方案


您可以将您的样式包裹button在 a中以水平居中您的. 例子:divtext-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>


推荐阅读