【问题标题】:Bootstrap negative margin in panel-heading not working面板标题中的引导负边距不起作用
【发布时间】:2017-08-13 14:15:42
【问题描述】:

.panel-heading 在引导程序中具有 15 像素左右的填充。我希望输入字段在.panel-heading 内扩展 100%。我尝试了负边距:

input.form-control {
   text-align:center;
   border: 5px solid #ccc;
   height: 40px;
   margin-left: -15px;
   margin-right: -15px;
}

但只有左侧在工作。我怎样才能让右侧也能正常工作?

.col-xs-12 {
	padding-left: 0;
	padding-right: 0;
}

.panel-heading {
	position: -webkit-sticky;
  	position: -moz-sticky;
  	position: -ms-sticky;
  	position: -o-sticky;
  	position: sticky;
	width: 100%;
	z-index: 99999;
	top: 0;
}

.glyphicon-arrow-left {
	margin-top: 5px;
}

.panel-heading h1 {
	padding-bottom: 0.65em;
	color: #3399ff;
	font-weight: bold;
}

.panel-body {
	padding: 0;
}

input.form-control {
   text-align:center;
   border: 5px solid #ccc;
   height: 40px;
   margin-left: -15px;
   margin-right: -15px;
}

.users {
	list-style-type: none;
	margin: 0;
	padding: 0;
}

.users li {
	border-bottom: 2px solid #eee;
	color: #000;
}

.users li .username {
	margin-left: 10px;
}

.users li:last-child {
	border-bottom: none;
}

.users a {
	display: block;
	height: 45px;
	padding: 15px 0;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class='container'>
	<div class='row'>
		<div class='col-sm-6 col-xs-12 col-centered'>
			<div class='panel panel-default'>
				<div class='panel-heading sticky'>
					<div class='back pull-left'><a href='chats.php'><span class='glyphicon glyphicon-arrow-left'></a></span></div>
					<h1 class='panel-title text-center'>New Chat</h1>
					<input class='form-control' id='searchUser' type='text' placeholder='Search'>
				</div>
				<div class='panel-body'>
					<ul class='users'>
						<li class='user text-center'>
							<a href='#'><span class='glyphicon glyphicon-user'></span><strong class='username'>John Appleseed</strong></a>
						</li>
					</ul>
				</div>
			</div>
		</div><!--end column-->
	</div><!--end row 1-->
</div><!--end container-->

【问题讨论】:

    标签: html css twitter-bootstrap


    【解决方案1】:

    这里有一个解决方案https://jsfiddle.net/enmyngeh/

    .col-xs-12 {
    	padding-left: 0;
    	padding-right: 0;
    }
    
    .panel-heading {
    	position: -webkit-sticky;
      	position: -moz-sticky;
      	position: -ms-sticky;
      	position: -o-sticky;
      	position: sticky;
    	width: 100%;
    	z-index: 99999;
    	top: 0;
    }
    
    .glyphicon-arrow-left {
    	margin-top: 5px;
    }
    
    .panel-heading h1 {
    	padding-bottom: 0.65em;
    	color: #3399ff;
    	font-weight: bold;
    }
    
    .panel-body {
    	padding: 0;
    }
    
    input.form-control {
       text-align:center;
       border: 5px solid #ccc;
       height: 40px;
       margin-left: -15px;
       width: calc(100% + 30px);
       
    }
    
    .users {
    	list-style-type: none;
    	margin: 0;
    	padding: 0;
    }
    
    .users li {
    	border-bottom: 2px solid #eee;
    	color: #000;
    }
    
    .users li .username {
    	margin-left: 10px;
    }
    
    .users li:last-child {
    	border-bottom: none;
    }
    
    .users a {
    	display: block;
    	height: 45px;
    	padding: 15px 0;
    }
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
    <div class='container'>
    	<div class='row'>
    		<div class='col-sm-6 col-xs-12 col-centered'>
    			<div class='panel panel-default'>
    				<div class='panel-heading sticky'>
    					<div class='back pull-left'><a href='chats.php'><span class='glyphicon glyphicon-arrow-left'></span></a></div>
    					<h1 class='panel-title text-center'>New Chat</h1>
    					<input class='form-control' id='searchUser' type='text' placeholder='Search'>
    				</div>
    				<div class='panel-body'>
    					<ul class='users'>
    						<li class='user text-center'>
    							<a href='#'><span class='glyphicon glyphicon-user'></span><strong class='username'>John Appleseed</strong></a>
    						</li>
    					</ul>
    				</div>
    			</div>
    		</div><!--end column-->
    	</div><!--end row 1-->
    </div><!--end container-->

    代码变更

    input.form-control {
       text-align:center;
       border: 5px solid #ccc;
       height: 40px;
       margin-left: -15px;
      width: calc(100% + 30px);
    }
    

    由于panel-heading 的左右两侧有额外的15px padding,因此您需要将缺少的空格添加到inputcalc(100% + 30px)

    【讨论】:

    • 谢谢。你知道为什么我的负边距不起作用吗?
    • @RobertRocha.. margin-left 工作正常,但问题出在父 div 上。父容器在 leftright 上有 padding15px。因此,即使儿童宽度为 100% 也会排除填充 15px。这就是我添加 30px 的原因。
    猜你喜欢
    • 1970-01-01
    • 2021-11-03
    • 2014-10-03
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 2018-12-19
    • 1970-01-01
    相关资源
    最近更新 更多