CSS styling of flash message

百般思念 提交于 2019-12-07 07:22:37

问题


What should I use to style my flash messages in my CSS? I can't seem to change it's styling. Here's the relevant code within the <body> of my application layout:

    <div class="container">
      <%= render 'layouts/header' %>
      <section class="round">
        <div id= "notice">
          <% flash.each do |key, value| %>
            <div class="flash <%= key %>">
              <%= value %>
            </div>
          <% end %>
        </div>
        <%= yield %>
      </section>
      <%= render 'layouts/footer' %>
      <%= debug(params) if Rails.env.development? %>
    </div>

The relevant original CSS was something like this but it doesn't currently work.

.error, .alert, .notice, .success, .info {padding:0.8em;margin-bottom:1em;border:2px solid #ddd;}
.success {background:#e6efc2;color:#264409;border-color:#c6d880;}

回答1:


Either add a rule for parent div with id notice:

#notice {
  css_formatting_here
}

Or add a rule for child divs:

.flash {
  css_formatting_here
}

The child div of errors container has multiple classes separated by whitespace. flash is one of them. Thus you can add a CSS rule for that class, and it will work.

Look here for more such examples: Hidden features of CSS




回答2:


Looks like the CSS calls for a class .notice, but the div has an ID instead.

EDIT

The CSS is not responding to the flash or <%= key %> classes, but it is responding to the notice id. Unfortunately, if I style with the notice id, some styling appears that should be dependent on whether there is a flash message or not. How can I fix this

You could add a conditional test around the div:

<% unless flash.empty? %>
  <div id="notice">
    <% flash.each do |key, value| %>
      <div class="flash <%= key %>">
        <%= value %>
      </div>
    <% end %>
  </div>
<% end %>



回答3:


In your css file type:

.alert-success {
    dosomestyling;
}

.alert-danger {
    dosomestyling;
}


来源:https://stackoverflow.com/questions/5190090/css-styling-of-flash-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!