I am trying to style some form labels by selecting them with their 'for' attribute. But nothing is being picked up when I preview it in IE7. I'm doing this because I'd like to style them differently to each other, without adding to the existing markup.
So if my css looks like the following, I get nothing:
<style>
label[for="foo"] {
background: blue;
padding: 1em
}
</style>
<form>
<label for="foo"/>bar</label>
<input name="foo" type="text"/>
</form>
But if I change it to this, the styling works.
<style>
label[fro="foo"] {
background: blue;
padding: 1em
}
</style>
<form>
<label fro="foo"/>bar</label>
<input name="foo" type="text"/>
</form>
Have you seen this kind of problem before? Is there a problem with the way I'm writing the CSS, IE7, or something else?
This user seems to have had the same problem you are having:
here
He says that because "for" is a reserved word, it can't be used as a property name. But 'htmlFor' is the DOM property name associated with the for attribute
Labels are paired with specific input fields, so is there any reason why you cannot use class instead of creating a multitude of selectors in your CSS for this purpose?
Related
In the following example I have a Bootstrap button style which is hijacked by the color: inherit entry set by .k-grid of Kendo-UI:
.k-grid a {
color: inherit;
}
<div class="k-grid">
<a class="btn btn-secondary" href="#">Button</a>
</div>
Demo here: http://jsfiddle.net/aq9Laaew/299912/
You can observe that the inherit property of .k-grid a bypasses any other classes passed to the a tag. Eventually the Bootstrap Button is displayed with the wrong color inside a Kendo-grid table.
What is the correct way to fix this? I am not sure that adding a !important to the SASS of Bootstrap is the best solution.
After taking a look at your fiddle, I can see in the inspector that Bootstrap's reset applies the following: a:not([href]):not([tabindex]) {color: inherit;}
On top of this, the anchor in your fiddle doesn't have an href so the above CSS applies.
<link href="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="k-grid">
Button
<a class="btn btn-secondary">Button</a>
</div>
So trying to style your button (without a href) with:
.btn-secondary {color: white;} will not work due to CSS specificity.
If you are still confused about CSS specificity, find yourself a specificity calculator like this one and paste both selectors in.
You will find that .btn-secondary is not specific enough to override this rule coming from Bootstrap's reset that applies styles for your button.
Given that kendo-ui is also affecting your button styles with: .k-grid a {color: inherit;}, the best way to solve your issue is by targeting the button with (you guessed it) a selector of higher specificity.
.k-grid a {
color: inherit;
}
.btn.btn-secondary {
color: white;
}
<link href="http://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="k-grid">
Button
<a class="btn btn-secondary">Button</a>
</div>
I recommend you to understand the css specificity
For example: http://cssspecificity.com
In your case .one-class is less specific than .on-class and an element
The inherit CSS keyword causes the element for which it is specified to take the computed value of the property from its parent element. It can be applied to any CSS property, including the CSS shorthand all.
For inherited properties, this reinforces the default behavior, and is only needed to override another rule.
This would help you :
https://developer.mozilla.org/en-US/docs/Learn/CSS/Introduction_to_CSS/Cascade_and_inheritance
If you want to dig in more :
https://developer.mozilla.org/en-US/docs/Web/CSS/inherit ,
https://developer.mozilla.org/en-US/docs/Web/CSS/computed_value
I have a form that is display:none and when I am going to hover on a another link, then the form comes up. How can I put the cursor into the input field? Is there a way to do it in css?
If I make the form to display:inline-block directly, the cursor is in the input field.
My CSS:
.search-bar {
display: none;
}
#main-nav a:hover .search-bar {
display: inline-block;
}
My Form in the html file:
<input id="search_search" type="text" autofocus="autofocus" placeholder="Placeholder.Search.Main" name="search[search]"></input>
We can use HTML5 auto focus attribute
For Example:
<input type="text" name="name" id="search_search" placeholder="Placeholder.Search.Main" name="search[search]" />
We can use JQuery to do this using foxus()
Javascript:
$("#show").hover(function(){
$("#search_search").show();
$("#search_search").focus();
});
Doesn't work in Firefox but works in Safari
I think you might be looking for some client-side scripting (Javascript or javascript with JQuery).
If you don't want to accomplish this through client-side scripting, and only want to support html5, then you should take a look into the "autofocus" attribute of html5. See the top answer to this post.
I'm stuck on this even though it is very simple.
I'm have a custom css file. In this I have added this piece of code:
input[type=text] {
margin-top:5px;
width:370px;
}
I have one page where I want to override this but when I try to with the following, the width of the text input does not change.
input[type=text] {
width:100px !important;
}
Try <input type=text style="width:100px;" /> That seems to work for me when using bootstrap .
You should be using CSS classes here, not to mention its better practise to use percentages rather than finite pixel values.
In any case, avoid inline CSS because thats bad practice.
CSS:
#narrow_input {
width:100px;
}
html:
<input type=text class="narrow_input" />
That way when you decide to make the narrow input red as well, you can just add color:red; to the stylesheet.
If you check the form at this link, you'll see that required fields have a class="required" in the CSS and a * in the markup.
http://drupal.org/user
Can the * which shows in the markup be added entirely with CSS for divs that have this class?
You can use the after pseudo class:
.required:after { content: "*"; }
or, because you explicitly asked for a div with that class:
div.required:after { content: "*"; }
Should work (for IE only since IE8)
You can apply any style to this, of course. You can even do things like this:
div.required:after:hover { /* Hello, I'm a geek. */ }
This can also be achieved with JavaScript. jQuery:
$(".required").append("*");
span:after { content:"*"; }
Demo: http://jsfiddle.net/W3gHU/
You can use the :after or before css pseudo element for this, more info, also abt which browsers support it here.
You could add an image of a star via CSS. This should work in all browsers.
.required
{
background-image:url(/path/to/your/images/dir/required-field.png);
background-position:top right;
background-repeat:no-repeat;
padding-right:10px;
}
Try this page:
<html>
<style>
.required:after {
color: red;
content: "*"
}
</style>
<body>
<div class="required">Name</div> <input type="text">
<div class="required">Email</div> <input type="text">
</body>
</html>
:after is understood by probably everything except for IE (hopefully IE9 will have support)
Update taking into account comment of Šime Vidas:
it was just example of using. Of course it would bring more sense if we make it this way:
.required:before {
color: red;
content: "*"
}
....
<div>Name <input type="text" class="required"> </div>
then we can even add unobtrusive javascript validation to that field (so this way brings good advantages). The problem is that this refactored page will be displayed as we want it only in Opera (I checked it on all last builds of browsers, except for FireFox 4, but I'm not sure FF will change the way they take that style into account).
:after and :before do not work for input and img elements; there is related discussion of why. $(".required").before("*") from jQuery however will work everywhere, but that's more about JavaScript then CSS (and was mentioned before by other people).
Is there some kind of "not" CSS selector?
For example when I write the following line in my CSS, all input fields inside an tag with class classname will have a red background.
.classname input {
background: red;
}
How do I select all input fields that are OUTSIDE of a tag with class classname?
With current browser CSS support, you can't.
Newer browsers now support it- see Sam's answer for more info.
(See other answers for the alternatives in CSS.)
If doing it in JavaScript/jQuery is acceptable, you can do:
$j(':not(.classname)>input').css({background:'red'});
Mozilla supports negation pseudo-class:
:not(.classname) input {background: red;}
See also: http://developer.mozilla.org/en/Mozilla_CSS_support_chart
Note that the negation pseudo class is in the Selectors Level 3 Recommendation and works in recent versions of Firefox, Chrome and Safari (at least). Sample code below.
<html>
<head>
<title>Negation pseudo class</title>
<style type="text/css">
div {
border: 1px solid green;
height: 10px;
}
div:not(#foo) {
border: 1px solid red;
}
</style>
</head>
<body>
<div id="foo"></div>
<div id="bar"></div>
<div id="foobar"></div>
</body>
</html>
Wouldn't you do that by setting the 'global' background to red, then using the classname to alter the others?
input { background: red; }
.classname input { background: white; }
I would do this
input { /* styles outside of .classname */ }
.classname input { /* styles inside of .classname, overriding above */ }
There is no way to select the parent of matched elements with CSS. You would have to use JavaScript to select them.
From your question I assume you have markup that looks more or less like this:
<form class="formclassname">
<div class="classname">
<input /> <!-- Your rule matches this -->
<input /> <!-- Your rule matches this -->
</div>
<input /> <!-- You want to select this? -->
<input /> <!-- You want to select this? -->
</form>
One option is to add a class to a higher element, say the <form>, and write a rule to style all of the inputs of the form. I.E:
.formclassname input {
/* Some properties here... */
}
Or
.formclassname > input {
/* Some properties here... */
}
If you want to select them based on the fact that they are not inside of an element with a specific class, you're out of luck without the use of JavaScript.
I think the closest you can get is to only affect direct descendants with a declaration
This code for example will only affect input fields directly under divs with class "maincontent"
div.maincontent > input {
// do something
}
Inputs are a bit annoying because, unlike most other html elements, there isn't necessarily a way of resetting all the css properties back to their default value.
If the styling is non-critical (ie a nice to have but doesn't affect functionality) I would use jQuery to get an array of all the inputs, check their parents, and then only carry out the styling on those outside that div. Something like:
$('input').each(function() {
if($(this).closest('.classname') == false)
{
// apply css styles
}
});
(By the way, I'm no jQuery expert, so there might be some errors in the above, but in principle something like this should work)