Web2Py 中 IMG() 的 CSS 样式

0 投票
2 回答
849 浏览
提问于 2025-04-18 21:25

我正在尝试在Web2Py中使用{{=IMG()}}来设置CSS的基本语法。为了实现这个,我试过以下几种方法:

 <style>img.bottom { vertical-align: text-bottom; } </style>
 <body>
     {{=IMG(_src=URL('static',args='images/golem.jpg', _style='img.bottom'),_alt="golem"}}
 </body>

失败了

 <body>
     {{=IMG(_src=URL('static',args='images/golem.jpg', styles={'CODE':'vertical-align: text-bottom;'}), _alt="golem")}}
 <body>

也失败了

 <style>img.bottom { vertical-align: text-bottom; } </style>
 <body>
      {{=IMG(_src=URL('static',args='images/golem.jpg'), _style='bottom',_alt="golem"}}
 </body>

还是失败了

 <style>img.bottom { vertical-align: text-bottom; } </style>
 <body>
      {{=IMG(_src=URL('static',args='images/golem.jpg'), _class='bottom',_alt="golem"}}
 </body>

依然失败了

我感觉我应该快要成功了,但在文档中找不到给IMG()设置样式的参数,而且似乎也无法覆盖现有的(无)样式。

如果有人想看看Web2Py的文档,答案并不在这里

2 个回答

0

虽然这个方法确实有效,但我不喜欢它。看起来很糟糕,我不认为这算是一个好的答案。我只是想提供一个解决办法,以防明年还有其他人遇到这个(没有答案的问题)。

 <style> img.bottom { vertical-align: text-bottom; } </style>
 <body>
     <img src={{=URL('static','images',args='golem.jpg')}} alt="golem"class=bottom>
 </body>
2

你可以这样做:

{{=IMG(_src=URL('static', 'images', args='golem.jpg'), _alt='golem',
       _style='vertical-align: text-bottom')}}

或者:

<style>img.bottom {vertical-align: text-bottom;}</style>
{{=IMG(_src=URL('static', 'images', args='golem.jpg'), _alt='golem',
       _class='bottom')}}

正如文档中提到的这里

以_开头的命名参数会被当作HTML标签的属性来理解(去掉下划线)。

虽然书里没有具体展示如何在IMG中使用"_style"或"_class"这些参数,但有很多关于"_class"参数以及其他HTML属性与其他HTML助手一起使用的例子。

撰写回答