如何在 HTML 中设置按钮的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25014849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:22:21  来源:igfitidea点击:

How to set the size of button in HTML

htmlbutton

提问by AdityaK

I have some buttons on my pure HTML/JS page. When the page is opened in browser, the button size is normal. But on refresh/reloading page, the button size is reduced. In fact, I have not set the button text value. The button's text is blank. How should I set the size of my button in HTML irrespective to the size of the text?

我的纯 HTML/JS 页面上有一些按钮。在浏览器中打开页面时,按钮大小是正常的。但是在刷新/重新加载页面上,按钮大小会减小。其实我还没有设置按钮文本值。按钮的文本是空白的。无论文本大小如何,我应该如何在 HTML 中设置按钮的大小?

回答by Gab

Do you mean something like this?

你的意思是像这样

HTML

HTML

<button class="test"></button>

CSS

CSS

.test{
    height:200px;
    width:200px;
}

If you want to use inline CSS instead of an external stylesheet, see this:

如果你想使用的,而不是外部的样式表内联CSS,看到这个

<button style="height:200px;width:200px"></button>

回答by Lemuel Botha

This cannot be done with pure HTML/JS, you will need CSS

这不能用纯 HTML/JS 来完成,你需要 CSS

CSS:

CSS:

button {
     width: 100%;
     height: 100%;
}

Substitute 100% with required size

用所需尺寸替换 100%

This can be done in many ways

这可以通过多种方式完成

回答by ybdiel

button { 
  width:1000px; 
} 

or even

甚至

 button { 
    width:1000px !important
 } 

If thats what you mean

如果这就是你的意思

回答by Jon Lee-White

If using the following HTML:

如果使用以下 HTML:

<button id="submit-button"></button>

Style can be applied through JS using the style object available on an HTMLElement.

可以使用 HTMLElement 上可用的样式对象通过 JS 应用样式。

To set height and width to 200px of the above example button, this would be the JS:

要将上面示例按钮的高度和宽度设置为 200px,这将是 JS:

var myButton = document.getElementById('submit-button');
myButton.style.height = '200px';
myButton.style.width= '200px';

I believe with this method, you are not directly writing CSS (inline or external), but using JavaScript to programmatically alter CSS Declarations.

我相信使用这种方法,您不是直接编写 CSS(内联或外部),而是使用 JavaScript 以编程方式更改 CSS 声明。

回答by Sadia Younas

#btn_l_w
{
height:100px;
width:200px;
font-size: 12px;
}
                           
<button  type="submit" id="btn_l_w">SAVE</button>