I am learning css and I noticed that if I applied background color to all elements using the "*" attribute, although its being applied to the whole page and element ,the horizontal area next to the element is not showing background color. I have tried applying background color to element and body separately but still it doesn’t get fixed. Any suggestions on how to fix this will be great help.
Here is the html code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="index.css">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<title>Doc</title>
</head>
<body>
<button>hi</button>
</body>
</html>
And here’s css code:
*{
background-color: black;
}
2
Answers
It’s because you’re using bootsrap, it defines background color on body element:
You can increase specificity of body selector, for example add a class to body
and then reference it by class in css:
When using Bootstrap, some default styles may override your custom styles. You can use more specific CSS selectors or add !important to your styles.
for example:
You can use !important rule to give the 1st priority.
or you can edit the background color for body tag.
for your reference. here you can see the background color overrides by bootstrap css.