Html 我可以使用 CSS 将图像放在 <td> 标签中吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4532276/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:51:21  来源:igfitidea点击:

Can I put an image in a <td> tag using CSS?

htmlcss

提问by Newbtophp

I have the following HTML:

我有以下 HTML:

<td colspan="23"><img src="images/layout_15.gif" width="887" height="115"></td>

But I was wondering if that can be changed to something like:

但我想知道是否可以将其更改为:

<td colspan="23" class="something">

or:

或者:

<td colspan="23" id="something">

and then can I define a style for the image in a CSS file? If so, how, and is it better doing it that way?

然后我可以在 CSS 文件中为图像定义样式吗?如果是这样,怎么做,这样做更好吗?

采纳答案by Paul D. Waite

I wouldn't replace the widthand heightattributes of an <img>tag with CSS ones. Bitmap image files have an inherent pixel width and height, and that information is useful to the browser (for page layout) before the CSS file is downloaded and parsed.

我不会用 CSS替换标签的widthheight属性<img>。位图图像文件具有固有的像素宽度和高度,在下载和解析 CSS 文件之前,该信息对浏览器(用于页面布局)很有用。

Having re-read your question though, maybe you're trying to get rid of the <img>tag altogether? If so, if you've got this HTML:

重新阅读您的问题后,也许您正试图<img>完全摆脱标签?如果是这样,如果你有这个 HTML:

<td colspan="23" class="something"></td>

You can get the image to show up in it like this:

您可以像这样显示图像:

.something {
    width: 887px;
    height: 115px;
    background: url(images/layout_15.gif) left top no-repeat;
}

Note that the image's file path in CSS is relative to the CSS file.

请注意,CSS 中图像的文件路径是相对于 CSS 文件的。

回答by Dave Anderson

You require a Descendant CSS Selectorso that you can target the image elements in a particular class/id of <td>.

您需要一个后代 CSS 选择器,以便您可以将特定类/id 中的图像元素作为目标<td>

回答by Paul S

If you want all images embedded in the td to have that same width and height, you can use:

如果您希望 td 中嵌入的所有图像具有相同的宽度和高度,您可以使用:

.something img{
 width:887px;
 height:115px;
}

回答by KTastrophy

Absolutely! It is much better. Just ensure that you aren't sill using them to do layouts. Tables should only be used for tabular data.

绝对地!好多了。只要确保您不会使用它们来进行布局。表格应该只用于表格数据。

if your html is:

如果您的 html 是:

<td class="first"></td>

Then your CSS can be

那么你的 CSS 可以是

.first {
background: #ccc;
border: 1px solid #999;
font-size: 16px;
}

For example, of course.

例如,当然。

回答by Douglas

Do you mean like this?

你的意思是这样吗?

CSS:

CSS:

.something img { border: 10px solid red; }

HTML:

HTML:

<td class="something"><img src="images/layout_15.gif"></td>