简体   繁体   English

如何将文本与其他元素垂直居中对齐?

[英]How can I align the text to be vertically centered with the other elements?

I am creating rows of objects that are currently in an HTML table as separate elements so that I have more flexibility when making the page responsive.我正在创建当前位于 HTML 表中的对象行作为单独的元素,以便在使页面响应时具有更大的灵活性。 However, I am unable to get the span text elements to center with the status div or button at all.但是,我根本无法让跨度文本元素以状态 div 或按钮为中心。 The span text seems to have a larger bottom 'padding' than the other elements and for the life of me I can't figure out why.跨度文本似乎比其他元素具有更大的底部“填充”,对于我来说,我无法弄清楚为什么。

I have to use floats for alignment (to my knowledge), unfortunately, as the website must fully support IE10+, otherwise I would use something like flex.不幸的是,我必须对 alignment 使用浮点数(据我所知),因为该网站必须完全支持 IE10+,否则我会使用类似 flex 的东西。 Most of the things I've looked at and researched online deal with one line of text another element and many suggest editing the line height which seems to me like it wouldn't be the best solution for this case.我在网上查看和研究的大部分内容都处理一行文本另一个元素,许多人建议编辑行高,在我看来这不是这种情况下的最佳解决方案。

The codepen for all the code is here: https://codepen.io/stevennava/pen/WNrzzKo and here is the code (per StackOverflow standards):所有代码的 codepen 都在这里: https://codepen.io/stevennava/pen/WNrzzKo这是代码(根据 StackOverflow 标准):

 #outer-border { border: 1px solid black; border-radius: 5px; padding: 1.3em 0; position: relative; } #centered-container { margin: 0; padding: auto; position: absolute; top: 50%; -ms-transform: translateY(-50%); transform: translateY(-50%); } #status { position: relative; border: 1px solid lightgrey; border-radius: 8px; padding: 0.2em 0.2em } #status-message { float: left; margin-left: 1.2em; } #status-circle { width: 12px; height: 12px; background-color: orange; border-radius: 50%; float: left; margin: 0; position: absolute; top: 50%; -ms-transform: translateY(-50%); transform: translateY(-50%); } #button { height: 1.5em; width: 1.5em; text-align: center; }.order-element { padding: 0; float: left; margin-left: 15px; }.clearfix { overflow: auto; }.clearfix::after { content: ""; clear: both; display: table; }
 <div id="outer-border" class="clearfix"> <div id="centered-container" class="clearfix"> <span name="user-name" class="order-element">John Smith</span> <div id="status" class="order-element"> <div id="status-circle" class=""></div> <span id="status-message" class="">Complete<span> </div> <div class="clearfix"> <span name="order-number" class="order-element">123456</span> <span name="order-amount" class="order-element">$35.45</span> <span name="fulfillment-date" class="order-element">07/07/2020 07:00 PM</span> <button id="button" class="order-element">...</button> </div> </div>

and this is this the result looks like:这就是结果看起来像: 行示例

Two things, the height of the button is causing the .clearfix div's height to stretch, and the border on the #status is causing it's height to stretch.两件事,按钮的高度导致.clearfix div 的高度拉伸, #status上的边框导致它的高度拉伸。

If you don't want to completely change this up, you can add padding to match the thickness of the border on the spans, and set the button height to auto.如果您不想完全改变它,您可以添加填充以匹配跨度上边框的厚度,并将按钮高度设置为自动。

 #outer-border { border: 1px solid black; border-radius: 5px; padding: 1.3em 0; position: relative; } #centered-container { margin: 0; padding: auto; position: absolute; top: 50%; -ms-transform: translateY(-50%); transform: translateY(-50%); } #status { position: relative; border: 1px solid lightgrey; border-radius: 8px; padding: 0.2em 0.2em } #status-message { float: left; margin-left: 1.2em; } #status-circle { width: 12px; height: 12px; background-color: orange; border-radius: 50%; float: left; margin: 0; position: absolute; top: 50%; -ms-transform: translateY(-50%); transform: translateY(-50%); } #button { height: auto; width: 1.5em; text-align: center; }.order-element { padding: 0; float: left; margin-left: 15px; }.clearfix { overflow: auto; }.clearfix::after { content: ""; clear: both; display: table; }.pad-1 { padding-top: 1px; padding-bottom: 1px; }
 <div id="outer-border" class="clearfix"> <div id="centered-container" class="clearfix"> <span name="user-name" class="order-element pad-1">John Smith</span> <div id="status" class="order-element"> <div id="status-circle" class=""></div> <span id="status-message" class="">Complete<span> </div> <div class="clearfix"> <span name="order-number" class="order-element pad-1">123456</span> <span name="order-amount" class="order-element pad-1">$35.45</span> <span name="fulfillment-date" class="order-element pad-1">07/07/2020 07:00 PM</span> <button id="button" class="order-element">...</button> </div> </div>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM