简体   繁体   English

CSS样式表令人沮丧的行为

[英]frustrating behavior with CSS stylesheets

I've having a rather stressful issue. 我遇到了一个压力很大的问题。 I applied a div class called input to style the boxes on my username /password fields like in the picture below. 我应用了一个名为input的div类来对用户名/ password字段上的框进行样式设置,如下图所示。

在此处输入图片说明

However now I want to apply a different styling to the search bar but no matter what I do even If I use a inline style sheet it uses the style of the old input element. 但是,现在我想对搜索栏应用不同的样式,但是无论我做什么,即使使用嵌入式样式表,它也会使用旧输入元素的样式。 I tried pretty much everything. 我几乎尝试了所有事情。

login html 登录html

<div class="form">  

<form name="form1" method="POST" action="login.php">
<div class="input">
  <input name="username" id="username" placeholder="Username">

  <input name="password" id="password" placeholder="Password">
  </div>

  <input type="submit" name="Submit" value="Login" class="submit">


</form>
</div>

search bar html 搜索栏html

<div class="ajax-div">

    <div class="searchbar">

     <input type="text" onKeyUp="getScriptPage('box','text_content')" id="text_content">

    </div>

    <div id="box"></div>
</div>

That last width: parameter in input dictates the width of all input bars on the page. 输入中的最后一个width:参数决定页面上所有输入条的宽度。

.form {

    margin-top:-182px;
    margin-left:1100px;
    position:static;
    width: 220px;
    height:150px;
    padding: 20px;
    padding-top:10px;
    border: 1px solid #270644;

  background:url(bg-right.png);

}


.label {
        font-size: 12px;
        font-family: arial, sans-serif;
        list-style-type: none;
        color: #999;
        text-shadow: #000 1px 1px;
        margin-bottom: 10px;
        font-weight: bold;
        letter-spacing: 1px;
        text-transform: uppercase;
        display: block;
    }   




input {
      /* input boxes*/
      -webkit-transition-property: -webkit-box-shadow, background;
      -webkit-transition-duration: 0.25s;
        padding: 6px;
        border-bottom: 0px;
        border-left: 0px;
        border-right: 0px;
        border-top: 1px solid #666;
        -moz-box-shadow: 0px 0px 2px #000;
        -webkit-box-shadow: 0px 0px 2px #000;
        margin-bottom: 10px;
        background: #FFF;
        width: 130px;

    }

the css i'm trying to apply to the search bar: 我正在尝试将CS​​S应用于搜索栏:

searchbar {

left: 350px;
top:100px;
width: 300px;

}

Your CSS selector is wrong. 您的CSS选择器错误。

Instead of this: 代替这个:

searchbar {

left: 350px;
top:100px;
width: 300px;

}

Try this: 尝试这个:

.searchbar #text_content {

left: 350px;
top:100px;
width: 300px;

}

A 'dot' denotes you want to target a class name. “点”表示您要定位类名。 A 'hash' denotes that you want to target a specific ID. “哈希”表示您要定位特定的ID。 CSS has a clue in its name; CSS的名称有一个线索。 Cascading style sheets. 级联样式表。 Styles "trickle down" from generic to the more specific. 样式从普通到更“具体”。 Therefore, if you want to target only one single element, then ID's are the way to go...which usually override any generic styles on the elements. 因此,如果您只想定位一个元素,那么使用ID即可...通常会覆盖元素上的所有通用样式。

Good luck! 祝好运!

Did you try appying the class directly to the input tag OR using this CSS - 您是否尝试过将类直接应用于输入标签使用此CSS-

.searchbar input{
// this CSS will only be applied to searchbar input box
left: 350px;
top:100px;
width: 300px;

}

The CSS you are trying to apply to searchbar is wrong. 您尝试应用于搜索栏的CSS是错误的。 Searchbar is the class of the div and not the input box. Searchbar是div的类,而不是输入框。 Try using the CSS above and check if it works 尝试使用上面的CSS并检查其是否有效

So, the searchbar selector should start with a . 因此,搜索searchbar选择器应以开头. character and to override the input selector it should probably be .searchbar input 字符并覆盖输入选择器,它可能应该是.searchbar input

And for input , you probably want .input input as the selector..? 对于input ,您可能希望.input input作为选择器..?

You're using a generic selector for all input fields (Last section of the CSS: input { ..). 您正在对所有输入字段使用通用选择器(CSS的最后部分:input {..)。 So the styling will be applied to all inputs. 因此,样式将应用于所有输入。

I'd suggest giving each input it's own class so you can assign different styles for each input. 我建议给每个输入一个自己的类,以便为每个输入分配不同的样式。 For example. 例如。

CSS 的CSS

.search
{
    background: red;
}

.login
{
    background: blue;
}

HTML 的HTML

<input type="text" class="search" />

Also, the selector 'searchbar' doesn't select anything as it's targeting an element called searchbar. 同样,选择器“搜索栏”也不会选择任何内容,因为它的目标是一个名为“搜索栏”的元素。 If you want to target classes you need to target with a . 如果要定位类,则需要使用进行定位。 For example: .searchbar 例如:.searchbar

input { } applies styles to all input elements input[type="text"] etc... input {}将样式应用于所有输入元素input [type =“ text”]等...

Instead apply .input input {} to apply styles for input elements inside input class 而是应用.input input {}为输入类中的输入元素应用样式

To apply styles for input elements inside searchbar use .searchbar input{} 要在搜索栏中应用输入元素的样式,请使用.searchbar input {}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM