我想将此框设置为居中

时间:2017-06-12 23:25:19

标签: html css simplemodal

我试图将此模态消息设为居中但总是落在左侧,我已尝试使用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>
&#13;
&#13;
&#13;

PS:单击按钮后会出现此消息。

3 个答案:

答案 0 :(得分:1)

messagio{
margin: auto;
}

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

另一种方法是使用Flexbox设置父级来弯曲和调整内容中心,或者左侧定位:50%并减去宽度的一半(150px)..但我认为保证金自动适合最佳。

希望这有帮助!

答案 1 :(得分:1)

添加:

margin:15% auto;

.messagio css class ...

答案 2 :(得分:1)

你可以使用flexbox轻松实现这一点,CSS中注明了修改,但基本上是:

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

段:

&#13;
&#13;
/* 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;
}
&#13;
<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>
&#13;
&#13;
&#13;