Russell
Russell

Reputation: 31

Object won't center

I'm really embarrassed to ask this, but I can't find out why my login object won't center. I'm really new to this and I'm sorry of this is a silly question. I hope someone can help me. Here's my code:

<div id="main">
<div id="login">
<h2>Login</h2>
<form action="" method="post">
<label>Username :</label>
<input id="name" name="username" placeholder="username" type="text">
<label>Password :</label>
<input id="password" name="password" placeholder="**********" type="password">
<input name="submit" type="submit" value=" Login ">
<span><?php echo $error; ?></span>
</form>
</div>
</div>

CSS:

#main {
width:960px;
margin:50px auto;
font-family:raleway;
text-align: center;
}
span {
color:red
}
h2 {
background-color: white;
text-align:center;
border-radius:10px 10px 0 0;
margin:-10px -40px;
padding:15px
}
hr {
border:0;
border-bottom:1px solid #ccc;
margin:10px -40px;
margin-bottom:30px
}
#login {
width:600px;
float:left;
border-radius:10px;
font-family:raleway;
border:2px solid #ccc;
padding:10px 40px 25px;
margin-top:70px;
}
input[type=text],input[type=password] {
width:100%;
padding:10px;
margin-top:8px;
border:1px solid #ccc;
padding-left:5px;
font-size:16px;
font-family:raleway
}
input[type=submit] {
width:103.5%;
background-color: #2F8059;
color:#fff;
border:2px solid #2F8059;
padding:10px;
font-size:20px;
cursor:pointer;
border-radius:5px;
margin-bottom:15px;
height: 50px;   

}
#profile {
padding:50px;
border:1px dashed grey;
font-size:20px;
background-color:#DCE6F7;
text-align: center; 

}
#logout {
float:right;
padding:5px;
border:dashed 1px gray
}
a {
text-decoration:none;
color:#6495ed
}
i {
color:#6495ed
}

Upvotes: 0

Views: 51

Answers (2)

patwoj98
patwoj98

Reputation: 447

You just need to put another 2 CSS:

 input{
   text-align:center; //center your placeholder
}
label { //center your labels
    display: block;
    text-align: center;
    line-height: 150%;
    font-size: .85em;
}

#main {
width:960px;
margin:50px auto;
font-family:raleway;
text-align: center;
}
span {
color:red
}
h2 {
background-color: white;
text-align:center;
border-radius:10px 10px 0 0;
margin:-10px -40px;
padding:15px
}
hr {
border:0;
border-bottom:1px solid #ccc;
margin:10px -40px;
margin-bottom:30px
}
#login {
width:600px;
float:left;
border-radius:10px;
font-family:raleway;
border:2px solid #ccc;
padding:10px 40px 25px;
margin-top:70px;
}
input[type=text],input[type=password] {
width:100%;
padding:10px;
margin-top:8px;
border:1px solid #ccc;
padding-left:5px;
font-size:16px;
font-family:raleway
}
input[type=submit] {
width:103.5%;
background-color: #2F8059;
color:#fff;
border:2px solid #2F8059;
padding:10px;
font-size:20px;
cursor:pointer;
border-radius:5px;
margin-bottom:15px;
height: 50px;   

}
#profile {
padding:50px;
border:1px dashed grey;
font-size:20px;
background-color:#DCE6F7;
text-align: center; 

}
#logout {
float:right;
padding:5px;
border:dashed 1px gray
}
a {
text-decoration:none;
color:#6495ed
}
i {
color:#6495ed
}
input{
   text-align:center;
}
label {
    display: block;
    text-align: center;
    line-height: 150%;
    font-size: .85em;
}
<div id="main">
<div id="login">
<h2>Login</h2>
<form action="" method="post">
<label>Username:</label>
<input id="name" name="username" placeholder="username" type="text">
<label>Password:</label>
<input id="password" name="password" placeholder="**********" type="password">
<input name="submit" type="submit" value=" Login ">
<span><?php echo $error; ?></span>
</form>
</div>
</div>

Upvotes: 0

Johannes
Johannes

Reputation: 67778

In the rule for #login, remove the float: left and insert display: inline-block, so that the text-centering of its parent can be effective:

#main {
  width: 960px;
  margin: 50px auto;
  font-family: raleway;
  text-align: center;
}

span {
  color: red
}

h2 {
  background-color: white;
  text-align: center;
  border-radius: 10px 10px 0 0;
  margin: -10px -40px;
  padding: 15px
}

hr {
  border: 0;
  border-bottom: 1px solid #ccc;
  margin: 10px -40px;
  margin-bottom: 30px
}

#login {
  width: 600px;
  display: inline-block;
  border-radius: 10px;
  font-family: raleway;
  border: 2px solid #ccc;
  padding: 10px 40px 25px;
  margin-top: 70px;
}

input[type=text],
input[type=password] {
  width: 100%;
  padding: 10px;
  margin-top: 8px;
  border: 1px solid #ccc;
  padding-left: 5px;
  font-size: 16px;
  font-family: raleway
}

input[type=submit] {
  width: 103.5%;
  background-color: #2F8059;
  color: #fff;
  border: 2px solid #2F8059;
  padding: 10px;
  font-size: 20px;
  cursor: pointer;
  border-radius: 5px;
  margin-bottom: 15px;
  height: 50px;
}

#profile {
  padding: 50px;
  border: 1px dashed grey;
  font-size: 20px;
  background-color: #DCE6F7;
  text-align: center;
}

#logout {
  float: right;
  padding: 5px;
  border: dashed 1px gray
}

a {
  text-decoration: none;
  color: #6495ed
}

i {
  color: #6495ed
}
<div id="main">
  <div id="login">
    <h2>Login</h2>
    <form action="" method="post">
      <label>Username :</label>
      <input id="name" name="username" placeholder="username" type="text">
      <label>Password :</label>
      <input id="password" name="password" placeholder="**********" type="password">
      <input name="submit" type="submit" value=" Login ">
      <span><?php echo $error; ?></span>
    </form>
  </div>
</div>

Upvotes: 1

Related Questions