【问题标题】:How to center link in td如何在 td 中居中链接
【发布时间】:2017-10-10 16:57:54
【问题描述】:

我对 td 中的中心链接有疑问。问题是我不知道这个 td 中有多少链接。有些只有 1 个,但也可以有 14 个。它们是水平放置的(但是当太多时,它们会转到下一行 - 它们有我添加的 float:left )。我无法让他们居中。右边总是有空间,特别是当一个链接的宽度太宽并且要到另一行时。我也尝试过为我的班级做 .hour {display: inline-block } 但是在小尺寸上,所有链接都在一行中,看起来很糟糕。有可能这样做吗?我不知道或者你理解我:D

  <div class="container">

    <div class="row">
        <div class="box">
            <div class="col-lg-12">
                <hr>
                <h2 class="intro-text text-center">{{$doctorsDeadlines['lekarz']['imie']}} {{$doctorsDeadlines['lekarz']['nazwisko']}}
                </h2>
                <hr>
                <hr class="visible-xs">
                <h4 class="text-center">Terminarz</h4>
                <br/>
                <div class="table-responsive">
                    <table class="table table-striped table-bordered">
                    <tr>
                        @foreach($doctorsDeadlines['terminy'] as $date => $hours)
                            <th class="text-center">{{$date}}</th>
                        @endforeach
                    </tr>

                    <tr>
                        @foreach($doctorsDeadlines['terminy'] as $date => $hours)
                        <td class="hour-container">
                            <div class="hour-div">
                            @foreach($hours as $hour )
                                <a class="hour" href="#">{{$hour}}</a>
                            @endforeach
                            </div>

                        </td>
                        @endforeach
                    </tr>
                </table>
            </div>
        </div>
    </div>
  </div>
</div>

CSS代码:

.hour {
color: white !important;
background-color: #6699CC;!important;
display: block;
padding: 5px;
float: left;
text-align: center;
margin: 3px;
font-size: 15px;
}

.hour:hover {
color: #000 !important;
background-color: #dee1ed;!important;
text-decoration: none;
}

.hour-div {
border: 1px solid black;
background-color: yellow;
display: inline-block;
}

【问题讨论】:

  • 你不能居中浮动元素,删除浮动并给你的 td text-align: center
  • text-align: center; 应该应用于要居中的文本/内联元素的父元素。不直接在您想要居中的元素上。此外,浮动锚点会使文本对齐无效,请撤消。

标签: html css twitter-bootstrap center


【解决方案1】:

试试看,否则评论;

替换

               <table class="table table-striped table-bordered">
                <tr>
                    @foreach($doctorsDeadlines['terminy'] as $date => $hours)
                        <th class="text-center">{{$date}}</th>
                    @endforeach
                </tr>

                <tr>
                    @foreach($doctorsDeadlines['terminy'] as $date => $hours)
                    <td class="hour-container">
                        <div class="hour-div">
                        @foreach($hours as $hour )
                            <a class="hour" href="../panel/wizyty/dodaj?id_lekarza={{$doctorsDeadlines['lekarz']['id']}}&data={{$date}}&godzina={{$hour}}">{{$hour}}</a>
                        @endforeach
                        </div>

                    </td>
                    @endforeach
                </tr>
            </table>

有了这个

<table class="table table-striped table-bordered">
                    <tr>
                        @foreach($doctorsDeadlines['terminy'] as $date => $hours)
                            <th class="text-center">{{$date}}</th>
                        @endforeach
                    </tr>

                    <tr>
                        @foreach($doctorsDeadlines['terminy'] as $date => $hours)
                        @foreach($hours as $hour )
                        <td class="hour-container">
                            <div class="hour-div">

                                <a class="hour" href="../panel/wizyty/dodaj?id_lekarza={{$doctorsDeadlines['lekarz']['id']}}&data={{$date}}&godzina={{$hour}}">{{$hour}}</a>

                            </div>

                        </td>
                        @endforeach
                        @endforeach
                    </tr>
                </table>

【讨论】:

  • 但是当我删除浮动时,每个链接都在单元格 100% 宽度上的一行中,一个下一个(垂直),但我希望我的链接一个接一个(水平),如果空间太小,则转到另一行并再次水平。
  • 编辑了答案
  • 不不不,现在它坏了;p
  • 我尝试了很多解决方案,但我无法处理它
  • 我编辑了主帖,有一个网站,正是我想要的样子,但时间没有居中。
猜你喜欢
  • 1970-01-01
  • 2011-05-17
  • 1970-01-01
  • 2017-10-21
  • 1970-01-01
  • 2019-06-16
  • 1970-01-01
  • 2016-03-02
  • 1970-01-01
相关资源
最近更新 更多