简体   繁体   English

具有半透明 100% div 和居中文本的 CSS 网格

[英]CSS Grid with semi-transparent 100% div and centered text

I tried so many thing, but I cannot get it to work.我尝试了很多东西,但我无法让它工作。 What I want to create is 7 item grid with a background image, a semi-transparent overlay to be able to darken the image in dark mode as well as some text that is centered in the grid-item.我想要创建的是带有背景图像的 7 个项目网格,一个半透明覆盖层,能够在黑暗模式下使图像变暗,以及一些以网格项目为中心的文本。

My current version is:我目前的版本是:

grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr;
  grid-auto-rows: 14vw;
  grid-gap: 0px;
}

grid-item {
  display: flex;            /* new */
  align-items: center;      /* new */
  justify-content: center;  /* new */
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
}
</style>

<grid-container>
  <grid-item style="background-image: url('https://media.istockphoto.com/photos/delicious-homemade-hamburger-and-french-fries-picture-id1254672762')"><span style="width: 100%;height: 100%; background-color:rgb(0,0,0, 0.4);"><h1 style="color: #fff;font-size: 42px;justify-self: center;
    align-self: center;">Monday</h1></span></grid-item>
  <grid-item style="background-image: url('https://media.istockphoto.com/photos/delicious-homemade-hamburger-and-french-fries-picture-id1254672762')"><span style="width: 100%;height: 100%; background-color:rgb(0,0,0, 0.4);"><h1 style="color: #fff;font-size: 42px;justify-self: center;
    align-self: center;">Tuesday</h1></span></grid-item>
    <grid-item style="background-image: url('https://media.istockphoto.com/photos/delicious-homemade-hamburger-and-french-fries-picture-id1254672762')"><span style="width: 100%;height: 100%; background-color:rgb(0,0,0, 0.4);"><h1 style="color: #fff;font-size: 42px;justify-self: center;
      align-self: center;">Wednesday</h1></span></grid-item>
      <grid-item style="background-image: url('https://media.istockphoto.com/photos/delicious-homemade-hamburger-and-french-fries-picture-id1254672762')"><span style="width: 100%;height: 100%; background-color:rgb(0,0,0, 0.4);"><h1 style="color: #fff;font-size: 42px;justify-self: center;
        align-self: center;">Thursday</h1></span></grid-item>
        <grid-item style="background-image: url('https://media.istockphoto.com/photos/delicious-homemade-hamburger-and-french-fries-picture-id1254672762')"><span style="width: 100%;height: 100%; background-color:rgb(0,0,0, 0.4);"><h1 style="color: #fff;font-size: 42px;justify-self: center;
          align-self: center;">Friday</h1></span></grid-item>
          <grid-item style="background-image: url('https://media.istockphoto.com/photos/delicious-homemade-hamburger-and-french-fries-picture-id1254672762')"><span style="width: 100%;height: 100%; background-color:rgb(0,0,0, 0.4);"><h1 style="color: #fff;font-size: 42px;justify-self: center;
            align-self: center;">Saturday</h1></span></grid-item>
            <grid-item style="background-image: url('https://media.istockphoto.com/photos/delicious-homemade-hamburger-and-french-fries-picture-id1254672762')"><span style="width: 100%;height: 100%; background-color:rgb(0,0,0, 0.4);"><h1 style="color: #fff;font-size: 42px;justify-self: center;
              align-self: center;">Sunday</h1></span></grid-item>
</grid-container>

But it just is not what I wanted - the text needs to be centered and all in-div center tricks did not seem to have worked.但这不是我想要的 - 文本需要居中,并且所有在 div 中心的技巧似乎都没有奏效。

Grateful for an explanation!感谢您的解释! fj缩略词

Just make the span flex containers as well.只需制作span flex容器即可。 For instance...例如...

 grid-container { display: grid; grid-template-columns: 1fr 1fr; grid-auto-rows: 100vh; grid-gap: 0px; } grid-item, span { display: flex; /* new */ align-items: center; /* new */ justify-content: center; /* new */ background-repeat: no-repeat; background-size: cover; background-position: center; }
 <grid-container> <grid-item style="background-image: url('https://media.istockphoto.com/photos/delicious-homemade-hamburger-and-french-fries-picture-id1254672762')"><span style="width: 100%;height: 100%; background-color:rgb(0,0,0, 0.4);"> <h1 style="color: #fff;font-size: 42px;justify-self: center; align-self: center;">Monday</h1> </span></grid-item> <grid-item style="background-image: url('https://media.istockphoto.com/photos/delicious-homemade-hamburger-and-french-fries-picture-id1254672762')"><span style="width: 100%;height: 100%; background-color:rgb(0,0,0, 0.4);"> <h1 style="color: #fff;font-size: 42px;justify-self: center; align-self: center;">Tuesday</h1> </span></grid-item> </grid-container>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM