简体   繁体   English

最大宽度px宽度100%

[英]Max-width px width 100%

Seems trivial but I seem to be struggling with this. 似乎微不足道,但我似乎为此感到挣扎。 I have a container div and content within. 我有一个容器div和内容。 The page needs to be responsive so naturally I am using max width (in px) on everything. 页面需要响应,所以自然地,我在所有内容上都使用了最大宽度(以像素为单位)。

I have added width: 100% to the div, h2 and p tags, but the width does not expand to the max-width. 我为div,h2和p标签添加了width:100%,但是宽度没有扩展到max-width。 How can I get the divs to expand to their max size without explicitly setting the width to this fixed value? 如何在不将宽度显式设置为此固定值的情况下使div扩展到最大大小?

css and html CSS和HTML

 .container {max-width:1200px; width:100%;} .container h2 {max-width:400px; width:100%;} .container p {max-width:900px; width:100%;} 
 <div class="container"> <h2>Dynamically populated using PHP</h2> <p>Dynamically populated using PHP</p> </div> 

EDIT: It was css buried in other divs causing the issue, the code above works. 编辑:这是CSS埋在其他div中引起的,上面的代码有效。

You forgot the semi-colons after the width rules. 您忘记了宽度规则后的分号。

<div class="container">
    <h2>Dynamically populated using PHP</h2>
    <p>Dynamically populated using PHP</p>
</div>

.container {max-width:1200px; width:100%; background-color: red;}
.container h2 {max-width:400px; width:100%; background-color: blue;}
.container p {max-width:900px; width:100%; background-color: green;}

http://jsfiddle.net/kr1b92mu/ http://jsfiddle.net/kr1b92mu/

I added the background colors to show that it's working :). 我添加了背景色以表明它正在工作:)。

The max-width property is used to set the maximum width of an element. max-width属性用于设置元素的最大宽度。 This prevents the value of the width property from becoming larger than max-width. 这样可以防止width属性的值变得大于max-width。 It is a "limit". 这是一个“极限”。

The width property is the one how stablish the exact width of an element. width属性是一种如何稳定元素的确切宽度的属性。

http://www.w3schools.com/cssref/pr_dim_max-width.asp http://www.w3schools.com/cssref/pr_dim_max-width.asp

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

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