如何在css和html中设置div的高度 [英] How to set div's height in css and html
问题描述
我使用Twitter Bootstrap。我在我的HTML中写道:
< div style =height:100px; class =span12>
asdfashdjkfhaskjdf
< / div>
为什么我打开该页面时高度不是100像素?
解决方案使用:
< div style =height:100px;>
asdfashdjkfhaskjdf
< / div>
内联样式用于一个目的,但在大多数情况下不建议使用。
更合适的解决方案是创建一个单独的CSS表,将其包含在HTML文档中,然后使用ID或类来引用您的div。
如果您具有文件结构:
index.html
> ;> / css /
>> /css/styles.css
在
< head>
和< / head>
之间的HTML文档中写入:< link href =css / styles.css =stylesheet/>
然后,将您的div结构更改为:
< div id =someidnameclass =someclassname>
asdfashdjkfhaskjdf
< / div>
在css中,您可以从ID或CLASS引用您的div。
这样写:
.someclassname {height:100px; }
或
code> #someidname {height:100px; }
请注意,如果你同时使用这两个文件结构,
例如...如果你有:
.someclassname {height:100px; }
.someclassname {height:150px; }
然后在这种情况下,高度将为150像素。
编辑:
要回答您的编辑中的次要问题,可能需要
overflow:
或overflow:visible;
。您也可以这样做:< div class =span12>
< div style =height:100px;>
asdfashdjkfhaskjdf
< / div>
< / div>
I'm using Twitter Bootstrap. I wrote in my html:
<div style="height:100px;" class="span12"> asdfashdjkfhaskjdf </div>
Why the height is not 100px when I open that page?
解决方案To write inline styling use:
<div style="height: 100px;"> asdfashdjkfhaskjdf </div>
Inline styling serves a purpose however, it is not recommended in most situations.
The more "proper" solution, would be to make a separate CSS sheet, include it in your HTML document, and then use either an ID or a class to reference your div.
if you have the file structure:
index.html >>/css/ >>/css/styles.css
Then in your HTML document between
<head>
and</head>
write:<link href="css/styles.css" rel="stylesheet" />
Then, change your div structure to be:
<div id="someidname" class="someclassname"> asdfashdjkfhaskjdf </div>
In css, you can reference your div from the ID or the CLASS.
To do so write:
.someclassname { height: 100px; }
OR
#someidname { height: 100px; }
Note that if you do both, the one that comes further down the file structure will be the one that actually works.
For example... If you have:
.someclassname { height: 100px; } .someclassname { height: 150px; }
Then in this situation the height will be 150px.
EDIT:
To answer your secondary question from your edit, probably need
overflow: hidden;
oroverflow: visible;
. You could also do this:<div class="span12"> <div style="height:100px;"> asdfashdjkfhaskjdf </div> </div>
这篇关于如何在css和html中设置div的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!