JPV
JPV

Reputation: 1079

Block positions

I am recently new to CSS and HTML and I have a problem while allocating my block under the header:

enter image description here

I have tried several solutions but I have not succeed. I would appreciate if you could give me a hint with it. Thanks

<!  DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Title goes here</title>
  <meta name="description" content="Description of your site goes here">
  <meta name="keywords" content="keyword1, keyword2, keyword3">
  <link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
    <div class="page">
    <div class="header" >
    <h1>  
        <img src="images/img1.jpg" width="250" height="190" float="right" />  
        <p> SOME TEXT HERE </p>
    </h1>
    </div>
    <div class="topmenu">
        <ul>
            <li><a href="index.html">Home</a></li>
            <li><a href="#">About&nbsp;Us</a></li>
            <li><a href="#">What's&nbsp;New</a></li>
            <li><a href="#">Services</a></li>
            <li><a href="#">Contact</a></li>
            <li><a href="#">Resources</a></li>
            <li><a href="#">Links</a></li>
        </ul>
    </div>
</body>
</html>

And my CSS CODE:

body {
    font-family: sans-serif,Arial;
    font-size: 12px;
    color: #000000;
    margin: 0px;
    background-color:#d9d7d7;
}
h1, h2, h3, h4, h5, h6, p, ul, ol, li, form, input, textarea {
    padding: 0px;
    margin: 0px;
    color: black;
}
a {
    color: #072FCF;
    text-decoration: underline;
}
a:hover {
    color: #072FCF;
    text-decoration: none;
}
.main-out {
    background-image: url(../images/trans.png);
    background-position: center top;
    width: 100%;
    float: left;
}
.main {
    width: 1000px;
    margin: 0px auto;
}
.page {
    width: 1000px;
    float: left;
    padding: 42px 0px 0px 0px;
    position: center;
}   
.header {
    position:absolute; 
    top:42px; 
    margin-left:-500px; 
    left:50%;
    width: 1000px;
    height: 200px;
    background-color: white;
    border-style: solid solid none solid;
    border-width: thick;    
}
.header h1{
    display: inline;
    text-align: left;
    font-family: cursive;
    font-size: 45px;
    color: black;

}
.header img {
    display: block;
    float: left;
}
.header p {
    line-height: 190px; /* Here is the trick... line-height = image height */
}
.topmenu {
    position:absolute;
    background-color: black;
    width: 1000px;
    height: 37px;
    border: 1px solid #000000;
}
.topmenu ul {
    width: 100%;
    height: 37px;
    list-style-type: none;
}
.topmenu ul li {
    height: 37px;
    float: left;
    padding-right: 24px;
    padding-left: 24px;
}
.topmenu ul li a {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    font-weight: bold;
    line-height: 37px;
    color: #FFFFFF;
    text-decoration: none;
    display: block;
    height: 37px;
    float: left;
    padding-right: 21px;
    padding-left: 21px;
}
.topmenu ul li a:hover {
    background-image: url(../images/menu-hov.jpg);
    background-repeat: repeat-x;
    background-position: left top;
}

Thanks

Upvotes: 2

Views: 122

Answers (4)

Pipo
Pipo

Reputation: 988

You need to understand three things to improve your html & css skills:

  • Always follow natural stacking order (first element in html will display before second element...),
  • Don't use position: absolute except if you know what you are doing as @Billy said,
  • Use html5 tags if you don't need to support IE8 and below. If you do, then use HTML5 Shiv to make them compatible.

Now here is a valid code that is also responsive (it will resize to your browser's viewport size). I have added a lot of comments in the code so that you can easily understand.

Good luck with your project!

.page {
  width: 100%; /* Makes the page responsive */
  max-width: 1000px; /* all the content inside this div will be 1000 px width */
  margin: 0 auto; /* To center your page div in the page */
}
.topmenu ul {
  list-style-type: none;
  padding: 0;
  margin: 0;
}
.topmenu ul li a {
  display: block;
  float: left;
  width: 14.2857142%; /* 100 / 7 (number of menu items) */
  background-color: #000;
  font: bold 12px Arial, Helvetica, sans-serif;
  color: #fff;
  text-decoration: none;
  padding: 10px 0;
  text-align: center;
}
.topmenu ul li a:hover {
  /* As a general rule, never use images for hovers */
  background-color: #fff;
  color: #000;
}
.topmenu:after { /* This is a clearfix to clear your floated elements */
  content: "";
  display: table;
  clear: both;
}
header img {
  display: inline-block;
  width: 250px;
  height: 190px;
}
header h1 {
  display: inline;
  text-align: left;
  font-family: cursive;
  font-size: 25px;
  color: black;
  vertical-align: top; /* if you want the text to start at the top of the picture. Otherwise try middle or bottom */
}
<div class="page">
  <!-- always start with the first element on your page: here it's your navigation -->
  <nav class="topmenu"> <!-- use html5 tags. If you need to support IE8 or below you can use HTML5 Shiv -->
    <ul>
      <li><a href="index.html">Home</a></li>
      <li><a href="#">About&nbsp;Us</a></li>
      <li><a href="#">What's&nbsp;New</a></li>
      <li><a href="#">Services</a></li>
      <li><a href="#">Contact</a></li>
      <li><a href="#">Resources</a></li>
      <li><a href="#">Links</a></li>
    </ul>
  </nav>
  <header> <!-- same, use html5 tags -->
    <!-- As a general rule, css is for styling not html so don't put any width, height or style in img tag -->
    <img src="http://placehold.it/250x190" alt="your picture description"/> <!-- always use alt text in images for accessibility purposes -->
    <h1>SOME TEXT HERE</h1>
  </header>
</div> <!-- don't forget this div that closes your .page -->

Upvotes: 2

Alex Char
Alex Char

Reputation: 33218

I have made several changes to your html/css:

body {
    font-family: sans-serif, Arial;
    font-size: 12px;
    color: #000000;
    margin: 0px;
    background-color:#d9d7d7;
}
h1, h2, h3, h4, h5, h6, p, ul, ol, li, form, input, textarea {
    padding: 0px;
    margin: 0px;
    color: black;
}
a {
    color: #072FCF;
    text-decoration: underline;
}
a:hover {
    color: #072FCF;
    text-decoration: none;
}
.main-out {
    background-image: url(../images/trans.png);
    background-position: center top;
    width: 100%;
    float: left;
}
.main {
    width: 1000px;
    margin: 0px auto;
}
.page {
    width: 1000px;
    margin: 0 auto;
}
.header {
    position: relative;
    width: 1000px;
    height: 200px;
    background-color: white;
    border-style: solid solid none solid;
    border-width: thick;
}
.header h1 {
    display: inline;
    text-align: left;
    font-family: cursive;
    font-size: 45px;
    color: black;
}
.header img {
    display: block;
    float: left;
}
.header p {
    line-height: 190px;
    /* Here is the trick... line-height = image height */
}
.topmenu {
    position:relative;
    background-color: black;
    width: 1000px;
    height: 37px;
    border: 1px solid #000000;
    padding-right: 8px;
}
.topmenu ul {
    width: 100%;
    height: 37px;
    list-style-type: none;
}
.topmenu ul li {
    height: 37px;
    float: left;
    padding-right: 24px;
    padding-left: 24px;
}
.topmenu ul li a {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    font-weight: bold;
    line-height: 37px;
    color: #FFFFFF;
    text-decoration: none;
    display: block;
    height: 37px;
    float: left;
    padding-right: 21px;
    padding-left: 21px;
}
.topmenu ul li a:hover {
    background-image: url(../images/menu-hov.jpg);
    background-repeat: repeat-x;
    background-position: left top;
}
<body>
    <div class="page">
                <div class="topmenu">
            <ul>
                <li><a href="index.html">Home</a>
                </li>
                <li><a href="#">About&nbsp;Us</a>
                </li>
                <li><a href="#">What's&nbsp;New</a>
                </li>
                <li><a href="#">Services</a>
                </li>
                <li><a href="#">Contact</a>
                </li>
                <li><a href="#">Resources</a>
                </li>
                <li><a href="#">Links</a>
                </li>
            </ul>
        </div>
        <div class="header">
             <h1>  
        <img src="images/img1.jpg" width="250" height="190" float="right" />  
        <p> SOME TEXT HERE </p>
    </h1>

        </div>
</body>

Upvotes: 2

Lam Yum
Lam Yum

Reputation: 51

I think you should rearrange your HTML markup.It doesn't seem like you are using the proper nested rule.I suggest you try to remove the img tag outside the h1 tag.Your div with class = "page" doesn't have a ending tag.

Upvotes: 0

Bill
Bill

Reputation: 3517

Remove all of your absolute positioning (and add in the missing </div> tag to finish the .page div - I'm assuming this is wrapping all of your content inside).

To center your content, replace your .page CSS rule with this:

.page{
  width: 1000px; // I would reccommend using 960px instead as it is more standard
  margin: 0 auto;
  //add your padding in if you need it
}

Don't use absolute positioning until you understand it and why/how/when you should use it

Upvotes: 0

Related Questions