时间:2021-07-01 10:21:17 帮助过:45人阅读
某天组长让我改一个表格的样式,要求底部局中。当时想很简单的嘛,哼哧哼哧开始写了,结果发现怎么样都达不到效果(考虑浏览器缩放)。一番思考加探讨之后总结出了三个方法针对于底部局中。
一、给form的父元素设置{width:60%;margin:0 auto;}
代码如下
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <style>
- *
- {
- padding: 0;
- margin: 0;
- }
- .main{
- width: 60%;
- margin:0 auto;
- }
- form{
- position:fixed;
- bottom: 0;
- width: 60%;
- display: block;
- }
- textarea
- {
- width: 80%;
- }
- input{
- width: 12%;
- height: 10%;
- position: relative;
- bottom:24px;
- }
- </style>
- <body>
- <div >
- <form id="wiselyForm" >
- <textarea rows="4" cols="60" name="text" id="wbk"></textarea>
- <input id="xxinput" type="submit" value="Send out"/>
- </form>
- </div>
- </body>
- </html>
使得div,form只占据页面中间一部分,这样只要达到form在底部即可
将textarea和input=submit宽度铺满width=60%即可达到底部局中。
二、text-align:center;
给页面布局时这是一个很强大的属性
- *
- {
- padding: 0;
- margin: 0;
- }
- .main{
- width: 100%;
- }
- form{
- position:fixed;
- bottom: 0;
- width: 100%;
- text-align: center;
- }
- input{
- position: relative;
- bottom:24px;
- }
当form宽度铺满整个屏幕时text-align:center实现居中,再将其固定到底部。
三、使用left:50%;margin-left:-半个身位;
这是一个很简单且快捷的方法
- <div class="main" >
- <form id="wiselyForm" >
- <textarea rows="4" cols="60" name="text" id="wbk"></textarea>
- <input id="xxinput" type="submit" value="Send out"/>
- </form>
- </div>
css:
- *{
- padding: 0 ;
- margin:0;
- }
- form{
- position: fixed;
- bottom: 0;
- left: 50%;/*相对于可视区窗口,距离窗口左边50%个可视区窗口*/
- margin-left:-237px ;/*表格左边距离浏览器左边50%,向左偏移一半的身位表格即可居中*/
- }
无论窗口放大缩小多少,表格都居中,重点在于margin-left= - 表格的半个身位 px
以上就是对css底部如何局中?css三种居中方法的全部介绍,如果您想了解更多有关CSS视频教程,请关注PHP中文网。
以上就是css底部如何局中?css三种居中方法的详细内容,更多请关注Gxl网其它相关文章!