HTML 为啥设置width不管用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HTML 为啥设置width不管用相关的知识,希望对你有一定的参考价值。
在html文件中的代码如下<html>
<head>
<meta charset="utf-8"/>
<link rel="stylesheet" href="css/stylefrag.css">
</head>
<body>
<p>I m testing...</p>
<p>Ruby on Rails. . .by now most people have heard the hype about it. It promises more effective code, total object orientation, and true MVC architecture to say the least. As far as my own personal experience, it has been all that and more. The code is beautiful, easy to maintain, and edit. In a recent project I was working on, it took me less than 10 hours to do the application, as compared to at least double that if I was writing in php. </p>
</body>
</html>
在stylefrag.css文件中代码如下:
body
width: 50%;<!--问题所在之处-->
color: red;
问题是,为什么我的width 不论写200px还是40%,网页body部分的排版丝毫没有放大缩小呢,快疯了。。
<html>
<head>
<title>width的测试</title>
<style>
.span1
width:40px; //span1是行级元素,这样写的没用的
height:40px;
.span2
display:block;
width:40px; //span2是块级元素,这样写是可以的
height:40px;
</style>
</head>
<body>
<div>
<span class='span1'>我是测试文字</span>
<span class='span2'>我是测试文字</span>
</div>
</body>
</html> 参考技术A
body的宽度没记错的话是不能设置的吧,默认就是浏览器宽度吧。你如果很想限制一下其中文字的宽度,你可可以加一个DIV啊。
<head><meta charset="utf-8"/>
<link rel="stylesheet" href="c
ss/stylefrag.css">
</head>
<body>
<div class="textarea">
<p>I m testing...</p><p>Ruby on Rails. . .by now most people have heard the hype about it. It promises more effective code, total object orientation, and true MVC architecture to say the least. As far as my own personal experience, it has been all that and more. The code is beautiful, easy to maintain, and edit. In a recent project I was working on, it took me less than 10 hours to do the application, as compared to at least double that if I was writing in PHP. </p>
</div>
</body>
</html>
完了之后设置DIV的格式就可以了
.textarea <!--CSS类选择器-->width: 50%;
color: red;
本回答被提问者采纳 参考技术B BODY是没有宽度之分的,BODY就是网页的主体,页咱们肉眼能看到的都称为主体,也就是说浏览器里展示出来的内容全是BODY,试想一下,BODY是浏览器里的全部,那么宽度必定也是随着浏览器的宽度走的。所以你设置width是不起作用的,如果想要实现这种效果,一般情况下是在body里加一个div把div的width设置为50%。这样就可以了。 参考技术C body的宽度属性是自动适应浏览器宽度的,不能设置,你可以在最外面加个层,给这个新夹层设置宽度即可,并且要记得加上margin:0 auto;居中 参考技术D 记得宽度100% 以body为单位。
body 里加一层 标签,例如 <table> 就可以:
<html>
<head> <meta charset="utf-8"/> </head>
<body>
<table width=300> <TR><TD>
<p>I m testing...</p><p>Ruby on Rails. . .by now most people have heard the hype about it. It promises more effective code, total object orientation, and true MVC bala bala....
</p>
</TD></TR></table>
</body>
</html>
以上是关于HTML 为啥设置width不管用的主要内容,如果未能解决你的问题,请参考以下文章
html和css的一个问题,关于body设置width后并且居中显示后为啥背景色不遵循约束?