css - 使用 :before CSS pseudo element to add image to modal

标签 css modal-dialog pseudo-element

我有一个 CSS 类 Modal,它是绝对定位的,z-indexed 在它的父级之上,并且用 JQuery 很好地定位。我想在模式框的顶部添加一个插入符号图像 (^),并正在考虑使用 :before CSS 伪选择器来干净地完成此操作。

图像需要绝对定位并在模式上方进行 z 索引,但我还没有找到任何方法在 content 属性中为图像添加适当的类:

.Modal:before{
  content:url('blackCarrot.png') /* with class ModalCarrot ??*/
}

.ModalCarrot{
   position:absolute;
   left:50%;
   margin-left:-8px;
   top:-16px;
}

第二个最佳选择 - 我可以在内容属性中添加内联样式吗?

最佳答案

http://caniuse.com/#search=::after

::after::beforecontent更好用,因为除 Internet Explorer 外,每个主流浏览器都支持它们,至少支持 5 个版本。 Internet Explorer 在版本 9+ 中完全支持,在版本 8 中部分支持。

这是您要找的吗?

.Modal::after{
  content:url('blackCarrot.png'); /* with class ModalCarrot ??*/
  position:relative; /*or absolute*/
  z-index:100000; /*a number that's more than the modal box*/
  left:-50px;
  top:10px;
}

.ModalCarrot{
   position:absolute;
   left:50%;
   margin-left:-8px;
   top:-16px;
}

如果不是,你能解释得更好一点吗?

或者你可以使用 jQuery,就像 Joshua 说的:

$(".Modal").before("<img src='blackCarrot.png' class='ModalCarrot' />");

关于css - 使用 :before CSS pseudo element to add image to modal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6668577/

相关文章:

html - CKEditor v4 : Indent after heading

php - 表格数据格式化(来自数据库的数据)

javascript - CSS 与 Javascript

scroll - iOS 15 上 safari 地址栏的 CSS 问题

mfc - 在运行时将无模式对话框转换为模式对话框

firefox - Firefox 中的视觉伪像,可能是由于 css-transform 或::after 元素?

html - 试图让 DIV 占据 100% 的视口(viewport)(div 转义父 div)

javascript - Angular Strap - 模态 Html 转换不起作用

hover - 如何在父项悬停时更改伪元素子项?

CSS如何添加新的伪内容