Why would max-width not work on this?

2020-02-25 06:30发布

Using plain old CSS, why won't 'max-width' not work on the following:

button {
  text-align: center;
  max-width: 540px;
  height: auto;
  display: block;
  padding: 10px 0;
  margin: 0 auto;
  border: none;
}

The wrapper for this element:

#wrapper {
  max-width: 1024px;
  overflow: hidden;
  position: relative;
  margin: 0 auto;
  padding: 0 50px;
  text-align: center;
}

EDIT

Code added to jsfiddle: http://jsfiddle.net/BXdrG/

标签: html button css
3条回答
一纸荒年 Trace。
2楼-- · 2020-02-25 06:53

Ah ok, I misunderstood its use. To get a fluid button that won't stretch to massive sizes I added the following:

width:100%;
max-width: 540px;

Thanks commenters!

查看更多
Animai°情兽
3楼-- · 2020-02-25 07:05
button {
  text-align: center;
  max-width: 540px;
  height: auto;
  display: block;
  padding: 10px 0;
  margin: 0 auto;
  border: none;

  width:100%; /* you forgot this */
}
查看更多
欢心
4楼-- · 2020-02-25 07:06

For max-width to work correctly, your element first needs a certain width. Use 100% to achieve what you want. See here:

http://jsfiddle.net/QsHa9/

查看更多
登录 后发表回答