【问题标题】:Align Close Button Inside CSS Cookie Consent Popup在 CSS Cookie 同意弹出窗口内对齐关闭按钮
【发布时间】:2020-05-20 14:11:23
【问题描述】:

我有一个 cookie 同意弹出窗口,我在其中添加了一个关闭 X 按钮。
我需要在弹出窗口的中间对齐按钮,但按钮不在中心。
我知道我可以设置一个边距顶部来解决问题,但我认为这不是解决问题的正确方法。

<div class="alert cookiealert" >
    By using our website you agree to our Cookie policy
   <div  class="acceptcookies">
      <div class="x"></div>
  </div>
</div>


.cookiealert {
    position: fixed;
    padding-left: 40px;
    font-size: 14px;
    bottom: 0;
    left: 0;
    width: 420px;
    margin-bottom: 20px;
    margin-left: 20px;
    z-index: 900;
    opacity: 0;
    visibility: hidden;
    border-radius: 50px;
    transform: translateY(80%);
    transition: all 500ms ease-out;
    color: #000;
    background-color: #ecf0f1;
}

.cookiealert.show {
    opacity: 1;
    visibility: visible;
    transform: translateY(0%);
    transition-delay: 1000ms;
}

.cookiealert a {
    text-decoration: underline
}

.cookiealert .acceptcookies {
    position: absolute;
    display: inline-block;
    margin-left: 25px;    
}

.acceptcookies .x {
    display: block;
    position: fixed;
    width: 12px;
    height: 12px;
    transition: transform .28s ease-in-out;
    border-color: rgb(255, 255, 255);
}

.acceptcookies .x:hover {
    transform: rotate(90deg);
}

.acceptcookies .x:before {
    content: "";
    position: absolute;
    display: block;
    margin: auto;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    width: 12px;
    height: 0;
    border-top: 1px solid rgba(0,0,0,0.5);
    transform: rotate(45deg);
    transform-origin: center;
}

.acceptcookies .x:after {
    content: "";
    position: absolute;
    display: block;
    margin: auto;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    width: 12px;
    height: 0;
    border-top: 1px solid rgba(0,0,0,0.5);
    transform: rotate(-45deg);
    transform-origin: center;
}



这是代码:https://codepen.io/m4573r00/pen/RwWEVWp

【问题讨论】:

    标签: html css cookies bootstrap-4 cookieconsent


    【解决方案1】:

    这样写,即使多行文字,也是居中的。

    .cookiealert.show {
      opacity: 1;
      visibility: visible;
      transform: translateY(0%);
      transition-delay: 1000ms;
    }
    .alert{
          position: relative;
        padding: .75rem 1.25rem;
        margin-bottom: 1rem;
        border: 1px solid transparent;
        border-radius: .25rem;
    }
    
    
    .cookiealert {
        font-size: 14px;
        bottom: 0;
        left: 0;
        width: 420px;
        margin-left: 20px;
        z-index: 900;
        transition: all 500ms ease-out;
        color: #000;
        background-color: #ecf0f1;
    }
    
    .cookiealert.show {
        opacity: 1;
        visibility: visible;
        transform: translateY(0%);
        transition-delay: 1000ms;
    }
    
    .cookiealert a {
        text-decoration: underline
    }
    
    .cookiealert .acceptcookies {
      position: absolute;
      top: 0;
      bottom: 0;
      height: 12px;
      margin-top: auto;
      margin-bottom: auto;
      right: 10px;
    }
    
    .acceptcookies .x {
        display: block;
        position: relative;
        width: 12px;
        height: 13px;
        transition: transform .28s ease-in-out;
        border-color: rgb(255, 255, 255);
    }
    
    .acceptcookies .x:hover {
        transform: rotate(90deg);
    }
    
    .acceptcookies .x:before {
        content: "";
        position: absolute;
        display: block;
        left: 0;
        right: 0;
        top: 6px; 
        bottom: 0;
        width: 12px;
        height: 0;
        border-top: 1px solid rgba(0,0,0,0.5);
        transform: rotate(45deg);
        transform-origin: center;
    }
    
    .acceptcookies .x:after {
        content: "";
        position: absolute;
        display: block;
        left: 0;
        right: 0;
        top: 6px;
        bottom: 0;
        width: 12px;
        height: 0;
        border-top: 1px solid rgba(0,0,0,0.5);
        transform: rotate(-45deg);
        transform-origin: center;
    }
    <div class="alert cookiealert show" >
        By using our website you agree to our Cookie policy
        By using our website you agree to our Cookie policy
        By using our website you agree to our Cookie policy
       <div  class="acceptcookies">
          <div class="x"></div>
      </div>
    </div>
    
      

    【讨论】:

    • 谢谢。我按照您的示例进行操作,并且能够使“x”居中,但是将鼠标悬停在“x”上时会出现奇怪的过渡。我花了 2 个小时试图修复它,但我不知道我做错了什么...... ? 这是代码:codepen.io/m4573r00/pen/RwWEVWp 我的 css 与您的示例不完全一样,因为您删除了淡入和淡入淡出cookiealert div 的 -out 转换 ...但 Codepen 不加载该转换。
    • @M4573R ' :hover ' 是否向下移动一点?这是因为“12px”中心的高度没有准确计算,“x”的线宽为“1px”。您想消除抖动效果,将高度从“12px”更改为“13px”。如果您不想要过渡,只需从“.x”中删除“过渡”。以上代码已修改
    • 效果很好!非常感谢你的帮助。 ? ...我希望我和你一样好。
    • @M4573R 我也经常自己摸索,因为找不到像这样学习的文档。
    【解决方案2】:

    您能否修改您的 HTML 代码并使用 Flex 来对齐“x”?另外,删除.cookiealert .acceptcookies 中的position: absolute 属性

    HTML

    <div class="alert cookiealert d-flex" >
       <div> By using our website you agree to our Cookie policy </div>
       <div class="acceptcookies d-flex align-items-center">
          <div class="x"></div>
      </div>
    </div>
    

    CSS

    .cookiealert .acceptcookies {
        /* position: absolute; */ /*REMOVE*/
        display: inline-block;
        margin-left: 25px;    
    }
    
    

    .cookiealert {
        position: fixed;
        padding-left: 40px;
        font-size: 14px;
        bottom: 0;
        left: 0;
        width: 420px;
        margin-bottom: 20px;
        margin-left: 20px;
        z-index: 900;
        opacity: 0;
        visibility: hidden;
        border-radius: 50px;
        transform: translateY(80%);
        transition: all 500ms ease-out;
        color: #000;
        background-color: #ecf0f1;
    }
    
    .cookiealert.show {
        opacity: 1;
        visibility: visible;
        transform: translateY(0%);
        transition-delay: 1000ms;
    }
    
    .cookiealert a {
        text-decoration: underline
    }
    
    .cookiealert .acceptcookies {
        /* position: absolute; */ /*REMOVE*/
        display: inline-block;
        margin-left: 25px;    
    }
    
    .acceptcookies .x {
        display: block;
        position: fixed;
        width: 12px;
        height: 12px;
        transition: transform .28s ease-in-out;
        border-color: rgb(255, 255, 255);
    }
    
    .acceptcookies .x:hover {
        transform: rotate(90deg);
    }
    
    .acceptcookies .x:before {
        content: "";
        position: absolute;
        display: block;
        margin: auto;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        width: 12px;
        height: 0;
        border-top: 1px solid rgba(0,0,0,0.5);
        transform: rotate(45deg);
        transform-origin: center;
    }
    
    .acceptcookies .x:after {
        content: "";
        position: absolute;
        display: block;
        margin: auto;
        left: 0;
        right: 0;
        top: 0;
        bottom: 0;
        width: 12px;
        height: 0;
        border-top: 1px solid rgba(0,0,0,0.5);
        transform: rotate(-45deg);
        transform-origin: center;
    }
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.min.css">
    
    <div class="alert cookiealert show d-flex">
      <div> By using our website you agree to our Cookie policy </div>
      <div class="acceptcookies d-flex align-items-center">
        <div class="x"></div>
      </div>
    </div>

    【讨论】:

    • 谢谢。它居中,但如果你仔细观察,“x”会有一个奇怪的过渡。这不是一个平稳的过渡。我试图解决这个问题,但我不确定它有什么问题。
    • 对不起,我真的不知道过渡。帮不上忙
    猜你喜欢
    • 1970-01-01
    • 2014-03-16
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    相关资源
    最近更新 更多