简体   繁体   English

使用CSS设置响应式通知消息的样式

[英]Styling a responsive notice message using CSS

I'm trying to style a notice message. 我正在尝试设置通知消息的样式。 That is an alert image with a message, like this . 这是一个警告图像与信息,像这样 I would like it to be with a minimal amount of html, so currently the html for the notice is only 我希望它使用的HTML数量最少,所以目前用于通知的HTML仅

<p class="notice error">This is a message</p>

The jsfiddle does what I want, except that the parent can be of any size, meaning the html above should be responsive. jsfiddle做了我想要的,除了父级可以是任何大小,这意味着上面的html应该是响应式的。 Is it possible to position the icon so that in any situation it is just before the message ? 是否可以将图标定位为在任何情况下都位于消息之前? And if this is not possible, other neat solutions are welcome too of course! 如果不可能,那么当然也欢迎其他整洁的解决方案!

Thnx n

Sorry for the previous solution, just edited my answer 对不起,以前的解决方案,只是编辑了我的答案

Use :before pseudo and it will do the job, this way you don't have to add an extra element in your HTML. 使用:before伪,它将完成工作,这样您就不必在HTML中添加额外的元素。

Demo 演示版

p.notice:before {
    content: "";
    height: 20px;
    width: 20px;
    display: inline-block;
    background-image: url('https://upload.wikimedia.org/wikipedia/commons/a/ac/Approve_icon.svg');
    background-repeat: no-repeat;
    background-size: 100%;
    vertical-align: middle;
}

Add img to HTML: img添加到HTML:

<div id="parent">
    <p class="notice error"><img src="http://www.foghornnews.com/wp-content/uploads/2013/02/Alert-Icon-.png" alt="warning" />This is a message</p>
</div>

And CSS: 和CSS:

html,body,#parent{
    width:100%;
}

p img{
    height:20px; /* some height */
}

JSFiddle JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM