Search code examples
htmlcsssimplemodal

I want set this box centered


I'm trying to set this modal-message centered but it always falling to the left side, I've tried with margin: 0 auto; float: center or left: 0 and right: 0 but this doesn't work.

.descrizione { 
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  transition: opacity 400ms;
} 

.descrizione .cancel { 
  position: absolute;
  width: 100%;
  height: 100%;
  cursor: default;
} 

.descrizione:target { 
  visibility: visible;
  opacity: 0;
  animation: animationFrames ease 1s;
  animation-iteration-count: 1;
  transform-origin: 50% 50%;
  animation-fill-mode: forwards;
  -webkit-animation: animationFrames ease 1s;
  -webkit-animation-iteration-count: 1;
  -webkit-transform-origin: 50% 50%;
  -webkit-animation-fill-mode: forwards;
  -moz-animation: animationFrames ease 1s;
  -moz-animation-iteration-count: 1;
  -moz-transform-origin: 50% 50%;
  -moz-animation-fill-mode: forwards;
  -o-animation: animationFrames ease 1s;
  -o-animation-iteration-count: 1;
  -o-transform-origin: 50% 50%;
  -o-animation-fill-mode: forwards;
  -ms-animation: animationFrames ease 1s;
  -ms-animation-iteration-count: 1; -ms-transform-origin: 50% 50%;
  -ms-animation-fill-mode: forwards;
} 

.messaggio { 
  z-index: 9999;
  padding: 20px;
  background: #fff;
  border: 1px solid #666;
  width: 300px;
  box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
  position: relative;
} 

.light .messaggio { 
  border-color: #aaa;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.25);
} 

.messaggio h2 { 
  margin-top: 0px;
  color: #666;
  text-transform: uppercase;
  border-bottom: 2px solid #2767ce;
  font-family: "Trebuchet MS", Tahoma, Arial, sans-serif;
} 

.messaggio .chiudi { 
  position: absolute;
  width: 20px;
  height: 20px;
  top: 20px;
  right: 20px;
  opacity: 0.8;
  transition: all 200ms;
  font-size: 24px;
  font-weight: bold;
  text-decoration: none;
  color: #666;
} 

.messaggio .chiudi:hover { 
  opacity: 1;
  display: block;
} 

.messaggio .testo { 
  max-height: 400px;
  overflow: auto;
  font-family: sans-serif;
  font-size: 16px;
} 
.messaggio p {
  margin: 0 0 1em;
} 

.messaggio p:last-child { 
  margin: 0; 
}
<div class="descrizione" id="hub">
<div class="messaggio">
<h2>hub</h2>
<a class="chiudi" href="#">&times;</a>

<div class="testo">
<p>Questa &egrave; una breve descrizione che deve essere modificate dal propietario della pagina di mc-ita, si prega di modificarla al pi&ugrave; presto</p>
</div>
</div>
</div>

PS: This message appears after you have clicked a button.


Solution

  • You can achieve this easily with flexbox, modifications are noted in the CSS but basically:

    • Add width:100%; & height: 100%; to html, body{}
    • Set .descrizione to display: flex; and align vertically with justify-content: center; and horizontally with align-items: center;

    Snippet:

    /* add these */
    
    html,
    body {
      width: 100%;
      height: 100%;
    }
    
    .descrizione {
      /* position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0; */
      transition: opacity 400ms;
      height: 100%;
      width: 100%;
      /* add these */
      display: flex;
      align-items: center;
      justify-content: center;
    }
    
    .descrizione .cancel {
      position: absolute;
      width: 100%;
      height: 100%;
      cursor: default;
    }
    
    .descrizione:target {
      visibility: visible;
      opacity: 0;
      animation: animationFrames ease 1s;
      animation-iteration-count: 1;
      transform-origin: 50% 50%;
      animation-fill-mode: forwards;
      -webkit-animation: animationFrames ease 1s;
      -webkit-animation-iteration-count: 1;
      -webkit-transform-origin: 50% 50%;
      -webkit-animation-fill-mode: forwards;
      -moz-animation: animationFrames ease 1s;
      -moz-animation-iteration-count: 1;
      -moz-transform-origin: 50% 50%;
      -moz-animation-fill-mode: forwards;
      -o-animation: animationFrames ease 1s;
      -o-animation-iteration-count: 1;
      -o-transform-origin: 50% 50%;
      -o-animation-fill-mode: forwards;
      -ms-animation: animationFrames ease 1s;
      -ms-animation-iteration-count: 1;
      -ms-transform-origin: 50% 50%;
      -ms-animation-fill-mode: forwards;
    }
    
    .messaggio {
      z-index: 9999;
      padding: 20px;
      background: #fff;
      border: 1px solid #666;
      width: 300px;
      box-shadow: 0 0 50px rgba(0, 0, 0, 0.5);
      position: relative;
      /* add these */
      margin: 0 auto;
    }
    
    .light .messaggio {
      border-color: #aaa;
      box-shadow: 0 2px 10px rgba(0, 0, 0, 0.25);
    }
    
    .messaggio h2 {
      margin-top: 0px;
      color: #666;
      text-transform: uppercase;
      border-bottom: 2px solid #2767ce;
      font-family: "Trebuchet MS", Tahoma, Arial, sans-serif;
    }
    
    .messaggio .chiudi {
      position: absolute;
      width: 20px;
      height: 20px;
      top: 20px;
      right: 20px;
      opacity: 0.8;
      transition: all 200ms;
      font-size: 24px;
      font-weight: bold;
      text-decoration: none;
      color: #666;
    }
    
    .messaggio .chiudi:hover {
      opacity: 1;
      display: block;
    }
    
    .messaggio .testo {
      max-height: 400px;
      overflow: auto;
      font-family: sans-serif;
      font-size: 16px;
    }
    
    .messaggio p {
      margin: 0 0 1em;
    }
    
    .messaggio p:last-child {
      margin: 0;
    }
    <div class="descrizione" id="hub">
      <div class="messaggio">
        <h2>hub</h2>
        <a class="chiudi" href="#">&times;</a>
        <div class="testo">
          <p>Questa &egrave; una breve descrizione che deve essere modificate dal propietario della pagina di mc-ita, si prega di modificarla al pi&ugrave; presto</p>
        </div>
      </div>
    </div>