【问题标题】:How to align a text vertically in a div while inside another div?如何在另一个div中垂直对齐一个div中的文本?
【发布时间】:2013-07-19 07:58:11
【问题描述】:

我正在尝试垂直对齐 div 内的文本。我已经为我当前的问题创建了一个小提琴。

您可以在此链接上查看我的代码 http://jsfiddle.net/NSvGc/

在我当前的代码中

<div style="display: block; width: 500px; border: 1px dashed #C0C0C0; margin: 0 auto; padding: 5px;">
<div style="display: inline-block; width: 300px; height: 100%; vertical-align: middle;"
    ><b>Text</b>
    </div>
    <div style="display: inline-block;">
        <div style="display:block; width: 180px;" >
            <input type="text" value="2013-01-01 12:45 PM" name="triggerOn[]" id="triggerOn_'+ v.call_code_id +'" readonly="readonly" style="width: 175px;"></div>
        <div style="display:block; margin: 5px;"><input type="checkbox" id="isAppointment" name="isAppointment" value="1" /> Make an appointment.</div>
</div>
</div>

我需要做的是垂直对齐单词“Text”

这里的问题是内部 div 的高度不是 100%。

【问题讨论】:

    标签: css css-float vertical-alignment


    【解决方案1】:

    我冒昧地将 HTML 中的一些 CSS 移到了 CSS 区域中。在您的网页上,您可能应该将其放在&lt;style&gt; 标签中,而不是硬编码到每个元素中以使其可读。

    我更改了您的 HTML 结构,因为您的 HTML 结构非常...有趣...我将其格式化为应该出现的样子:一个包含所有内容的容器 -> 左右两个区域。从您的情况来看,这似乎是一次性修复,您不需要很大的灵活性,所以我想出了this hard coded solution。它涉及floating 正确的内容和绝对定位左侧的内容以使其居中。如果您的内容不仅仅是“文本”,则需要调整该类的 top 值\

    这是我编辑的 HTML

    <div class='container'>
    <b class='text'>Text</b>
        <div class='appointment'>
            <input type="text" value="2013-01-01 12:45 PM" name="triggerOn[]" id="triggerOn_'+ v.call_code_id +'" readonly="readonly" style="width: 175px;">
            </br>
            <input type="checkbox" id="isAppointment" name="isAppointment" value="1" />
            <div id='spacing'></div>Make an appointment.</div>
    </div>
    

    和我编辑的 CSS(分开,不在标签中)

    .container {
        display: block;
        width: 500px;
        height:70px;
        border: 1px dashed #C0C0C0;
        margin: 0 auto;
        padding: 5px; 
    }
    .appointment {
        display: inline-block;
        float:right;
    }
    .text {
        position:absolute;
        top:35px;
        margin-left:15px;
    }
    #spacing {
        width:10px;
        height:2px;
        display:inline-block;
    }
    

    编辑:

    由于您希望左侧区域完全垂直居中,因此我更改了 HTML 的结构并更改了 CSS 以适应它,但如果没有一些 javascript 帮助,这确实是不可能的。我使用 jQuery 将每个完美居中。另一种解决方案是使用 Flexbox,但我选择了 jQuery 路由。 Here is the new dynamic solution

    这是我更新的 HTML

    <div class="container">
          <div class="bubble-left">
              <b class='text'>"Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</b>
          </div>
        <div class='bubble-right'>
            <div class='appointment'>
            <input type="text" value="2013-01-01 12:45 PM" name="triggerOn[]" id="triggerOn_'+ v.call_code_id +'" readonly="readonly" style="width: 175px;"></br>
            <input type="checkbox" id="isAppointment" name="isAppointment" value="1" /><div id='spacing'></div>Make an appointment.</div>
        </div>
    </div>
    

    CSS

    .container {
        width: 500px; 
        height: auto;
        position: relative;
        border: 1px dashed #C0C0C0;
        margin: 0 auto;
        padding: 5px;
    }
    .bubble-left { 
        position: absolute; 
        left: 5px; 
        top: 0; 
        width: 300px; 
        height: 100%; 
        display: table; 
    }
    
    .bubble-left b {
        display: table-cell;
        vertical-align: middle; 
        text-align: center; 
    }
    
    .bubble-right {
        position: absolute; 
        padding-top:5px;
        right: 50px; 
        top: 40%; 
        width: 135px; 
        height: 30px; 
        display: table; 
    }
    #spacing {
        width:10px;
        height:2px;
        display:inline-block;
    }
    

    并添加了javascript

    var biggestHeight = "0";
    // Loop through elements children to find & set the biggest height
    $(".container *").each(function(){
     // If this elements height is bigger than the biggestHeight
     if ($(this).height() > biggestHeight ) {
       // Set the biggestHeight to this Height
       biggestHeight = $(this).height();
     }
    });
    
    // Set the container height
    $(".container").height(biggestHeight);
    
    //position right content verically
    $('.bubble-right').css('top', $('.bubble-right').parent().height()/2 - $('.bubble-right').height()/2);
    

    【讨论】:

    • 谢谢。仅当您知道文本的长度时,此代码才有效,因此您可以对绝对值进行硬编码。但在我的情况下,我不知道文本的长度。因此,如果我有一个 1 段长的文本,我希望文本在中间对齐,并且复选框/日期在中间对齐。
    • 那你下次应该在问题里这么说。检查我的更新以获取更新的解决方案
    • @Mike 我们的解决方案是否满足您的需求?
    【解决方案2】:

    如果是我,我会使用这样的表格:JSFiddle Link
    我为样式测试添加了红色边框。

    <table style="text-align: left; width: 500px; height: 100px; border: 1px dashed black; padding: 10px;" border="0"
     cellpadding="0" cellspacing="0">
      <tbody>
        <tr>
          <td style="vertical-align: bottom; font-size: 20px;">Text!</td>
          <td style="vertical-align: top; width: 150px;">
    
              <div style="display: inline-block;">
    
            <div style="display:block; width: 180px;" >
    
                <input type="text" value="2013-01-01 12:45 PM" name="triggerOn[]" id="triggerOn_'+ v.call_code_id +'" readonly="readonly" style="width: 175px;">
                </div>
    
                <div style="display:block; margin: 5px;">
                    <input type="checkbox" id="isAppointment" name="isAppointment" value="1" /> Make an appointment.
                </div>
    
            </div></div>
    
            </td>
        </tr>
      </tbody>
    </table>
    

    【讨论】:

    • 嗯,使用表格单元格,您可以垂直对齐到顶部、中间或底部。并使用 text-align to left、center 或 right 进行水平对齐。所以,要解决这个问题,只需将 td 标签更改为 &lt;td style="vertical-align: middle;"
    • 我为此使用了表格。我试图远离桌子,但这在这里不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    • 2018-05-02
    • 2014-06-24
    • 1970-01-01
    • 2023-03-07
    相关资源
    最近更新 更多