【问题标题】:Button is not centred even with resolution independent alignment即使与分辨率无关对齐,按钮也不居中
【发布时间】:2015-12-21 08:05:05
【问题描述】:

我的网站上有一个按钮,我使用了与分辨率无关的对齐方式。我检查了两个具有不同分辨率的不同计算机屏幕,它看起来还不错,但是,一旦在更大的纵横比屏幕或移动/平板设备上打开它就会重新定位。在宽高比较大的屏幕上,按钮向左移动,在较小的设备上,它向右浮动。我不是 100% 确定可能出了什么问题。下面是我的 CSS 和 HTML 代码。

HTML:

<div class="Welcome">
    <h1>Welcome to Beauty Factory Bookings</h1>
    <a href="website_link"><button>Book Appointment</button></a>
</div>

CSS:

    body {
        background: url('http://i.imgur.com/4mKmYMb.jpg') no-repeat fixed center center;
        background-size: cover;
        font-family: Montserrat;
        margin: 0;
        padding: 0;

    .Welcome {
        margin-top: 13%;
    }

    .Welcome h1 {
        text-align: center;
        font-family: Montserrat;
        font-size: 50px;
        text-transform: uppercase;
    }

    .Welcome button {
        height: 60px;
        width: 340px;
        background: #ff656c;
        border: 1px solid #e15960;
        color: #fff;
        font-weight: bold;
        text-transform: uppercase;
        font-size: 17px;
        font-family: Montserrat;
        outline: none;
        cursor: pointer;
        text-align: center;
        margin-left: 33%;
        margin-top: 3%;
        border-radius: 5px;
    }

知道如何解决这个问题吗?

【问题讨论】:

  • 您确实需要创建一个MCVE。基本上,这意味着除非您无法使此处的按钮表现得像您网站上的按钮,否则我们将无法为您提供帮助。
  • 只做我使用弹性盒子所做的事情。
  • 我试过了,还是不行。您的解决方案可能非常好,我认为我的其余代码也有问题。
  • @Filip 在那里无论如何我可以看到你的网站我可以通过这种方式检查你的 src 文件.. 或者只是尝试将其上传到 codepen 或 GIThub

标签: html css


【解决方案1】:

您将一个 html 元素居中,该元素具有 标准位置,并且是带有margin-left: automargin-right: auto块元素

将classes和ids的首字母小写是惯例。

Example

【讨论】:

    【解决方案2】:

    只是另一种方法:

    我将您的按钮包裹在div 中,并使用text-align:center 将按钮居中 不要忘记在你的css中取出margin-left: 33%;

    HTML

    <div class="Welcome">
        <h1>Welcome to Beauty Factory Bookings</h1>
        <div class="center">
            <a href="website_link"><button>Book Appointment</button></a>
        </div>
    </div>
    

    CSS

     .Welcome {
       margin-top: 13%;
     }
    
     .Welcome h1 {
       text-align: center;
       font-family: Montserrat;
       font-size: 50px;
       text-transform: uppercase;
     }
    
     .center {
       text-align: center;
     }
    
     .Welcome button {
       height: 60px;
       width: 340px;
       background: #ff656c;
       border: 1px solid #e15960;
       color: #fff;
       font-weight: bold;
       text-transform: uppercase;
       font-size: 17px;
       font-family: Montserrat;
       outline: none;
       cursor: pointer;
       text-align: center;
       margin-top: 3%;
       border-radius: 5px;
     }
    

    示例:http://codepen.io/anon/pen/EPyjXm

    【讨论】:

      【解决方案3】:

      a 标签创建一个新的 CSS 属性并将显示设置为 flex(占用 100% 的宽度;并将内容对齐到中心。

      编辑

      查看您的网站后, 有来自某处的 15px 的左边距。如果有可能,请摆脱它,否则只需将margin-left: 0 添加到.Welcome a {...}

      .Welcome a {
        display: flex;
        /*set display mode of anchor to flex*/
        justify-content: center; /* Justify content center*/
        margin-left: 0; /* Add !important in case it is overwritten*/
      }
      

      .Welcome {
        margin-top: 13%;
      }
      .Welcome h1 {
        text-align: center;
        font-family: Montserrat;
        font-size: 50px;
        text-transform: uppercase;
      }
      .Welcome a {
        display: flex;
        /*set display mode of anchor to flex*/
        justify-content: center; /* Justify content center*/
      }
      .Welcome button {
        height: 60px;
        width: 340px;
        background: #ff656c;
        border: 1px solid #e15960;
        color: #fff;
        font-weight: bold;
        text-transform: uppercase;
        font-size: 17px;
        font-family: Montserrat;
        outline: none;
        cursor: pointer;
        text-align: center;
        border-radius: 5px;
      }
        <div class="Welcome">
          <h1>Welcome to Beauty Factory Bookings</h1>
      
            <a href="website_link">
              <button>Book Appointment</button>
            </a>
      
        </div>

      【讨论】:

      • 在页面容器上显示 flex?他页面中的其他元素会发生什么变化?
      • 老实说,我在所有事情上都使用 flex,因为我可以控制很多事情)。我几乎不使用margin: 0 auto 来集中内容。每个人都有自己的喜好。
      • 我也从不使用0 auto,因为我不喜欢在孩子身上设置text-align: initial。我使用 flex 或 trasnform 我不能使用 flex
      • 是的.. flex 让 css 样式变得更加简单有趣哈哈
      猜你喜欢
      • 2018-11-26
      • 1970-01-01
      • 2017-06-03
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 2012-02-11
      相关资源
      最近更新 更多