按钮应该继承父div宽度

时间:2018-01-23 15:55:14

标签: javascript jquery html css

我需要在产品盒底部设置3个按钮,如左侧的图像,也应该响应,但不能这样做:

To do

以下是HTML&按钮不继承父div宽度的CSS:

.pro-box{
	height: 416px;
	overflow-y: hidden;
	padding: 6px 6px 0 6px;
	background-color: #4dbaef;
	color: #fff;
}
.pro-box>img{
	display: block;
	margin: 0 auto;
	margin-bottom: 15px;
	background-color: #fff;
    width: 100%;
    height: 242px;
}
.btn-pro{
	text-transform: uppercase;
	letter-spacing: 1px;
	font-family: 'Roboto Regular', sans-serif;
	font-size: 13px;
	background-color: #339dd1;
	border-radius: 0;
	color: #fff;
	padding: 12px;
	margin-right: 2px;
	transition: 0.3s;
}
.pro-box>.btn-group{
	position: absolute;
	bottom: 20px;
	left: 20px;
	width: 100%;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-lg-3">
<div class="pro-box">
  <img src="http://via.placeholder.com/300" alt="" class="img-responsive">
  <h4 class="pro-name">MAQ03422</h4>
  <p class="pro-desc">Titanium Delicate Touch Microvascular Needle Holders Needle Holders and Passers</p>
  <div class="btn-group" role="group">
    <a href="#" class="btn btn-pro">Tray</a>
    <a href="#" class="btn btn-pro">cart</a>
    <a href="#" class="btn btn-pro">compare</a>
  </div>
</div>
</div>

1 个答案:

答案 0 :(得分:0)

您可以通过向容器添加display:flex来简单地使用flex,然后将flex:1设置为按钮。您需要在主容器上设置position:relative,因为您使用的是position:absolute

.pro-box {
  height: 416px;
  overflow-y: hidden;
  padding: 6px 6px 0 6px;
  background-color: #4dbaef;
  color: #fff;
  position:relative;
}

.pro-box>img {
  display: block;
  margin: 0 auto;
  margin-bottom: 15px;
  background-color: #fff;
  width: 100%;
  height: 242px;
}

.btn-pro {
  text-transform: uppercase;
  letter-spacing: 1px;
  font-family: 'Roboto Regular', sans-serif;
  font-size: 13px;
  background-color: #339dd1;
  border-radius: 0;
  color: #fff;
  padding: 12px;
  margin-right: 2px;
  transition: 0.3s;
}

.pro-box>.btn-group {
  position: absolute;
  display:flex;
  bottom: 0;
  left:0;
  right:0;
  box-sizing:border-box;
}
.pro-box>.btn-group > .btn {
  flex:1;
  box-sizing:border-box;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="col-lg-3">
  <div class="pro-box">
    <img src="http://via.placeholder.com/300" alt="" class="img-responsive">
    <h4 class="pro-name">MAQ03422</h4>
    <p class="pro-desc">Titanium Delicate Touch Microvascular Needle Holders Needle Holders and Passers</p>
    <div class="btn-group" role="group">
      <a href="#" class="btn btn-pro">Tray</a>
      <a href="#" class="btn btn-pro">cart</a>
      <a href="#" class="btn btn-pro">compare</a>
    </div>
  </div>
</div>