CSS 选择器 - 未到达我的元素

标签 css css-selectors

我正在努力跟上 CSS 的步伐。我有一个按钮样式,需要在好几页中重复使用。我创建了一个名为 bigbutton 的类,我在以下 html 文件中使用它。我还在其他 html 页面中使用了相同的类。

您还会看到我的 css 文件,我在其中定义了此按钮的外观。我遇到的问题是,当我将 css 文件定义为 .bigbutton 时,这些设置无效。如果我用 #big_button_container .bigbutton 定义 css 文件,那么一切都会完美无缺。不幸的是,我不能这样做,因为在其他 html 文件中,这个类不一定包含在这个 big_button_container div 中。

所以,我的问题是如何编写 css 选择器,以便它对这个 bigbutton 类产生预期的效果?谢谢!

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>Dashboard</title>
    <link rel="stylesheet" type="text/css" href="styles/mystyles.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="styles/bigbuttons.css" media="screen" />

                <style media="screen" type="text/css">
                    #big_button_container{
                        margin-left:85px;
                        width:809px;
                        padding:14px;
                    }
                </style>
</head>

<body>

    <div id="frame">
        <div id="page">
            <img id="mainpic" src="images/banner.png" height="390" width="982">

            <div id="big_button_container">        
                <table>
                    <tr>
                        <td>
                            <form action="coaches.html">
                                <input type="submit" value="COACHES" class="bigbutton"> 
                            </form>
                        </td>

                        <td>
                                <input type="button" value="GROUPS" class="bigbutton" onclick="()"</td>
                        <td><input type="button" value="WORKOUTS" class="bigbutton" onclick="()"</td>
                        <td><input type="button" value="ATHLETES" class="bigbutton" onclick="()"</td>
                    </tr>

                    <tr>
                        <td><input type="button" value="PACE GROUPS" class="bigbutton" onclick="()"</td>
                        <td><input type="button" value="USER TYPES" class="bigbutton" onclick="()"</td>
                        <td><input type="button" value="WORKOUT TYPES" class="bigbutton" onclick="()"</td> 
                    </tr>
            </div>

        </div>
    </div>

</body>

</html>

CSS

//#big_button_container .bigbutton{    <--this works, but only for this html file
.bigbutton{
    clear:both;
    margin-right:3px;
    margin-top:3px;
    border-style:none; 
    width:200px;
    height:125px;
    background:#c1d72e;
    text-align:center;
    line-height:50px;
    color:#444444;
    font-size:16px;
    font-weight:bold;
}

最佳答案

好吧,我让它工作了。我更改了这一行:

<div id="big_button_container">

<div class="big_button_container">  

然后将此容器的 css 移动到同一个“bigbuttons.css”文件中。所以,现在我的 css 文件看起来像这样:

body{
font-family:"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif;
font-size:12px;
}
p, h1, form, button{border:0; margin:0; padding:0;}

.big_button_container{
    margin-left:85px;
    width:809px;
    padding:14px;
}

.bigbutton{
    clear:both;
    margin-right:3px;
    margin-top:3px;
    border-style:none; 
    width:200px;
    height:125px;
    background:#c1d72e;
    text-align:center;
    line-height:50px;
    color:#444444;
    font-size:16px;
    font-weight:bold;
}

我之前曾尝试将 big_button_container 设为一个类,所以我真的不知道为什么会这样。但它不仅适用于此文件,而且适用于使用相同按钮样式的其他 html 文件。我从来不喜欢偶然解决这样的事情,但至少它已经解决了。如果有人能告诉我为什么它以前行不通,我很想听听。

谢谢大家,很抱歉打扰了大家。感谢您的帮助!

关于CSS 选择器 - 未到达我的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17386874/

相关文章:

java - 使用 WebDriver 无法通过 CSS 选择器找到 WebElement

javascript - 在动画 CSS3 或 js 方面,哪种语言允许更大的灵活性?

html - 3 div 不会在水平行中对齐

html - 悬停时无法更改另一个元素的背景颜色

css - 使 div 的宽度为 20%,每个 div 之间有填充,但不在第一个的左侧或最后一个的右侧

CSS:仅当元素包含特定元素时才选择该元素

html - CSS3 :not() selector not working on sibling element

HTML CSS 隐形按钮

css @font-face 在 FF 中不起作用

html - 居中宽度未知的div