【问题标题】:unable to move elements in flexbox无法在 flexbox 中移动元素
【发布时间】:2020-01-04 02:57:05
【问题描述】:

如果不干扰右侧的锚标记,我无法将提交按钮向左移动 1vw。此外,锚边框有足够的空间,但是当减小浏览器的宽度时,我无法让文本和图像保持在同一行。

更新:通过将锚点 display:block 更改为 display:inline-flex,能够将 img 和文本保持在锚点的同一行。

仍然无法在不干扰锚标签的情况下将提交按钮向右移动

#footer-top{
  background-color:white;
  padding: 0 2.87rem;
}

h3{
  color:#b3d7f8;
  font-size: 1.5vw;
  padding-top:3vw;
  padding-left:2vw;
  background-color:#538231;
  margin:0
}

#footer-container{
   padding: 0 2.87rem;
   background-color:white;
}

.myFooter{
  background-color:white;
  display: flex;  
  flex-wrap: nowrap;
}



.myFooter .left{
  flex: 1 1 auto;
  background-color:#538231;
  padding-top:3vw;
  padding-left:2vw;
  padding-bottom:2vw;
  
}

.myFooter label{
  display: block;
  color: #c2d59b;
  margin-bottom:1vw;
  font-size:1vw;
}

.myFooter input{
  padding: .5vw .5vw;
}

#email{
  height:1vw;
  line-height:1;
  font-size:1vw;
  width:25vw;
}

.submit-button {
  background-color: white!important; 
  border: none!important;
  color: black!important;
  padding: .5vw .5vw!important;
  text-align: center!important;
  text-decoration: none!important;
  display: inline-block!important;
  font-size: 1vw!important;
  height:2vw!important;
  width:5vw!important;
  line-height:1vw!important;
}



.myFooter .right{
  flex: 1 1 ;
  background-color:#538231;
  padding-bottom:2vw;
}

.myFooter .right a{
  display: inline-flex;
  color: white;
  border: 1px solid white;
  width: 71%;
  margin: 1vw 0;
  text-decoration:none;
}

 


.myFooter .right img{
  width: 1.5vw;
  height: 100%;
  padding: .59vw 0;
  display:block;
  float:left;
  margin:0 1vw;
}

.myFooter .right span{
  font-size:1vw;
  padding:1vw 0;
  display:inline-block;
  line-height:1;
}






@media screen and (max-width: 961px) {#footer-top,#footer-container{
  padding:0 1.7rem;}
}
<div id ="footer-top">
   <h3>Help create a sustainable and healthy town of Weston</h3>
</div>
<div id="footer-container">
   <div class="myFooter">   
      <div class="left">    
         <form name="message" method="post">
            <section>  
               <label for="email">Join our mailing list:</label><input id="email" type="email"  name="email" placeholder="enter email">
               <input class="submit-button" type="submit" value="Submit"> 
            </section>
         </form>
      </div>
      <div class="right">
         <a href="https://www.facebook.com/groups/1960906387454685">
            <img class="foot-icons" src="https://sustainablewestonma.000webhostapp.com/wp-content/uploads/2019/08/facebook-square-brands-green.png">
            <span class="foot-txt">Like us on Facebook</span>
         </a>

         <a href="https://www.instagram.com/sustainablewestonactiongroup/">
            <img class="foot-icons" src="https://sustainablewestonma.000webhostapp.com/wp-content/uploads/2019/08/instagram-brands-green.png">
            <span class="foot-txt">follow us on Instagram</span>
         </a>

         <a href="https://twitter.com/Weston_SWAG">
            <img class="foot-icons" src="https://sustainablewestonma.000webhostapp.com/wp-content/uploads/2019/08/twitter-square-brands-green.png">
            <span class="foot-txt">Retweet us on Twitter</span>
         </a>
      </div>
   </div>
<div>

【问题讨论】:

    标签: css layout flexbox responsive


    【解决方案1】:

    由于您的.left.right 元素应用了flex-grow: 1(通过flex 速记),它们已经扩展到所有可用空间并且是感人的。所以任何改变一个元素宽度的东西都会影响另一个元素。

    我建议的第一件事是摆脱该规则,而是在容器上使用justify-content: space-between

    然后,通过给它们white-space: nowrap 来防止右侧的锚点缠绕。同时删除宽度限制,使文本不会溢出。

    #footer-top {
      background-color: white;
      padding: 0 2.87rem;
    }
    
    h3 {
      color: #b3d7f8;
      font-size: 1.5vw;
      padding-top: 3vw;
      padding-left: 2vw;
      background-color: #538231;
      margin: 0
    }
    
    #footer-container {
      padding: 0 2.87rem;
      background-color: white;
    }
    
    .myFooter {
      /* background-color: white; */
      background-color: #538231; /* adjustment */
      display: flex;
      flex-wrap: nowrap;
      justify-content: space-between; /* new */
    }
    
    .myFooter .left {
      /* flex: 1 1 auto; */
      background-color: #538231;
      padding-top: 3vw;
      padding-left: 2vw;
      padding-bottom: 2vw;
    }
    
    .myFooter label {
      display: block;
      color: #c2d59b;
      margin-bottom: 1vw;
      font-size: 1vw;
    }
    
    .myFooter input {
      padding: .5vw .5vw;
    }
    
    #email {
      height: 1vw;
      line-height: 1;
      font-size: 1vw;
      width: 25vw;
    }
    
    .submit-button {
      background-color: white !important;
      border: none !important;
      color: black !important;
      padding: .5vw .5vw !important;
      text-align: center !important;
      text-decoration: none !important;
      display: inline-block !important;
      font-size: 1vw !important;
      height: 2vw !important;
      width: 5vw !important;
      line-height: 1vw !important;
      
        margin-left: 5px; /* change values here; right anchors don't move anymore */
    }
    
    .myFooter .right {
      /* flex: 1 1; */
      background-color: #538231;
      padding-bottom: 2vw;
        padding-right: 2vw; /* new */
    }
    
    .myFooter .right a {
      display: block;
      color: white;
      border: 1px solid white;
      /* width: 71%; */
      margin: 1vw 0;
      text-decoration: none;
      white-space: nowrap;  /* new */
      padding-right: 2vw;  /* new */
    }
    
    .myFooter .right img {
      width: 1.5vw;
      height: 100%;
      padding: .59vw 0;
      display: block;
      float: left;
      margin: 0 1vw;
    }
    
    .myFooter .right span {
      font-size: 1vw;
      padding: 1vw 0;
      display: inline-block;
      line-height: 1;
    }
    
    @media screen and (max-width: 961px) {
      #footer-top,
      #footer-container {
        padding: 0 1.7rem;
      }
    }
    <div id="footer-top">
      <h3>Help create a sustainable and healthy town of Weston</h3>
    </div>
    <div id="footer-container">
      <div class="myFooter">
        <div class="left">
          <form name="message" method="post">
            <section>
              <label for="email">Join our mailing list:</label><input id="email" type="email" name="email" placeholder="enter email">
              <input class="submit-button" type="submit" value="Submit">
            </section>
          </form>
        </div>
        <div class="right">
          <a href="https://www.facebook.com/groups/1960906387454685">
            <img class="foot-icons" src="https://sustainablewestonma.000webhostapp.com/wp-content/uploads/2019/08/facebook-square-brands-green.png">
            <span class="foot-txt">Like us on Facebook</span>
          </a>
    
          <a href="https://www.instagram.com/sustainablewestonactiongroup/">
            <img class="foot-icons" src="https://sustainablewestonma.000webhostapp.com/wp-content/uploads/2019/08/instagram-brands-green.png">
            <span class="foot-txt">follow us on Instagram</span>
          </a>
    
          <a href="https://twitter.com/Weston_SWAG">
            <img class="foot-icons" src="https://sustainablewestonma.000webhostapp.com/wp-content/uploads/2019/08/twitter-square-brands-green.png">
            <span class="foot-txt">Retweet us on Twitter</span>
          </a>
        </div>
      </div>
      <div>

    jsFiddle demo

    【讨论】:

      猜你喜欢
      • 2019-07-19
      • 2016-08-23
      • 2019-12-19
      • 1970-01-01
      • 1970-01-01
      • 2019-07-06
      • 1970-01-01
      • 2021-03-10
      • 1970-01-01
      相关资源
      最近更新 更多