代码之家  ›  专栏  ›  技术社区  ›  DCR

无法移动FlexBox中的元素

  •  0
  • DCR  · 技术社区  · 6 年前

    我无法将提交按钮移到左边1vw而不干扰右边的锚标签。此外,锚定边框有足够的空间,但当减小浏览器宽度时,我无法使文本和图像保持在同一行。

    更新:通过将anchor display:block更改为display:in line flex,可以将img和文本保持在anchor中的同一行。

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

    #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>
    0 回复  |  直到 6 年前
        1
  •  1
  •   Michael Benjamin William Falcon    6 年前

    自从你 .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