Search code examples
javascripthtmlcssbootstrap-4positioning

Why are my float right elements coming in left more and more


I'm working my way through a todo list tutorial. It was working properly until I was trying to style it up a little bit. I have a Font Awesome trash can icon inside of a button with the Class "remove". Basically I took all the styles off the button so its just the trash icon. Users click it and there todo list item deletes, as it should. I wanted to right align the trash can from the task that pops up. It does that but everytime I add a new task the trash can floats right less and less.

Ill try to just post the relevant code but I'm not sure why this is happening. I'm using bootstrap to help style and that and the plus sign circular button don't seem to be interfering with the tasks as they are created. Any ideas would be awesome, thanks everyone!

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


Solution

  • It's because the floated element above it is in the way. One way to avoid this is to add clear: right or clear: both to the styles for the floated element.

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