Sam
Sam

Reputation: 4284

centering div inside a div

I am a beginner level web designer. This question might have been asked, but I have refered that answers too but they have not helped. My problem is I have a div called "header" and 3 divs inside this div. These are not aligning to the center in the parent div. I have tried many answers but they are not working.

Here is my code:

    #header {
    height: 176px;
    text-align: center;
    position: absolute;
       }
   #header div {
    display: inline;
    text-align: center;
    margin: auto;
    float: left;
    position: relative;

}

#logo {
    height: 156px;
    width: 218px;
    background-image: url(../images/logo_03.jpg);


}

#tagline {
    width: 250px;


}
#badge {
    width: 300px;


}

here is html code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ebhar media</title>
<style type="text/css">
</style>
<link href="style/homestyle.css" rel="stylesheet" type="text/css" />
<style type="text/css">
body {

    min-width:1407px;
}
</style>
</head>

<body leftmargin="0px" topmargin="0px" marginwidth="0px" marginheight="0px">
<div id="base">
  <div id="header" align="center">
    <div id="logo"></div>
    <div id="tagline">YOUR SUCESS IS OUR SUCESS</div>
    <div id="badge">Content for  id "badge" Goes Here</div> 
  </div>
  <div id="navbar">Content for  id "navbar" Goes Here</div>
</div>
</body>
</html>

Upvotes: 2

Views: 360

Answers (4)

Roy Sonasish
Roy Sonasish

Reputation: 4599

remove the position:absolute from #header and remove position:relative, float:left from the #header div

#header {
    height: 176px;
    text-align: center;
/*    position: absolute;*/
}
#header div {
    display: inline;
    text-align: center;
    margin: auto;
/*    float: left;
    position: relative;*/
}

I think this will solve your issue.

Demo: http://jsfiddle.net/UYWqt/

Upvotes: 3

LadyBo
LadyBo

Reputation: 145

Is that what you exactly need?

http://jsfiddle.net/nJqfn/

Some remarks for future - don't use

<body leftmargin="0px" topmargin="0px" marginwidth="0px" marginheight="0px">.  

It's better to include normalize.css or reset.css for cross-browser view.
That's

<div id="header" align="center"> 

not good too. Try to separate view and code.

Upvotes: 0

edd
edd

Reputation: 1364

You can simply add some attribute on your div try something like this

<div align="center">
   <div></div>
</div>

Upvotes: 3

KaraokeStu
KaraokeStu

Reputation: 768

The following should work for you:

#header {
    height: 176px;
    width: 100%;
}

#header div { margin: auto; }

#logo {
    height: 156px;
    width: 218px;
    background-image: url(../images/logo_03.jpg);
}

#tagline {
    width: 250px;
}
#badge {
    width: 300px;
}

Upvotes: 0

Related Questions