页边距不会移动单个元素

时间:2019-03-25 16:17:29

标签: html css margin

我正在尝试使一个与名称框水平的注释框,因为文本框固定在顶部。当我尝试在文本区域和窗体上应用margin-top时,它会将名称框和整个配置文件向下移动。有没有一种方法可以向下移动文本区域,而不影响其他任何内容。这是我的代码:

body {
	margin: 0;
}
p.name {
	font-family: "Roboto";
	margin-top: 90px;
	margin-left: 50px;
	margin-bottom: 0;
	border: 1px solid black;
	display: inline-block;
	padding: 20px 70px;
	font-size: 25px;
}
div.info {
	margin-left: 50px;
	border: 1px solid black;
	padding: 20px 13.5px 20px;
	border-top: none;
	display: inline-block;
	margin-bottom: 0;
}
div.date {
	margin-left: 50px;
	border: 1px solid black;
	border-top: none;
	display: inline-block;
	padding: 0px 17px 0px;
	text-align: center;
}
form {
	display: inline-block;
	margin-left: 100px;
}
textarea {
  width: 300px;
  height: 150px;
  display: inline-block;
  padding: 12px 20px;
  box-sizing: border-box;
  border: 2px solid #ccc;
  border-radius: 4px;
  background-color: #f8f8f8;
  font-size: 16px;
  resize: none;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="profile.css">
<link href='https://fonts.googleapis.com/css?family=Roboto' rel='stylesheet'>
</head>
<body>
<p class="name">henWrek</p>
<form>
<label for="fname">Profile Comments:</label>
<br>
<textarea>Enter your comment here...</textarea>
</form>
<br>
<div class="info">
<img src="default.jpg" height="200" width="210">
</div>
<br>
<div class="date">
<p>Joined: 11/1/2018</p>
<p>Last Online: 11/9/2018 3:21PM</p>
<p>Post Count: 2</p>
</div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

我已经看过您的代码并找到了问题,我的建议是在您的.textarea类中添加两个属性,根据您的要求分别为position:relativetop

textarea{
   position:relative;
   top:100px;
}

答案 1 :(得分:0)

另一种选择是在标签标签上放置一个位置,并且该标签具有相对位置,您可以根据需要更改顶部和右侧移动。

body {
	margin: 0;
}
p.name {
	font-family: "Roboto";
	margin-top: 90px;
	margin-left: 50px;
	margin-bottom: 0;
	border: 1px solid black;
	display: inline-block;
	padding: 20px 70px;
	font-size: 25px;
}
  
div.info {
	margin-left: 50px;
	border: 1px solid black;
	padding: 20px 13.5px 20px;
	border-top: none;
	display: inline-block;
	margin-bottom: 0;
}
label{
    position:relative;
    top:50px;
    right:100px;
}
div.date {
	margin-left: 50px;
	border: 1px solid black;
	border-top: none;
	display: inline-block;
	padding: 0px 17px 0px;
	text-align: center;
}
form {
	display: inline-block;
	margin-left: 100px;
}
textarea {
  width: 300px;
  height: 150px;
  display: inline-block;
  padding: 12px 20px;
  box-sizing: border-box;
  border: 2px solid #ccc;
  border-radius: 4px;
  background-color: #f8f8f8;
  font-size: 16px;
  resize: none;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="profile.css">
<link href='https://fonts.googleapis.com/css?family=Roboto' rel='stylesheet'>
</head>
<body>
<p class="name">henWrek</p>
<form>
<label class="label-prueba" for="fname"> Profile <br> Comments:</label>
<br>
<textarea>Enter your comment here...</textarea>
</form>
<br>
<div class="info">
<img src="default.jpg" height="200" width="210">
</div>
<br>
<div class="date">
<p>Joined: 11/1/2018</p>
<p>Last Online: 11/9/2018 3:21PM</p>
<p>Post Count: 2</p>
</div>
</body>
</html>