【问题标题】:Bootstrap flash alert CSS not working correctly in mobile screenBootstrap flash alert CSS 在移动屏幕上无法正常工作
【发布时间】:2016-06-12 10:13:59
【问题描述】:

我目前正在构建一个 Rails 应用程序。在我的应用程序中,我使用 Bootstrap 的 CSS 显示 Flash 消息。我没有完全使用 Bootstrap 的 CSS,但由于 CSS 重叠,我只在我的应用中添加了与警报相关的内容。

Flash 的 CSS 适用于普通桌面屏幕。它的位置居中且固定。下图:

但是,当我在移动屏幕上填充闪光灯(小于 500 像素)时,无论我做什么,它都会偏离中心向左。下图:

我很困惑为什么会这样。就像桌面屏幕一样,我想固定闪光灯在手机屏幕中的位置,并始终填充在同一个位置。

以下是我对 flash 的查看代码

<div id="flash-message-wrapper">
    <% flash.each do |type, message| %>
        <div class="alert <%= alert_class_for(type) %> alert-dismissible fade in">
            <button type="button" class="close" data-dismiss="alert">
                <span aria-hidden="true">&times;</span>
                <span class="sr-only">Close</span>
            </button>
            <%= message %>
     <% end %>
</div>

这段代码的 CSS 是:

.alert {
  padding: 15px;
  margin-bottom: 20px;
  border: 1px solid transparent;
  border-radius: 4px;
}
.alert h4 {
  margin-top: 0;
  color: inherit;
}
.alert .alert-link {
  font-weight: bold;
}
.alert > p,
.alert > ul {
  margin-bottom: 0;
}
.alert > p + p {
  margin-top: 5px;
}
.alert-dismissable,
.alert-dismissible {
  padding-right: 35px;
}

.alert-dismissable .close,
.alert-dismissible .close {
  position: relative;
  top: -2px;
  right: -21px;
  color: inherit;
}
.alert-success {
  color: #3c763d;
  background-color: #dff0d8;
  border-color: #d6e9c6;
}
.alert-success hr {
  border-top-color: #c9e2b3;
}
.alert-success .alert-link {
  color: #2b542c;
}
.alert-info {
  color: #31708f;
  background-color: #d9edf7;
  border-color: #bce8f1;
}
.alert-info hr {
  border-top-color: #a6e1ec;
}
.alert-info .alert-link {
  color: #245269;
}
.alert-warning {
  color: #8a6d3b;
  background-color: #fcf8e3;
  border-color: #faebcc;
}
.alert-warning hr {
  border-top-color: #f7e1b5;
}
.alert-warning .alert-link {
  color: #66512c;
}
.alert-danger {
  color: #a94442;
  background-color: #f2dede;
  border-color: #ebccd1;
}
.alert-danger hr {
  border-top-color: #e4b9c0;
}
.alert-danger .alert-link {
  color: #843534;
}

.fade {
  opacity: 0;
  -webkit-transition: opacity .15s linear;
       -o-transition: opacity .15s linear;
          transition: opacity .15s linear;
}
.fade.in {
  opacity: 1;
}

.close {
  float: right;
  font-size: 21px;
  font-weight: bold;
  line-height: 1;
  color: #000;
  text-shadow: 0 1px 0 #fff;
  filter: alpha(opacity=20);
  opacity: .2;
}
.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
  filter: alpha(opacity=50);
  opacity: .5;
}
button.close {
  -webkit-appearance: none;
  padding: 0;
  cursor: pointer;
  background: transparent;
  border: 0;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}
.sr-only-focusable:active,
.sr-only-focusable:focus {
  position: static;
  width: auto;
  height: auto;
  margin: 0;
  overflow: visible;
  clip: auto;
}

#flash-message-wrapper {
    position: fixed;
    top: 50px;
        left: 50%;
        margin-left: -250px;
    width: 600px;
}

@media (max-width: 500px) {
    #flash-message-wrapper {
        position: fixed;
        top: 50px;
            right: 50%;
            margin-right: -250px;
        width: 300px;
    }
}

任何帮助将不胜感激!

【问题讨论】:

    标签: javascript html css ruby-on-rails twitter-bootstrap


    【解决方案1】:

    你需要把#flash-message-wrapper的样式改成这个

    #flash-message-wrapper {
      width: 600px;
      margin: 10px auto 0 auto;
    }
    
    @media (max-width: 500px) {
      #flash-message-wrapper {
        width: 300px;
        margin: 10px auto 0 auto;
      }
    }
    

    这是一个有效的fiddle。希望这会有所帮助。

    更新

    如果您想悬停通知,您需要将以下这些属性添加到您现有的样式中。

    position: absolute;
    left: 0;
    right: 0;
    

    更新fiddle

    【讨论】:

    • 尝试调整预览窗格的大小以查看效果。
    • 这个问题是它没有悬停在我拥有的元素上。这段代码将推倒我不想要的整个视图。
    • 查看更新。让我知道这是否对您有用。
    猜你喜欢
    • 1970-01-01
    • 2018-07-29
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 1970-01-01
    • 2019-06-30
    相关资源
    最近更新 更多