Every time you create an HTML file, each browser interprets the elements differently. For instance, some browsers have additional margin configurations for a <p>
tag or different line-height properties. To address these inconsistencies, developers often use a Reset CSS (for example: ). This helps to standardize default line heights, margins, font sizes of headings, and more.
In this case, the h1
element defaults to some configurations that may cause it to extend beyond the boundaries of the div
box due to margin and padding settings, as I observed. You can resolve this issue by adding the following to the <h1>
element: margin: 0; padding: 0;. I recommend using a Reset CSS for future projects to maintain greater control over such design elements.
Another recommendation is to utilize a separate CSS file to organize your styles. Inline styling can become cumbersome when making common modifications, requiring changes across multiple files. With CSS, you can make a single change in the file, which will be reflected across all HTML files using it.
Here is my suggested CSS fix:
HTML:
<div>
<h1>Welcome to a Website </h1>
</div>
CSS:
div {
/* Make title occupy entire screen */
width: 100%;
display: block;
/* Visual configurations */
height:40px;
background:grey;
border:3px solid;
border-radius:10px;
opacity:0.85;
overflow:hidden;
line-height:40px;
}
div h1 {
margin: 0;
padding: 0;
text-align:center;
}
I used width:100%; display:block instead of width:1000px under the assumption that you desire a block that spans 100% of the screen width. View a working demo: http://jsfiddle.net/brunoluiz/NyLAD/
Good luck with your HTML and CSS endeavors!