簡體   English   中英

Flex Container CSS:如何居中div元素?

[英]Flex Container CSS : How to center div element?

我有以下HTML / CSS代碼,但在將內容居中放置在flex容器中時遇到了問題。 結果是:

結果

在我的腦海中,我想將環放置在flex容器內,並將文本(溫度)和CSS動畫(箭頭)置於環動畫內的溫度之后。

這個怎么做?

 body { background-color: #0d74ff; } .container { padding: 20px; } .flexwrap { display: flex; flex-wrap: wrap; } .cell { position: relative; height: 200px; width: 200px; border: 1px solid rgba(0, 0, 0, 0.25); } .loader-ring { width: 150px; height: 150px; } .loader-ring-light { width: 150px; height: 150px; border-radius: 150px; box-shadow: 0 4px 0 #ffffff inset; animation: rotate-360 6s linear infinite; } .loader-text { font-weight: bold; font-size: 2em; } .scroll-down { border: 2px solid #fff; border-radius: 100px; width: 30px; height: 30px; } .scroll-down i { display: block; border-radius: 100px; transition: all 0.4s ease; width: 100%; height: 100%; background-size: 0 auto; animation: pulse 1.5s 0s infinite normal ease forwards; background-image: url("https://jamesmuspratt.com/codepen/img/arrow-down.svg"); background-repeat: no-repeat; } @keyframes rotate-360 { from { transform: rotate(0); } to { transform: rotate(360deg); } } @keyframes pulse { 0% { opacity: 0; background-position: center top; background-size: 0 auto; } 10% { opacity: 0; } 50% { opacity: 1; background-size: 75% auto; } 90% { opacity: 0; } 100% { opacity: 0; background-position: center bottom; background-size: 0 auto; } } 
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> <div class="container"> <div class="flexwrap"> <div class="cell"> <div class='loader-ring'> <div class='loader-ring-light'></div> <div class='loader-ring-track'> <div class='loader-text'>26.6&deg;</div> <div class="scroll-down"><i></i></div> </div> </div> </div> </div> </div> 

您在這里不需要很多塊。 您需要絕對定位,因為元素將需要彼此堆疊。 另外,為了使絕對定位的元素居中,您需要這種樣式

.loader-ring,
.loader-ring-track {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

演示:

 body { background-color: #0d74ff; } .container { padding: 20px; } .cell { position: relative; height: 200px; width: 200px; border: 1px solid rgba(0, 0, 0, 0.25); } /* center absolutely positioned items */ /* both vertically and horizontally */ .loader-ring, .loader-ring-track { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } .loader-ring-track { display: flex; /* new */ } .loader-ring { width: 150px; height: 150px; } .loader-ring-light { width: 150px; height: 150px; border-radius: 150px; box-shadow: 0 4px 0 #ffffff inset; animation: rotate-360 6s linear infinite; } .loader-text { font-weight: bold; font-size: 2em; } .scroll-down { border: 2px solid #fff; border-radius: 100px; width: 30px; height: 30px; } .scroll-down i { display: block; border-radius: 100px; transition: all 0.4s ease; width: 100%; height: 100%; background-size: 0 auto; animation: pulse 1.5s 0s infinite normal ease forwards; background-image: url("https://jamesmuspratt.com/codepen/img/arrow-down.svg"); background-repeat: no-repeat; } @keyframes rotate-360 { from { transform: rotate(0); } to { transform: rotate(360deg); } } @keyframes pulse { 0% { opacity: 0; background-position: center top; background-size: 0 auto; } 10% { opacity: 0; } 50% { opacity: 1; background-size: 75% auto; } 90% { opacity: 0; } 100% { opacity: 0; background-position: center bottom; background-size: 0 auto; } } 
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> <div class="cell"> <div class='loader-ring'> <div class='loader-ring-light'></div> <div class='loader-ring-track'> <div class='loader-text'>26.6&deg;</div> <div class="scroll-down"><i></i></div> </div> </div> </div> 

如何居中<p>里面的元素</p><div>容器?</div><div id="text_translate"><p> 我希望我的<p>元素位於容器<div>的中心,就像完全居中一樣——頂部、底部、左側和右側的邊距均等地分割空間。</p><p> 我怎樣才能做到這一點? </p><p></p><div class="snippet" data-lang="js" data-hide="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> div { width: 300px; height: 100px; } p { position: absolute; top: auto; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> <div> <p>I want this paragraph to be at the center, but it's not.</p> </div></pre></div></div><p></p></div>

[英]How to center a <p> element inside a <div> container?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM