Mark Alexa Mark Alexa - 1 month ago 9
CSS Question

Unnecessary margin above input box

Working on project and for some mysterious reason I'm getting margin above all my input boxes.



.header {
height: 100px; width: 100%;
background-color: #ef5fc2;
position: relative; color: white;
}

.header p {
position: relative; top: 25px;
margin-top: 0;
}

body {
font-family: 'Itim', cursive;
font-size: 40px;
margin: 0px; padding: 0px;
text-align: center;
background-color: pink;
}

body form p {
font-size: 18px;
}

.container {
height: 700px;
}

.footer {
background-color: red;
color: white;
height: 70px;
width: 100%;
}

.footer p {
font-size: 20px;
padding: 0px;
margin: 0px;
}

.footer p:first-child {
padding-top: 10px;
}

.container form p {
margin-bottom: 0;
padding: 0;
}

form input {
margin-top: 0;
padding: 0;
}

form {
position: relative;
top: 30px;
}

#submitbtn {
position: relative;
border: 5px solid #ff3385;
border-radius: 6px;
background-color: #ff3385;
padding: 5px 10px;
text-decoration: none;
color: white;
}

.submitContainer {
height: 50px;
margin-bottom: 20px;
}

#agreement {
font-size: 20px;
width: 320px;
top: 300px;
position: absolute;
left: 120px;
text-align: left;
border: 2px solid red;
padding: 10px 20px;
}

<form>
<p>Your first name</p>
<input type="text" name="first name" value="">
<p>Your last name</p>
<input type="text" name="last name" value="">
<p>Your address (inc. zip/post code)</p>
<input id="userAddress" class="addressBox" type="text" name="address box" value="">
<p>Your date of birth</p>
<input type="text" name="date of birth" value="">
<p>Your email</p>
<input type="email" name="email" value="">
<p>Your cell phone number</p>
<input id="marginBottom" type="number" name="phone number" value="">
</form>





When I peeked into developer's tools in Chrome and took a closer look at margin and padding highlighting tool in that place where I have extra margin it doesn't show anything useful. It just jumps from the
p
tag straight to
input
tag without acknowledging the unnecessary gap. It does acknowledge the margin above the
p
tags but those don't bug me.

Any ideas ?

Answer

That gap was caused by the body's font-size: 40px, now changed to 16px

I also changed the p to label and gave them display: block so they break line.

Now you can adjust the gaps using margin's on the input's and label's

As mentioned in a comment, there should be no spaces in the name attributes so I removed them

.header {
  height: 100px;
  width: 100%;
  background-color: #ef5fc2;
  position: relative;
  color: white;
}
.header p {
  position: relative;
  top: 25px;
  margin-top: 0;
}
body {
  font-family: 'Itim', cursive;
  font-size: 16px;
  margin: 0px;
  padding: 0px;
  text-align: center;
  background-color: pink;
}
body form label {
  font-size: 18px;
  display: block;
}
.container {
  height: 700px;
}
.footer {
  background-color: red;
  color: white;
  height: 70px;
  width: 100%;
}
.footer p {
  font-size: 20px;
  padding: 0px;
  margin: 0px;
}
.footer p:first-child {
  padding-top: 10px;
}
.container form a {
  margin-bottom: 0;
  padding: 0;
}
form input {
  margin: 0 0 10px;
  padding: 0;
}
form {
  position: relative;
  top: 30px;
}
#submitbtn {
  position: relative;
  border: 5px solid #ff3385;
  border-radius: 6px;
  background-color: #ff3385;
  padding: 5px 10px;
  text-decoration: none;
  color: white;
}
.submitContainer {
  height: 50px;
  margin-bottom: 20px;
}
#agreement {
  font-size: 20px;
  width: 320px;
  top: 300px;
  position: absolute;
  left: 120px;
  text-align: left;
  border: 2px solid red;
  padding: 10px 20px;
}
<form>
  <label>Your first name</label>
  <input type="text" name="firstname" value="">
  <label>Your last name</label>
  <input type="text" name="lastname" value="">
  <label>Your address (inc. zip/post code)</label>
  <input id="userAddress" class="addressBox" type="text" name="addressbox" value="">
  <label>Your date of birth</label>
  <input type="text" name="dateofbirth" value="">
  <label>Your email</label>
  <input type="email" name="email" value="">
  <label>Your cell phone number</label>
  <input id="marginBottom" type="number" name="phonenumber" value="">

</form>

Comments