gpt4 book ai didi

html - 为什么 CSS head 样式不适用于 Bootstrap HTML 文件

转载 作者:行者123 更新时间:2023-11-28 15:16:18 26 4
gpt4 key购买 nike

我正在使用这个 bootstrap html 文件,如下所示:

<head>

<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

</head>

<body>

<div class="w3-sidebar w3-bar-block" style="width:35%">
<h2 style="margin-left:15px;">Some Favorites</h2>
<a href="#" class="w3-bar-item w3-button" style="color:blue;">Celery Root</a>
<a href="#" class="w3-bar-item w3-button" style="color:blue;">Spaghetti Squash</a>
<a href="#" class="w3-bar-item w3-button" style="color:blue;">Killer Mushroom</a>
<input type="text" class="form-control" style="margin-left: 15px;">
<button type="button" class="btn btn-primary" style="margin-left:15px; width: 230px; height:50px; font-size: 20px;">Search Recipes</button>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

</body>

如您所见,我为 anchor 添加了内联样式,因此字体为蓝色。

但是,当我将 CSS 样式写入 at head 部分时,无法实现所需的 CSS 效果。通过编写内联样式来获得所需的 CSS 效果是完全完美的。谁能解释一下?

最佳答案

这是因为 bootstrap 用他自己的 css 覆盖了一些 html 标签,所以不会工作:

<head>

<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

<style>
a {
color: blue;
}
</style>

</head>

<body>

<div class="w3-sidebar w3-bar-block" style="width:35%">
<h2 style="margin-left:15px;">Some Favorites</h2>
<a href="#" class="w3-bar-item w3-button myAnchor">Celery Root</a>
<a href="#" class="w3-bar-item w3-button myAnchor">Spaghetti Squash</a>
<a href="#" class="w3-bar-item w3-button myAnchor">Killer Mushroom</a>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

</body>

所以要解决这个问题,您有 3 种方法。

1) 将样式放入 html 标记内,就像您在示例中所做的那样。

2) 使用!important规则覆盖标签并始终应用css,不建议滥用此方法并始终使用它。

3) 添加classid 并使用它们,这样css 就可以正常工作了。这是最推荐的方法。

这里你得到了每个点的工作示例:

<head>

<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">

<style>
/* first-of-type to select only the first <a> tag and make this example*/
a:first-of-type{
color: green !important;
}

/* this class is inside the 3º <a> tag*/
.myAnchor {
color: blue;
}
</style>

</head>

<body>

<div class="w3-sidebar w3-bar-block" style="width:35%">
<h2 style="margin-left:15px;">Some Favorites</h2>
<a href="#" class="w3-bar-item w3-button">Celery Root</a>
<a style="color:red;"href="#" class="w3-bar-item w3-button">Spaghetti Squash</a>
<a href="#" class="w3-bar-item w3-button myAnchor">Killer Mushroom</a>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

</body>

关于html - 为什么 CSS head 样式不适用于 Bootstrap HTML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47149665/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com