【发布时间】:2012-03-30 17:45:07
【问题描述】:
考虑以下 HTML 代码:
<!DOCTYPE html>
<html>
<head>
<title>TEST</title>
<style>
button {
display: block;
margin: 10px;
}
</style>
</head>
<body>
<div style="width: 100px; border: 1px solid black">
<button>hello</button>
<button>hi</button>
</div>
</body>
</html>
我的问题是,如果按钮的 display 是 block,为什么按钮不会拉伸到 100% 宽度。如何做到这一点?我无法将按钮样式设置为 width: 100%,因为它们会因为边距而溢出其父块。
【问题讨论】:
-
你添加了 position:relative;然后添加 width:100% ?
-
Qtax:
box-sizing: border-box对我不起作用。不知道为什么。 -
EvilP:
position: relative在这里什么都不做。 -
我知道上面只是一个例子,但你的 DOCTYPE 声明中有一个错误。