英文:
Background Colour for DIV class
问题
我现在有一个DIV类,里面包含不同的元素。现在,我想给整个div
类(而不是container
,只是login
的div
)设置一个背景颜色,它会在顶部、底部、左侧和右侧(大约10px)留下一点空间,完全包围这个div
。然而,当我尝试给div
设置背景颜色时,它出现了这种情况:
元素的背景颜色到处都是。我不想移动元素到任何不同的位置。我只想背景颜色完美地包围元素,顶部、底部、左侧和右侧只留下一点空间(10px)。我应该如何实现这一点?
这是我的全部代码:
@import url('https://fonts.googleapis.com/css2?family=Lora&display=swap');
body {
background-color: #DBE2EF;
position: fixed;
}
.textHead {
font-family: Lora;
margin-left: 340px;
margin-top: 130px;
}
.logintxt {
font-family: Lora;
margin-left: 285px;
margin-top: 50px;
}
.loginemailtxt {
font-family: Lora;
border: 1px solid black;
width: 250px;
height: 30px;
margin-left: 285px;
margin-top: 40px;
outline: none;
border-radius: 5px;
}
.loginemailtxt::placeholder {
color: black;
padding-left: 3px;
}
.loginpwdtxt {
font-family: Lora;
border: 1px solid black;
width: 250px;
height: 30px;
margin-left: -256px;
margin-top: 130px;
outline: none;
border-radius: 5px;
position: fixed;
}
.loginpwdtxt::placeholder {
color: black;
padding-left: 3px;
}
.loginbtn {
position: fixed;
margin-top: 220px;
margin-left: -190px;
width: 120px;
height: 40px;
border-radius: 10px;
background-color: #3F72AF;
border: none;
cursor: pointer;
font-family: Lora;
}
<div class="container">
<div class="login">
<h2 class="textHead">MedConnect</h2>
<h2 class="logintxt">Log In to Your Account</h2>
<input type="text" class="loginemailtxt" name="email" placeholder="Email Address">
<input type="password" class="loginpwdtxt" name="password" placeholder="Password">
<button class="loginbtn" type="button" name="button">Log In</button>
<h5 style="margin-top: 230px; margin-left: 310px; font-family: Lora;">Don't have an Account? <u style="cursor: pointer;">Sign Up</u></h5>
<h6 style="margin-top: 40px; margin-left: 260px; font-family: Lora;">By clicking "Log In", you agree to our <u style="cursor: pointer;">Terms</u> and <u style="cursor: pointer;">Privacy Policy</u></h6>
</div>
</div>
英文:
I have a DIV class filled with different elements as of now. Now, I want to give that entire div
class (not the container
, just the login
div
) a background-colour, which just surrounds that div
with a little bit of space on the top, bottom, left and right (around 10px
). However, when I try giving the div
a background colour, it does this:
The background colour for the elements is all over the place. I don't want to move the elements in any different position. I just want the background colour to surround the elements perfectly, with just a little space on the top, bottom, left and right (of 10px
). How can I accomplish this?
This is all my code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
@import url('https://fonts.googleapis.com/css2?family=Lora&display=swap');
body {
background-color: #DBE2EF;
position: fixed;
}
.textHead {
font-family: Lora;
margin-left: 340px;
margin-top: 130px;
}
.logintxt {
font-family: Lora;
margin-left: 285px;
margin-top: 50px;
}
.loginemailtxt {
font-family: Lora;
border: 1px solid black;
width: 250px;
height: 30px;
margin-left: 285px;
margin-top: 40px;
outline: none;
border-radius: 5px;
}
.loginemailtxt::placeholder {
color: black;
padding-left: 3px;
}
.loginpwdtxt {
font-family: Lora;
border: 1px solid black;
width: 250px;
height: 30px;
margin-left: -256px;
margin-top: 130px;
outline: none;
border-radius: 5px;
position: fixed;
}
.loginpwdtxt::placeholder {
color: black;
padding-left: 3px;
}
.loginbtn {
position: fixed;
margin-top: 220px;
margin-left: -190px;
width: 120px;
height: 40px;
border-radius: 10px;
background-color: #3F72AF;
border: none;
cursor: pointer;
font-family: Lora;
}
<!-- language: lang-html -->
<div class="container">
<div class="login">
<h2 class="textHead">MedConnect</h2>
<h2 class="logintxt">Log In to Your Account</h2>
<input type="text" class="loginemailtxt" name="email" placeholder="Email Address">
<input type="password" class="loginpwdtxt" name="password" placeholder="Password">
<button class="loginbtn" type="button" name="button">Log In</button>
<h5 style="margin-top: 230px; margin-left: 310px; font-family: Lora;">Don't have an Account? <u style="cursor: pointer;">Sign Up</u></h5>
<h6 style="margin-top: 40px; margin-left: 260px; font-family: Lora;">By clicking "Log In", you agree to our <u style="cursor: pointer;">Terms</u> and <u style="cursor: pointer;">Privacy Policy</u></h6>
</div>
</div>
<!-- end snippet -->
答案1
得分: 0
你需要删除所有position: fixed
声明,因为它们通常会影响布局(一般来说,它们很难处理)。
还调整了一些不需要的宽度和边距。主要需要像这样的样式:
.login{
margin: 0 auto;
padding: 10px;
background-color: green;
width: 300px;
text-align: center;
}
请参阅下面的示例代码(这应该让你更接近你想要的效果):
<div class="container">
<div class="login">
<h2 class="textHead">MedConnect</h2>
<h2 class="logintxt">Log In to Your Account</h2>
<input type="text" class="loginemailtxt" name="email" placeholder="Email Address">
<br/>
<input type="password" class="loginpwdtxt" name="password" placeholder="Password">
<br/>
<button class="loginbtn" type="button" name="button">Log In</button>
<h5>Don't have an Account? <u style="cursor: pointer;">Sign Up</u></h5>
<h6>By clicking "Log In", you agree to our <u style="cursor: pointer;">Terms</u> and <u style="cursor: pointer;">Privacy Policy</u></h6>
</div>
</div>
以上为翻译的内容,代码部分保持原样不变。
英文:
You have to remove all sorts of position: fixed
declarations as they tend to mess with layouts (in general, they're hard to work with)
Also adjusted some widths and margins that are not needed. Mainly, you need somethign like this:
.login{
margin: 0 auto;
padding: 10px;
background-color: green;
width: 300px;
text-align: center;
}
See sample code below (which should get you closer to what you want)
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
@import url('https://fonts.googleapis.com/css2?family=Lora&display=swap');
body {
background-color: #DBE2EF;
font-family: Lora;
}
.login{
margin: 0 auto;
padding: 10px;
background-color: green;
width: 300px;
text-align: center;
}
input, button{
margin: 10px;
}
.loginemailtxt {
border: 1px solid black;
width: 250px;
height: 30px;
outline: none;
border-radius: 5px;
}
.loginemailtxt::placeholder {
color: black;
padding-left: 3px;
}
.loginpwdtxt {
border: 1px solid black;
width: 250px;
height: 30px;
outline: none;
border-radius: 5px;
}
.loginpwdtxt::placeholder {
color: black;
padding-left: 3px;
}
.loginbtn {
width: 120px;
height: 40px;
border-radius: 10px;
background-color: #3F72AF;
border: none;
cursor: pointer;
}
<!-- language: lang-html -->
<div class="container">
<div class="login">
<h2 class="textHead">MedConnect</h2>
<h2 class="logintxt">Log In to Your Account</h2>
<input type="text" class="loginemailtxt" name="email" placeholder="Email Address">
<br/>
<input type="password" class="loginpwdtxt" name="password" placeholder="Password">
<br/>
<button class="loginbtn" type="button" name="button">Log In</button>
<h5>Don't have an Account? <u style="cursor: pointer;">Sign Up</u></h5>
<h6>By clicking "Log In", you agree to our <u style="cursor: pointer;">Terms</u> and <u style="cursor: pointer;">Privacy Policy</u></h6>
</div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论