如何用 JavaScript 设置元素的最小宽度?


在 JavaScript 中使用 minWidth 属性来设置最小宽度。你可以尝试运行以下代码来用 JavaScript 设置元素的最小宽度 -

示例

实时演示

<!DOCTYPE html>
<html>
   <head>
      <style>
         #box {
            width: 50%;
            background-color: gray;
         }
      </style>
   </head>
   <body>
      <p>Click below to set Minimum width.</p>
      <button type="button" onclick="display()">Min Width</button>
      <div id="box">
         <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
         <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
         <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
         <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
         <p>This is a div. This is a div. This is a div. This is a div. This is a div.</p>
      </div>
      <br>
      <script>
         function display() {
            document.getElementById("box").style.minWidth = "500px";
         }
      </script>
   </body>
</html>

更新于: 2020 年 6 月 23 日

949 次浏览

开启你的 职业

通过完成课程获得认证

立即开始
广告