doukai1226 2015-04-27 07:51
浏览 144
已采纳

动态设置背景图像

I need to apply a background-image to a div that gets set dynamically. So the editor uploads an image on a specific page to a specific attribute and I catch this image then and display those.

Therefore I'd do something like this:

<div class="foo" style="background:url(<?php echo $attribute; ?>) no-repeat top right; background-size: 140px;">

Is there a better approach to it by not using style="..." ?

Thanks

  • 写回答

5条回答 默认 最新

  • dqwmhrxt68679 2015-04-27 08:03
    关注

    You approach is correct but I would change something.

    In the css I would make this:

    .foo {
        background-repeat: no-repeat;
        background-position: top right;
        background-size: 140px;
    }
    

    And in the HTML i would only keep the url.

    <div class="foo" style="background:url(<?php echo $attribute; ?>);">
    

    This way on the server-side it's easier to see where you have dynamic content. And you only need to change the url for the corresponding image.

    So if later you work with another developer he knows exactly where he should change the images without going to your css.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源