Sử dụng bên trong mã có trong các truy vấn @media của bạn là cách hiệu quả để thêm thuộc tính động vào các phần tử trang của bạn, trong khi vẫn giữ nguyên phần css mặc định của bạn để bảo trì dễ dàng.
Ví dụ:
<html>
<head>
<style>
@media all and (max-width: 768px) {
/* CSS styles targeting screens of smaller sizes and/or resolutions (IE phones and tablets) */
#semantically-named-element p
{ width: 60% !important;
background: rgba(0,0,255,1)!important;
margin: auto !important; }
#semantically-named-element span
{ display: none !important; }
}
/* Look ma no media queries needed for default CSS below! Very easy to discern default styles, when the default code is the stuff not wrapped in queries. Period. */
/* Default CSS style attributes for any viewports that do not fit into the low or high resolution/screen size parameters set above and below. Also the fallback for browsers that still disregard CSS media queries */
#semantically-named-element p
{ width: 90%;
background: rgba(255,0,0,1);
margin: auto; }
#semantically-named-element span
{ display: block;
background: rgba(0,0,0,0.8);
color: #FFF;
text-align: center;}
@media all and (min-width: 968px) {
/* CSS styles targeting very large or very high resolution viewports at the 769 pixels wide "break-point" */
#semantically-named-element p
{ width: 80% !important;
background: rgba(0,255,0,1)!important;
margin: auto !important; }
#semantically-named-element span
{ display: block !important;
background: rgba(0,0,0,0.8)!important;
color: #FFF !important; font-size: 200%; }
}
</style>
</head>
<body>
<div id="semantically-named-element">
<p> Usage of !important inside of the code contained within your @media queries is an effective way to add dynamic style properties to your pages elements, while leaving your 'default' css intact for easy maintenance.<span>hidden until tablet greater 768 viewport pixel width</span></p>
</div>
</body>
</html>
Mã của bạn hoạt động tốt trên hệ thống của tôi. Tôi đã thử nghiệm trong Firefox, chrome và safari trên Mac. Môi trường thử nghiệm của bạn là gì? – Anji
Xin lỗi vì trả lời muộn. Tôi đã cố gắng tìm ra lý do tại sao nó không hoạt động. Đây là: http://jsfiddle.net/TUL6h/3/ Cố gắng làm cho màu xanh lam bằng cách thay đổi kích thước. Nó không bao giờ xảy ra – jQuerybeast
Nó sẽ chỉ chọn màu sắc trong đó là cuối cùng trong phần css. – jQuerybeast