简体   繁体   English

帮助在MVC3中使用CSS ID /类ID

[英]Help with using CSS id/class id's within MVC3

Hello all I am trying to work through (learn) MVC3 and I was playing aroudn with formatting my view using CSS. 大家好,我正在尝试学习(学习)MVC3,并且我正在使用CSS格式化视图。 When using html / webforms have been able to use div tags and apply to that div teh approiate css style. 使用html / webforms时,已经可以使用div标签并将其应用于该div,从而采用CSS样式。

When using MVC adn creating the layout in my view I have found that multiple CSS styles are not being inheritted. 使用MVC和在我的视图中创建布局时,我发现没有继承多个CSS样式。

for example: (sorry for the image but razor syntax seemed to fail on the code cut/paste 例如:(抱歉图片,但在剪切/粘贴代码时剃刀语法似乎失败了

在此处输入图片说明

In the above I had a div tag for certain sections. 在上面,我对某些部分使用了div标签。 however when I used the class property 但是当我使用class属性时

<div class="myfirststyle" >some content</div>

for each of these div tags only the first one would be detected. 对于每个div标签,只有第一个会被检测到。 The remaining div tags seemed to inherit from the main body style. 其余的div标签似乎继承自主体样式。 also defined in the main CSS file. 也定义在主CSS文件中。

Can anyone point me to some information on how to setup and style div tags within a view using CSS instead of the stylel property as shown in the above image OR is this correct as you cannot directly call CSS styles from a view as they are only recognized by the page layout.cshtml or masterpage? 谁能为我指出一些有关如何使用CSS而不是上图所示的stylel属性在视图中设置和标记div标签的信息,或者这是正确的,因为您不能直接从视图中调用CSS样式,因为它们只能被识别通过页面layout.cshtml还是母版页?

Thanks in advance 提前致谢

Update : to help troubleshoot what is happening please refer to the following 更新 :为了帮助解决正在发生的事情,请参考以下内容

Code within the view (using razor engine) 视图中的代码(使用剃须刀引擎) 在此处输入图片说明

code within the CSS CSS中的代码

CSS中的代码

output from developer toolbar notice how first div layer attributes are detected 开发人员工具栏的输出通知如何检测第一个div层属性 在此处输入图片说明

output from developer toolbar notice now how image div does not have a style applied 开发人员工具栏的输出现在注意,图像div如何未应用样式

在此处输入图片说明

What is interesting is that when viewing the source in html you can still see the CSS tags within the DIV tags they are just not being picked up or inherited for some reason after the first node/div tag 有趣的是,当在html中查看源代码时,您仍然可以在DIV标签中看到CSS标签,只是由于某些原因,它们在第一个node / div标签之后并未被拾取或继承

Thanks again for any pointers 再次感谢您的指导

Definitely not the case. 绝对不是这样。 Something else is going on. 发生了其他事情。

Did you include your CSS file in the view or it's master page? 您是否在视图或其母版页中包含CSS文件?

<link href="@Url.Content("~/Content/YourCSS.css")" rel="stylesheet" type="text/css" />

Or manually add the CSS to your view? 还是手动将CSS添加到视图中?

<style type="text/css">
    div.myfirststyle { ... }
</style>

If so, have you confirmed the spelling of classes? 如果是这样,您是否确认班级的拼写? Typos happen, and we can't see from your image whether or not you added classes to divs. 错别字发生了,我们无法从您的图像中看到您是否向div添加了类。 Add some of your code even if you can't format it well enough :) 添加一些代码,即使您无法很好地格式化它:)

There is definitely something else funky going on... it has nothing to do with MVC*, it occurs when the browser tries to render it, so the problem must lie with the html & css. 肯定还有其他时髦的事情……与MVC *无关,它在浏览器尝试呈现它时发生,因此问题必须出在html和css上。

If would recommend validating your HTML and CSS, this will show errors with missing closing tags or anything of the likes that can cause these sorts of errors. 如果建议验证您的HTML和CSS,这将显示缺少结束标记的错误或可能导致此类错误的类似错误。

I would recommend using Firefox or Chrome with the Web Developer Toolbar to make html & css validation easier. 我建议将Firefox或Chrome与Web Developer Toolbar结合使用 ,以简化html&css验证。

*nothing to do with MVC directly... indirectly, sure. *与MVC直接无关...可以肯定,间接与之无关。

Unfortunantly the error was all on my part. 不幸的是,错误全在我身上。 While I did have an error withiin my CSS file regarding the use of the position attribute within my styles. 尽管我的CSS文件中确实存在关于在样式中使用position属性的错误。 below is the updated style: 以下是更新后的样式:

.resultController
{
    position:static;
    width:90%;
    border: 1px solid #7C90BE;
    margin:5px;

}

.resultImage
{    
    display:table-cell;
    color:Green;
    height:36px;
    width:36px;
}

.resultContext
{
    display:table-cell;
    padding:3px;
    left:50px;
    color:Red;

}
.resultButtons
{
   top:5px;
}

The larger problem (all my fault) was the browser was not purging teh cache on close. 更大的问题(我所有的错)是浏览器没有关闭缓存。 After dumping the cache and reloading the page the styles began to render correctly. 转储缓存并重新加载页面后,样式开始正确呈现。 Thanks to all for taking the time to look at this and offer your input. 感谢大家抽出宝贵时间来研究此问题并提供您的意见。

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

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