【问题标题】:Why is my CSS Grid row not spanning across entire row width? And why are my grid rows not properly setting up?为什么我的 CSS Grid 行没有跨越整个行宽?为什么我的网格行设置不正确?
【发布时间】:2021-09-19 21:43:55
【问题描述】:

我正在尝试在我的模态页面中创建一个网格。我希望关闭按钮单独在一行上,标题/标题在它下面,也单独在一行上。

由于某种原因,我的关闭按钮和标题位于同一行。我以为我的网格模板行会将它们放在不同的行上,但它似乎无法正常工作。

.modal {
  height: 100%;
  width: 100%;
  position: absolute;
  top: 0;
  background-color: rgba(255, 128, 213, 0.5);
  display: grid;
  justify-content: center;
  align-items: center;
  display: none;
}

.modal>.modalContent {
  background-color: white;
  height: 100%;
  width: 75%;
  display: grid;
  grid-template-rows: 1fr 1fr auto 1fr;
  align-items: center;
  position: relative;
}

.closeButton {
  position: absolute;
  top: 0;
  right: 14px;
  font-size: 50px;
  transform: rotate(45deg);
  padding: 0%;
  cursor: pointer;
  background-color: none;
  border-style: unset;
  grid-row: 1 / 2;
}

.modalContent>.main {
  display: grid;
  grid-row: 3/4;
  grid-template-columns: 1fr 1fr;
}

div>.modalTxt {
  width: 80%;
}

.modalContent>div {
  padding-left: 10%;
  padding-right: 10%;
}

div>.modalImg {
  width: 353px;
  height: 447px;
}

#title {
  background-color: #ff80d5;
  font-family: Gill Sans, sans-serif;
  text-align: left;
  vertical-align: center;
  color: white;
  font-weight: 100;
  block-size: 50px;
  display: grid;
  flex-wrap: wrap;
  align-items: center;
  padding-left: 2%;
  grid-row: 2 / 3;
}
<div ID="modalAcidFish" class="modal">
  <div class="modalContent" ID="modalAcidFishContent">

    <div>
      <h2 ID="title">Acid Fish</h2>
    </div>

    <div><span class="closeButton">+</span></div>

    <div class="main"><img class="modalImg" ID="modalAcidFishImg" src="Images/Acid Fish 2.jpeg"></div>

    <div class="main">
      <p>Green was always one of my favorite colors. After seeing watching a woman on tv wear a bold green outfit, I wanted to make a piece inspired by this color. The first time I tried using green as the primary color in one of my works, the painting,
        and the color did not come out so well. I sat on the idea of using green despite my first failed attempt. When I first painted some of the canvas, the green was a bit too heavy, so I wet a paper towel and decided to smear some of the paint around
        which helped to creat an inkblot affect.
      </p>

      <p>The next component was thinking of what color paper to use. I dwelled on this for a while, and decided the orange and black printed paper worked perfectly. Not only did the colors pop, the print correleated well with the painted background. In order
        to create balance, I thought it was best to keep the overall layout and design of the origami relatively simple, leading me to create two fishes "kissing" or facing each other.
      </p>
    </div>
  </div>

</div>

【问题讨论】:

  • 查看grid-template-areas 这将是按预期完成设计的最简单方法。

标签: html css css-grid


【解决方案1】:

我会这样做:

<div class="modal">
    <div>&times;</div>
    <div>
        <h1>Hello You!</h1>
        <h6>Have a look at this!</h6>
    </div>
    <img src="https://unsplash.it/300" alt="Acid Fish Placeholder">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Animi asperiores, atque dolorem ducimus earum eveniet fugiat impedit ipsa ipsam odit!</p>

</div>
    :root {
        --modal-height: 500px;
    }

    * {
        margin: 0;
        padding: 0;
    }
    .modal {
        display: grid;
        grid-template-rows: 1rem 4rem 3fr 3fr;
        height: var(--modal-height);
        inset: 0;
        margin: auto;
        justify-content: center;
        width: var(--modal-height);
        background-color: gray;
    }
    .modal img {
        max-width: 100%;
        max-height: 100%;
        min-width: 0;
        min-height: 0;
    }

    .modal > :first-child {
        justify-self: flex-end
    }
    .modal > :last-child {
        padding: 1rem 0;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多