简体   繁体   English

使按钮的宽度与高度相同

[英]Make button same width as height

I have a toolbar on the top of my website, which is a fixed div . 我的网站顶部有一个工具栏,它是fixed div In there I have another div , with some buttons in it: 在那里,我还有另一个div ,其中有一些按钮:

|somethingsomething.com                                       |
---------------------------------------------------------------
|statusBar____________________________________________|buttons|
|rest of website                                              |

HTML: HTML:

<div class="statusBar">
    <div class="statusBarMenuButtons">
        <form class="homeButtonForm" action="?id=1" method="post"><button class="homeButton"></button></form>
        <form class="subscriptionsButtonForm" action="?id=2" method="post"><button class="subscriptionsButton"></button></form>
        <form class="searchButtonForm" action="?id=3" method="post"><button class="searchButton"></button></form>                   
    </div>
</div>

CSS: CSS:

div.statusBar{  
    position:fixed;
    top:0;
    width:100%;
    height:5%;
}

button{
    height:100%;
    margin-right:10px;
}

form{
    height:100%;
}

/* This is the same for all buttons, just different background-images */
button.homeButton{
    background:url(../resources/homeButton.png) no-repeat;
    background-size:auto 100%; /* width height */
}

Now, my problem is that I have no way of knowing which height the button will get, and therefore cannot know which width I should specify. 现在,我的问题是我无法知道按钮将达到的height ,因此无法知道应该指定的width

I have search a lot on this website and Google and found lots of answers about div s, also I found this jQuery : 我在这个网站和Google上进行了大量搜索,找到了很多有关div的答案,我也发现了这个jQuery

<script type="text/javascript">
    var height = $('button.homeButton').height();
    $('button.homeButton').css({
        'width' : height + 'px';
    });
</script>

But it does not seem to work. 但这似乎不起作用。 I have pulled this snippet form here and thought maybe the problem was that I am using a button instead of a div. 我在这里提取了这段代码形式并认为问题可能出在我使用的是按钮而不是div。

Help is very much appreciated. 非常感谢您的帮助。

Try this Fiddle This Fiddle is with the class you wanted 试试这个小提琴这个小提琴与您想要的课程一起使用

 var height = $('button.homeButton').height(); $('button').width(height); 
 div.statusBar { top:0; width:100%; height:5%; } button { height:100px; margin-right:10px; } form { height:100%; } /* This is the same for all buttons, just different background-images */ button.homeButton { background:url(../resources/homeButton.png) no-repeat; background-size:auto 100%; /* width height */ } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="statusBar"> <div class="statusBarMenuButtons"> <form class="homeButtonForm" action="?id=1" method="post"> <button class="homeButton"></button> </form> <form class="subscriptionsButtonForm" action="?id=2" method="post"> <button class="subscriptionsButton"></button> </form> <form class="searchButtonForm" action="?id=3" method="post"> <button class="searchButton"></button> </form> </div> </div> 

You don't need JS. 您不需要JS。

div.statusBar will take the height from child elements. div.statusBar将采用子元素的高度。 If form or button don't have a fixed height, it won't work. 如果表单或按钮没有固定的高度,则将无法使用。 Using height:5%; 使用height:5%; is taking height from body or html, which may be undefined. 从body或html取得高度,这可能是不确定的。

Try adding a fixed height to div.statusBar 尝试将固定高度添加到div.statusBar

div.statusBar {
   height: 60px;
}

Also, add display: block; 另外,添加display: block; to the button to apply width and height properties correctly. button以正确应用width和height属性。

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

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