I am new to HTML and CSS. I was experimenting with displaying <div>
-s side by side. I noticed that simply adding a div inside the body will display with unexpected margins.
I tried messing about with margin, border and padding properties in my css file but nothing seems to work. the div leaves a gap to every side of the page, please help, I don’t know how to get rid of it.
The HTML:
* {
box-sizing: border-box;
}
.box {
float: left;
width: 50%;
}
#box1 {
background-color: grey;
}
#box2 {
background-color: lightblue;
}
.clearfix::after {
content: "";
clear: both;
display: table;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="clearfix">
<div id="box1" class="box">
<p>Some text in box 1.</p>
</div>
<div id="box2" class="box">
<p>Some text in box 1.</p>
</div>
</div>
</body>
</html>
3
Answers
You can clear the margin and padding by adding styles to the
html
andbody
tags:Another common fix would be to use a CSS reset.
I updated your code snippet:
Another option would be to set the box
position
s toabsolute
(as in this answer)