我想将此框设置为居中

I want set this box centered

我正在尝试将此模态消息设置为居中,但它总是落在左侧,我已经尝试使用 margin: 0 auto; float: centerleft: 0right: 0 但这没有没用。

.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:单击按钮后会出现此消息。

messagio{
margin: auto;
}

因为 messagio 只有 300px 并且它的容器跨越父级的宽度,所以你必须使用边距来居中它。

另一种方法是使用 Flexbox 将父级设置为 flex 并对齐内容中心,或者定位左侧:50% 并减去宽度的一半 (150px)。但我认为 margin auto 最适合。

希望对您有所帮助!

添加:

margin:15% auto;

.messagiocssclass...

您可以使用 flexbox 轻松实现此目的,修改已在 CSS 中注明,但基本上:

  • width:100%; & height: 100%; 添加到 html, body{}
  • .descrizione设置为display: flex;,垂直对齐justify-content: center;,水平对齐align-items: center;

片段:

/* 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>