css HTML按钮在div标签中未居中对齐[重复]

jchrr9hc  于 2023-06-07  发布在  其他
关注(0)|答案(4)|浏览(223)

此问题已在此处有答案

How can I horizontally center an element?(134回答)
How to center a button within a div?(13个回答)
昨天关门了。
我正在为学校创建一个网站,我被一个我到处寻找解决方案的问题卡住了。我非常了解HTML和CSS,所以我希望我没有问一些愚蠢的事情。我有一个行div标签与2 div标签内的2列,因为这个网页是有点分裂2。在右边的一栏,我有一个按钮,不会对齐到右栏的中心。对于该列中的文本,使用display将其居中对齐:flex和justify-content:中心;但按钮就是不动。我可以只使用边距,但当我调整页面大小时,按钮保持不变。
下面是代码:

@import url("https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600;700&display=swap");

/* ---- Set up site grid ---- */

@media (max-width: 600px) {
  .left,
  .right {
    width: 100%;
  }
}

* {
  margin: 0;
  padding: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

body {
  background: gray;
  font-family: "Montserrat", sans-serif;
}

main {
  grid-area: main;
  background: #272727;
}

footer {
  grid-area: footer;
  background-color: black;
  text-align: right;
  color: white;
}

h1.titleH1 {
  font-family: 'Montserrat', sans-serif;
  font-size: 2.3vw;
  padding-left: 150px;
  margin: auto;
  color: white;
  /* -- Transform to vertically center heading */
  position: relative;
  top: 50%;
  transform: translateY(-50%);
  -webkit-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
}

img.roundleft {
  border-top-left-radius: 10px;
  border-bottom-left-radius: 10px;
  box-shadow: 1px 0px 10px #000;
  float: left;
}

/*--- Navigation Bar ---*/

.wrapper {
  display: grid;
  grid-template-areas: 'headerLeft headerRight';
  grid-template-columns: 600px 1fr;
}

.headerLeft {
  text-align: left;
  background-color: black;
}

.headerRight {
  text-align: right;
  background: linear-gradient(to left, #55524e, black);
}

.navMenu {
  margin-top: 30px;
  margin-right: 20px;
}

.navMenu a {
  color: #f6f4e6;
  text-decoration: none;
  font-size: 1vw;
  text-transform: uppercase;
  font-weight: 500;
  margin-left: 50px;
  margin-right: 50px;
  -webkit-transition: all 0.2s ease-in-out;
  transition: all 0.2s ease-i n-out;
}

.navMenu a:hover {
  color: #fddb3a;
}

/*---- Main Pages (Rows and Columns) ----*/

.row {
  display: flex;
}

.column {
  height: 800px;
}

.left {
  background-color: black;
  width: 50%;
  float: left;
}

.right {
  background: linear-gradient(to left, #55524e, #171716);
  width: 50%;
  float: left;
}

.homeImage {
  max-width: 100%;
  height: 800px;
}

h1.mainH1 {
  color: white;
  font-size: 2.5vw;
  display: flex;
  justify-content: center;
  margin-top: 1em;
}

h2.mainH2 {
  color: white;
  font-size: 2vw;
  font-family: "Montserrat", sans-serif;
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.listItems {
  color: white;
  font-size: 1vw;
  font-family: "Montserrat", sans-serif;
  display: flex;
  justify-content: center;
}

.btnQuiz {
  font-family: "Montserrat", sans-serif;
  background-color: #ffe31c;
  border-radius: 10px;
  border: none;
  color: black;
  padding: 11px 28px;
  text-align: center;
  text-decoration: none;
  font-size: 16px;
  display: flex;
  justify-content: center;
}
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<main>
  <div class="row">

    <div class="column left">
      <!-- Left -->

      <img class="homeImage" src="images/barbell.jpg" alt="Man lifting barbell">
    </div>

    <div class="column right">
      <!-- Right -->
      <h1 class="mainH1">Welcome to the Fitness project</h1>

      <h2 class="mainH2">Here you can:<br></h2>
      <br>
      <ul>
        <li class="listItems">Learn the fundementals to fitness as well as diet</li>
        <br>
        <li class="listItems">Take a quiz to optimise the information for your goals</li>
        <br>
        <li class="listItems">Expand your knowledge with usefull body building tips</li>
      </ul>

      <button class="btnQuiz">Take Quiz</button>

    </div>

  </div>

</main>

我很抱歉,有很多CSS与此页面无关。但我希望你能理解我的问题,任何解决方案肯定是赞赏。

zi8p0yeb

zi8p0yeb1#

你可以用它来居中一个按钮。

.center {
  margin: 0;
  position: absolute;
  top: 50%;
  left: 50%;
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}
<div class="center">
  <button>Centered Button</button>
</div>
bt1cpqcv

bt1cpqcv2#

**Please try this code. I hope you can get right solution.**
**Change button css**
.btnQuiz{
 font-family: "Montserrat", sans-serif;
  background-color: #ffe31c;
  border-radius: 10px;
  border: none;
  color: black;
  text-align: center;
  text-decoration: none;
  font-size: 16px;
  display: flex;
}

**To**
.btnQuiz{
  
 font-family: "Montserrat", sans-serif;
  background-color: #ffe31c;
  border-radius: 10px;
  border: none;
  color: black;
  text-align: center;
  text-decoration: none;
  font-size: 16px;
  display: flex;
  margin: 0;
  position: absolute;
  top: 50%;
  left: 75%;
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}

 
 
     @import url("https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600;700&display=swap");
    /* ---- Set up site grid ---- */

    @media (max-width: 600px) {
      .left, .right{
        width: 100%;
      }
    }

    * {
      margin: 0;
      padding: 0;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
    }


    body {
      background: gray;
      font-family: "Montserrat", sans-serif;
    }





    main{
        grid-area: main;
        background: #272727;
        
        
        
    }


    footer{
        grid-area: footer;
        background-color: black;
        text-align: right;
        color: white;
    }



    h1.titleH1 {
        font-family: 'Montserrat', sans-serif;  
        font-size: 2.3vw;
        padding-left: 150px;
        margin: auto;
        color: white;
            
        /* -- Transform to vertically center heading */
        position: relative;
        top: 50%;
        transform: translateY(-50%);
        -webkit-transform: translateY(-50%);
        -ms-transform: translateY(-50%);
        
        }

    img.roundleft{
        border-top-left-radius: 10px;
        border-bottom-left-radius: 10px;
        box-shadow: 1px 0px 10px #000;
        float: left;
    }



    /*--- Navigation Bar ---*/

    .wrapper{
      display: grid;
      grid-template-areas:
        'headerLeft headerRight';

      grid-template-columns: 600px 1fr;

      
              
    }

    .headerLeft{
      text-align: left;
      background-color: black;
    }

    .headerRight{
      text-align: right;
      background: linear-gradient(to left,#55524e, black);
    }

    .navMenu {
      margin-top: 30px;
      margin-right: 20px;
       
      
    }

    .navMenu a {
      color: #f6f4e6;
      text-decoration: none;
      font-size: 1vw;
      text-transform: uppercase;
      font-weight: 500;
      margin-left: 50px;
      margin-right: 50px;
      -webkit-transition: all 0.2s ease-in-out;
      transition: all 0.2s ease-i n-out;
    }

    .navMenu a:hover {
      color: #fddb3a;
    }


    /*---- Main Pages (Rows and Columns) ----*/

    .row{
      display: flex;

    }

    .column
    {
      height: 800px;
    }

    .left
    {
      background-color: black;
      width: 50%;
      float: left; 
      
    }

    .right{
      background: linear-gradient(to left,#55524e, #171716);
      width: 50%;
      float: left;
      
    }

    .homeImage{
      max-width: 100%;
      height: 800px;
     

    }

    h1.mainH1{
      color: white;
      font-size: 2.5vw;
      display: flex;
      justify-content: center;

      margin-top: 1em;

      

    }

    h2.mainH2{
      color: white;
      font-size: 2vw;
      font-family: "Montserrat", sans-serif;
      display: flex;
      justify-content: center;
      margin-top: 20px;

    }

    .listItems{
      color: white;
      font-size: 1vw;
      font-family: "Montserrat", sans-serif;
      display: flex;
      justify-content: center;
      

    }

    .btnQuiz{
      
     font-family: "Montserrat", sans-serif;
      background-color: #ffe31c;
      border-radius: 10px;
      border: none;
      color: black;
      text-align: center;
      text-decoration: none;
      font-size: 16px;
      display: flex;
      
     
       margin: 0;
      position: absolute;
      top: 50%;
      left: 75%;
      -ms-transform: translate(-50%, -50%);
      transform: translate(-50%, -50%);
    }
     <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>

    <main>
        <div class="row">

            <div class="column left"> <!-- Left -->
                
                <img class="homeImage" src="images/barbell.jpg" alt="Man lifting barbell"> 
            </div>

            <div class="column right"> <!-- Right -->
                <h1 class="mainH1">Welcome to the Fitness project</h1>
                    
                    <h2 class="mainH2">Here you can:<br></h2>
                    <br>
                    <ul>
                        <li class="listItems">Learn the fundementals to fitness as well as diet</li>
                        <br>
                        <li class="listItems">Take a quiz to optimise the information for your goals</li>
                        <br>
                        <li class="listItems">Expand your knowledge with usefull body building tips</li>
                    </ul>

                <button class="btnQuiz">Take Quiz</button>

                
                

            </div>

        </div>

        
    </main>



**Any doubt please comment to me.**
vxf3dgd4

vxf3dgd43#

有两种简单的方法可以做到这一点:

方法一:

将以下css属性赋予列右类名:

.right {
  display: flex;
  justify-content: center;
  align-items: center;
}

Above将把div中所有的内容都放在classname为'column right'的中心

  • 方法#2*

如果你只想让button居中,其他元素保持原样,那么用一个div容器 Package button,如下所示:

<div class='btn-wrapper'>
  <button class="btnQuiz">Take Quiz</button>
</div>

然后给予这个css给你的css中的btn-wrapper:

.btn-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
}
pexxcrt2

pexxcrt24#

试试用

<center> <!-- button code here --> </center>

它是硬编码的,但工作得很好。

相关问题