【问题标题】:Can't position button element无法定位按钮元素
【发布时间】:2022-02-12 18:34:40
【问题描述】:

我使用引导程序创建了一个提交按钮,但我想将其放置在箭头指向的位置,但无论我写什么,位置都不会改变,就像按钮粘在位置上,我无法重新定位它 我怎样才能把它放在那里?

.container {
  position: absolute;
  top: 30%;
  left: 30%;
  transform: translate(-50%, -50%);
  text-align: center;
  /*give the text bolder font*/
  font-weight: bold;
}
/*set the input under the span element*/
input[type=text] {
  text-align: center;
  font-weight: bold;
  font-size: 20px;
  border: 2px solid #ccc;
  border-radius: 4px;
  padding: 12px 10px;
  width: 100%;
  box-sizing: border-box;
  margin-top: 6px;
  margin-bottom: 16px;
  /*set the space from left to 30%*/
  margin-left: 30%;
}
/* set the button under the input element*/
button {
  background-color: #4CAF50;
  color: white;
  padding: 12px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  /*set the space from left to 30%*/
  margin-left: 30%;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>quiz</title>
    <link rel="stylesheet" href="quiz2.css">

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

</head>
<body>
  <div class="container">
    <span id="question">What type of file do you need to open javascript file ?</span>
    <div class="input-group flex-nowrap">
      <input type="text" id="answer" class="form-control" placeholder="Answer Here..." aria-label="Username" aria-describedby="addon-wrapping">
    </div>
    <button type="button" id="submit" class="btn btn-outline-success">Submit</button>
  </div>

【问题讨论】:

  • 我刚刚用完整代码更新了帖子

标签: html css flexbox css-position


【解决方案1】:

如果您能够更改 HTML,这里有一个方法:

将您的按钮包裹在另一个div中:

<div class="button-wrapper">
   <button type="button" id="submit" class="btn btn-outline-success">Submit</button>
</div>

然后在该包装上设置margin-left

.button-wrapper {
  /*set the space from left to 30%*/
  margin-left: 30%;
}

检查这个 sn-p 的结果。

.container {
  position: absolute;
  top: 30%;
  left: 30%;
  transform: translate(-50%, -50%);
  /*give the text bolder font*/
  font-weight: bold;
}


/*set the input under the span element*/

input[type=text] {
  text-align: center;
  font-weight: bold;
  font-size: 20px;
  border: 2px solid #ccc;
  border-radius: 4px;
  padding: 12px 10px;
  width: 100%;
  box-sizing: border-box;
  margin-top: 6px;
  margin-bottom: 16px;
  /*set the space from left to 30%*/
  margin-left: 30%;
}


/* set the button under the input element*/

button {
  background-color: #4CAF50;
  color: white;
  padding: 12px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.button-wrapper {
  /*set the space from left to 30%*/
  margin-left: 30%;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>quiz</title>
  <link rel="stylesheet" href="quiz2.css">

  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

</head>

<body>
  <div class="container">
    <span id="question"></span>
    <div class="input-group flex-nowrap">
      <input type="text" id="answer" class="form-control" placeholder="Answer Here..." aria-label="Username" aria-describedby="addon-wrapping">
    </div>
    <div class="button-wrapper">
      <button type="button" id="submit" class="btn btn-outline-success">Submit</button>
    </div>
  </div>

【讨论】:

  • 很抱歉,哪些文字正在消失?
  • 没关系,我修好了。谢谢
【解决方案2】:

我只是定义了一个 button-container 类并使用 flex & justify:

.container {
  position: absolute;
  top: 30%;
  left: 30%;
  transform: translate(-50%, -50%);
  text-align: center;
  /*give the text bolder font*/
  font-weight: bold;
}


/*set the input under the span element*/

input[type=text] {
  text-align: center;
  font-weight: bold;
  font-size: 20px;
  border: 2px solid #ccc;
  border-radius: 4px;
  padding: 12px 10px;
  width: 100%;
  box-sizing: border-box;
  margin-top: 6px;
  margin-bottom: 16px;
  /*set the space from left to 30%*/
  margin-left: 30%;
}


/* set the button under the input element*/

button {
  background-color: #4CAF50;
  color: white;
  padding: 12px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  /*set the space from left to 30%*/
  margin-left: 30%;
}

.button-container {
  display: flex;
  justify-content: start;
  margin-left: 30%;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>quiz</title>
  <link rel="stylesheet" href="quiz2.css">

  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>

</head>

<body>
  <div class="container">
    <span id="question">What type of file do you need to open javascript file ?</span>
    <div class="input-group flex-nowrap">
      <input type="text" id="answer" class="form-control" placeholder="Answer Here..." aria-label="Username" aria-describedby="addon-wrapping">
    </div>
    <div class="button-container">
      <button type="button" id="submit" class="btn btn-outline-success">Submit</button>
    </div>
  </div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-11
    相关资源
    最近更新 更多