媒体查询
媒体查询会写如下代码即可:
body { background-color: gray; } /* 大于等于768px 为粉色 */ @media (min-width: 768px) { body { background-color: pink; } } /* 大于等于992px 为蓝色 */ @media (min-width: 992px) { body { background-color: skyblue; } } /* 大于等于992px 为绿色 */ @media (min-width: 1200px) { body { background-color: green; } }
媒体查询使用场景
约束移动端不要超过规定大小
body { max-width: 540px; margin: 0 auto; } /* 大于等于540px 则不允许 html文字大小再改动,强制定为 54px*/ @media (min-width: 540px) { html { font-size: 54px !important; } }
元素的显示和隐藏
@media (max-width: 800px) { .box div:nth-child(2) { display: none; } .box div:last-child { width: 350px; } }