The way you stated isn't the best practice, however, if you really want to implement your way then you must know the following:
1. Default style sheet and resetting:
Every browser has its own predefined stylesheet which affects many aspects especially the margin and the padding for all HTML elements, and as a general rule, any website first CSS line should be overriding this default behavior.
There are some ways to override these values, but as a beginner, you can use this:
* {
margin: 0;
padding: 0;
}
But this is a bad practice also, kind of performance issues, because using *
means that you tell the browser that it should get all available HTML tags on earth and apply the style for them, another perspective - actually the formal one - is to mention all used tags in your website instead of *
:
div, span, h1, h2, h3, h4, h5, h6, p, ul, ol, li ...{
margin: 0;
padding: 0;
}
2. Elements display values (Block-level elements and inline elements)
You've used h3
, div
which are block-level elements, and h3
tag has its own default font size which will not be fitted - as is - in the specified space for its div
container 50px * 20px, so to workaround it you can change the font-size
of the h3
to be a percentage of its parent container size.
This is a full snippet:
<!DOCTYPE html>
<html>
<head>
<style>
* { /* ! Don't use asterik (*) and replace it with all possible tags */
margin: 0;
padding: 0;
}
.icon {
width: 50px;
height: 20px;
}
.icon h3 {
background-color: yellow;
font-size: 85%;
}
</style>
</head>
<body bgcolor="gray">
<div class="icon">
<center> <!--Center the h3 in the div-->
<h3> google </h3>
</center>
</div>
</body>
</html>