html - 文本和按钮之间的间距相等

标签 html css

<分区>


想改进这个问题吗? 通过 editing this post 添加细节并澄清问题.

关闭 5 年前

我希望文本和按钮之间的间距相等,但间距不相等。看我附上的图片;红色的“删除”按钮和之前的文本有不相等的空格。

是否有我可以使用的任何 CSS 属性,以便文本不会将按钮推到右侧?

enter image description here

function todo() {
  var ParentDiv = document.getElementById('mydiv');
  var newPara = document.createElement('p');
  var value = document.getElementById('input-box').value;
  var newText = document.createTextNode(value);
  var del = document.createElement('input');
  randomId = Math.random();
  // newPara.setAttribute("id",randomId);
  newPara.setAttribute("class", "para");
  del.setAttribute("type", "button");
  del.setAttribute("value", "delete");
  del.setAttribute("onclick", "delt(this)");
  del.setAttribute("class", "delbtn");
  newPara.appendChild(newText);
  newPara.appendChild(del);
  ParentDiv.appendChild(newPara);
}

// function delt(me) {
//     var parentId = me.parentElement.getAttribute("id");
//     //var x = me.parentElement.nodeName;
//     //console.log(parentId);  
//     document.getElementById(parentId).remove();
// }
function delt(me) {
  var parent = me.parentNode;
  var grandParent = parent.parentNode;
  grandParent.removeChild(parent);
  //console.log(grandParent);

}

//delt();
body {
  margin-left: 25rem;
  margin-top: 5rem;
  margin-right: auto;
}

h1 {
  margin-left: 4rem;
  margin-right: auto;
}

#input-box {
  font-size: 1.5em;
  width: 50%;
  margin-top: 5px;
  padding: 5px;
  border: none;
  border-bottom: 2px solid blue;
}

#mybtn {
  background-color: rgb(21, 170, 240);
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 10px 0 0 75px;
  cursor: pointer;
}

.delbtn {
  background-color: rgb(238, 53, 28);
  border: none;
  color: white;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 15px;
  margin-top: 5px;
  margin-left: 10px;
  cursor: pointer;
}

.para {
  text-align: left;
  padding: 0;
  margin: 0;
}
<h1>TODO APP</h1>
<input type="text" name="" id="input-box" maxlength="25">
<br><button id="mybtn" onclick="todo();">Add Item</button>
<div id="mydiv"></div>

View on GitHub

最佳答案

你可以使用 <table>而不是 <div>并附加 p在左栏中,按钮在右栏中,因此按钮始终位于彼此之上:

function todo() {
  var ParentDiv = document.getElementById('mytable');
  var newRow = document.createElement('tr');
  var leftCell = document.createElement('td');
  var rightCell = document.createElement('td');
  var newPara = document.createElement('p');
  var value = document.getElementById('input-box').value;
  var newText = document.createTextNode(value);
  var del = document.createElement('input');
  randomId = Math.random();
  // newPara.setAttribute("id",randomId);
  newPara.setAttribute("class", "para");
  del.setAttribute("type", "button");
  del.setAttribute("value", "delete");
  del.setAttribute("onclick", "delt(this)");
  del.setAttribute("class", "delbtn");
  newPara.appendChild(newText);
  rightCell.appendChild(del);
  leftCell.appendChild(newPara);
  newRow.appendChild(leftCell);
  newRow.appendChild(rightCell);
  ParentDiv.appendChild(newRow);
}

// function delt(me) {
//     var parentId = me.parentElement.getAttribute("id");
//     //var x = me.parentElement.nodeName;
//     //console.log(parentId);  
//     document.getElementById(parentId).remove();
// }
function delt(me) {
  var parent = me.parentNode;
  var grandParent = parent.parentNode;
  grandParent.removeChild(parent);
  //console.log(grandParent);

}

//delt();
body {
  margin-left: 25rem;
  margin-top: 5rem;
  margin-right: auto;
}

h1 {
  margin-left: 4rem;
  margin-right: auto;
}

#input-box {
  font-size: 1.5em;
  width: 50%;
  margin-top: 5px;
  padding: 5px;
  border: none;
  border-bottom: 2px solid blue;
}

#mybtn {
  background-color: rgb(21, 170, 240);
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 10px 0 0 75px;
  cursor: pointer;
}

.delbtn {
  background-color: rgb(238, 53, 28);
  border: none;
  color: white;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 15px;
  margin-top: 5px;
  margin-left: 10px;
  cursor: pointer;
}

.para {
  text-align: left;
  padding: 0;
  margin: 0;
}
<h1>TODO APP</h1>
<input type="text" name="" id="input-box" maxlength="25">
<br><button id="mybtn" onclick="todo();">Add Item</button>
<table id="mytable"></table>

关于html - 文本和按钮之间的间距相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49416128/

上一篇:php - 根据值更改 td 类?

下一篇:html - 定位在一个DIV里面

相关文章:

javascript - 将按钮的 Action 与其 div 的 Action 隔离

javascript - 如何在 Javascript 中格式化日期

javascript - 使用选定的表格行预填充选择下拉列表

javascript - SlickGrid中如何做多列表头(multi layer header)?

html - 如何处理在 ASP.NET MVC 中返回文件的 Controller 结果

css - 设置高度 100% 不工作

html - Twitter Bootstrap - Row Div 和 Span Div 1px 大小

html - 如何使用 CSS 和 <div> 让文本出现在 <td> 的底部?

html - 如何在div中隐藏滚动条

javascript - 谁能给我一个信息窗口内 div 元素的 jquery 动画示例代码?