form表单无法居中显示

form表单无法居中显示

http://img1.sycdn.imooc.com//climg/5e8afd0b0985bd3e09180699.jpg

如图无法居中显示,css中已设置margin:100px auto; 怎样才能让表单居中呢

html:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Career Builder</title>

<link rel="stylesheet" type="text/css" href="css/common.css">

<script type="text/javascript" src="js/js.js"></script>

</head>

<body>

<div class="header">

<div class="logo">

<a href="###"><img src="images/logo.png"></a>

</div>

<ul>

<li><a href='###'>HOME</a></li>

<li><a href='###'>ABOUT</a></li>

<li><a href='###'>GALLERY</a></li>

<li><a href='###'>FACULTY</a></li>

<li><a href='###'>EVENTS</a></li>

<li><a href='###'>CONTACT</a></li>

</ul>

</div>

<div class="banner">

<div class="banner-img">

<img src="images/banner3.jpg">

<div class='cap'></div>

<form>

<!-- placeholder属性设置默认显示 -->

<input type="text" name="name" placeholder='your Name'><br>

<input type="text" name="phone" placeholder="your Phone"><br>

<input type="text" name="email" placeholder="your Email"><br>

<input type="text" name="comment" placeholder="Write Your Comment Here"><br>

<input type="button" name="send_btn" value="SEND MESSAGE">

</form>

</div>

</div>

</body>

</html>

css:

*{

padding: 0px;

margin: 0px;

font-family: Microsoft YaHei UI;

}


body{

/*浏览器100%显示*/

width: 100%;

/*将页面左右居中*/

margin: 0px auto;

}


a{

/*取消超链接颜色样式*/

text-decoration: none;

color: white;

}


.header{

height: 60px;

background-color: #07cbc9

}


.header .logo img{

margin-left:100px;

float: left;

margin-top: 6px;

}


.header ul{

margin-right: 70px;

}


.header ul li{

list-style: none;

float: right;

margin-right: 30px;

padding-top: 22px;

box-sizing: border-box;

height: 60px;

font-weight: bold;

}


.header ul li:hover{

background-color: #000;

}


.banner .banner-img img{

height: 800px;

width: 100%;

position: absolute;

z-index: 10;

}

.banner .banner-img{

width: 100%;

}


.banner .banner-img .cap{

height: 800px;

width: 100%;

position: absolute;

z-index: 20;

background-color: black;

opacity: 50%

}


.banner .banner-img form{

position: absolute;

width: 100px;

margin: 100px auto;

z-index: 30;

}


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

.banner .banner-img form{

position: absolute;

width: 100px;

margin: 100px auto;

z-index: 30;

}

这里有错

第一个:width不能是100px,而是100%

第二个:设置居中text-align: center;

改正后

.banner .banner-img form{

position: absolute;

width: 100%;

margin: 100px auto;

z-index: 30;

text-align: center;

}


  • 慕妹6391370 提问者 #1
    为什么表单是通过text-align:center;来实现居中的呢?是表单独有的吗 ?
    2020-04-06 21:13:20
  • 慕丝1539783 回复 提问者 慕妹6391370 #2
    除了p、h1-h6可以使用text-align,div、form这样的容器也可以使用text-align,或者说,块级元素都可以使用text-align。 但是要实现居中,重点是看这个元素下面的子元素是什么,如果子元素是文字、input、img,就可以用text-align:center实现子元素居中;子元素如果是容器(比如div),就没法使这个子元素容器居中,但是能影响这个子元素下的孙元素居中。可能不太好理解,自己试试就知道了。所以text-align重点还是看子元素是什么。
    2020-04-06 22:45:10
  • 慕丝1539783 回复 提问者 慕妹6391370 #3
    另外,实现子元素居中方法有很多,只不过text-align:center是最快的。
    2020-04-06 22:47:21
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
3.从网页搭建入门Python Web
  • 参与学习           人
  • 提交作业       218    份
  • 解答问题       3562    个

本阶段带你用Python开发一个网站,学习主流框架Django+Flask是Python Web开发的第一步,在基础知识上实现积分商城的项目开发,体验真实的项目开发流程,提高解决编程问题和效率的能力。

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师