我如何在HTML和CSS的网格堆栈后面添加边框?

cx6n0qe3  于 2023-08-09  发布在  其他
关注(0)|答案(5)|浏览(97)

我在想如何完成这个设计。该图像有一个网格堆栈,带有边框,允许网格从页面上弹出。


的数据

body {
  margin: 0;
  padding: 0;
}

.contact-form-container {
  position: relative;
  width: 50%;
}

.contact-form {
  background-color: white;
  padding: 20px;
  border-radius: 5px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

.contact-form:before {
  content: "";
  position: absolute;
  top: -10px;
  left: -10px;
  right: -10px;
  bottom: -10px;
  border: 3px solid red;
  z-index: -1;
  border-radius: 10px;
}

个字符

kdfy810k

kdfy810k1#

您的代码示例似乎已经很好地完成了这一点。我所做的唯一更改是将.contact-form-container居中,并调整::before选择器上的特定位置

body {
  margin: 0;
  padding: 0;
}

.contact-form-container {
  position: relative;
  margin: 20px auto 0 auto;
  width: 50%;
}

.contact-form {
  background-color: white;
  padding: 20px;
  border-radius: 5px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

.contact-form:before {
  content: "";
  position: absolute;
  top: 5px;
  left: -20px;
  right: -20px;
  bottom: 5px;
  border: 3px solid red;
  z-index: -1;
  border-radius: 10px;
}

个字符

j7dteeu8

j7dteeu82#

你已经很接近答案了。
问题是您使用-10px作为顶部和底部值。这意味着它离开了它应该在后面的元素。这就是为什么它适用于左派和右派。如果你将顶部和底部设置为10px,它将进入并位于项目后面。

body {
    display: grid;
    height: 100vh;
    margin: 0;
    place-items: center;
}

.contact-form-container {
  position: relative;
  width: 50%;
}

.contact-form {
  background-color: white;
  padding: 20px;
  border-radius: 5px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

.contact-form:before {
  content: "";
  position: absolute;
  top: 10px;
  left: -10px;
  right: -10px;
  bottom: 10px;
  border: 3px solid red;
  z-index: -1;
  border-radius: 10px;
}

个字符

vom3gejh

vom3gejh3#

您可以通过向网格容器添加after元素并将其放置在内容下来实现这一点。

rhfm7lfc

rhfm7lfc4#

要完成此设计,您可以在网格或Flex容器上声明一个比内容短的高度,并通过居中对齐来允许内容在顶部和底部溢出。

body {
  display: grid;
  place-content: center;
  min-height: 100vh;
  margin: 0;
}

div.container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  height: 125px;
  gap: 10px;
  padding: 10px;
  align-content: center;
  border: 3px solid firebrick;
  box-sizing: border-box;
}

div.container > div {
  width: 150px;
  height: 50px;
  background-color: silver;
}

个字符

vuktfyat

vuktfyat5#

这个应该能用我添加了显示网格和一个列模式,同时稍微修改了before元素的顶部和底部位置。还要确保表单没有颜色
你差点就到了

body {
  width: 100vw;
  height: 100vh;
  margin: 0;
  padding: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}

.contact-form-container {
  position: relative;
  width: 50%;
}

.contact-form {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  padding: 20px;
  border-radius: 5px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}

.contact-form:before {
  content: "";
  position: absolute;
  top:  30px;
  left: -10px;
  right: -10px;
  bottom:  30px;
  border: 3px solid red;
  z-index: -1;
  border-radius: 10px;
}

个字符

相关问题