在禁用的标签上更改光标

时间:2016-12-07 16:43:57

标签: css css-selectors

我正在弄乱这个css并且无法弄清楚如果禁用它内部的输入,如何覆盖控件类游标属性('指针')。有人可以帮助我吗?

Codepen Link

<label class="control control--checkbox">Disabled
    <input type="checkbox" disabled="disabled"/>
    <div class="control__indicator"></div>
</label>

.control {
  font-size: 18px;
  position: relative;
  display: block;
  margin-bottom: 15px;
  padding-left: 30px;
  cursor: pointer;
}

.control__indicator {
  position: absolute;
  top: 2px;
  left: 0;
  width: 20px;
  height: 20px;
  background: #e6e6e6;
}

3 个答案:

答案 0 :(得分:5)

/* Disabled state */
.control input:disabled ~ .control__indicator {

  cursor: not-allowed;
    opacity: .6;
    background: red;
}

所以你需要删除pointer-events:none

在此处详细了解https://developer.mozilla.org/en/docs/Web/CSS/pointer-events

答案 1 :(得分:1)

使用方括号参考CSS中的属性。这是一个例子:

label {
  display: block;
}
input {
  cursor: pointer;
}
input[disabled] {
  cursor: default;
}
<label>
  Enabled
  <input type="checkbox">
</label>

<label>
  Disabled
  <input type="checkbox" disabled>
</label>

请注意,仅使用CSS,您将无法根据其状态重新设置input的父级,但您可以重新设置在DOM之后的兄弟:

label {
  display: block;
}
input[disabled] ~ span {
  color: red;
}
<label>
  <input type="checkbox">
  <span>Enabled</span>
</label>

<label>
  <input type="checkbox" disabled>
  <span>Disabled</span>
</label>

答案 2 :(得分:1)

由于您无法在css中使用:has,因此无法通过子级声明父级样式。因此,请从cursor: pointer;中删除label并将其提供给复选框的所有兄弟姐妹,包括复选框,并使用后继选择器~指定样式

.control {
	font-size: 18px;
	position: relative;
	display: block;
	margin-bottom: 15px;
	padding-left: 30px;
}

.control input {
	position: absolute;
	z-index: -1;
	opacity: 0;
}

.control__indicator {
	position: absolute;
	top: 2px;
	left: 0;
	width: 20px;
	height: 20px;
	background: #e6e6e6;
}

.control--radio .control__indicator {
	border-radius: 50%;
}

/* Hover and focus states */
.control:hover input ~ .control__indicator,
.control input:focus ~ .control__indicator {
	background: #ccc;
}

/* Checked state */
.control input:checked ~ .control__indicator {
	background: #5579FB;
}

/* Hover state whilst checked */
.control:hover input:not([disabled]):checked ~ .control__indicator,
.control input:checked:focus ~ .control__indicator {
	background: #5579FB;
}

/* Disabled state */
.control input:disabled ~ .control__indicator {
	pointer-events: none;
	opacity: .6;
	background: #e6e6e6;
}

/* Check mark */
.control__indicator:after {
	position: absolute;
	display: none;
	content: '';
}

/* Show check mark */
.control input:checked ~ .control__indicator:after {
	display: block;
}

/* Checkbox tick */
.control--checkbox .control__indicator:after {
	top: 4px;
	left: 8px;
	width: 3px;
	height: 8px;
	transform: rotate(45deg);
	border: solid #fff;
	border-width: 0 2px 2px 0;
}

/* Disabled tick colour */
.control--checkbox input:disabled ~ .control__indicator:after {
	border-color: #7b7b7b;
}

/* Radio button inner circle */
.control--radio .control__indicator:after {
	top: 7px;
	left: 7px;
	width: 6px;
	height: 6px;
	border-radius: 50%;
	background: #fff;
}

/* Disabled circle colour */
.control--radio input:disabled ~ .control__indicator:after {
	background: #7b7b7b;
}

label {
  display: block;
}
input[disabled] ~ .control__indicator {
  color: red;
  cursor:not-allowed;
}
input, input ~ * {
  cursor: pointer;
}
input[disabled], input[disabled] ~ * {
  cursor: default;
}
<div class="control-group">
	<label class="control control--checkbox">
		<input type="checkbox" checked="checked"/>
    <span>First checkbox</span>
		<div class="control__indicator"></div>
	</label>
	<label class="control control--checkbox">
		<input type="checkbox"/>
    <span>Second checkbox</span>
    
		<div class="control__indicator"></div>
	</label>
	<label class="control control--checkbox">
		<input type="checkbox" disabled="disabled"/>
    <span>Disabled</span>
    
		<div class="control__indicator"></div>
	</label>
	<label class="control control--checkbox">
		<input type="checkbox" disabled="disabled" checked="checked"/>
    <span>Disabled & checked</span>
    
		<div class="control__indicator"></div>
	</label>
</div>


<div class="control-group">
	<label class="control control--radio">
		<input type="radio" name="radio" checked="checked"/>
    <span>First radio</span>
    
		<div class="control__indicator"></div>
	</label>
	<label class="control control--radio">
		<input type="radio" name="radio"/>
    <span>Second radio</span>
    
		<div class="control__indicator"></div>
	</label>
	<label class="control control--radio">
		<input type="radio" name="radio2" disabled="disabled"/>
    <span>Disabled</span>
    
		<div class="control__indicator"></div>
	</label>
	<label class="control control--radio">
		<input type="radio" name="radio2" disabled="disabled" checked="checked"/>
    <span>Disabled & checked</span>
		<div class="control__indicator"></div>
	</label>
</div>