浮动右侧元素不完全浮动

时间:2016-06-16 18:18:45

标签: html css css3 css-float css-transitions

我有两列方框。有左侧和右侧。由于某种原因,右侧不会一直浮动到容器的末端。我没有任何限制阻止它这样做,所以我不确定为什么它不会一直都没有。

你可以在这里看到小提琴:

Here

如果您查看“点击服务以了解更多信息”的小文本/注释,它会一直向右浮动并位于容器的末尾。右侧标签需要与那些标签一样正确。

另外,旁注。你可以看到我有一个悬停效果,从左到右拉出一个新的背景颜色。我不确定为什么背景颜色不像它滑动时那样过渡。我有转换代码到主元素。

有谁知道为什么这两件事不起作用?

#service-tabs {
    width: 100%;
    padding: 100px 0;
    height: auto;
    background: #F0F0F0;
    overflow: auto;
}
#service-tabs-container {
    width: 70%;
    margin: 0 auto;
    text-align: center;
}
#service-tabs-container-title {
    color: #404040;
    font-size: 1.3em;
    margin-bottom: 15px;
    text-align: left;
}
#service-tabs-container-title2 {
    color: #578570;
    font-size: 2.8em;
    margin-bottom: 20px;
    line-height: 1.4em;
    text-align: left;
}
#service-tabs-container-note {
    margin: 40px 0;
    font-size: .7em;
    text-align: right;
}
#service-tabs-left {
    float: left;
    width: 50%;
}
#service-tabs-right {
    float: right;
    width: 50%;
}
.service-tab-block {
    position: relative;
    font-size: 1.6em;
    padding: 3.5em 20px;
    /*padding: 1em 25px;*/
    width: 85%;
    text-align: center;
    color: #FFF;
    display: block;
    margin: 30px 0;
    cursor: pointer;
    border: none;
    background-image: linear-gradient(to right, #000 50%, #578570 50%);
    background-size: 201% 100%;
    background-repeat: no-repeat;
    background-position: bottom right;
    transition:width 0.2s ease;
    -webkit-transition:width 0.2s ease;
}
.service-tab-block.active {
    background: #000;
    color: #FFF;
}
.service-tab-block:hover {
    -webkit-transition: all 0.2s ease-in;
    transition: all 0.2s ease-in;
    background-position: bottom left;
    color: #FFF;
    border: none;
}


<div id="service-tabs">
  <div id="service-tabs-container">
    <div id="service-tabs-container-title">WHAT WE OFFER</div>
    <div id="service-tabs-container-title2">Our Services</div>
    <div id="service-tabs-container-note">* Click a service to find out more.</div>
    <div id="service-tabs-left">
        <h1 class="service-tab-block" id="service_tab1">DEMOLITION</h1>
        <h1 class="service-tab-block" id="service_tab2">CONCRETE CRUSHING</h1>
        <h1 class="service-tab-block" id="service_tab3">SCRAP METAL RECYCLING</h1>
    </div>
    <div id="service-tabs-right">
        <h1 class="service-tab-block" id="service_tab4">ASSET RECOVERY</h1>
        <h1 class="service-tab-block" id="service_tab5">FOUNDATION REMOVAL</h1>
        <h1 class="service-tab-block" id="service_tab6">SITE WORK</h1>
    </div>
  </div>
</div>

5 个答案:

答案 0 :(得分:2)

Updated fiddle(左/右浮动着色,以便您可以看到会发生什么)

由于CSS规则中的这两个属性

.service-tab-block {
    padding: 3.5em 20px;
    width: 85%;

我更改/添加到此

.service-tab-block {
    width: calc(100% - 60px);   /* compensate for the padding and margin */
    ....
}
#service-tabs-right .service-tab-block {
    margin-left: 20px;
}

对于转换,请更改

transition:width 0.2s ease;
-webkit-transition:width 0.2s ease;

transition:all 0.2s ease;
-webkit-transition:all 0.2s ease;

答案 1 :(得分:1)

这里是可行的工作转换解决方案

CSS

#service-tabs {
    width: 100%;
    padding: 100px 0;
    height: auto;
    background: #F0F0F0;
    overflow: auto;
}
#service-tabs-container {
    width: 70%;
    margin: 0 auto;
    text-align: center;
}
#service-tabs-container-title {
    color: #404040;
    font-size: 1.3em;
    margin-bottom: 15px;
    text-align: left;
}
#service-tabs-container-title2 {
    color: #578570;
    font-size: 2.8em;
    margin-bottom: 20px;
    line-height: 1.4em;
    text-align: left;
}
#service-tabs-container-note {
    margin: 40px 0;
    font-size: .7em;
    text-align: right;
}
#service-tabs-left {
    float: left;
    width: 50%;
}
#service-tabs-right {
    float: right;
    width: 50%;
}

.right1{
margin-right: -3%;
}

.service-tab-block {
    position: relative;
    font-size: 1.6em;
    padding: 3.5em 20px;
    /*padding: 1em 25px;*/
    width: 85%;
    text-align: center;
    color: #FFF;
    display: block;
    margin: 30px 0;
    cursor: pointer;
    border: none;
    background-image: linear-gradient(to right, #000 50%, #578570 50%);
    background-size: 201% 100%;
    background-repeat: no-repeat;
    background-position: bottom right;
    transition:width 0.2s ease;
    -webkit-transition:all 0.2s ease;
}
.service-tab-block.active {
    background: #000;
    color: #FFF;
}
.service-tab-block:hover {
    -webkit-transition: all 0.2s ease-in;
    transition:0.4s all ease-in;
    background-position: bottom left;
    color: #FFF;
    border: none;
}

HTML

<div id="service-tabs">
  <div id="service-tabs-container">
    <div id="service-tabs-container-title">WHAT WE OFFER</div>
    <div id="service-tabs-container-title2">Our Services</div>
    <div id="service-tabs-container-note">* Click a service to find out more.</div>
    <div id="service-tabs-left">
        <h1 class="service-tab-block" id="service_tab1">DEMOLITION</h1>
        <h1 class="service-tab-block" id="service_tab2">CONCRETE CRUSHING</h1>
        <h1 class="service-tab-block" id="service_tab3">SCRAP METAL RECYCLING</h1>
    </div>
    <div id="service-tabs-right" class="right1">
        <h1 class="service-tab-block" id="service_tab4">ASSET RECOVERY</h1>
        <h1 class="service-tab-block" id="service_tab5">FOUNDATION REMOVAL</h1>
        <h1 class="service-tab-block" id="service_tab6">SITE WORK</h1>
    </div>
  </div>
</div>

答案 2 :(得分:0)

如何将此添加到您的CSS

#service-tabs-right .service-tab-block{
  float: right;
  margin-bottom:0px;
}

编辑答案。

答案 3 :(得分:0)

试试此代码

&#13;
&#13;
#service-tabs {
  width: 100%;
  padding: 100px 0;
  height: auto;
  background: #F0F0F0;
  overflow: auto;
}
#service-tabs-container {
  width: 100%;
  text-align: center;
}
#service-tabs-container-title {
  color: #404040;
  font-size: 1.3em;
  margin-bottom: 15px;
  text-align: left;
}
#service-tabs-container-title2 {
  color: #578570;
  font-size: 2.8em;
  margin-bottom: 20px;
  line-height: 1.4em;
  text-align: left;
}
#service-tabs-container-note {
  margin: 40px 0;
  font-size: .7em;
  text-align: right;
}
#service-tabs-left {
  float: left;
  max-width: 50%;
}
#service-tabs-right {
  float: right;
  max-width: 50%;
}
.service-tab-block {
  position: relative;
  font-size: 1.6em;
  padding: 3.5em 20px;
  padding: 1em 25px;
  width: 85%;
  text-align: center;
  color: #FFF;
  display: block;
  margin: 30px 0;
  cursor: pointer;
  border: none;
  background-image: linear-gradient(to right, #000 50%, #578570 50%);
  background-size: 201% 100%;
  background-repeat: no-repeat;
  background-position: bottom right;
  transition: width 0.2s ease;
  -webkit-transition: width 0.2s ease;
  -webkit-transition: all 0.2s ease-in;
  transition: all 0.2s ease-in;
}
.service-tab-block.active {
  background: #000;
  color: #FFF;
}
.service-tab-block:hover {
  background-position: bottom left;
  color: #FFF;
  border: none;
}
&#13;
<div id="service-tabs">
  <div id="service-tabs-container">
    <div id="service-tabs-container-title">WHAT WE OFFER</div>
    < <div id="service-tabs-container-title2">Our Services</div>
  <div id="service-tabs-container-note">* Click a service to find out more.</div>
  <div id="service-tabs-left">
    <h1 class="service-tab-block" id="service_tab1">DEMOLITION</h1>
    <h1 class="service-tab-block" id="service_tab2">CONCRETE CRUSHING</h1>
    <h1 class="service-tab-block" id="service_tab3">SCRAP METAL RECYCLING</h1>
  </div>
  <div id="service-tabs-right">
    <h1 class="service-tab-block" id="service_tab4">ASSET RECOVERY</h1>
    <h1 class="service-tab-block" id="service_tab5">FOUNDATION REMOVAL</h1>
    <h1 class="service-tab-block" id="service_tab6">SITE WORK</h1>
  </div>
</div>
</div>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

请尝试以下代码。

如果我们对div进行分区,我们应该给出父宽度的css:100%,之后每个div都有50%的宽度。

#service-tabs-container {
    clear: both !important;
    margin: 0 auto;
    text-align: center;
    width: 100%;
}
<div id="service-tabs-container">
        <div id="service-tabs-container-title">WHAT WE OFFER</div>
        <div id="service-tabs-container-title2">Our Services</div>
        <div id="service-tabs-container-note">* Click a service to find out more.</div>


      <div class="" style="width:100%">
            <div id="service-tabs-left">
                <h1 id="service_tab1" class="service-tab-block">DEMOLITION</h1>
                <h1 id="service_tab2" class="service-tab-block">CONCRETE CRUSHING</h1>
                <h1 id="service_tab3" class="service-tab-block">SCRAP METAL RECYCLING</h1>
            </div>
            <div id="service-tabs-right">
                <h1 id="service_tab4" class="service-tab-block">ASSET RECOVERY</h1>
                <h1 id="service_tab5" class="service-tab-block">FOUNDATION REMOVAL</h1>
          <h1 id="service_tab6" class="service-tab-block">SITE WORK</h1>
      </div></div>

希望你这对你有帮助。