当浏览器宽度达到 1460 像素或更小时,我希望表单标签和输入字段的字体大小从 18 像素缩小到 10 像素。
我读到当浏览器宽度减小时不可能让字体自动“缩小”,我需要使用媒体查询来代替。
因此,我在样式标签的顶部放置了一个媒体查询,询问我的标签和输入的字体大小,以便在屏幕尺寸为 1460 像素时以 10 像素显示,但它似乎不起作用。不过,我的其余代码工作正常,所以这一定与我编码媒体查询的方式有关。
如果有人可以提供一些帮助,将不胜感激..我的代码粘贴在下面。
@media only screen and (max-width: 1460px) {
label input {
font-size: 10px;
}
}
* {
box-sizing: border-box;
}
input[type=text],
select {
width: 95%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 3px;
resize: vertical;
transition: 0.3s;
outline: none;
font-family: Typ1451-Medium;
font-size: 18px;
margin: 7px;
}
input[type=text]:focus {
border: 1.25px solid #ea0088;
}
label {
padding: 21px 12px 12px 12px;
margin-left: 5px;
display: inline-block;
font-family: Typ1451-Medium;
font-size: 18px;
color: #999;
}
.container {
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
margin: 2.5% 20% 0 20%;
}
.col-25 {
float: left;
width: 25%;
margin-top: 6px;
}
.col-75 {
float: left;
width: 75%;
margin-top: 6px;
}
.left,
.right {
width: 50%;
}
form {
display: flex;
}
<div class="container">
<form action="signin.php" method="post">
<div class="left">
<div class="row">
<div class="col-25">
<label for="fname">First Name</label>
</div>
<div class="col-75">
<input type="text" id="fname" name="firstname" placeholder="* Please complete">
</div>
</div>
</div>
<div class="right">
<div class="row">
<div class="col-25">
<label for="lname">Last Name</label>
</div>
<div class="col-75">
<input type="text" id="lname" name="lastname" placeholder="* Please complete">
</div>
</div>
</div>
</form>
</div>
最佳答案
您的选择器 — label input
— 与您的 HTML 中的任何元素都不匹配。
没有你的input
元素是您的 label
的后代元素。
也许你的意思是label, input
选择 label
元素和 input
元素。如果是这样,那么它仍然无法工作,因为您定义了 input
稍后使用更具体的选择器(以及最具体的选择器 wins the cascade )和 label
的字体大小以类似的方式(它没有更具体的选择器,但当选择器相等时,最后一个将赢得级联)。
关于html - 媒体查询不适用于 HTML 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52073038/