【问题标题】:margin auto not taken into account [closed]未考虑保证金自动[关闭]
【发布时间】:2021-10-10 07:53:34
【问题描述】:

我已经定义了以下 CSS 类

.tiles .tile > a {
      display: flex;
      -webkit-flex-direction: column;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      transition: background-color 0.5s ease, transform 0.5s ease;
      position: absolute;
      top: 0;
      margin-left: auto !important; 
      margin-right: auto !important;
      max-width: 300px;
      width: 100%;
      height: 100%;
      padding: 1em;
      border-radius: 4px;
      border-bottom: 0;
      color: #FFFFFF;
      text-align: center;
      text-decoration: none;
      z-index: 3; }

但是,margin-right: auto 和 margin-left: auto 没有被考虑在内(有或没有!important,所以我的标签没有居中。 是因为柔韧吗? 你知道是什么原因造成的吗?

谢谢!

【问题讨论】:

标签: css margin


【解决方案1】:

感谢 TerminalFlow 帮我找出问题的根源,下面是解决问题的代码:

.tiles .tile > a {
      display: flex;
      -webkit-flex-direction: column;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      transition: background-color 0.5s ease, transform 0.5s ease;
      margin-left: auto !important; 
      margin-right: auto !important;
      max-width: 300px;
      width: 100%;
      height: 200px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%,-50%);
      padding: 1em;
      border-radius: 4px;
      border-bottom: 0;
      color: #FFFFFF;
      text-align: center;
      text-decoration: none;
      z-index: 3; }

这样,标签居中并且仍然尊重我想要的叠加层。

【讨论】:

    【解决方案2】:

    事实上你已经设置了display: flex,然后设置了position: absolutetechnically doesn't matter,但是你的top: 0覆盖了所有的flex属性。

    您应该能够将所有内容都集中在您的 css 中:

    .tiles .tile > a {
          display: flex;
          -webkit-flex-direction: column;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          transition: background-color 0.5s ease, transform 0.5s ease;
          max-width: 300px;
          width: 100%;
          height: 100%;
          padding: 1em;
          border-radius: 4px;
          border-bottom: 0;
          color: #FFFFFF;
          text-align: center;
          text-decoration: none;
          z-index: 3; }

    【讨论】:

    • 感谢您的回答!实际上,当我按照您的示例进行操作时,我的标签会居中,但是我会松开使用 position: absolute 设置的覆盖。你会碰巧知道我怎么能绕过它吗?在内部创建子类是否可行?
    • 没关系,感谢您的指点,我找到了绕过问题的方法,我刚刚发布了答案。谢谢一百万!
    • 没问题,很高兴它有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 2015-10-25
    相关资源
    最近更新 更多