为什么我的float right元素越来越多地出现在left中

时间:2019-02-07 21:55:08

标签: javascript html css bootstrap-4 positioning

我正在通过待办事项列表教程工作。在我尝试对其进行一些样式设置之前,它一直工作正常。我在带有“删除”类的按钮内有一个“超赞”垃圾桶图标。基本上,我取消了按钮的所有样式,因此只是垃圾图标。用户单击它,然后就可以删除待办事项列表项。我想从弹出的任务中正确对齐垃圾桶。这样做是可行的,但是每次我添加新任务时,垃圾桶都会越来越少地漂浮。

我将尝试仅发布相关代码,但是我不确定为什么会这样。我正在使用引导程序来帮助样式,并且加号圆形按钮似乎在创建任务时不会干扰它们。任何想法都很棒,谢谢大家!

Here is a pic

function get_todos() {
    var todos = new Array;
    var todos_str = localStorage.getItem('todo');
    if (todos_str !== null) {
        todos = JSON.parse(todos_str); 
    }
    return todos;
}
 
function add() {
    var task = document.getElementById('task').value;
 
    var todos = get_todos();
    todos.push(task);
    localStorage.setItem('todo', JSON.stringify(todos));
 
    show();
 
    return false;
}
   




//This is mostly where the remove button takes place 


function remove() {
    var id = this.getAttribute('id');
    var todos = get_todos();
    todos.splice(id, 1);
    localStorage.setItem('todo', JSON.stringify(todos));
 
    show();
 
    return false;
}
 
function show() {
    var todos = get_todos();
 
    var html = '<ul>';
    for(var i=0; i < todos.length; i++) {
        html += '<li>' + todos[i] + '<button class="remove" id="' + i  + '"><i class="fa fa-trash" aria-hidden="true"></i></button></li>';
    };
    html += '</ul>';
 
    document.getElementById('todos').innerHTML = html;
 
    var buttons = document.getElementsByClassName('remove');
    for (var i=0; i < buttons.length; i++) {
        buttons[i].addEventListener('click', remove);
    };
}
 
document.getElementById('add').addEventListener('click', add);
show();
 .remove { /*Trash can button*/
	color:#C0C0C0;
	font-size:24px;
	border:0;
	padding:0;
	background-color:transparent;
	float:right;

}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/css/bootstrap.min.css" integrity="sha384-AysaV+vQoT3kOAXZkl02PThvDr8HYKPZhNT5h/CXfBThSRXQ6jW5DO2ekP5ViFdi" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js" integrity="sha384-3ceskX3iaEnIogmQchP8opvBy3Mi7Ce34nWjpBIwVTHfGYWQS9jwHDVRnpKKHJg7" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.3.7/js/tether.min.js" integrity="sha384-XTs3FgkjiBgo8qjEjBk0tGmf3wPrWtA6coPfQDfFEY8AnYJwjalXCiosYRBIBZX8" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.5/js/bootstrap.min.js" integrity="sha384-BLiI7JTZm+JWlgKa0M0kGRpJbF2J8q+qreVrKBC47e3K6BW78kGLrCkeRX6I9RoK" crossorigin="anonymous"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">


<div class="container" style="margin-top:200px;">
		<div class="row justify-content-center">
			<div class="col-sm-12 col-lg-8">
				<h1 style="padding-bottom:20px;">Test</h1>
				<div class="form-group">
					<input type="text" class="form-control" id="task">
				</div>
			</div>
		</div>
		<div class="row justify-content-center" style="margin-top:20px;">
			<div class="col-sm-12 col-sm-8">
				<div id="todos"></div>
			</div> 
		</div>
		<div class="row">
			<div class="col"></div>
			<div class="col-xs-2" style="margin-right:15px; margin-top:30px;">
				<button id="add" style="border-radius:50%; font-size:35px; width:65px;" class="btn btn-danger">+</button>
			</div>
		</div>
	</div>

here is the fiddle

2 个答案:

答案 0 :(得分:1)

这是因为上方的浮动元素妨碍了操作。一种避免这种情况的方法是将clear: rightclear: both添加到浮动元素的样式中。

.remove { /*Trash can button*/
    color:#C0C0C0; 
    font-size:24px;
    border:0;
    padding:0;
    background-color:transparent;
    float:right;
    clear:right;
}

答案 1 :(得分:0)

我在CSS上玩耍,发现以下CSS样式起作用。更正,请参见以下更改。它将使列表看起来更好。

font-size: 20px; 元素上

添加

font-size: 24px;
float:right;

打开 。删除 课程

删除

position: absolute;
right: 20px;

添加

{{1}}