如何在css中获取窗口大小?

时间:2013-03-09 21:31:58

标签: css css3 html header

我目前正在为我的网站编写标题,我有一位朋友正在帮助我。我们有两台非常不同的计算机(PC和Mac),并使用不同的浏览器来确保它是多平台和一切,我们发现标题不适合他的屏幕。所以我意识到,只需获得窗口宽度并将其除以4并使其成为按钮的大小就更容易了。我该怎么做呢?

非常感谢!

编辑: 这是我正在使用的:

    div.horizontal
{
    position: relative;
    width: 100%;
    height:45px;
}   

div.horizontal ul
{
list-style-type:none;
margin:0;
padding:0;
} 

div.horizontal li
{
float:left;
}

div.horizontal a
{
display:block;
background: -moz-linear-gradient(top,  rgba(255,255,255,0) 0%, rgba(255,255,255,0.10) 52%, rgba(255,255,255,0) 100%), url("../img/truefactzheader.png");
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(255,255,255,0)), color-stop(52%,rgba(255,255,255,0.10)), color-stop(100%,rgba(255,255,255,0))), url("../img/truefactzheader.png");
background: -webkit-linear-gradient(top,  rgba(255,255,255,0) 0%,rgba(255,255,255,0.10) 52%,rgba(255,255,255,0) 100%), url("../img/truefactzheader.png");
background: -o-linear-gradient(top,  rgba(255,255,255,0) 0%,rgba(255,255,255,0.10) 52%,rgba(255,255,255,0) 100%), url("../img/truefactzheader.png");
background: -ms-linear-gradient(top,  rgba(255,255,255,0) 0%,rgba(255,255,255,0.10) 52%,rgba(255,255,255,0) 100%), url("../img/truefactzheader.png");
background: linear-gradient(to bottom,  rgba(255,255,255,0) 0%,rgba(255,255,255,0.10) 52%,rgba(255,255,255,0) 100%), url("../img/truefactzheader.png");
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00ffffff', endColorstr='#00ffffff',GradientType=0 );
width:25%;
height:35px;
}

div.horizontal a:link,div.horizontal a:visited
{
font-weight:bold;
color:#FFFFFF;
text-align:center;
vertical-align:middle;
padding:4px;
text-decoration:none;
text-transform:uppercase;
}

div.horizontal a:hover,div.horizontal a:active
{
height:45px;
background: -moz-linear-gradient(top,  rgba(0,0,0,0.25) 0%, rgba(0,0,0,0.25) 100%), url("../img/truefactzheader.png");
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0.25)), color-stop(100%,rgba(0,0,0,0.25))), url("../img/truefactzheader.png");
background: -webkit-linear-gradient(top,  rgba(0,0,0,0.25) 0%,rgba(0,0,0,0.25) 100%), url("../img/truefactzheader.png");
background: -o-linear-gradient(top,  rgba(0,0,0,0.25) 0%,rgba(0,0,0,0.25) 100%), url("../img/truefactzheader.png");
background: -ms-linear-gradient(top,  rgba(0,0,0,0.25) 0%,rgba(0,0,0,0.25) 100%), url("../img/truefactzheader.png");
background: linear-gradient(to bottom,  rgba(0,0,0,0.25) 0%,rgba(0,0,0,0.25) 100%), url("../img/truefactzheader.png");
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#99000000', endColorstr='#99000000',GradientType=0 );
a:link {text-decoration:none;}
a:visited {text-decoration:none;}
}

1 个答案:

答案 0 :(得分:3)

好吧,假设您的标题元素是屏幕宽度的100%,并且您希望按钮宽度相等,则数学应该是> 100/4 = 25%。每个按钮应占据标题宽度的25%。

因此,在CSS中,您应该按如下方式定义它们:

#my_header {
    width: 100%;
    float: left;
}
    #my_header a.button {
        display: block;
        float: left;
        width: 25%;
        height: 30px
    }

请参阅此jsFiddle demo

因此,您需要按如下方式更新代码(为清晰起见,我已经取出了特定于浏览器的属性:

div.horizontal {
    position: relative;
    width: 100%;
    height:45px;
}   

div.horizontal ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
} 
div.horizontal li {
    float: left;
    width: 25%;
}

div.horizontal a {
    display: block;
    background: linear-gradient(to bottom,  rgba(255,255,255,0) 0%,rgba(255,255,255,0.10) 52%,rgba(255,255,255,0) 100%), url("../img/truefactzheader.png");
    height:35px;
}