css 我如何保持我的输入字段在同一个地方,无论错误消息显示与否?

osh3o9ms  于 2023-02-01  发布在  其他
关注(0)|答案(1)|浏览(149)

我正在开发一个表单,当表单中有错误时,它会显示特定的错误消息。我的问题是,当显示错误消息时,我的表单输入显示为我想要的。这是显示我想要的显示的图像(这是带有错误显示属性的注解。它显示为:无)

但是,当display属性(display:none)处于活动状态时,输入字段的位置会发生如下变化:

我应该在代码中做些什么修改来确保我的文本输入字段看起来像这样。

我该如何实现这一目标。
我的代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <!-- displays site properly based on user's device -->

  <link rel="icon" type="image/png" sizes="32x32" href="./images/favicon-32x32.png">
  <link rel="stylesheet" href="/styles/style.css">
  <title>Frontend Mentor | Intro component with sign up form</title>
</head>
<body>
  <section class="container">
    <header>
      <h1>Learn to code by watching others</h1>
    </header>

    <article class="first-para">
      <p class="main-paragraph"> See how experienced developers solve <span class="second-line">problems in real-time.
          Watching</span>
        <span class="third-line">scripted tutorials is great, but</span> understanding how developers think is
        <span class="invaluable">invaluable.</span></p>
    </article>

    <section class="try-box">
      <p><span class="try">Try it free 7 days</span> then <br /> $20/mo. thereafter</p>
    </section>

    <form class="claim-form" action="/">
      <input type="text" placeholder="First Name" id="first-name">
      <img class='error-img' src="/images/icon-error.svg" alt="error">
      <div class="err err-first">First Name cannot be empty</div>

      <input type="text" placeholder="Last Name" id="last-name">
      <img class='error-img' src="/images/icon-error.svg" alt="error">
      <div class="err err-last">Last Name cannot be empty</div>

      <input type="email" placeholder="Email Address" id="email">
      <img class='error-img' src="/images/icon-error.svg" alt="error">
      <div class="err err-email">Looks like this is not an email</div>

      <input type="password" placeholder="Password" id="password">
      <img class='error-img' src="/images/icon-error.svg" alt="error">
      <div class="err err-pass">Password cannot be empty</div>

      <button class="claim-btn">CLAIM YOUR FREE TRIAL</button>
      <p class="agree">By clicking the button, you are agreeing to our <span class="terms">Terms and
          Services</span></p>
    </form>
  </section>
</body>
<script src="script.js"></script>
</html>

CSS:

@import url('css-reset.css');
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap');

:root {
    --color-red: hsl(0, 100%, 74%);
    --color-green: hsl(154, 59%, 51%);
    --color-blue: hsl(248, 32%, 49%);
    --color-dark-blue: hsl(249, 10%, 26%);
    --color-grayish-blue: hsl(246, 25%, 77%);
    --color-white: hsl(0, 0%, 100%);
    --font-weight-four: 400;
    --font-weight-five: 500;
    --font-weight-six: 600;
    --font-weight-seven: 700;
}

body {
    font-family: 'Poppins',
        sans-serif;
    background: url('/images/bg-intro-mobile.png') var(--color-red);
}

.container {
    max-width: 500px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    margin-top: 70px;
}

header h1 {
    color: var(--color-white);
    font-weight: var(--font-weight-six);
    font-size: 1.7em;
    padding: 50px 50px 20px 50px;
    margin-left: 50px;
    line-height: 1.2;
}

.first-para {
    max-width: 17rem;
}

.main-paragraph {
    color: var(--color-white);
    font-weight: var(--font-weight-four);
    font-size: 0.85em;
    margin-bottom: 50px;
    line-height: 2;
}

.second-line {
    margin-left: 13px;
}

.third-line {
    margin-left: 20px;
}

.invaluable {
    margin-left: 92px;
}

.try-box {
    width: 270px;
    height: 65px;
    background-color: var(--color-blue);
    border-radius: 10px;
    color: var(--color-white);
    text-align: center;
    padding: 15px;
    font-size: 0.8em;
    margin-bottom: 20px;
    box-shadow: 0px 4px 5px 1px rgba(0, 0, 0, 0.29);
}

.try-box p:not(.try) {
    font-weight: var(--font-weight-four);
}

.try {
    font-weight: var(--font-weight-six);
}

.claim-form {
    width: 270px;
    background-color: var(--color-white);
    height: 450px;
    border-radius: 10px;
    box-shadow: 0px 4px 5px 1px rgba(0, 0, 0, 0.29);
    margin-bottom: 30px;
}

#first-name,
#last-name,
#email,
#password {
    width: 240px;
    padding: 7px;
    margin: 13px 0 0px 15px;
}

.error-img {
    /* display: none; */
    position: relative;
    left: 220px;
    bottom: 24px;
    height: 16px;
    width: 16px;
}

.err {
    /* display: none; */
    color: var(--color-red);
    font-style: italic;
    font-size: 0.7em;
    margin-left: 90px;
    margin-top: -10px;
}

.err::placeholder {
    color: var(--color-dark-blue);
    /* opacity for firefox */
    opacity: 70%;
    font-size: 0.8em;
    font-weight: var(--font-weight-seven);
}

.claim-btn {
    width: 240px;
    margin: 13px 0 13px 15px;
    padding: 12px;
    color: var(--color-white);
    border-radius: 5px;
    border: 1px;
    background-color: var(--color-green);
    font-size: 0.8em;
    font-weight: var(--font-weight-four);
    box-shadow: 0px 4px 5px 1px rgba(0, 0, 0, 0.29);
    cursor: pointer;
}

.claim-btn:active {
    opacity: 50%;
}

.agree {
    font-size: 0.6em;
    color: var(--color-grayish-blue);
    margin-left: 35px;
}

.terms {
    color: var(--color-red);
    font-weight: var(--font-weight-six);
    margin-left: 65px;
    cursor: pointer;
}

谢谢你。

tf7tbtn2

tf7tbtn21#

display: none;会从文档中删除元素,您可以使用visibility: hidden来代替display: none;。设置visibility会隐藏元素,而不会更改文档的布局。另一个类似的选项是opacity: 0
这两个选项对于视力正常的用户都很好,但是当aria-hidden不可见时,您还应该将其设置为true,以避免屏幕阅读器读取错误。

相关问题