Yacomini Yacomini - 3 months ago 8
Javascript Question

Why isn't my stylesheet overwritting bootstrap's?

I read millons of time that if you define a bootstrap class in your own CSS stylesheet you will overwrite them so your created element will have your predefined style as you want.

While this is true to half of the page, my input button using the class

form-control
it just won't work, and I can't figure it out the reason why, as anybody else is having this problem.

Here's a snippet with my issue:



var searcher = document.getElementById('buscador');

searcher.addEventListener("click", function()
{
var master = searcher.parentNode;
searcher.removeChild(searcher.childNodes[0]);
searcher.style.animationName = "expandismo";
searcher.style.animationDuration = "1s";
setTimeout(function(){
master.removeChild(searcher);
var barraSearch = document.createElement('input');
barraSearch.setAttribute('type', 'text');
barraSearch.setAttribute('id', 'barraSearch');
barraSearch.setAttribute('class', 'form-control');
barraSearch.setAttribute('placeholder', 'Buscar...');
var spanCheto = document.createElement('span');
var botonCheto = document.createElement('button');
var secondSpan = document.createElement('span');
spanCheto.setAttribute('class', 'input-group-btn');
botonCheto.setAttribute('class', 'btn btn-default');
secondSpan.setAttribute('class', 'glyphicon glyphicon-search');
secondSpan.setAttribute('aria-hidden', 'true');
botonCheto.appendChild(secondSpan);
spanCheto.appendChild(botonCheto);



master.appendChild(barraSearch);
barraSearch.focus();

}, 1000);

}, false);

.container-fluid
{
text-align: center;
}
#toplane
{

color: white;
background-image: url(citybackground.jpg);
background-size: 100% 100%;
min-height: 400px;
}
#botlane
{

}
#headone
{
margin-top: 130px;
color: black;
text-shadow: 0px 0px 12px white, 0px 0px 8px white, 0px 0px 3px white, 0px 0px 5px white;
font-family: "Times New Roman", Times, monospace;
font-size: 60px;
}
#buscador
{
color: #595959;
width: 40px;
height: 40px;
border-radius: 18px;
background-color: lightgrey;
cursor: text;
}
#buscador:hover
{
background-color: lightgrey;
opacity: 1;
}
#buscador:active
{
background-color: white;
}
.input-group
{
width: 100%;
text-align: center;
}
.form-control
{
width: 70%;
border-radius: 18px;
background-color: lightgrey;
height: 40px;
padding-left: 10px;
font-size: 20px;
color: black;
}
.form-control:focus
{
box-shadow: 0px 0px 5px #66d9ff;
box-shadow: 0px 0px 10px #66d9ff;
box-shadow: 0px 0px 8px #66d9ff;
box-shadow: 0px 0px 12px #66d9ff;
}
@keyframes expandismo
{
from{width: 40px}
to{width: 70%}
}

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Price Surfer FAQ</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.2.0/normalize.min.css">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="wean1.css">

</head>
<body>
<div class="container-fluid" id="toplane">
<h1 id="headone">PRICE SURFER FAQ</h1>
<div class="input-group">
<button id="buscador"><span class="glyphicon glyphicon-search" aria-hidden="true"></span></button>
</div>
</div>
<div class="container-fluid" id="botlane">

</div>
<script type="text/javascript" src="wean1.js"></script>
</body>
</html>




Answer
.container-fluid .input-group .form-control{
   /* yous css*/
}

This is because the bootstrap get the .form-control with two clases and this type of selection .input-group .form-control is stronger then just .form-control

.input-group .form-control{

}

Use this website to calculate which selector is stronger .

Check this screenshot the first selectors have 3 points and the second has only two so doesn't matter where you place the first selectors are stronger than second. Every id has 100 points , classes have 10 points and tags have 1 point