Click here to Skip to main content
15,909,953 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<html>
<head>
    <title>content behavior</title>

    <style>

        .h1_wrapper {
            background: orange;
            position: relative;
            height: 5em;
            width: 45em;
            margin-top: 1em;
            border: 2px solid;
        }
        
        .h1_wrapper h1 {
            background: green;
            position: absolute;
            top: 1em;
            right: 25%;
            bottom: 10px;
            left: 1em;
            background-clip: content-box;
        }
        
        #marginless {
            margin: 0;
        }
    
</style>

</head>

<body>

    <div class="h1_wrapper">
        <h1 id="marginless">Hey I'm a header inside a div tag.</h1>
    </div>

</body>
</html>


What I have tried:

<html>
<head>
    <title>content behavior</title>

    <style>

        .h1_wrapper {
            background: orange;
            position: relative;
            height: 5em;
            width: 45em;
            margin-top: 1em;
            border: 2px solid;
        }
        
        .h1_wrapper h1 {
            background: green;
            position: absolute;
            top: 1em;
            right: 25%;
            bottom: 10px;
            left: 1em;
            background-clip: content-box;
        }
        
        #marginless {
            margin: 25px;
        }

    </style>

</head>

<body>

    <div class="h1_wrapper">
        <h1 id="marginless">Hey I'm a header inside a div tag.</h1>
    </div>

</body>
</html>
Posted
Comments
Richard MacCutchan 1-Sep-17 5:18am    
Please edit your question and add a proper explanation of your problem.
AzuraLight 1-Sep-17 13:38pm    
Thank you, Sir, for looking into my question! I just wanted to know why the margins when they totally wrap the content area in this example and nothing is left of it. The content itself "Hey I'm a header inside a div tag," go down? I'm just not getting the reason, for example, why not go up instead? (Knowing that the more margin I add, the more "Hey I'm a header inside a div tag," will get down).

1 solution

Are you trying to align it in the middle?

You're using
CSS
top:1em;
that is why it is going so far down.

You could do something like this:
CSS
.h1_wrapper {
  background: orange;
  padding:1em;
  width: 45em;
  margin-top: 1em;
  border: 2px solid;
  box-sizing:border-box;
}
        
.h1_wrapper h1 {
  background: green;
  margin: 0;
  display:inline-block;
  padding:0.25em;
}


Or

CSS
.h1_wrapper {
  background: orange;
  padding:1em;
  width: 45em;
  margin-top: 1em;
  border: 2px solid;
  box-sizing:border-box;
}
        
.h1_wrapper h1 {
  background: green;
  margin: 0;
}
 
Share this answer
 
v2
Comments
AzuraLight 1-Sep-17 13:42pm    
Thank you, Sir, for looking into my question! I just wanted to know why the margins when they totally wrap the content area in this example and nothing is left of it. The content itself "Hey I'm a header inside a div tag," go down? I'm just not getting the reason, for example, why not go up instead? (Knowing that the more margin I add, the more "Hey I'm a header inside a div tag," will get down).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900