dongshi8359 2012-07-13 14:04
浏览 29
已采纳

如何动态更改CSS

I need my administrator to be able to change/update the banner of my site.

This is the banner code

<div class="containertop">This depends on the background of the div</div>

and this is the CSS for that

.containertop
{
     width:1000px;
     height:300px;  
     **background:url(../images/1.png) no-repeat center;**
     margin: 0 auto;
     margin-top: 40px;
}

What I would like to happen is the same as a Facebook cover photo. When a new banner is uploaded, the CSS will be updated(something like that). But of course, the new banner must be fetched from the database.

So I am thinking that the CSS would become like this:

Fetch the saved banner source and then:

background:url(<?php echo $row['image']; ?>);

but can I do the PHP connection to database (include 'dbname.php') inside a CSS txt?

  • 写回答

4条回答 默认 最新

  • doulou1970 2012-07-13 14:17
    关注

    I agree with Ben. If you make a little embedded css section in your page, you could put the .containerTop css code there. Then, put your code in the page. So, in your actual web page, put this:

    <style type="text/css">
    .containertop{
    width:1000px;
    height:300px;  
    background:url(<?php echo $row['image']; ?>);
    margin: 0 auto;
    margin-top: 40px;
    }
    </style>
    

    Of course, your background url will not update until it is reloaded. If you decide to do it this way, don't forget to take the .containerTop definition out of your existing css. Having said all that, I really like dystroy's answer. Very nice. I never thought of doing that.

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

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值