【问题标题】:I want set this box centered我想把这个框居中
【发布时间】:2017-06-12 23:25:18
【问题描述】:

我正在尝试将此模式消息设置为居中,但它总是落在左侧,我尝试使用 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:点击按钮后会出现此消息。

【问题讨论】:

  • 您是否尝试过添加一个宽度为 100% 的包含 div,然后将 margin: auto 放在描述 div 上?
  • 您说您尝试了margin: 0 auto,但添加.messaggio { margin: 0 auto; } 居中....您是说这不起作用?
  • 是的迈克尔科克
  • 是的乔纳森荷兰
  • @Lafa:要在此处 ping 用户,您必须使用带有用户名的 @。选项卡完成可用。您可以快速编辑这些消息,也可以删除它们并重新发布。

标签: html css simplemodal


【解决方案1】:

您可以使用 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>

【讨论】:

    【解决方案2】:
    messagio{
    margin: auto;
    }
    

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

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

    希望这会有所帮助!

    【讨论】:

      【解决方案3】:

      添加:

      margin:15% auto;
      

      .messagiocss 类...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-15
        • 2019-09-11
        相关资源
        最近更新 更多