2016-09-04 5 views
1

Работает на сайте генератора цитат. Я добавил бутстрап, а в коде css у меня есть часть, где я устанавливаю фоновый цвет элемента HTML в сине-зеленый оттенок. bootstrap просто решает переопределить CSS-код и устанавливает часть страницы, на которой цвет фона содержимого имеет белый цвет. Почему и как я могу сохранить свой цвет фона, что я хочу, чтобы это былоBootstrap change background-color

var quotes = [ 
 
    [ 
 
     "To be prepared for war is one of the most effectual means of preserving peace.", 
 
     "George Washington" 
 
    ], 
 
    [ 
 
     "One man with courage is a majority.", 
 
     "Thomas Jefferson" 
 
    ], 
 
    [ 
 
     "National honor is a national property of the highest value.", 
 
     "James Monroe" 
 
    ], 
 
    [ 
 
     "The only thing we have to fear is fear itself.", 
 
     "Theodore D. Roosevelt" 
 
    ], 
 
    [ 
 
     "Ask not what your country can do for you, but what you can do for your country.", 
 
     "John F. Kennedy" 
 
    ] 
 
    
 
]; 
 
var currentQuote = 0; 
 
function showNewQuote() { 
 
\t if (currentQuote >= 5) { 
 
\t \t currentQuote = 0; \t 
 
\t } 
 
\t var Quote = [quotes[currentQuote][0], quotes[currentQuote][1]]; 
 
\t document.getElementById("header").innerHTML = "\"" + Quote[0] + "\""; 
 
\t document.getElementById("paragraph").innerHTML = Quote[1]; 
 
\t currentQuote++; 
 
}
html { 
 
\t background-color: #33cccc; 
 
} 
 

 
#header, #paragraph { 
 
\t font-family: sans-serif; 
 
\t 
 
} 
 

 
#header { 
 
\t padding-top: 10%; 
 
\t font-size: 60px; 
 
} 
 

 
#paragraph { 
 
\t padding-left: 80%; 
 
\t font-size: 20px; 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <title>Random Quote Generator</title> 
 
    <script src = "script.js"></script> 
 
    <link rel="stylesheet" type="text/css" href="stylesheet.css"> 
 
\t <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 
 
</head> 
 
<body> 
 
\t <div class = "container"> 
 
    <h1 id="header">Click the button to generate a quote!</h1> 
 
\t <p id="paragraph"></p> 
 
\t <button type="button" class="btn btn-info" onclick="showNewQuote()">Generate New Quote</button> 
 
\t </div> 
 
</body> 
 
</html>

ответ

1

Вариант 1: Убедитесь, что ваш CSS загружается послеbootstrap.css файла.

var quotes = [ 
 
    [ 
 
     "To be prepared for war is one of the most effectual means of preserving peace.", 
 
     "George Washington" 
 
    ], 
 
    [ 
 
     "One man with courage is a majority.", 
 
     "Thomas Jefferson" 
 
    ], 
 
    [ 
 
     "National honor is a national property of the highest value.", 
 
     "James Monroe" 
 
    ], 
 
    [ 
 
     "The only thing we have to fear is fear itself.", 
 
     "Theodore D. Roosevelt" 
 
    ], 
 
    [ 
 
     "Ask not what your country can do for you, but what you can do for your country.", 
 
     "John F. Kennedy" 
 
    ] 
 
    
 
]; 
 
var currentQuote = 0; 
 
function showNewQuote() { 
 
\t if (currentQuote >= 5) { 
 
\t \t currentQuote = 0; \t 
 
\t } 
 
\t var Quote = [quotes[currentQuote][0], quotes[currentQuote][1]]; 
 
\t document.getElementById("header").innerHTML = "\"" + Quote[0] + "\""; 
 
\t document.getElementById("paragraph").innerHTML = Quote[1]; 
 
\t currentQuote++; 
 
}
<script src = "script.js"></script> 
 
<link rel="stylesheet" type="text/css" href="stylesheet.css"> 
 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 
 
<style> 
 
body { 
 
\t background-color: #33cccc; 
 
} 
 

 
#header, #paragraph { 
 
\t font-family: sans-serif; 
 
\t 
 
} 
 

 
#header { 
 
\t padding-top: 10%; 
 
\t font-size: 60px; 
 
} 
 

 
#paragraph { 
 
\t padding-left: 80%; 
 
\t font-size: 20px; 
 
} 
 
    </style> 
 
<body> 
 
\t <div class = "container"> 
 
    <h1 id="header">Click the button to generate a quote!</h1> 
 
\t <p id="paragraph"></p> 
 
\t <button type="button" class="btn btn-info" onclick="showNewQuote()">Generate New Quote</button> 
 
\t </div> 
 
</body> 
 
</html>

Вариант 2: Используйте !important ключевое слово:

var quotes = [ 
 
    [ 
 
     "To be prepared for war is one of the most effectual means of preserving peace.", 
 
     "George Washington" 
 
    ], 
 
    [ 
 
     "One man with courage is a majority.", 
 
     "Thomas Jefferson" 
 
    ], 
 
    [ 
 
     "National honor is a national property of the highest value.", 
 
     "James Monroe" 
 
    ], 
 
    [ 
 
     "The only thing we have to fear is fear itself.", 
 
     "Theodore D. Roosevelt" 
 
    ], 
 
    [ 
 
     "Ask not what your country can do for you, but what you can do for your country.", 
 
     "John F. Kennedy" 
 
    ] 
 
    
 
]; 
 
var currentQuote = 0; 
 
function showNewQuote() { 
 
\t if (currentQuote >= 5) { 
 
\t \t currentQuote = 0; \t 
 
\t } 
 
\t var Quote = [quotes[currentQuote][0], quotes[currentQuote][1]]; 
 
\t document.getElementById("header").innerHTML = "\"" + Quote[0] + "\""; 
 
\t document.getElementById("paragraph").innerHTML = Quote[1]; 
 
\t currentQuote++; 
 
}
body { 
 
\t background-color: #33cccc !important; 
 
} 
 

 
#header, #paragraph { 
 
\t font-family: sans-serif; 
 
\t 
 
} 
 

 
#header { 
 
\t padding-top: 10%; 
 
\t font-size: 60px; 
 
} 
 

 
#paragraph { 
 
\t padding-left: 80%; 
 
\t font-size: 20px; 
 
}
<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <title>Random Quote Generator</title> 
 
    <script src = "script.js"></script> 
 
    <link rel="stylesheet" type="text/css" href="stylesheet.css"> 
 
\t <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 
 
</head> 
 
<body> 
 
\t <div class = "container"> 
 
    <h1 id="header">Click the button to generate a quote!</h1> 
 
\t <p id="paragraph"></p> 
 
\t <button type="button" class="btn btn-info" onclick="showNewQuote()">Generate New Quote</button> 
 
\t </div> 
 
</body> 
 
</html>

(Обратите внимание, что я также сделал переход от html к body в вашем CSS)

+0

Благодаря она работала. Это не позволит мне отметить это как ответ, пока не пройдет 5 минут, но я отметю его, когда время восстановления завершено. Он отлично работает сейчас –

+0

Уверен :) приветствую вас – Dekel

0

Как упомянуто выше, сначала загрузите бутстрап, а затем ваш css. Вместо использования! Важно, что вы можете использовать ваши браузеры dev-tools (я рекомендую Chromes) и искать селектор цвета фона для начальной загрузки. Его специфика в большинстве случаев довольно скрыта, и вам нужно добавить более высокую специфичность для вашего стиля.

Читать эту статью на MDN для получения дополнительной информации: MDN CSS Specificity