【问题标题】:CSS Button Unknownly CenteredCSS 按钮未知居中
【发布时间】:2015-02-14 04:14:21
【问题描述】:

好的,我有一个按钮,我不知道为什么它会自动居中。我的第一个怀疑是在 css 中,但我使用/更改的任何类型的 alignposition 都不起作用。

这里是 html 和 css。你可能会比我更快地找到它。 (如果有人感兴趣,这里是 jsfiddle 链接:http://jsfiddle.net/takkun/63uktgyo/

/* ROUNDED ONE */
.Gcheckbox {
/*CHANGES GRAY SQUARE DIMENSIONS*/
	width: 16px;
	height: 33px;
	background: #ff0000;
	
	/*GRAY SQUARE BACKGROUND FADER*/
	background: linear-gradient(top, #000000 100%, 0000ff 0%, #ff0000 0%);
	background: -webkit-linear-gradient(top, 0000ff 100%, 0000ff 0%, #ff0000 0%);
	background: -moz-linear-gradient(top, #000000 100%, 0000ff 0%, #ff0000 0%);
	background: -o-linear-gradient(top, #000000 100%, 0000ff 0%, #ff0000 0%);
	background: -ms-linear-gradient(top, #000000 100%, 0000ff 0%, #ff0000 0%);
	filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fcfff4', endColorstr='#b3bead',GradientType=0 );
	margin: 0px auto;
	
	position: relative;
}

.Gcheckbox label {
	cursor: pointer;
	position: absolute;
	width: 10px;
	height: 10px;

/*MAKES THE GREEN CIRCLE A CIRCLE*/
	-webkit-border-radius: 10px;
	-moz-border-radius: 10px;
	border-radius: 10px;
/********************************/

	left: 3px;
	top: 11.25px;

	box-shadow: inset 0px 0px 0px rgba(239,239,238,1), 0px 0px 0px rgba(239,239,238,1);
	-webkit-box-shadow: inset 0px 0px 0px rgba(239,239,238,1), 0px 0px 0px rgba(239,239,238,1);
	-moz-box-shadow: inset 0px 0px 0px rgba(239,239,238,1), 0px 0px 0px rgba(239,239,238,1);

	/*BEHIND GREEN CIRCLE BACKGROUND*/
	background: -webkit-linear-gradient(top, #0000ff 0%, #0000ff 100%);
	background: -moz-linear-gradient(top, #0000ff 0%, #0000ff 100%);
	background: -o-linear-gradient(top, #0000ff 0%, #0000ff 100%);
	background: -ms-linear-gradient(top, #0000ff 0%, #0000ff 100%);
	background: linear-gradient(top, #0000ff 0%, #0000ff 100%);
	filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#0000ff', endColorstr='#0000ff',GradientType=1 );
}

.Gcheckbox label:after {
	-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
	filter: alpha(opacity=0);
	opacity: 0;
	content: '';
	position: absolute;
	width: 10px;
	height: 10px;
	background: #00bf00;

	background: linear-gradient(top, #00ff00 0%, #00ff00 100%);
	background: -webkit-linear-gradient(top, #00ff00 0%, #00ff00 100%);
	background: -moz-linear-gradient(top, #00ff00 0%, #00ff00 100%);
	background: -o-linear-gradient(top, #00ff00 0%, #00ff00 100%);
	background: -ms-linear-gradient(top, #00ff00 0%, #00ff00 100%);

	-webkit-border-radius: 5px;
	-moz-border-radius: 5px;
	border-radius: 5px;
	bottom: 0px;
	right: 0px;

	-webkit-box-shadow: inset 0px 0px 0px #00ff00, 0px 0px 0px rgba(42,171,43);
	-moz-box-shadow: inset 0px 0px 0px #00ff00, 0px 0px 0px rgba(42,171,43);
	box-shadow: inset 0px 0px 0px #00ff00, 0px 0px 0px rgba(42,171,43);
}

/*CHANGE HOVER EFFECT OPACITY*/
.Gcheckbox label:hover::after {
	-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=30)";
	filter: alpha(opacity=30);
	opacity: 0.3;
}

.Gcheckbox input[type=checkbox]:checked + label:after {
	-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
	filter: alpha(opacity=100);
	opacity: 1;
}

input[type=checkbox] {
	visibility:hidden;
}
<!DOCTYPE HTML>
<html>
    <head>
	<link rel="stylesheet" href="styleCIRCLE.css" type="text/css">
	<title>custom checkbox testing</title>
    </head>
<body>


<!--styleCIRCLE -->
<div class="Gcheckbox">
		<input type="checkbox" value="None" id="Gcheckbox" name="check" />
			<label for="Gcheckbox"></label>
		</div>
		


</body>
</html>

【问题讨论】:

    标签: html css button center


    【解决方案1】:

    它是水平居中的,因为包含父级.Gcheckbox具有左右自动边距,具有使元素水平居中的效果。

    通过删除或注释掉该行,我们将使元素按照默认的从左到右布局行为向左对齐。

    /* ROUNDED ONE */
    .Gcheckbox {
      /*CHANGES GRAY SQUARE DIMENSIONS*/
      width: 16px;
      height: 33px;
      background: #ff0000;
    
      /*GRAY SQUARE BACKGROUND FADER*/
      background: linear-gradient(top, #000000 100%, 0000ff 0%, #ff0000 0%);
      background: -webkit-linear-gradient(top, 0000ff 100%, 0000ff 0%, #ff0000 0%);
      background: -moz-linear-gradient(top, #000000 100%, 0000ff 0%, #ff0000 0%);
      background: -o-linear-gradient(top, #000000 100%, 0000ff 0%, #ff0000 0%);
      background: -ms-linear-gradient(top, #000000 100%, 0000ff 0%, #ff0000 0%);
      filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#fcfff4', endColorstr='#b3bead',GradientType=0 );
      /*margin: 0px auto;*/
    
      position: relative;
    }
    
    .Gcheckbox label {
      cursor: pointer;
      position: absolute;
      width: 10px;
      height: 10px;
    
      /*MAKES THE GREEN CIRCLE A CIRCLE*/
      -webkit-border-radius: 10px;
      -moz-border-radius: 10px;
      border-radius: 10px;
      /********************************/
    
      left: 3px;
      top: 11.25px;
    
      box-shadow: inset 0px 0px 0px rgba(239,239,238,1), 0px 0px 0px rgba(239,239,238,1);
      -webkit-box-shadow: inset 0px 0px 0px rgba(239,239,238,1), 0px 0px 0px rgba(239,239,238,1);
      -moz-box-shadow: inset 0px 0px 0px rgba(239,239,238,1), 0px 0px 0px rgba(239,239,238,1);
    
      /*BEHIND GREEN CIRCLE BACKGROUND*/
      background: -webkit-linear-gradient(top, #0000ff 0%, #0000ff 100%);
      background: -moz-linear-gradient(top, #0000ff 0%, #0000ff 100%);
      background: -o-linear-gradient(top, #0000ff 0%, #0000ff 100%);
      background: -ms-linear-gradient(top, #0000ff 0%, #0000ff 100%);
      background: linear-gradient(top, #0000ff 0%, #0000ff 100%);
      filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#0000ff', endColorstr='#0000ff',GradientType=1 );
    }
    
    .Gcheckbox label:after {
      -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
      filter: alpha(opacity=0);
      opacity: 0;
      content: '';
      position: absolute;
      width: 10px;
      height: 10px;
      background: #00bf00;
    
      background: linear-gradient(top, #00ff00 0%, #00ff00 100%);
      background: -webkit-linear-gradient(top, #00ff00 0%, #00ff00 100%);
      background: -moz-linear-gradient(top, #00ff00 0%, #00ff00 100%);
      background: -o-linear-gradient(top, #00ff00 0%, #00ff00 100%);
      background: -ms-linear-gradient(top, #00ff00 0%, #00ff00 100%);
    
      -webkit-border-radius: 5px;
      -moz-border-radius: 5px;
      border-radius: 5px;
      bottom: 0px;
      right: 0px;
    
      -webkit-box-shadow: inset 0px 0px 0px #00ff00, 0px 0px 0px rgba(42,171,43);
      -moz-box-shadow: inset 0px 0px 0px #00ff00, 0px 0px 0px rgba(42,171,43);
      box-shadow: inset 0px 0px 0px #00ff00, 0px 0px 0px rgba(42,171,43);
    }
    
    /*CHANGE HOVER EFFECT OPACITY*/
    .Gcheckbox label:hover::after {
      -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=30)";
      filter: alpha(opacity=30);
      opacity: 0.3;
    }
    
    .Gcheckbox input[type=checkbox]:checked + label:after {
      -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
      filter: alpha(opacity=100);
      opacity: 1;
    }
    
    input[type=checkbox] {
      visibility:hidden;
    }
    <div class="Gcheckbox">
      <input type="checkbox" value="None" id="Gcheckbox" name="check" />
      <label for="Gcheckbox"></label>
    </div>

    【讨论】:

      【解决方案2】:

      这是边距:0px auto;在您的 .Gcheckbbox div 上。

      删除它,一切都很好,花花公子:)

      【讨论】:

      • 天哪,我太笨了,我爱你。谢谢。
      • 不用担心。长时间查看大块代码时,您可能会有点迷失。
      猜你喜欢
      • 2013-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-02
      • 2012-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多