css - 是否可以设置由 "content"属性添加的图像的样式

标签 css

这是我的代码:

#my_div:before
{
  /* displaying the image */
  content: url("img path");
  /* centering the image */
  display: block;
  text-align: center;
  /* making the image responsive */
  max-width: 100%;
}
<div id="my_div"></div>

我试图通过 max-width:100% 属性使图像响应,但它不起作用

我的问题:有可能做这样的事情吗?

编辑

这个问题不是重复的,我希望尺寸在屏幕调整大小时自动缩放,而另一个问题为图像设置固定大小

最佳答案

试试这个:

#my_div:before
{
  /* displaying the image */
  content: url("img path");
  /* centering the image */
  display: block;
  text-align: center;
  /* making the image responsive */
  max-width: 100%;
  height: auto;
}

如果这不起作用,请尝试删除 :before 伪元素

#my_div
{
  /* displaying the image */
  content: url("img path");
  /* centering the image */
  display: block;
  text-align: center;
  /* making the image responsive */
  max-width: 100%;
  height: auto;
}

在不查看 html 上下文的情况下,这是我可以提供的最佳解决方案。我们需要更多信息才能更好地了解您的情况。

关于css - 是否可以设置由 "content"属性添加的图像的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32204664/

相关文章:

html - 部分不从页面下移至顶部

javascript - 如果兄弟 div 中多于 n 行,则隐藏显示更多/更少 div

jquery - 使用多个按钮和 div 展开和折叠

html - 如何制作其父项的 flex 元素高度?

html - CSS div 不 float 在居中的 div 旁边

html - Level 3 <nav> 在该隐藏的时候没有隐藏

html - 如何使用此模板搜索按钮代替表单中的提交按钮?

html - div 元素未选取的外部样式

javascript - 我的 mousedown 函数不是从最近的点画线,而是从左上角画线

javascript - 在 JQuery 中加载文档时淡化主体渐变