首页 > 解决方案 > 外容器上的溢出属性导致负边距不起作用

问题描述

我正在渲染一个列表,我希望它上面的悬停颜色跨越外部容器的宽度。我正在使用 anegative margin来实现这一点。它可以正常工作而无需为外部包装器overflow提供属性,但是当我提供属性时,它会限制为列表项的宽度,并且不会跨越到外部容器的宽度。overflow

下面的代码显示了没有溢出属性的预期行为,

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px solid;
}
.container {
  border: 1px solid;
  padding: 20px 30px;
  background-color: white;
}
.wrapper {
  width: 400px;
  height: 800px;
}
li {
  padding: 15px 10px;
}

li:hover {
  background-color: green;
  cursor: pointer;
  color: white;
  margin: 0 -30px;
  padding: 15px 40px;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
</head>
<body>
  <div class='container'>
    <ul class='wrapper' style="list-style-type:none;">
        <li>List item 1</li>
        <li>List item 2</li>
        <li>List item 3</li>
        <li>List item 4</li>
        <li>List item 5</li>
        <li>List item 6</li>
        <li>List item 7</li>
    </ul>
  </div>
</body>
</html>

这是将溢出-y:自动添加到外部包装器时的代码,

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px solid;
}
.container {
  border: 1px solid;
  padding: 20px 30px;
  background-color: white;
}
.wrapper {
  width: 400px;
  height: 800px;
  overflow-y: auto;
}
li {
  padding: 15px 10px;
}

li:hover {
  background-color: green;
  cursor: pointer;
  color: white;
  margin: 0 -30px;
  padding: 15px 40px;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
</head>
<body>
  <div class='container'>
    <ul class='wrapper' style="list-style-type:none;">
        <li>List item 1</li>
        <li>List item 2</li>
        <li>List item 3</li>
        <li>List item 4</li>
        <li>List item 5</li>
        <li>List item 6</li>
        <li>List item 7</li>
    </ul>
  </div>
</body>
</html>

在外包装上使用属性时如何实现预期的行为?overflow-y: auto

标签: javascripthtmlcssoverflowmargin

解决方案


避免使用负边距,只需将填充从 ul 传递给 li。

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px solid;
}
.container {
  border: 1px solid;
  padding: 0;
  background-color: white;
}
.wrapper {
  width: 400px;
  height: 800px;
  overflow-y: auto;
}
li {
  padding: 15px 40px;
}
li:first-child {margin-top: 35px;}
li:last-child {margin-bottom: 35px;}

li:hover {
  background-color: green;
  cursor: pointer;
  color: white;
}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
</head>
<body>
  <div class='container'>
    <ul class='wrapper' style="list-style-type:none;">
        <li>List item 1</li>
        <li>List item 2</li>
        <li>List item 3</li>
        <li>List item 4</li>
        <li>List item 5</li>
        <li>List item 6</li>
        <li>List item 7</li>
    </ul>
  </div>
</body>
</html>


推荐阅读