With this input checkbox:
<input type="checkbox">Click moi!
...and this CSS:
input[type=checkbox] {
color:green;
font-family: Consolas, Baskerville, 'Segoe UI', sans-serif;
}
input[type=checkbox]:hover {
box-shadow:0px 0px 10px #1300ff;
}
...the hover bit works fine (the checkbox itself changes), but the color / font of the text ("Click moi!") is not affected by setting color and font-family.
jsfiddle: http://jsfiddle.net/QRBEx/
How can I affect the text attributes via CSS?
The text should be within a label. Add a for attribute to attach it to the checkbox too.
jsFiddle example - it works.
<input id="checkbox" type="checkbox"/><label for="checkbox">Click me</label>
Then change the CSS:
label {
color:green;
font-family: Consolas, Baskerville, 'Segoe UI', sans-serif;
}
input[type=checkbox]:hover {
box-shadow:0px 0px 10px #1300ff;
}
First, mark up the text correctly. You have a label for the input, use a label element.
<input type=checkbox id=myCheckbox> <label for=myCheckbox> Click moi! </label>
Then, make the assumption that the label for a checkbox will always immediately follow that checkbox in the markup and use the adjacent sibling combinator:
input[type="checkbox"] + label {
}
The <input> tag does not have an end tag - it's a self-closing tag. So the text next to it is not part of it. You need to style the text separately as its own element, like a <label>, for instance.
One good idea is to use a <span> tag:
<style>
#cb_span{color:green;}
</style>
<input type="checkbox"><span id="cb_span">Click moi!</span>
This text is not the part of the checkbox. It's the normal floating text and it belongs to its container (which is also checkbox's container). If you have your chackbox + text pair wrapped in some container e.g.
<div id="container">
<input type="checkbox">Click moi!
</div>
then you can add such CSS
#container {
color:green;
font-family: Consolas, Baskerville, 'Segoe UI', sans-serif;
}
The text itself isn't part of the input element.
By instead placing the text in an HTML element and using a bit of CSS trickery, we can change the text like in the included fiddle:
http://jsfiddle.net/QRBEx/8/
The text needs to be in a label. create a label for the checkbox.
i hope this helps
Related
div {
height: 400px;
cursor: pointer;
font-family: sans-serif;
}
<div>
div has pointer and sans-serif font
<p>and paragraph also has it</p>
<code>but code element doesn't have sans-serif font but pointer.</code>
</div>
p element has cursor text and serif as font-family as default. Both are being overridden.
code element has cursor text and monospace as font-family as default. Only cursor has been overridden.
Why does this happen? In code element, the font-family isn't overridden. Why? How can I set default html styles in certain chosen elements? (p and code elements here)
Apparently, even all: revert doesn't work!
div {
height: 400px;
cursor: pointer;
font-family: sans-serif;
}
p {
all: revert;
}
code {
all: revert;
}
<div>
div has pointer and sans-serif font
<p>and paragraph also has it</p>
<code>but code element doesn't have sans-serif font but pointer.</code>
</div>
all: initial works, but I don't want original CSS implementation applied. I want the user agent stylesheet rules applied. I used revert but to no avail.
According to MDN:
By default, the content text is displayed using the user agent's
default monospace font.
and:
A CSS rule can be defined for the code selector to override the
browser's default font face. Preferences set by the user might take
precedence over the specified CSS.
So you do need to specify the font required specifically. If there is worry that the correct setting is not carried forward/may be changed in the future you could define it as a CSS variable so it only needs to have its value changed in one place.
div {
height: 400px;
cursor: pointer;
--font: sans-serif;
font-family: --font;
}
p {
all: revert;
}
code {
all: revert;
font-family: --font;
}
<div>
div has pointer and sans-serif font
<p>and paragraph also has it</p>
<code>and the code element also has sans-serif</code>
</div>
* {
font-family: sans-serif;
}
.sample {
font-family: monospace !important;
}
.sample>* {
font-family: monospace !important;
}
<div class="sample" contenteditable>
This is not inside span. <span>Go to the next line and append some text to see inconsistent effects.</span>
</div>
Playing around doing more editing only exposes more inconsistencies. How to fix it? How to set monospace font to all the text inside the div? I tried removing !important, but to no avail.
Removing <span> is an option, but I can't do that in the original project I'm working on because those elements have certain formatting. Neither can I remove the * rule declaration, because I want sans-serif font outside of the div. Quite annoyingly, both solve the problem. Any other way?
> selects immediate children. When you create a new line, you'll notice that the span tags are wrapped in a div.
Instead, you can try selecting all span elements that are children of .sample:
* {
font-family: sans-serif;
}
.sample {
font-family: monospace !important;
}
.sample span {
font-family: monospace !important;
}
<div class="sample" contenteditable>
This is not inside span. <span>Go to the next line and append some text to see inconsistent effects.</span>
</div>
The problem is that, by defining .sample>* the font will only be applied to the immediate child, excluding all others. If you check the result of jumping to a new line and writing something, you will see that it creates a new div and a new span layer, that's why your CSS is not applied. If you simply change .sample>* to .sample * it will work as expected.
* {
font-family: sans-serif;
}
.sample {
font-family: monospace !important;
}
.sample * {
font-family: monospace !important;
}
<div class="sample" contenteditable>
This is not inside span. <span>Go to the next line and append some text to see inconsistent effects.</span>
</div>
So I have this form, and inside it's got a div with a paragraph. The paragraph belongs to the class "create-account". But inside create-account in my CSS, where I set the font-size and font-style. The code doesn't affect my paragraph. And I literally have no clue why. Any help would is greatly appreciated.
.create-account {
font-size: 24;
font-style: arial;
}
<form>
<div>
<p class="create-account">Create your account here</p>
<hr/>
</div>
</form>
You are missing some units to your font-size property, and you also would need to use font-family instead of font-style
.create-account {
font-size: 24px;
font-family: Arial;
}
The font won't apply because you set it on font-style. You need to use font-family: arial;
I have a basic html form where label tags are used to define field names and where label tags are used around checkboxes so that a user clicking on the text next to a checkbox also selects the checkbox.
<label>Valid?</label>
<label>
<input type="checkbox" />
Yes
</label>
What CSS is the best practice so that my field name is bold ("Valid?"), but my checkbox descriptor is not bold?
I have tried several variations adding different :not and :empty, but I'm not hitting the right selector - either both are bold or neither are bold. I know my :empty isn't working since the text element messes that up, but there must be a simple way to only bold labels that have only text elements.
label:empty {
font-weight: bold;
}
JS Fiddle: http://jsfiddle.net/z77tq8bs/
You can use the next sibling selector, like this:
label {
font-weight: bold;
}
label + label {
font-weight: normal
}
Check the fiddle: https://jsfiddle.net/8cLuhznb/
The :empty pseudo-class targets elements that have no children (not even a space).
The pseudo-class can be used in the following way: http://jsfiddle.net/3z1pnv71/.
HTML:
<label></label>
<label>
<input type="checkbox" />
Yes
</label>
CSS:
label:empty:before {
content: "Valid?";
font-weight: bold;
}
EDIT: It's also possible to keep all the textual elements in HTML and use the following approach, if it is suitable: http://jsfiddle.net/cqugufex/.
HTML:
<label data-text = "Valid?"></label>
<label>
<input type="checkbox" />
Yes
</label>
CSS:
label:empty:before {
content: attr(data-text);
font-weight: bold;
}
Found a few solutions to this, both of which work because the label descriptor is always the first label within the parent element, and any checkboxes are subsequent labels
Solution 1: first-of-type
label:first-of-type {
font-weight: bold;
}
Solution 2: first-child
label:first-child {
font-weight: bold;
}
I still haven't found a solution that finds if a label has only a text element, but this at least works for most cases.
How can I format the text of a placeholder in italics, but when someone writes inside the textbox, the text shouldn't be in italics.
code:
<input type="text" id="search" placeholder="Search" style="font-style:italic">
How can I place italic text only on placeholder but not whole input?
Sure. Just apply the style to these CSS rules:
::-webkit-input-placeholder {
font-style: italic;
}
:-moz-placeholder {
font-style: italic;
}
::-moz-placeholder {
font-style: italic;
}
:-ms-input-placeholder {
font-style: italic;
}
Probably not all of these rules are needed. I always just reference CSS Tricks because I always forget how to do it.
Edit: Note that these rules cannot be combined into one selector. They must be declared separately as noted by Dave Kennedy in the comments below.
From MDN, you can use the :placeholder-shown pseudo-class.
input:placeholder-shown {
font-style: italic;
}
This has the advantage of being un-prefixed, and the browser support is decent (92-ish%) at the time of this writing: https://caniuse.com/#feat=css-placeholder-shown.
Another way: the :focus selector is your friend. To make it only for the placeholder use the :not selector. This have the CSS rule only if the input field is NOT current selection.
Define in your CSS :
.class-of-your-input-field:not(:focus) {
font-style:italic;
}