2014-09-02 113 views
-1

我有一个元素的流体宽度(由于不同的屏幕分辨率)和125px高度。我希望它从左边125px,右边125px。在某种程度上,我想是以中心为中心的。我怎么能做到这一点?也许我很措辞我的问题,但我没有找到答案,当我谷歌,甚至当我搜索stackoverflow。如何从左侧和右侧平均分配元素?

这里的CSS(我知道right:125px;不工作,但我增加它在你们来还挺了解我想要达到什么效果?):

#header { 
position:fixed; 
width:100%; 
height:125px; 
top:0; 
left:125px; 
right:125px; 
text-align:right; 
background:black; 
color:white; 
font-size:100px; } 

和这里的HTML:

<div id="header">header</div> 

and the fiddle:http://jsfiddle.net/k4tefz34/4/
非常感谢你们; o;

回答

1

您必须删除宽度:100%。试试这个:

#header { 
    position:fixed; 
    height:125px; 
    top:0; 
    left:125px; 
    right:125px; 
} 
0

改变位置,不要把它固定

#header { 
    position : relative; 
    width : 100% ; 
    height : 125px; 
    top : 0 ; 
    left : 125px; 
    right : 125px; 
} 
3

它的工作很好,但只是你定义的宽度为100%左右,这是造成从右侧显示。只是减少250个像素(左值+右值),宽度为100%,则请参阅左125px,右125px:

#header { 
    position:fixed; 
    width: calc(100% - 250px); 
    height:125px; 
    top:0; 
    left:125px; 
    right:125px; 
    text-align:right; 
    background:black; 
    color:white; 
    font-size:100px; 
} 

demo

0

你必须undertsand是什么,如果事情是100%的宽度,它不能有margin左右

同样,对于fixed DIV,你不能设置页边距

所以,你必须减少100%宽度也position除非你真的需要它

demoposition

demoposition

#header { 
    position:fixed; 
    width:50%; /* anything except 100% */ 
    height:125px; 
    top:0; 
    margin-left:125px; 
    margin-right:125px; 
    text-align:right; 
    background:black; 
    color:white; 
    font-size:100px; 
} 
0

你可以使用这个 -

#header { 
    background: none repeat scroll 0 0 black; 
    color: #fff; 
    height: 125px; 
    width: 100%; 
} 
.warp { 
    margin: 0 125px 0 126px; 
    position: relative; 
} 

JSFiddle

0

我想你应该改变你HTML代码这样

HTML

<div id="header"> 
    <div class="sub-header">header</div> 
</div> 

CSS

#header{ 
    position:fixed; 
    width:100%; 
    top:0; 
    left:0; 
} 
    #header .sub-header{ 
     background-color:#000000; 
     color:#FF6600; 
     height:125px; 
     margin:0 125px 0 125px; 
    } 

DEMO JSFiddle

0

有关设置保证金,而不是左,右的价值观,像这样怎么办?

position:fixed; 
width:100%; 
height:125px; 
top:0; 
margin:0px 125px; 
text-align:right; 
background:black; 
color:white; 
font-size:100px; 

这将中心你的div。

相关问题