“DIV类的背景颜色”

huangapple go评论60阅读模式
英文:

Background Colour for DIV class

问题

我现在有一个DIV类,里面包含不同的元素。现在,我想给整个div类(而不是container,只是logindiv)设置一个背景颜色,它会在顶部、底部、左侧和右侧(大约10px)留下一点空间,完全包围这个div。然而,当我尝试给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:

“DIV类的背景颜色”

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(&#39;https://fonts.googleapis.com/css2?family=Lora&amp;display=swap&#39;);
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 -->

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;login&quot;&gt;
    &lt;h2 class=&quot;textHead&quot;&gt;MedConnect&lt;/h2&gt;
    &lt;h2 class=&quot;logintxt&quot;&gt;Log In to Your Account&lt;/h2&gt;
    &lt;input type=&quot;text&quot; class=&quot;loginemailtxt&quot; name=&quot;email&quot; placeholder=&quot;Email Address&quot;&gt;
    &lt;input type=&quot;password&quot; class=&quot;loginpwdtxt&quot; name=&quot;password&quot; placeholder=&quot;Password&quot;&gt;
    &lt;button class=&quot;loginbtn&quot; type=&quot;button&quot; name=&quot;button&quot;&gt;Log In&lt;/button&gt;
    &lt;h5 style=&quot;margin-top: 230px; margin-left: 310px; font-family: Lora;&quot;&gt;Don&#39;t have an Account? &lt;u style=&quot;cursor: pointer;&quot;&gt;Sign Up&lt;/u&gt;&lt;/h5&gt;
    &lt;h6 style=&quot;margin-top: 40px; margin-left: 260px; font-family: Lora;&quot;&gt;By clicking &quot;Log In&quot;, you agree to our &lt;u style=&quot;cursor: pointer;&quot;&gt;Terms&lt;/u&gt; and &lt;u style=&quot;cursor: pointer;&quot;&gt;Privacy Policy&lt;/u&gt;&lt;/h6&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- 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(&#39;https://fonts.googleapis.com/css2?family=Lora&amp;display=swap&#39;);
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 -->

&lt;div class=&quot;container&quot;&gt;
  &lt;div class=&quot;login&quot;&gt;
    &lt;h2 class=&quot;textHead&quot;&gt;MedConnect&lt;/h2&gt;
    &lt;h2 class=&quot;logintxt&quot;&gt;Log In to Your Account&lt;/h2&gt;
    &lt;input type=&quot;text&quot; class=&quot;loginemailtxt&quot; name=&quot;email&quot; placeholder=&quot;Email Address&quot;&gt;
    &lt;br/&gt;
    &lt;input type=&quot;password&quot; class=&quot;loginpwdtxt&quot; name=&quot;password&quot; placeholder=&quot;Password&quot;&gt;
    &lt;br/&gt;
    &lt;button class=&quot;loginbtn&quot; type=&quot;button&quot; name=&quot;button&quot;&gt;Log In&lt;/button&gt;
    &lt;h5&gt;Don&#39;t have an Account? &lt;u style=&quot;cursor: pointer;&quot;&gt;Sign Up&lt;/u&gt;&lt;/h5&gt;
    &lt;h6&gt;By clicking &quot;Log In&quot;, you agree to our &lt;u style=&quot;cursor: pointer;&quot;&gt;Terms&lt;/u&gt; and &lt;u style=&quot;cursor: pointer;&quot;&gt;Privacy Policy&lt;/u&gt;&lt;/h6&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月6日 15:40:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626539.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定