我有一个带有输入字段的简单表格。现在,在笔记本电脑的屏幕上,输入的宽度占据了正确屏幕的30%。
但是,我想做的是,如果设备的宽度为600px或更小(移动设备),则将宽度增加到60%。
问题在于它没有改变移动设备的宽度。似乎它仅占移动设备的30%,而不是60%。我不确定我在基于研究使用@media标记时在做什么错误。
<section id="marketing-email">
<form class="marketing-email-form" method="post" action="https://metis-online.com/marketing-email.php">
<div>
<label for="email"><b>Be part of our mailing list to receive exclusive promotional offers on our courses and services:</b></label><br/>
<input type="email" id="market-email" name="market-email" required placeholder="Email"/>
<button type="submit" class="marketing-btn">Send</button>
</div>
</form>
</section>
@media only screen and (max-width: 600px) {
#market-email{
width:60%;
}
}
/*--------Marketing Email-------*/
#marketing-email{
padding: 1em;
width:100%;
text-align:center;
}
#market-email{
padding: 0.5em;
width:30%;
}
.marketing-btn{
background: #1034A6;
color:white;
padding: 0.5em;
}
在CSS中,文档后面的样式会覆盖前面的样式。因此,最特定的@media查询必须始终位于文档的最后。
At the moment,
width: 30%;
is applied to all screen sizes and comes later in the document, therefore overriding the earlier style ofwidth: 60%;
.The solution is simply to change the position of the
@media
query within your CSS:Just move
@media only screen and (max-width: 600px)
part to the end of the page. Because now the second code#market-email
withwidth:30%;
rewriting your@media
rule.在移动设备上宽度未增加到60%的原因是因为在您的CSS代码中,您首先说#market-email的宽度应为60%,然后您将其重置为30%。
要正常工作,这是应该做的