繁体   English   中英

如何在div中垂直居中放置跨度和列表?

[英]How to vertically center a span and a list inside a div?

我在浏览器顶部固定了一个div。 div的宽度为100%,高度为90px。 里面是应该在左边的文本范围,应该在右边的水平列表。 现在,我将跨度向左浮动,将列表向右浮动。 如何在div中将范围和列表垂直居中?

http://jsfiddle.net/4keeS/6/

这是一个解决方案...

http://jsfiddle.net/4keeS/7/

...在span和li上使用它

display:inline-block;
line-height:70px;
height: 70px;

不幸的是,垂直对齐仅适用于表格单元格(即TDTH元素)。
因此,您必须使用TABLE才能垂直对齐内容。

<html>
<head>
  <style>
    .text         { float: left; }
    .list         { list-style: none; float: right; margin: 0; }
    .list li      { float: left; }
    #vdivwrap     { width: 100%; }
    #vdivcontent  { height:90px; vertical-align: center; background:#8c8; }
  </style>
</head>
<body>
  <table id="vdivwrap" border="0" cellspacing="0" cellpadding="0">
    <td id="vdivcontent">
      <span class="text">[text span]</span>
      <ul class="list">
        <li>[list item 1]</li>
        <li>[list item 2]</li>
      </ul>
    </td>
  </table>
</body>
</html>

如果您知道标题跨度和UL内容的高度,则可以使用

position:absolute /* or relative */;
top:50%;
margin-top: -X /* where X is half the height of your content. That's a negative margin pulling the content up so it's center aligns with the center of the parent div */;

看到这里: http : //jsfiddle.net/XAgyv/1/

在不知道内容高度垂直居中的情况下,我没有找到任何好的方法-我期待看到其他答案。

另一个解决方案是添加另一个css规则,如下所示

span, ul {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
  -webkit-transform: translateY(-50%);
  -o-transform: translateY(-50%);
}

这是工作的jsfiddle http://jsfiddle.net/logiccanvas/4keeS/9/

如何将<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内垂直居中span / svg 如何在div内的span内垂直居中img? 如何在div内垂直居中放置列表? 如何在div中垂直居中放置跨度? 如何在SPAN中垂直居中图像 在悬停时垂直居中div中的跨度 如何在IE中的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