2016-09-04 3 views
1

Arbeiten an einer Zitat-Generator-Website. Ich habe Bootstrap hinzugefügt, und im CSS-Code habe ich einen Teil, wo ich die Hintergrundfarbe des HTML-Elements auf eine blau-grüne Mischfarbe setze. Bootstrap entscheidet sich nur dafür, den CSS-Code zu überschreiben und setzt den Teil der Seite, der die Hintergrundfarbe des Inhalts hat, auf Weiß. Warum und wie kann ich meinen Hintergrund-Farbe behalten, was ich will esBootstrap changing 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>

Antwort

1

Option sein 1: Stellen Sie sicher, Ihre CSS nach die bootstrap.css-Datei geladen wird.

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>

Option 2: Verwenden Sie das !important Stichwort:

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>

(Beachten Sie, dass ich auch eine Änderung html-body in Ihrem CSS gemacht)

+0

Dank es funktionierte. Es lässt mich das nicht als die Antwort markieren, bis 5 Minuten vergangen sind, aber ich werde es markieren, wenn die Abklingzeit beendet ist. Es funktioniert perfekt jetzt –

+0

Sure :) Sie sind willkommen – Dekel

0

Wie oben erwähnt zuerst Bootstrap und dann Ihre CSS laden. Anstatt wichtig zu sein, können Sie Ihre Browser-Dev-Tools verwenden (ich empfehle Chromes) und nach der Auswahl der Bootstrap-Hintergrundfarbe suchen. Seine Spezifität ist in den meisten Fällen ziemlich verstecken und Sie müssen eine höhere Spezifität für Ihren Stil hinzufügen.

diesen Artikel auf MDN Lesen Sie für weitere Informationen: MDN CSS Specificity

Verwandte Themen