是的,您提到的所有元素都是可能的。
拿这个 HTML:
<body>
<div class="canvas">
<p>This is a really lovely but meaningless piece of text</p>
<div class="form-container">
<form>
<input type="text" name="example" placeholder="Enter some example text" />
</form>
</div>
</div>
</body>
所以我们想要将一些元素居中。让我们从包含 DIV #canvas 开始,然后从那里开始。
/* First a very crude reset on <body> and declare it's dimensions */
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
/* Next we must declare a definitive width of our #canvas to allow
us to centre itself and elements within it. */
#canvas {
width:960px;
position:relative;
margin: 0 auto; /* This 'auto' on left / right margin centers it
}
/* Note that <p> tags are auto 100% width so centering them is not
going to do anything with the above technique. Instead: */
#canvas p {
text-align: center;
}
/* Now form elements, no need to style <form> it is a meta container
and really shouldn't be used for anything other than that, lets
style the actual input elements instead */
#canvas form input {
width: 30%; /* Lets define a width so that we can center it in */
margin: 0 auto;
}
希望对您有所帮助。