首页 > 解决方案 > 在 CSS 中获取 div、按钮等的宽度

问题描述

button {
    width: auto;
    display: inline-block;
    (...)
}
button:before {
    width: (???);
    (...)
}

button:before当按钮的宽度不固定时,如何获取按钮的宽度?

标签: htmlcss

解决方案


button {
    width: auto;
    display: inline-block;
    position:relative;
    (...)
}
button:before {
    content:""; *// never forget to put content with empty strings. If you forget it will not work`*
   position:absolute; *//set this to position absolute and the parent which is the btn set position:relative*
   width:100%;
   height:100%; *// if you want the full size of the btn is like this*
   top:0;
   left:0
}

``

推荐阅读