Home > other >  How to center partially transparent div?
How to center partially transparent div?

Time:01-03

I have a very simple page set up, showing a background image in a hero card.

I want to have a transparent blurred section over this hero card. I have this setup so that it works with headers, but my attempts to expand the model and center it all result in something not being shown.

How can I center the transparent blurred div (named module in code example) and have it be a fixed size without requiring absolute positioning?

body {
  background-color: #fff;
  height: 200vh;
  position: relative; }

h1 {
  font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";}

.hero {
  height: 100vh;
  width: 100%;
  background-size: cover;
  background-position: center center;
  background-repeat: no-repeat;   
  background: url('https://images.unsplash.com/photo-1641073912526-378e7f5af084?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1975&q=80');
  background-attachment: fixed;
  overflow: hidden;
}


.module {
  background: inherit;
  background-attachment: fixed;
  width: 80%;
  height: 20%;
  position: relative;
  overflow: hidden;
  border-radius: 15px;
  
}
.module > header {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 20px 10px;
  background: inherit;
  background-attachment: fixed;
  overflow: hidden;
}
.module > header::before {
  content: "";
  position: absolute;
  top: -20px;
  left: 0;
  width: 200%;
  height: 200%;
  background: inherit;
  background-attachment: fixed;
  -webkit-filter: blur(4px);
  filter: blur(4px);
}
.module > header::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  #background: rgba(0, 0, 0, 0.25)
}
.module > header > h1 {
  margin: 0;
  border-radius: 15px; 
  color: white;
  position: relative;
  z-index: 1;
}
<!doctype html>
<html lang="en">
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
      <link rel="stylesheet" href="css/bootstrap.min.css">
      <title>Test</title>
   </head>
   <body>
      <div >
         <div >
            <header>
               <h1 >
                  TEST TEXT HERE
               </h1>
            </header>
         </div>
      </div>
   </body>
</html>

CodePudding user response:

You can use flexbox on div.hero and position the div.module with flex's align-items and justify-content properties like the example below.

body {
  background-color: #fff;
  height: 200vh;
  position: relative;
}

h1 {
  font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
}

.hero {
  align-items: center;
  display: flex;
  height: 100vh;
  justify-content: center;
  width: 100%;
  background-size: cover;
  background-position: center center;
  background-repeat: no-repeat;
  background: url('https://images.unsplash.com/photo-1641073912526-378e7f5af084?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1975&q=80');
  background-attachment: fixed;
  overflow: hidden;
}


.module {
  background: inherit;
  background-attachment: fixed;
  width: 80%;
  position: relative;
  overflow: hidden;
  border-radius: 15px;

}

.module>header {
  width: 100%;
  padding: 20px 10px;
  background: inherit;
  background-attachment: fixed;
  overflow: hidden;
}

.module>header::before {
  content: "";
  position: absolute;
  top: -20px;
  left: 0;
  width: 200%;
  height: 200%;
  background: inherit;
  background-attachment: fixed;
  -webkit-filter: blur(4px);
  filter: blur(4px);
}

.module>header::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  #background: rgba(0, 0, 0, 0.25)
}

.module>header>h1 {
  margin: 0;
  border-radius: 15px;
  color: white;
  position: relative;
  text-align: center;
  z-index: 1;

}
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.1.3/js/bootstrap.min.js"></script>
<div >
         <div >
            <header>
               <h1 >
                  TEST TEXT HERE
               </h1>
            </header>
         </div>
      </div>

CodePudding user response:

Make h1 width 100vw and add text-align: center; Then you can adjust your blur from that. I added left: 40%; I also adjusted your background-size so it is actually covering the full width without repeats.

body {
  background-color: #fff;
  height: 200vh;
  position: relative; }

h1 {
  font-family: "Open Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";}

.hero {
  height: 100vh;
  width: 100%;
  background-position: center;
  background-repeat: no-repeat;   
  background: url('https://images.unsplash.com/photo-1641073912526-378e7f5af084?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1975&q=80');
  background-attachment: fixed;
  background-size: cover;
  overflow: hidden;
}


.module {
  background: inherit;
  background-attachment: fixed;
  width: 80%;
  height: 20%;
  position: relative;
  overflow: hidden;
  border-radius: 15px;
  
}
.module > header {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 20px 10px;
  background: inherit;
  background-attachment: fixed;
  overflow: hidden;
}
.module > header::before {
  content: "";
  position: absolute;
  top: -20px;
  left: 40%;
  width: 200%;
  height: 200%;
  background: inherit;
  background-attachment: fixed;
  -webkit-filter: blur(4px);
  filter: blur(4px);
}
.module > header::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  text-align: center;
  #background: rgba(0, 0, 0, 0.25)
}
.module > header > h1 {
  margin: 0;
  border-radius: 15px; 
  color: white;
  position: relative;
  z-index: 1;
  width: 100vw;
  text-align: center;
}
<!doctype html>
<html lang="en">
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
      <link rel="stylesheet" href="css/bootstrap.min.css">
      <title>Test</title>
   </head>
   <body>
      <div >
         <div >
            <header>
               <h1 >
                  TEST TEXT HERE
               </h1>
            </header>
         </div>
      </div>
   </body>
</html>

  • Related