Html 无法更改 <div> 的高度/宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15779250/
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
Can't change height/width of <div>
提问by Alex G
I'm learning HTML/CSS and decided to try this:
我正在学习 HTML/CSS 并决定尝试这个:
<p>A paragraph with no margins specified.</p>
<div style="width=300px; height=300px; background-color:yellow;><p>This paragraph is in a divider.</p></div>
<p>A paragraph with no margins specified.</p>
After some googling, I found that you can't change width/height for inline elements. So I tried making both the <div>and <p>into block elements using display:block, but that didn't change anything. The width goes on infinitely off-screen, and the height is only as high as the <p>is.
I want there to be a <p>in a 300x300 <div>, so how can I do this? Also, why doesn't any of these ways work (even when I change to display:block)?
Thanks.
Also, I know you shouldn't use excessive CSS like I did. This is only because I was testing stuff with W3's "Try-It-Yourself" HTML editor.
经过一番谷歌搜索后,我发现您无法更改内联元素的宽度/高度。因此,我尝试使用 将<div>和制作<p>成块元素display:block,但这并没有改变任何东西。宽度在屏幕外无限延伸,高度仅与实际一样高<p>。我希望有<p>一个 300x300 <div>,那么我该怎么做呢?另外,为什么这些方法中的任何一种都不起作用(即使我更改为display:block)?谢谢。
另外,我知道您不应该像我一样使用过多的 CSS。这只是因为我正在使用 W3 的“Try-It-Yourself”HTML 编辑器测试内容。
回答by Praveen Kumar Purushothaman
Errors in your code:
您的代码中的错误:
<div style="width=300px; height=300px; background-color:yellow;>
-----------------^-------------^-------------------------------^
Replace with : Missing "
You are giving it wrong. Replace =with :, and you missed a ":
你错了。替换=为:,你错过了一个":
<div style="width:300px; height:300px; background-color:yellow;">
回答by fletch
Both divand pare block elements by default, so you do not need to specify display:block;for them. You are using old-school html attribute name/value setting (name = value) inside your style attribute.
双方div并p在默认情况下块元素,这样你就不需要指定display:block;他们。您在样式属性中使用老式 html 属性名称/值设置(名称 = 值)。
You'll want to use CSS properties inside the style attribute that look like name:value;.
您需要在样式属性中使用 CSS 属性,类似于name:value;.
style="width:300px; height:300px; background-color:#FFFF00;"
You might want to take a look at some of the resources at Team Treehouse: http://teamtreehouse.com/library/websites/build-a-simple-website/website-basics
您可能想查看 Team Treehouse 的一些资源:http: //teamtreehouse.com/library/websites/build-a-simple-website/website-basics
or Code School: http://www.codeschool.com/paths/html-css
或代码学校:http: //www.codeschool.com/paths/html-css
Hope this helps you continue learning.
希望这有助于您继续学习。
回答by user1837155
You can also try using "max-width" instead of just "width."
您也可以尝试使用“max-width”而不仅仅是“width”。

