水平和垂直居中

1.7.1   高度小于1ex的图标的垂直居中:

“x”:x的底部是css中的基线;

x-height:小写x的高度;

1ex:一个小写x的高度。

原理:1ex相当于字母x,所以图片与文字TianJin的对齐,相当于文字x与文字TianJin的对齐,font-size都继承了父元素,自然能对齐。

<divid="down-weather">

<aclass="weather-which-city">

              <pclass="name-city">TianJin</p>

              <iclass="icon-select-city"></i>

       </a>

       <divid="weather_list">

              <imgid="weather_img"src="images/weather.png"/>

       </div>

</div>

.weather-which-city{

       height:24px;

       width:78.7344px;

       margin-bottom:18px;

       color:rgb(63,63,63);

       font-size:20px;

}

.name-city{

       display:inline-block;

       width:46.7188px;

}

.icon-select-city{

       display:inline-block;

       height:1ex;

       width:20px;

       background-image:url(../images/select.png);

       background-repeat:no-repeat;

       background-position:center;

}

1.7.2   line-height和vertical-align配合垂直居中:

首先,设置父级块元素的line-height,因为vertical-align是相对于ling-height来算的;

其次,设置需要居中的元素vertical-align:middle,此时距离居中还差半个x的距离;

最后,设置父级元素的font-size:0,完全居中。

<divid="left-peo-pic">

       <imgsrc="images/katie.png"alt="人"id="Katie" ">     

</div>

#left-peo-pic{

       width:300px;

       height:110px;

       line-height:110px;

       display:inline-block;

       font-size:0;

       background-color: aquamarine;

}

#left-peo-picimg{

       width:84px;

       height:84px;

vertical-align:middle;

}

简单便捷的居中办法,添加以下em代码,并设置需要居中的元素vertical-align:middle。

<divid="left-peo-pic">

       <imgsrc="images/katie.png"alt="人"id="Katie"style="vertical-align:middle;">    

       <emstyle="vertical-align:middle;height:100%;width:0;display:inline-block;">x</em>

</div>

1.7.3   利用text-align:center水平居中:

在父级元素中设置text-align:center可使得它的子元素水平居中显示,如下修改上面的样式可得到水平居中的效果。

#left-peo-pic{

       width:300px;

       height:110px;

       line-height:110px;

       display:inline-block;

       font-size:0;

       background-color: aquamarine;

       text-align:center;

}

1.7.4   利用margin水平居中:

首先,将需要居中的元素设置为块状元素display:block,其次设置margin-left:auto和margin-right:auto,就可以实现水平居中的效果。

值得注意的是,将元素设置为块状元素后,无法通过以上方法实现垂直居中的效果,因为上述方法针对的是内联元素。


### HTML CSS 实现三线表单元格文本水平垂直居中的方法 要实现三线表并使单元格内的文本水平垂直居中,可以结合 HTML 的 `<table>` 结构以及 CSS 样式来完成。以下是具体的方法: #### 使用 HTML 定义表格结构 通过 `<table>`, `<tr>`, `<td>` 或者 `<th>` 来定义基本的表格结构[^1]。 ```html <table> <thead> <tr> <th>表头1</th> <th>表头2</th> </tr> </thead> <tbody> <tr> <td>数据1</td> <td>数据2</td> </tr> </tbody> </table> ``` #### 设置三线表样式 为了创建三线表的效果,可以通过 `border` 属性设置上边框、下边框底部总边框,并隐藏其他多余的线条。 ```css /* 去除默认边框 */ table { border-collapse: collapse; } /* 上方横线 */ thead tr:first-child th { border-top: 1px solid black; border-bottom: 1px solid black; } /* 下方横线 */ tbody tr:last-child td { border-bottom: 1px solid black; } ``` #### 单元格内容水平垂直居中 利用 CSS 中的 `text-align` `vertical-align` 属性可以让单元格的内容达到水平垂直方向上的居中效果[^3]。 ```css th, td { text-align: center; /* 水平居中 */ vertical-align: middle; /* 垂直居中 */ padding: 8px; /* 调整间距以优化显示效果 */ } ``` 完整的代码如下所示: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>三线表示例</title> <style> table { width: 50%; margin: auto; border-collapse: collapse; } thead tr:first-child th { border-top: 1px solid black; border-bottom: 1px solid black; } tbody tr:last-child td { border-bottom: 1px solid black; } th, td { text-align: center; vertical-align: middle; padding: 8px; } </style> </head> <body> <table> <thead> <tr> <th>表头1</th> <th>表头2</th> </tr> </thead> <tbody> <tr> <td>数据1</td> <td>数据2</td> </tr> </tbody> </table> </body> </html> ``` 上述代码实现了具有三线样式的表格,并且其中的单元格内容能够做到水平垂直居中
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值