【问题标题】:How do I use media query to make form responsive for all devices [duplicate]如何使用媒体查询使表单响应所有设备
【发布时间】:2022-01-23 18:20:24
【问题描述】:

我使用 HTML 和 CSS 创建了一个简单的表单,但是当我尝试通过调整宽度在浏览器上检查它时,一切都变得不合适,即我希望网站的响应能力能够巧妙地将组件缩放到更小的屏幕尺寸。

抱歉,我目前还没有机会在问题中发布图片

这是我的不同宽度的表格:

initial @1903px(如预期)

adjusted @1619px(如预期)

adjusted @1164px(与预期不同,即@ 987654330@buysell 应与 fullname 的边距保持一定距离)

adjusted@1103px(不像预期的那样,即distributebuysell应该在一起)

adjusted@784px(与预期不同,即distributebuysell应该在一起)

adjusted@483px(不像预期的那样,即distributebuysell 应该在一起,drop-down-arrow for distribution-route 不合适)

HTML 代码

<!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">
    <title>Form</title>
  
    

</head>
<body>
 
    <!--Form-->
    <section class="applicant">
        <div class="login-center">
            <div class="text">
                <h2>CREATE</h2>
            </div>
            <div class="links">
                <button type="button" class="app-button active">Distribute</button>
                <button type="button" class="app-button">Buy</button>
                <button type="button" class="app-button">Sell</button>
                
            </div>
            <div class="form-u">
                <form action="" class="form">
                     
                    
                    <label for="uname"><b>Full Name:</b></label>
                    <input type="text" name="student matric no"placeholder="Enter full name"required>

                    <label for="psw"><b>Password:</b></label>
                    <input type="password" name="password" placeholder="Enter password"required>

                     
                    <label for="sem"><b>Location:</b></label>
                    <div class="selector">
                      <div id="selectField">
                        <p id="selectText">Select a Location</p>
                        <img src="logo/arrow_down.png" alt="" id="arrowIcon">
                      </div>
                    </div>

                    <button type="submit" class="button-u">SUBMIT</button>
                   
                     
                 
                    
              

                   


                    
                </form>


                

            </div>
            


        </div>

    </section>

 <!--Here is the CSS:-->
<style>
* {
  box-sizing: border-box;
  list-style: none;
  text-decoration: none;
  padding: 0;
  margin: 0;
}
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
    Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
  display: flex;
  font-size: 16px;
  user-select: none;
}

/* APPLICATION */
.applicant {
  width: 60%;
  margin: auto;
  text-align: left;
  margin: 90px auto;
  padding-top: 100px;
  padding-bottom: 100px;
}
.login-center {
  height: 100%;
  flex-basis: 41%;
  background: #ffffff;
  padding: 35px 5px 0px 5px;
  box-sizing: border-box;

  box-shadow: 0 0 50px 0px #e1e1e1;
}
.text h2 {
  text-align: center;
  font-size: 35px;
  font-weight: 600;
  color: #000;
}
.links {
  text-align: center;
  margin-left: -20px;
  margin-right: -20px;
  margin: 0 auto;
  padding-top: 50px;
}
.app-button {
  color: #c4c4c4;
  background-color: #ffffff;
  border: 1px solid #000;
  font-weight: bold;
  font-size: 17px;
  width: 200px;
  margin: 0 20px;
  display: inline-block;
  line-height: 40px;
  padding: 5px;
  cursor: pointer;
}
.app-button:hover {
  background: #c4c4c4;
  color: #000000;
  transition: 0.5s;
}
/* ACTIVE STATE */
.links .active,
.app-button:hover {
  border: 1px solid #c4c4c4;
  background-color: #c4c4c4;
  color: #000;
}

/* FORM */
.form input::placeholder {
  font-size: 14px;
  color: #000;
}
.form label {
  color: #000;
  margin: 20px 0;
  font-size: 17px;
}
.form-u {
  margin: 70px 0;
  padding: 0px 100px;
}
.form input {
  width: 100%;
  padding: 20px;
  margin: 20px 0;
  box-sizing: border-box;
  border: none;
  outline: none;
  background: #ffffff;
  border: 1.7px solid #e1e1e1;
}
.form input:focus {
  border-color: #c4c4c4;
  box-shadow: 0 0 15px 0px #e1e1e1;
}
.button-u {
  color: #c4c4c4;
  background-color: #000;
  border: 1px solid rg#c4c4c4;
  font-weight: bold;
  font-size: 17px;
  width: 100%;
  margin: 40px 0;
  display: inline-block;
  line-height: 40px;
  padding: 5px;
  cursor: pointer;
}
/* DROPDOWN FOR LOCATION*/
.form input {
  font-size: 15px;
  color: #000;
}
.selector {
  width: 100%;
  padding-top: 20px;
  margin-bottom: 25px;
  position: relative;
}
#selectField p {
  font-size: 15px;
}
#selectField {
  width: 100%;
  background: #ffffff;
  box-sizing: border-box;
  border: 1px solid #c4c4c4;
  padding: 20px 20px;
  color: #000;
  font-size: 15px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  position: relative;
}

/*RESPONSIVE*/
/* WHAT DO I DO ? */
</style>
   
 

我的目标是使用媒体查询使表单在所有屏幕上都具有响应性,请问如何使我的表单具有智能响应?希望有人可以提供帮助:)

【问题讨论】:

  • 你想用更窄的视口实现什么?不清楚你想要什么样的响应能力,或者你有什么样的替代布局策略。 “响应性”是一个非常宽泛的术语,并不能转化为具体的、理想的布局。
  • 特里您好,感谢您的回复。我的意思是它在更大的屏幕上查看时的外观示例@ width 1903px 并希望它在调整屏幕时看起来相同,直到它达到宽度@ 483px 甚至更小(即对于移动设备)..这就是什么我的意思是..希望你理解
  • 我再次编辑了这个问题,以便更好地了解我想要实现的目标

标签: javascript html css media-queries responsive


【解决方案1】:

您的问题是如何使用媒体查询,对吧?
这真的很简单。假设我有宽度为 200 像素的按钮。
我希望宽度按钮在较小的设备中变得更小。

/* // Extra small devices (portrait phones, less than 576px) */
@media (max-width: 575.98px) {
    button{
        width: 100px;
    }
}

/* // Small devices (landscape phones, 576px and up) */
@media (min-width: 576px) and (max-width: 767.98px) {
    button{
        width: 150px;
    }
}

/* // Medium devices (tablets, 768px and up) */
@media (min-width: 768px) and (max-width: 991.98px) { 
    button{
        width: 180px;
    }
}

/* // Large devices (desktops, 992px and up) */
@media (min-width: 992px) and (max-width: 1199.98px) { 
    button{
        width: 190px;
    }
 }

/* // Extra large devices (large desktops, 1200px and up) */
@media (min-width: 1200px) {  
    button{
        width: 200px;
    }

}

只需在您的 css 上添加媒体查询,并将您想要自定义的元素或类名称添加到每个媒体查询中。

【讨论】:

    【解决方案2】:

    您可以使用 vwvhvminvmax 单位来调整内容的大小视口大小。

    <!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">
        <title>Form</title>
      
        
    
    </head>
    <body>
     
        <!--Form-->
        <section class="applicant">
            <div class="login-center">
                <div class="text">
                    <h2>CREATE</h2>
                </div>
                <div class="links">
                    <button type="button" class="app-button active">Distribute</button>
                    <button type="button" class="app-button">Buy</button>
                    <button type="button" class="app-button">Sell</button>
                    
                </div>
                <div class="form-u">
                    <form action="" class="form">
                         
                        
                        <label for="uname"><b>Full Name:</b></label>
                        <input type="text" name="student matric no"placeholder="Enter full name"required>
    
                        <label for="psw"><b>Password:</b></label>
                        <input type="password" name="password" placeholder="Enter password"required>
    
                         
                        <label for="sem"><b>Location:</b></label>
                        <div class="selector">
                          <div id="selectField">
                            <p id="selectText">Select a Location</p>
                            <img src="logo/arrow_down.png" alt="" id="arrowIcon">
                          </div>
                        </div>
    
                        <button type="submit" class="button-u">SUBMIT</button>
                       
                         
                     
                        
                  
    
                       
    
    
                        
                    </form>
    
    
                    
    
                </div>
                
    
    
            </div>
    
        </section>
    
     <!--Here is the CSS:-->
    <style>
    * {
      box-sizing: border-box;
      list-style: none;
      text-decoration: none;
      padding: 0;
      margin: 0;
    }
    body {
      font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
        Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
      display: flex;
      font-size: 1vw;
      user-select: none;
    }
    
    /* APPLICATION */
    .applicant {
      width: 60%;
      margin: auto;
      text-align: left;
      margin: 6.2vw auto;
      padding-top: 6.5vw;
      padding-bottom: 6.5vw;
    }
    .login-center {
      height: 100%;
      flex-basis: 41%;
      background: #ffffff;
      padding: 2.5vw 0.2vw 0px 0.2vw;
      box-sizing: border-box;
    
      box-shadow: 0 0 50px 0px #e1e1e1;
    }
    .text h2 {
      text-align: center;
      font-size: 3vw;
      font-weight: 600;
      color: #000;
    }
    .links {
      text-align: center;
      margin-left: -1.3vw;
      margin-right: -1.3vw;
      margin: 0 auto;
      padding-top: 3.2vw;
    }
    .app-button {
      color: #c4c4c4;
      background-color: #ffffff;
      border: 1px solid #000;
      font-weight: bold;
      font-size: 1vw;
      width: 13vw;
      margin: 0 1.3vw;
      display: inline-block;
      line-height: 2.6vw;
      padding: 0.2vw;
      cursor: pointer;
    }
    .app-button:hover {
      background: #c4c4c4;
      color: #000000;
      transition: 0.5s;
    }
    /* ACTIVE STATE */
    .links .active,
    .app-button:hover {
      border: 1px solid #c4c4c4;
      background-color: #c4c4c4;
      color: #000;
    }
    
    /* FORM */
    .form input::placeholder {
      font-size: 1vw;
      color: #000;
    }
    .form label {
      color: #000;
      margin: 1.3vw 0;
      font-size: 1vw;
    }
    .form-u {
      margin: 4.5vw 0;
      padding: 0px 6.5vw;
    }
    .form input {
      width: 100%;
      padding: 1.3vw;
      margin: 1.3vw 0;
      box-sizing: border-box;
      border: none;
      outline: none;
      background: #ffffff;
      border: 1.7px solid #e1e1e1;
    }
    .form input:focus {
      border-color: #c4c4c4;
      box-shadow: 0 0 15px 0px #e1e1e1;
    }
    .button-u {
      color: #c4c4c4;
      background-color: #000;
      border: 1px solid rg#c4c4c4;
      font-weight: bold;
      font-size: 1vw;
      width: 100%;
      margin: 2.6vw 0;
      display: inline-block;
      line-height: 2.5vw;
      padding: 0.3vw;
      cursor: pointer;
    }
    /* DROPDOWN FOR LOCATION*/
    .form input {
      font-size: 1vw;
      color: #000;
    }
    .selector {
      width: 100%;
      padding-top: 1.3vw;
      margin-bottom: 1.5vw;
      position: relative;
    }
    #selectField p {
      font-size: 1vw;
    }
    #selectField {
      width: 100%;
      background: #ffffff;
      box-sizing: border-box;
      border: 1px solid #c4c4c4;
      padding: 1.3vw 1.3vw;
      color: #000;
      font-size: 1vw;
      display: flex;
      align-items: center;
      justify-content: space-between;
      cursor: pointer;
      position: relative;
    }
    
    /* I only used vw here, but it's not necessarily the best approach */
    </style>
    

    您可以快速查找CSS Units,如果您需要更多说明,请在 Google 上进行深入了解。

    此外,如果您需要了解视口大小:What is my viewport

    【讨论】:

    • 谢谢 Epsilon。试过你的方法,你说最好 vw 不是最好的方法。我真正想问的是如何使用媒体查询使表单在所有设备上都响应跨度>
    • @EinStyn,你没有。如果您想要所有屏幕尺寸的布局,则不应使用媒体查询。媒体查询被称为断点是有原因的。如果您只想更改 483 像素的布局,请使用媒体查询。
    猜你喜欢
    • 2020-09-17
    • 2014-12-18
    • 1970-01-01
    • 2021-02-23
    • 2012-10-02
    • 2016-07-25
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    相关资源
    最近更新 更多