Nested CSS counter does not show increment [duplicate] - css

This question already has answers here:
How do I stop <div> tags interfering with counters?
(2 answers)
Closed 7 years ago.
I am trying to use CSS counters to be applied to a series of elements and I miserably fail to get it right.
The HTML code is very simple:
body {
counter-reset: objgen;
}
span.general:before {
content: counter(objgen);
counter-increment: objgen;
}
span.general {
counter-reset: objesp;
font-weight: bold;
}
span.esp:before {
content: counter(objgen) "." counter(objesp);
counter-increment: objesp;
}
span.esp {
font-weight: bold;
}
<p>inputs: </p>
<p><span class="general"></span> <input type="text" value="test1"></p>
<p><span class="esp"></span> <input type="text" value="test2"></p>
<p><span class="esp"></span> <input type="text" value="test3"></p>
<p><span class="general"></span> <input type="text" value="test4"></p>
<p><span class="esp"></span> <input type="text" value="test5"></p>
<p><span class="esp"></span> <input type="text" value="test6"></p>
The objgen counter runs OK but the objesp never increments its value: it is always equal to one. What am I doing wrong?
Is the problem coming from the fact that the span with class "esp" is not nested within the span with class "general"?
COMMENT
It seems that as pointed by Paulie_D the problem is with the structure, the following HTML works with no problem (it is not the only possibility):
<span class="general"></span><input type="text" value="test1">
<span class="esp"></span><input type="text" value="test2">
<span class="esp"></span><input type="text" value="test3">
<span class="general"></span><input type="text" value="test1">
<span class="esp"></span><input type="text" value="test2">
<span class="esp"></span><input type="text" value="test3">

you are using class wrong. you don't need to assign the element span if you already assigned the element a class.
So instead of
span.general -> .general
That might solve your problem.
body {
counter-reset: objgen;
}
.general::before {
content: counter(objgen) ;
counter-increment: objgen;
}
.general {
counter-reset: objesp;
font-weight: bold;
}
.esp::before {
content: counter(objgen) "." counter(objesp);
counter-increment: objesp;
}
.esp {
font-weight: bold;
}
how about this?

Related

css selector for text after checked input [duplicate]

This question already has answers here:
Target the label of a checked input
(2 answers)
Closed 2 years ago.
I am trying make the text after input bold if input is checked but I am failing. I think my is not finding the text. I appreciate any help or hint.
input[type="radio"]:checked+ {
font-weight: bold;
}
<label for="radio-foobar">
<input type="radio" id="radio-3" value="3" />
Hello world!
</label>
focus-within and a transition hack can approximate this. The transition is to make sure the style is kept even if you click outside.
label {
font-weight: 400;
transition:0s 999s;
}
label:focus-within {
font-weight: 900;
transition:0s;
}
<label for="radio-3">
<input type="radio" id="radio-3" value="3" >
Hello world!
</label>
On html, it is needed to cover Hello World! into html tag like <span> and on CSS you can select that span next to checked input using + CSS selector.
input[type="radio"]:checked + span {
font-weight: bold;
}
<label for="radio-foobar">
<input type="radio" id="radio-3" value="3" />
<span>Hello world!</span>
</label>

How to hide a specific class field?

I need to hide this field "estimated move-out date". How can I do it with CSS?
<p class="mphb_sc_search-check-out-date frm_form_field">
<label for="mphb_check_out_date-mphb-search-form-5eb2a4b467b3b">
Estimated move-out date <abbr title="Formatted as dd/mm/yyyy">*</abbr>
</label>
<br>
<input id="mphb_check_out_date-mphb-search-form-5eb2a4b467b3b" data-datepick-group="mphb-search-form-5eb2a4b467b3b" value="" placeholder="Estimated move-out date" required="required" type="text" name="mphb_check_out_date" class="mphb-datepick mphb_datepicker is-datepick" autocomplete="off">
</p>
So far I've tried the following but it doesn't work:
.mphb_sc_search-check-out-date frm_form_field { display: none; }
Tried also:
.mphb_sc_search-check-out-date.frm_form_field { visibility: hidden; }
Worked. Except it left a big blank hole in the middle. Not ideal but...
Finally I figured it out!
.mphb_sc_search-check-out-date.frm_form_field {
display:none !important;
}
Create a new class and add it to the element.
.display-none { display:none; }

How to create an EditorTemplate for bootstrap checkbox?

I am working with a bootstrap template and its checkbox template is like this:
<div class="checkbox">
<label>
<input type="checkbox" class="checkbox style-1" checked="checked">
<span>Checkbox 1</span>
</label>
</div>
I need an MVC EditorTemplate for boolean values to use this template.
MVC CheckBoxFor default template is not like this template, it has another hidden input field to hold data and there is no span to show checkbox icon (it uses a default icon not stylable by css).
MVC CheckBoxFor default template :
<input checked="checked" data-val="true" data-val-required="required." id="IsActive"
name="IsActive" type="checkbox" value="true">
<input name="IsActive" type="hidden" value="false">
I tried many ways to do this with no success. For example if I use a conditional template like below, it does not return value after submit.
My Boolean EditorTemplate:
#model Boolean?
<div class="checkbox">
<label>
#if (Model.Value)
{
<input id="#ViewData.TemplateInfo.GetFullHtmlFieldId("")" name="#ViewData.TemplateInfo.GetFullHtmlFieldId("")"
type="checkbox" class="checkbox style-1" checked="checked" value="true" />
}
else
{
<input id="#ViewData.TemplateInfo.GetFullHtmlFieldId("")" name="#ViewData.TemplateInfo.GetFullHtmlFieldId("")"
type="checkbox" class="checkbox style-1" value="false" />
}
<span>#Html.LabelFor(m => m)</span>
</label>
</div>
Can anyone help please?
Update:
A part of css codes relevent to checkbox icon :
label input[type=checkbox].checkbox + span:before {
font-family: FontAwesome;
font-size: 12px;
border-radius: 0;
content: " ";
display: inline-block;
text-align: center;
vertical-align: middle;
padding: 1px;
height: 12px;
line-height: 12px;
min-width: 12px;
margin-right: 5px;
border: 1px solid #bfbfbf;
background-color: #f4f4f4;
font-weight: 400;
margin-top: -1px;
}
label input[type=checkbox].checkbox + span:before {
content: " ";
}
label input[type=checkbox].checkbox:checked + span:before {
content: "";
color: #2e7bcc;
}
label input[type=checkbox].checkbox:checked + span {
font-weight: 700;
}
The CheckBoxFor() method generate 2 inputs to ensure a value is posted back (unchecked checkboxes to not submit a value so the hidden input ensures false is submitted), and you should not attempt to change this behavior. Your attempt at an EditorTempate could not work for a number of reasons including a checkbox (which has 2 states) cannot bind to a nullable bool (which has 3 states) and your else block means that a vale of false will always be submitted, even if the checkbox is checked.
Instead, use the CheckBoxFor() method, but adjust your css selectors
<div class="checkbox">
<label>
#Html.CheckBoxFor(m => m.IsActive, new { #class = "checkbox style-1" })
<span>Checkbox 1</span>
</label>
</div>
will generate
<div class="checkbox">
<label>
<input type="checkbox" name="IsActive" class="checkbox style-1" ... value="true">
<input type="hidden" name="IsActive" value="false">
<span>Checkbox 1</span>
</label>
</div>
So your current selector
label input[type=checkbox].checkbox + span:before {
which gets the span element placed immediately after the checkbox element needs to be changed to
label input[type=checkbox].checkbox ~ span:before {
And ditto for the other selectors (i.e. change + to ~). The ~ selector matches the second element if it is preceded by the first, and both share a common parent (refer General sibling selectors)

CSS pseudo-class :required does not work together with pseudo-element ::before

My intent is to put a * on labels of required fields.
I am testing with Chrome 47, Firefox 43 and Opera 34.
None of these can understand the CSS selector
span:required::before
According to http://caniuse.com/#feat=form-validation they all should be able to understand it, and if you use
span:hover::before
instead, it actually works.
What do I do wrong?
Here is my Code:
<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
span::before {
content: "\00A0";
}
span:required::before { /* This does NOT work! */
content: "*";
}
span:hover::before { /* But this DOES work! */
content: "_";
}
</style>
</head>
<body>
<form>
<p>
<span required>Name</span>
<input id="name" type="text" />
</p>
<p>
<span>Date of Birth</span>
<input id="birth" type="text" />
</p>
</form>
</body>
</html>
Two problems:
Form labels have a dedicated element, label. You should be using that, not span.
The required attribute only applies to the controls themselves, that is, input, select, textarea, etc. A span is just plain text (and a label is basically that on its own) and the required attribute makes no sense on such an element.1
If you're trying to style a label of a required input, you will need to give it a class name instead.
This has nothing to do with :required::before, though, given that most form elements are replaced elements, it's unlikely you'll find that pseudo-class and that pseudo-element together.
1 contenteditable notwithstanding.
The required attribute must be in the form control, and you should use labels instead of spans.
Then, you can use the selector :required + label::before. To make it work the form control must appear before the label in the DOM order, but then you can use floats or flexbox to rearrange.
p {
overflow: hidden;
}
label {
float: left;
}
label::before {
content: "\00A0";
font-family: monospace;
}
:required + label::before {
content: "*";
}
<form>
<p>
<input id="name" type="text" required />
<label for="name">Name</label>
</p>
<p>
<input id="birth" type="text" />
<label for="birth">Date of Birth</label>
</p>
</form>

I am trying to target/select elements in a different parent with CSS

Take a look at the code below...
As you can see the 'HAZEL(NUT)' and 'HASSEL(NØD)' has a different parent to the checkbox. Which is why I think the checkbox works for the font-weight part, but doesn't work for selecting #hazelnut or #hasselnod. If anyone could help me with the correct selector for #hazelnut and #hasselnod I would be very grateful.
Hope this is clear, I'm quite a newbie to HTML and CSS, so have trouble explaining what I mean sometimes!
HTML here:
<div class="container" id="lang">
<input type="radio" id="english" name="language" value="english" checked="checked" />
<input type="radio" id="dansk" name="language" value="dansk" />
<ul>
<label for="english"><li id="en">ENGLISH</li></label>
<label for="dansk"><li id="dk">DANSK</li></label>
</ul>
</div>
<div class="container" id="myname">
<h1 id="hazelnut">HAZEL<br>(NUT)</h1>
<h1 id="hasselnod">HASSEL<br>(NØD)</h1>
</div>
CSS here:
#dansk:checked ~ * #dk {
font-weight: 700;
}
#dansk:checked ~ * #en {
cursor: pointer;
}
#dansk:checked * #hazelnut {
display: none;
}
#english:checked ~ * #en {
font-weight: 700;
}
#english:checked ~ * #dk {
cursor: pointer;
}
#english:checked * #hasselnod {
display: none;
}
Many thanks!
In CSS, for the ~ selector to work, the elements must have the same parent. As I see it, I'm afraid you'll have to involve some javascript in here.
What I'd do, is have the radio buttons change a data attribute of #lang, so it would be transparent to the css:
<div id="lang" data-value="en">
and then use the following css rules:
/*when #myname is preceded by #lang with data-value attribute 'en',
select direct child #hasselnod */
#lang[data-value='en'] ~ #myname > #hasselnod {
/* and set its display to none*/
display: none;
}
Now, we'll need the javascript to change the data-value attribute of #lang. Look at the onclick function in the following snippet:
<input type="radio" id="dansk" name="language" value="dansk"
onclick="this.parentNode.setAttribute('data-value', 'da')" />
Check out this fiddle: http://jsfiddle.net/wkL7q/2/
To target elements in a different parent, I think you need to use jQuery:
$("#lang input").change(function () {
var lang = $(this).val();
if(lang == 'dansk') {
$('#hazelnut').hide();
$('#hasselnod').show();
} else {
$('#hazelnut').show();
$('#hasselnod').hide();
}
});
Check out this fiddle: http://jsfiddle.net/X3ZtK/

Resources