简体   繁体   English

在 Bootstrap 3 中将表列移向表中心,但不居中对齐

[英]Moving table columns toward center of table, but not center aligned, in Bootstrap 3

I have two side-by-side tables using Bootstrap 3. The only issue is I'd like the columns to be "moved in" toward the center of the table, with equal spacing on the left and right ends (but I don't want the text aligned center).我有两个使用 Bootstrap 3 并排的桌子。唯一的问题是我希望将列“移入”到桌子的中心,左右两端的间距相等(但我不不希望文本居中对齐)。

I can do this by wrapping the table cells in a <div> and giving them some padding.我可以通过将表格单元格包装在<div>中并给它们一些填充来做到这一点。 But I wasn't sure if there was a more responsive way to achieve this.但我不确定是否有更灵敏的方式来实现这一目标。

Edit : Just to clarify, I know I can use text-align: center to center align the columns and this will move the content toward the center.编辑:澄清一下,我知道我可以使用text-align: center来居中对齐列,这会将内容移向中心。 But I don't really want the text centered.但我真的不希望文本居中。 I just want the contents moved inward.我只希望内容向内移动。

Below is an example.下面是一个例子。 Table 1 is before I add any padding.表 1 在我添加任何填充之前。 Table 2 is with padding added to the first <div> .表 2 在第一个<div>中添加了填充。 The way Table 2 looks is closer to what I'd like, with the first column moved toward the center.表 2 看起来更接近我想要的,第一列移向中心。 But ideally I'd like equal whitespace at the left and right of both columns.但理想情况下,我希望两列的左侧和右侧有相等的空白。 Is there any way to achieve this without abusing the CSS or breaking Bootstrap's responsiveness?有没有办法在不滥用 CSS 或破坏 Bootstrap 的响应能力的情况下实现这一目标?

 td>div { padding-left: 35px; }
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <div class="container"> <div class="row"> <div class="col-sm-6"> <table class="table table-striped table-condensed table-borderless"> <thead> <tr> <th>Table 1</th> </tr> </thead> <tbody> <tr> <td> Some Field Name </td> <td>428 mg/L</td> </tr> <tr> <td> Some Field Name </td> <td>428 mg/L</td> </tr> <tr> <td> Some Field Name </td> <td>428 mg/L</td> </tr> </tbody> </table> </div> <div class="col-sm-6"> <table class="table table-striped table-condensed"> <thead> <tr> <th>Table 2</th> </tr> </thead> <tbody> <tr> <td> <div> Some Field Name</div> </td> <td> <div> 428 mg/L</div> </td> </tr> <tr> <td> <div> Some Field Name</div> </td> <td> <div> 428 mg/L</div> </td> </tr> <tr> <td> <div> Some Field Name</div> </td> <td> <div> 428 mg/L</div> </td> </tr> </tbody> </table> </div> </div> </div>

using text-align:center;使用文本对齐:中心;

 table, thead, tr, tbody, th, td { text-align: center; }.table td { text-align: center; }
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <div class="container"> <div class="row"> <div class="col-sm-6"> <table class="table table-striped table-condensed table-borderless"> <thead> <tr> <th>Table 1</th> </tr> </thead> <tbody> <tr> <td> Some Field Name </td> <td>428 mg/L</td> </tr> <tr> <td> Some Field Name </td> <td>428 mg/L</td> </tr> <tr> <td> Some Field Name </td> <td>428 mg/L</td> </tr> </tbody> </table> </div> <div class="col-sm-6"> <table class="table table-striped table-condensed"> <thead> <tr> <th>Table 2</th> </tr> </thead> <tbody> <tr> <td> <div> Some Field Name</div> </td> <td> <div> 428 mg/L</div> </td> </tr> <tr> <td> <div> Some Field Name</div> </td> <td> <div> 428 mg/L</div> </td> </tr> <tr> <td> <div> Some Field Name</div> </td> <td> <div> 428 mg/L</div> </td> </tr> </tbody> </table> </div> </div> </div>

If you are using table如果您使用表

{
    text-align: center; 
    vertical-align: middle;
}

is best to make the content center.最好是制作内容中心。

But I really recommend you use Flex to build tables, it gives you a lot of options on styling and placement.但我真的建议您使用 Flex 来构建表格,它为您提供了很多样式和放置选项。

Example of table on flex: flex 上的表格示例:

    <div class="screening-table">
        <div class="flex table-heading">
            <div class="min-width table-heading__cell heading__cell--1">Sample</div>
            <div class="min-width table-heading__cell heading__cell--2">Target</div>
            <div class="min-width table-heading__cell heading__cell--3">Target Form</div>
            <div class="min-width table-heading__cell heading__cell--4">Target Amt.</div>
            <div class="min-width table-heading__cell heading__cell--5">Supplement</div>
            <div class="min-width table-heading__cell heading__cell--6">Purpose</div>
            <div class="min-width table-heading__cell heading__cell--7">Expected Tracer Behavior/Explanation
            </div>
        </div>

        <div class="table-all-body">
            <div class="flex table-body">
                <div class="flex min-width table-body__cell heading__cell--1">
                    <div class="more-icon">
                        <img src="images/screening/ic_more_vert_24px.png" alt="view more">
                    </div>
                    1
                </div>
                <div class="min-width table-body__cell heading__cell--2">NTRK4</div>
                <div class="flex min-width table-body__cell heading__cell--3">
                    <div class="info-icon">
                        <img src="images/screening/ic_info_24px.png" alt="info">
                    </div>
                    <span>Avi-NTRK4(1-796) DP</span>
                </div>
                <div class="min-width table-body__cell heading__cell--4">250 pmol</div>
                <div class="flex min-width table-body__cell heading__cell--5">
                    <div class="info-icon">
                        <img src="images/screening/ic_info_24px.png" alt="info"></div>
                    <span>None</span>
                </div>
                <div class="min-width table-body__cell heading__cell--6">
                    Identify small molecules that bind the non-phosphorylated full-length target
                </div>
                <div class="min-width table-body__cell heading__cell--7">Unclear whether this tracer binds
                    the
                    non-phosphorylated and phosphorylated NTRK4 with similar affinities
                </div>
            </div>
            <div class="rectangle"></div>

CSS: CSS:

.screening-table {
    margin-top: 72px;
    // margin-left: 26px;
    // margin-top: 56px;
    margin-left: 246px;

    .min-width {
        min-width: 42px;
    }

    .table-heading {
        flex-direction: row;
    }

    .table-heading {
        background: $catsill-white;
        box-shadow: 0px -1px 0px $catsill-white, 0px 1px 0px $catsill-white;
        padding-top: 14px;
        padding-bottom: 14px;
    }

    .table-heading__cell {
        font-style: normal;
        font-weight: 500;
        font-size: 12px;
        line-height: 14px;
        margin-left: 14px;
        margin-right: 14px;
        color: $gull-gray;
    }

    .table-body {
        background: $primary-color;
        box-shadow: 0px 2px 6px rgba($text-color--black, 0.102874);
        border-radius: 3px;
    }

    .table-body__cell {
        margin-left: 14px;
        margin-right: 14px;
        padding-top: 20px;
        padding-bottom: 10px;

        font-style: normal;
        font-weight: normal;
        font-size: 16px;
        line-height: 19px;

        color: $plantation;

    }

    .info-icon {
        width: 24px;
        height: 24px;
    }

    .more-icon {
        margin-left: 8px;
    }

    .rectangle {
        border: 1px solid $catsill-white;
    }




}

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

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