HTML DOM Style minWidth 属性

htmlweb developmentfront end technology更新于 2025/6/9 0:44:17

DOM Style minWidth 属性用于返回并修改 HTML 文档中元素的最小宽度。

语法

以下是语法 −

  • 返回 minWidth

object.style.minWidth
  • 修改 minWidth

object.style.minWidth = “value”

值

此处,值可以为 -

值说明
inherit从其父元素继承此属性值。
initial将此属性值设置为其默认值。
length以长度单位设置值。
percentage(%)以父元素的百分比宽度设置值。

示例

让我们看一个 Style minWidth 属性的示例 −

<!DOCTYPE html>
<html>
<head>
<style>
   body {
      color: #000;
      background-image: linear-gradient(to top, #a18cd1 0%, #fbc2eb 100%);
      height: 100vh;
   }
   p {
      border: 2px solid #fff;
      width: 30%;
   }
   .btn {
      background: coral;
      border: none;
      height: 2rem;
      border-radius: 2px;
      width: 40%;
      display: block;
      color: #fff;
      outline: none;
      cursor: pointer;
   }
</style>
</head>
<body>
<h1>DOM Style minWidth Property Example</h1>
<p>
   This is paragraph 1 with some dummy text.
   This is paragraph 1 with some dummy text.
   This is paragraph 1 with some dummy text.
   This is paragraph 1 with some dummy text.
   This is paragraph 1 with some dummy text.
   This is paragraph 1 with some dummy text.
</p>
<button onclick="add()" class="btn">Set minWidth</button>
<script>
   function add() {
      document.querySelector('p').style.minWidth = "300px";
   }
</script>
</body>
</html>

输出

这将产生以下输出 −

点击"设置最小宽度"按钮设置段落元素的最小宽度。


相关文章


有用资源