【问题标题】:CSS Flexbox Form With Responsive Media Query Layout具有响应式媒体查询布局的 CSS Flexbox 表单
【发布时间】:2021-03-10 20:14:59
【问题描述】:

我正在尝试创建一个 html 表单,当屏幕宽度达到 x 像素数量时(使用 flexbox 和媒体查询),它将更改为列布局。我遇到的问题是提交按钮不会将自己定位在不同的字段下,例如姓名、您出生的城镇、全屏电子邮件,但会在手机大小的屏幕中。

我尝试将表单按钮div 放在联系人div 的内部和外部。只有当我将其留在联系人div 中时,我才能获得以移动格式显示在消息框下方的按钮。然后当我把它拿出来联系div它适用于移动设备但不能全屏。

我在下面包含的前两张图片是我希望全屏和移动设备的外观(请排除其他元素的响应性,这是一项正在进行的工作)。最后两张图是我现在的状态。

最后,我包含了表单的 HTML 和 CSS 代码。我还包括了主要的 CSS 样式表,以防其中有一些东西可能会影响我的表单。按钮/按钮 div 的代码可能看起来很简单,我尝试了许多不同的 flexbox 属性,但没有一个有效,所以我删除了它们以避免不必要的混乱。

Full Screen CorrectSmall Screen CorrectFull Screen IncorrectSmall Screen Incorrect


    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link rel="stylesheet" href="style.css" />
        <link rel="stylesheet" href="formStyle.css" />
        <title>Contact Us</title>
      </head>
      <body>
        <header>
          <img src="Images/logo.png" alt="logo" id="logo" />
          <!--Logo-->
          <nav>
            <ul class="nav_links">
              <li><a href="./index.html">Home</a></li>
              <li><a href="./merchandise.html">Merchandise</a></li>
              <li><a href="./about.html">About</a></li>
              <li><a href="./contactUs.html">Contact Us</a></li>
            </ul>
          </nav>
        </header>
        <h1>CONTACT US</h1>
        <br /><br />
        <h4>
          Have a quick question? Use contact form below and we'll be back to you as
          soon as possible!
        </h4>
        <br />
        <form action="#" class="contactForm">
          <div class="message">
            <label for="msg">Message</label>
            <textarea id="msg"></textarea>
          </div>
          <div class="contact">
            <label for="name">Name</label>
            <input type="text" id="name" />
    
            <label for="townborn">Town you were born in</label>
            <input type="text" id="townborn" />
    
            <label for="email">Email Address</label>
            <input type="email" id="email" />
          </div>
          <div class="form-button">
            <button type="submit">Submit</button>
          </div>
        </form>
      </body>
    </html>

  
  .contactForm {
      display: flex;
      background-color: beige;
      border-radius: 3px;
      padding: 1.8em;
    }
    .message {
      display: flex;
      flex-direction: column;
      order: 3;
    }
    .message > textarea {
      flex: 1;
      min-width: 18em;
    }
    .contact {
      flex: 1;
      order: 1;
      margin-right: 2em;
    }
    .contact input {
      width: 100%;
    }
    .contact input {
      padding: 1em;
      margin-bottom: 1em;
    }
    
    @media only screen and (max-device-width: 480px) {
      .contactForm {
        flex-direction: column;
      }
      .message {
        margin-right: 2em;
        order: 2;
      }

  .form-button {
    order: 3;
  }
}


    @import url("https://fonts.googleapis.com/css2?family=Montserrat:wght@500&display=swap");
    
    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }
    
    body {
      background-image: linear-gradient(to top, #ddf2eb, #d3cdd7);
      background-repeat: no-repeat;
      height: 100vh;
    }
    
    header {
      display: flex;
      padding: 10px 5%;
      background-color: #ddf2eb;
      align-items: center;
      align-self: stretch;
    }
    
    #logo {
      height: 70px;
      width: 70px;
      cursor: pointer;
    }
    
    .nav_links {
      list-style: none;
      margin-left: 10%;
      white-space: nowrap;
    }
    
    .nav_links li {
      display: inline-block;
      padding: 0px 20px;
    }
    
    .nav_links li a {
      transition: all 0.3s ease 0;
      font-family: "Montserrat", "sans-serif";
      font-weight: 500;
      font-size: 15px;
      color: #606d5d;
      text-decoration: none;
    }
    
    .nav_links li a:hover {
      color: honeydew;
    }
    
    .flex-container {
      display: flex;
    }

【问题讨论】:

    标签: html css forms flexbox


    【解决方案1】:

    我不确定 flex 是否可行,因为 flex 既可以是行也可以是列,而您同时需要两者。 您可以尝试使用网格!

    网格示例

    * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
    }
    
    .contactForm label {
        display: block;
    }
    
    .contactForm input,
    .contactForm textarea {
        height: 100%;
        width: 100%;
    }
    
    .contactForm > div {
        width: 100%;
    }
    
    .contact-name {
        grid-area: name;
    }
    
    .contact-townborn {
        grid-area: townborn;
    }
    
    .contact-email {
        grid-area: email;
    }
    
    .contact-msg {
        grid-area: msg;
        padding-bottom: 20px;
    }
    
    .contact-form-button {
        grid-area: button;
        margin-top: -20px;
        padding-top: 10px;
    }
    
    .contactForm {
        background-color: #2196F3;
        display: grid;
        grid-gap: 20px;
        grid-template-areas: 'name' 'townborn' 'email' 'msg' 'button';
        padding: 20px;
        max-width: 800px;
    }
    
    @media (min-width: 480px) {
        .contactForm {
            grid-template-areas: 'name msg' 'townborn msg' 'email msg' 'button msg';
        }
        .contact-form-button {
            margin-top: 0;
        }
    } 
    <form action="#" class="contactForm">
        <div class="contact-name">
            <label for="name">Name</label>
            <input type="text" id="name" />
        </div>
        <div class="contact-townborn">
            <label for="townborn">Town you were born in</label>
            <input type="text" id="townborn" />
        </div>
        <div class="contact-email">
            <label for="email">Email Address</label>
            <input type="email" id="email" />
        </div>
        <div class="contact-msg">
            <label for="msg">Message</label>
            <textarea id="msg"></textarea>
        </div>
        <div class="contact-form-button">
            <button type="submit">Submit</button>
        </div>
    </form>

    来源:W3schools CSS Grid Layout

    【讨论】:

    • 非常感谢,在 grid、flex、float 等之间做出选择时真的很吃力,不知道什么时候用。很高兴听到在这件事上有更多经验的人的来信。我现在将尝试并实施。
    • 这实际上是我第一次尝试使用网格。我一直在使用 flex 很短的时间,并且不确定何时使用什么。但是当你需要行和列时,网格是你的朋友,否则 flex 就足够了
    猜你喜欢
    • 2018-09-13
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-11
    • 2013-05-18
    相关资源
    最近更新 更多