【问题标题】:Trouble while applying padding in form element在表单元素中应用填充时出现问题
【发布时间】:2022-01-19 21:09:11
【问题描述】:

这是一个基本表单网页的快照,而更改填充灰色背景的值并没有缩小。

我附上了一张 CSS 代码的图片。在那,我使用了 padding = 20px,但是在图像中只有顶部和底部的 padding 是 20px,左右的 padding 远远超出了。

我正在表单元素上应用样式。

这是完整的 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">
    <link rel="stylesheet" href="style.css">
    <title>StartUp survey form</title>
</head>
<body>  
    <h1 id="title">StartUp survey form</h1>
    <p id="description">Thank you for taking the time to help us improve the platform</p>
    <form id="survey-form">
    <div>
        <label for="name" id="name-label">Name<br>
            <input id="name" placeholder="Enter your name" type="text">
            <br>
        </label>
        <label for="email" id="email-label">Email<br><input id="email" type="email" placeholder="Enter your Email"><br></label>
        <label for="number" id="number-label">Age(optional)<br><input id="number" type="number" min="20" max="60" placeholder="Age"><br></label>
        <label for="dropdown">Which option best describes your current role?<br>
        <select id="dropdown" name="dropdown list option">
            <option value="select current role" selected="true" disabled="disabled">Select current role</option>
            <option value="student">Student</option>
            <option value="full time job">Full Time Job</option>
            <option value="full time learner">Full Time Learner</option>
            <option value="prefer not to say">Prefer not to say</option>
            <option value="other">Other</option>
        </select>
    <br></label>
        <p>Would you recommend StartUp to a friend ?
        </p>
        <input type="radio" name="radio" value="definitely">
        <label>Definitely<br></label>
        <input type="radio" name="radio" value="maybe">
        <label>Maybe<br></label>
        <input type="radio" name="radio" value="not sure">
        <label>Not Sure<br></label>
        <label>
            Which is your favourite feature of StartUp?<br>
            <select id="dropdown" name="dropdown list option ff">
                <option value="select an option" selected="true" disabled="disabled">Select and option</option>
                <option value="challenges">Challenges</option>
                <option value="projects">Projects</option>
                <option value="community">Community</option>
                <option value="open source">Open Source</option>
            </select>
        </label>
        <p>What would you like to seeimproved? (Check all that apply)</p>
        <input type="checkbox" value="fep">
        <label>Front-end Projects<br></label>
        <input type="checkbox" value="bep">
        <label>Back-end Projects<br></label>
        <input type="checkbox" value="dv">
        <label>Data Visualization<br></label>
        <input type="checkbox" value="c">
        <label>Challenges<br></label>
        <input type="checkbox" value="osc">
        <label>Open Source Community<br></label>
        <input type="checkbox" value="ghr">
        <label>Gitter help rooms<br></label>
        <input type="checkbox" value="v">
        <label>Videos<br></label>
        <input type="checkbox" value="cm">
        <label>City Meetups<br></label>
        <input type="checkbox" value="w">
        <label>Wiki<br></label>
        <input type="checkbox" value="f">
        <label>Forum<br></label>
        <input type="checkbox" value="ac">
        <label>Additional Courses<br></label>
        <label>Any comments or suggestions?<br>
        <input type="text" placeholder="comment">
        </label>
        <label><br><button type="button">Submit</button>
        </label>    
    </div>
    </form>
</body>
</html>

这是完整的 CSS 代码:

 body{
        background-image: url(background.jpg);
        text-align: center;
        padding: 0px;
    }
    h1{
        font-family:'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
        color: rgb(216, 213, 28);
        margin-top: 40px;
        margin-bottom: 0px;
    }
    #description{
        font-family: cursive;
        margin-top: 10px;
        font-size: 20px;
        color: rgb(231, 224, 224);
    }
    
    form{
        background-color: rgba(99, 98, 100, 0.9);
        border-radius: 5px;
        padding: 20px;
    }

【问题讨论】:

  • 如果不显示有关此问题的一些代码,将很难提供任何帮助
  • 是否可以显示 HTML 代码?您可以将表单放在 div 标签中并更改边距和填充,以便灰色背景缩小
  • 我已经上传了整个 HTML 和 CSS 代码。

标签: html css padding


【解决方案1】:

边距:右上左下;你可以通过相对度量和绝对来设置

body {  padding: 20px 20px 20px 20px; }

【讨论】:

  • 我已经在表单元素中应用了这段代码,但没有按预期工作。
  • 使用边距,或按弹性框元素占位,请提供最小可重复样本。请检查
  • 使用flexbox问题很容易解决,感谢帮助:)
【解决方案2】:

确保您使用的是 box-sizing: border-box;,并且您的主体或任何其他容器类使用的是max-width:100%;

*, ::after, ::before {
    box-sizing: border-box;
}

【讨论】:

    【解决方案3】:

    内容确实应该在 div 标签中,如果您希望灰色背景更小,您应该使用边距:

    form {
           margin: 20px 20px 20px 20px;
         }
    

    这将以下列方式应用边距:上、右、下、左

    【讨论】:

    • 是的,使用边距可以缩小灰色背景,但填充呢?填充 - 元素内容与其边框之间的空间。表单元素包含各种编号。作为其内容的输入元素的数量,因此应减少其内容和边框之间的填充空间,这最终会导致灰色背景的缩小。请在这个实施方向上帮助我。
    猜你喜欢
    • 1970-01-01
    • 2011-05-07
    • 2020-05-03
    • 2015-08-02
    • 2017-07-16
    • 2013-02-21
    • 2019-04-12
    • 2019-03-26
    • 2012-04-26
    相关资源
    最近更新 更多