DIV 宽度不听我的 CSS
下面的代码是我想要做的示例,我需要这个 div 跨度为 600px 宽,但是无论我是否放置 max-width: 600px; 或宽度:600px; 它仍然是我的电脑屏幕的宽度。
我怎样才能让它宽600px?
<style>
.errormsg3 {
text-align: center;
font-weight:bold;
color:white;
font-size: 16px;
background-color:red;
padding:8px;
max-width: 600px;
}
</style>
<?PHP
if (isset($_SESSION['sess_msg'])) {
echo '<div class="errormsg3">' .$_SESSION['sess_msg']. '</div>';
unset($_SESSION['sess_msg']);
}
?>
更新
现在可以正常工作了,firefox 似乎正在缓存我的网站,所以也许这就是它以前无法工作的原因。
div 是红色的,是否可以在红色部分之外的 div 中添加填充,因此就像白色的填充/空间
The code below is an example of what I am trying to do, I need this div to span 600px wide however regardless if I put max-width: 600px; or width: 600px; it is still the width of my PC screen.
How can I make it 600px wide?
<style>
.errormsg3 {
text-align: center;
font-weight:bold;
color:white;
font-size: 16px;
background-color:red;
padding:8px;
max-width: 600px;
}
</style>
<?PHP
if (isset($_SESSION['sess_msg'])) {
echo '<div class="errormsg3">' .$_SESSION['sess_msg']. '</div>';
unset($_SESSION['sess_msg']);
}
?>
UPDATE
It is working now, firefox seems to be caching my site so maybe thats why it wouldn't work before.
The div is red, is it possible to add padding to the div, outside of the red part, so there is like a padding/space that is white
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
CSS 对我来说效果很好。 您确定没有其他 CSS 规则覆盖它吗? 尝试使用 Firebug 等工具,它可以向您显示元素的计算样式及其来源。
That CSS works fine for me. Are you sure you don't have another CSS rule that's overriding it? Try using a tool like Firebug that can show you the computed style for an element and where it came from.
我会尝试使用 Firebug 对其进行调试,以确保没有任何内容覆盖该 div 类,并检查包含元素的布局方式。
I would try debugging it with Firebug to ensure that nothing is overriding that div class, and checking to see how the containing elements are laid out.
IE 不喜欢最大宽度。 已在 IE/FF/Chrome 中测试并运行。
IE doesn't like max-width. Tested and works in IE/FF/Chrome.
确保你的内部没有任何东西比div更大。 IE 有时(取决于版本)通过扩展 div 来容纳内容,从而呈现具有更大内部内容的 div。
测试这一点的简单方法是添加溢出:隐藏并查看它是否隐藏了任何内容。
Make sure there is nothing inside of your that is larger than the div. IE sometimes (depends on version) renders divs with larger interior content by expanding the div to account for the content.
The easy way to test this is to add overflow:hidden and see if it hides anything.
使用它作为 HTML 文件,
我在 FF 3.5.2、Opera 9.64、Vista 32 位上的 IE 8.0.6001.18813 上得到居中 600px 宽的消息。 IE 需要额外的文本对齐来使块居中,而其他浏览器需要使用
margin: 0 auto
。Using this as a HTML file
I get a centred 600px wide message on FF 3.5.2, Opera 9.64, IE 8.0.6001.18813 on Vista 32 bit. IE needs the extra text-align to centre the block, the
margin: 0 auto
does for the other browsers.通常在属性后面添加 important 会有所帮助,因为它将覆盖父级声明的所有其他宽度。 你需要这样写:
Often adding important behind the attribute will help, because it will override all the other widths the parents declared. You need to write it like this:
添加溢出:自动; 到你的CSS。
Add overflow: auto; to your CSS.