对齐仅在移动设备上居中的文本

时间:2019-09-05 13:35:48

标签: html css alignment responsive

我希望文本仅在移动设备上居中对齐。在桌面上,文本应右对齐。

@media only screen and (min-device-width: 320px) and (max-device-width: 800px) {
  .centerMobileOnly {
    text-align: center !important;
  }
}

@media only screen and (min-device-width: 600px) {
  .rightDesktopOnly {
    text-align: right !important;
  }
}
<div>
  <h3 class="rightDesktopOnly centerMobileOnly">02:00</h3>
</div>

1 个答案:

答案 0 :(得分:1)

您不必为媒体css添加两个类,而只能通过单个类。

.centerMobileOnly{
  text-align: right;
}
@media (max-device-width: 800px) {
  .centerMobileOnly {
    text-align: center;
  }
}
<div>
  <h3 class="centerMobileOnly">02:00</h3>
</div>