简体   繁体   English

浏览器宽度的100%宽度div

[英]100% width div for browser width

I am trying to add two divs inside the parent div, which has a button inside each div. 我想在父div中添加两个div,每个div中都有一个按钮。 I need to fix the width in pixels only for the second div and the 1st div should be having width in % so that the button inside the 1st div should be covering the entire space of the browser. 我需要为第二个div修复宽度(以像素为单位),第一个div的宽度应为%,这样第一个div内的按钮应该覆盖浏览器的整个空间。

I need all the widths in % and also I don't want to change either html structure and css because it is already implemented so i just need changes in css property. 我需要%的所有宽度,我也不想改变html结构和CSS,因为它已经实现,所以我只需要更改css属性。

Here is my demo http://jsfiddle.net/zuyyT/2/ 这是我的演示http://jsfiddle.net/zuyyT/2/

PS : When I scale the browser, the second div is coming in next line. PS:当我缩放浏览器时,第二个div将进入下一行。 Please scale it and check once. 请缩放并检查一次。

Fiddle is working on and off ... you can go either one of two ways; 小提琴正在上班和下班......你可以选择以下两种方式之一; using floats (need to change the order of your markup) or positioning - like such ... 使用浮动(需要更改标记的顺序)或定位 - 像这样......

<div class="block">
    <div class="block_right"><a href=""> <span>last button</span></a> </div>
    <div class="block_left"><a href="" class="scButton score" > <span>Lorem ipsum</span></a></div>
</div>

and your CSS ... 和你的CSS ......

.block {
    display:block; background-color:#FFC; width:100%; float:left; height:30px
}
.block_left{
    background-color:#C93; margin-right: 150px;  
}
.block_left a{
    background-color:#CCC; border-radius:4px; padding:4px; width:100%; display:block
}
.block_right{
    float:right; width:130px; background-color:#CC9
}

... using position, you'll need to add position:relative to .block and then right:0 to .block_right; ...使用位置,你需要添加position:relative对于.block然后right:0到.block_right; keep the margin on .block_left 保留.block_left的保证金

Using positioning, you won't need to change the order of the elements in your markup (should that be an issue). 使用定位,您无需更改标记中元素的顺序(如果这是一个问题)。

This may be what you require. 这可能是你需要的。 :-) :-)

.block_right{
    position :absolute;
    right:0;
    top:0;
    float:right; width:130px; background-color:#CC9
}

If you give your block_left a width:100% and then use margin-right:-130px; 如果你给你的block_left width:100%然后使用margin-right:-130px; you can leave your html exactly as it is. 你可以完全保留你的html。

The negative right margin leaves space on the right hand side for other elements to fit into even though the element has a 100% width. 负右边距在右侧留出空间,以便其他元素适合,即使元素具有100%宽度。

发生这种情况是因为右侧div的宽度给父母100%,第一个孩子80%。所以,当浏览器大小为500px(比方说)时,第一个孩子将占用400px(80%)它...当你给第二个孩子130 px时,它会到达下一行...这很明显因为它在第一行没有足够的空间......所以它应该是<= 100px(本例)......

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

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