繁体   English   中英

如何在IE中的div中垂直居中放置文本?

[英]How to vertically center text inside a div in IE?

我已经多次看到这个问题,没有一个解决方案对我有用。 正如您在小提琴中看到的那样,这完全符合我希望在Chrome中使用的方式,但是在IE中,文本停留在框的顶部,而不是中间。 如果您在IE浏览器和Chrome中并行运行它,您将会明白我的意思。 我想要它完全在中间,使用margin:auto; 在chrome中可以完美运行,但在IE中则不能。 我不确定会否将段落下移,以便使其在垂直和水平方向上都在中间对齐。

<section class="info"> 
<p class="infofont">Size: Large<br><br>
100% Cotton<br><br>
Excellent Condition!
</p>

</section>

CSS:

.info{
display:flex;
width:325px;
margin:auto;    
}

.infofont{
font-weight:bold;
text-align:center;
margin:auto;
}

https://jsfiddle.net/fmu8g38h/

margin:auto用于水平对齐文本。

要居中对齐,顶部必须分开对齐

试试这个代替:

.info{
    display:flex;
    top:50%;
    left:50%;   
    transform: translate(-50%, -50%);
    position:absolute;
}

https://jsfiddle.net/k1cbjvj7/1/

您忘了写flex-direction: row / column; align-items: center; justify-content: center; flex-direction: row / column; align-items: center; justify-content: center; displayflex且内部内容在verticallyhorizontally居中时,应删除margin: auto; 来自.infofont class

下面是更新您的CSS代码。

.info{
    border: 1px solid black;
    align-items: center;
    display: flex;
    flex-direction: row;
    justify-content: center;
    width: 325px;
    margin:auto auto;   
    height: 500px;  
}

.infofont{
    font-weight:bold;
    text-align:center;
}

检查已剪断

 .info{ border: 1px solid black; align-items: center; display:flex; flex-direction: row; justify-content: center; width:325px; margin:auto auto; height:500px; } .infofont{ color: green; font-size: 16px; font-weight:bold; line-height: 1.2; margin: 0; text-align:center; } *, *:after, *:before{ box-sizing: inherit; } html{ box-sizing: border-box; } 
 <section class="info"> <p class="infofont">Size: Large<br><br> 100% Cotton<br><br> Excellent Condition! </p> </section> 

注意:我仅在IE11,Chrome和Safari上测试此代码。

如何将<kbd>内容</kbd>垂直居中<div></div><div id="text_translate"><p>我正在尝试制作钢琴,但我从基础开始。 我住在以<strong>div</strong>的内部内容为中心。 我希望它位于他父亲 div 的中间并居中</p><p>继承人代码:html</p><pre> &lt;div class="keys"&gt; &lt;div class="key" id="keyA"&gt; &lt;kbd&gt;A&lt;/kbd&gt; &lt;/div&gt; &lt;div class="key" id="keyS"&gt; &lt;kbd&gt;S&lt;/kbd&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> css:</p><pre> * { margin: 0; padding: 0; box-sizing: border-box; } body{ background-color: chocolate; }.keys{ padding: 4px 5px; position: fixed; height: 40%; max-height: 60%; width: 60%; max-width: 80%; display: flex; justify-content: space-around; top: 50%; left: 50%; transform: translate(-50%, -50%); border: white solid; align-items: center; text-align: center; }.key{ border: black solid 1px; font-family: 'Montserrat', sans-serif; font-size: 22px; max-height: 95%; height: 90%; width: 70px; max-width: 95%; border-radius: 10px; } kbd{ /*border to see the with and weight*/ border: pink solid 1px; color: aliceblue; }</pre><p> 这是一个小提琴: <a href="https://jsfiddle.net/hnd9jr4y/" rel="nofollow noreferrer">https://jsfiddle.net/hnd9jr4y/</a>谢谢</p></div>

[英]How to center vertically <kbd> content inside an <div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在div中垂直居中按钮和文本 在div中垂直居中文本 如何在div中垂直居中放置文本? 如何在div内水平和垂直居中放置图像和文本? 如何在 div 块内居中文本(水平和垂直)? 如何在具有动态变化高度的div中垂直居中放置文本? 将div中文本的未知长度垂直居中 DIV中的垂直居中文本 在div中垂直居中放置文字? 如何将<kbd>内容</kbd>垂直居中<div></div><div id="text_translate"><p>我正在尝试制作钢琴,但我从基础开始。 我住在以<strong>div</strong>的内部内容为中心。 我希望它位于他父亲 div 的中间并居中</p><p>继承人代码:html</p><pre> &lt;div class="keys"&gt; &lt;div class="key" id="keyA"&gt; &lt;kbd&gt;A&lt;/kbd&gt; &lt;/div&gt; &lt;div class="key" id="keyS"&gt; &lt;kbd&gt;S&lt;/kbd&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> css:</p><pre> * { margin: 0; padding: 0; box-sizing: border-box; } body{ background-color: chocolate; }.keys{ padding: 4px 5px; position: fixed; height: 40%; max-height: 60%; width: 60%; max-width: 80%; display: flex; justify-content: space-around; top: 50%; left: 50%; transform: translate(-50%, -50%); border: white solid; align-items: center; text-align: center; }.key{ border: black solid 1px; font-family: 'Montserrat', sans-serif; font-size: 22px; max-height: 95%; height: 90%; width: 70px; max-width: 95%; border-radius: 10px; } kbd{ /*border to see the with and weight*/ border: pink solid 1px; color: aliceblue; }</pre><p> 这是一个小提琴: <a href="https://jsfiddle.net/hnd9jr4y/" rel="nofollow noreferrer">https://jsfiddle.net/hnd9jr4y/</a>谢谢</p></div>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM