简体   繁体   English

CSS-为什么这不起作用?

[英]CSS- Why this doesn't work?

I have a CSS Class: 我有一个CSS类:

.ImageBorder
{
border-style:solid; border-color:Black;
} 

I then set the CSS Class to an asp.net Image: 然后我将CSS类设置为asp.net图像:

<asp:Image ID="Image3" runat="server" Height="150px" 
                ImageUrl="~/Photos/defaultA.jpg" CssClass="ImageBorder"  />

But it doesn't work. 但它不起作用。 It will work if I put the image in a div tag and then set the divs css class, but I'd rather not do this? 如果我将图像放在div标签然后设置divs css类,它会工作,但我宁愿不这样做? What gives? 是什么赋予了?

尝试给边框宽度:

border: 1px solid black;

As noted in other answers, you should specify the border-width. 如其他答案中所述,您应指定边框宽度。 You may experience an additional "gotcha", though: asp images may have a "border-width:0" inline styling. 但是,您可能会遇到额外的“陷阱”:asp图像可能具有“border-width:0”内联样式。 In which case, your rule would need the !important declaration to override it: 在这种情况下,您的规则需要!important声明来覆盖它:

.ImageBorder
{
border: 1px solid black !important;
}

The !important declaration ignores the normal rules of precedence that CSS follows. !important声明忽略了CSS遵循的正常优先规则。 Normally, inline styling takes precedence over embedded or external style sheets, which take precedence over the user agent's styling. 通常,内联样式优先于嵌入式或外部样式表,这些样式表优先于用户代理的样式。 An important property will take precedence over all non-important properties, regardless of where it is declared. 无论声明的位置如何,重要属性都将优先于所有非重要属性。

Using !important can lead to some frustration, however. 然而,使用!important会导致一些挫败感。 Suppose you wanted to have an image with all the same qualities as your ImageBorder images, but with a width of 100. You might write 假设您想要一个与ImageBorder图像具有相同质量的图像,但宽度为100.您可能会写

<img src="superBorderedImage.png" class="ImageBorder" style="border-width:100"/>

... But this will not work. ......但这不起作用。 You specified with great importance that ImageBorder images have width 1, so your "super bordered image" won't have its special thick border. 您非常重视ImageBorder图像的宽度为1,因此您的“超边框图像”将没有特殊的粗边框。 For this reason, you should use !important only when you're certain you won't need to override it later for special cases. 因此,只有当您确定以后在特殊情况下不需要覆盖它时,才应使用!important。

As noted by David, you're not specifying any width for your border, which means it has no width and consequently, you can't see it. 正如David所说,你没有为边框指定任何宽度,这意味着它没有宽度,因此你看不到它。

You can use the shorthand: 你可以使用速记:

.ImageBorder
{
    border: 1px solid black;
}

or the longhand way: 或者是简单的方式:

.ImageBorder
{
    border-style: solid;
    border-color: Black;
    border-width: 1px;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM