中心响应式绝对定位元素具有固定边距

时间:2018-10-15 12:10:54

标签: html css centering absolute

我有一个绝对定位的CSS横幅,该横幅根据视口大小具有响应的宽度和高度。该元素位于台式机和平板电脑设备的中心,但不在移动设备上(横幅的宽度开始缩小时)。

我不能使用经典的margin: 0 auto;技巧,因为必须将边距设置为固定值才能使横幅正常工作。

代码如下:

HTML:

<h1 class="banner">A Simple CSS Banner</h1>

CSS:

@import url(https://fonts.googleapis.com/css?family=Rye);

body 
{ background: #eee; }

.banner {
  position: absolute;
  left: 50%;
  display: block;
  margin: 100px -200px;
  width: 400px;
  max-width: calc(100% - 150px);
  height: 60px;
  border: 1px solid #8a1;
  font: normal 30px/60px 'Rye';
  text-align: center;
  color: #451;
  background: #9b2;
  border-radius: 4px;
  box-shadow: 0 0 30px rgba(0,0,0,.15) inset,
    0 6px 10px rgba(0,0,0,.15);
}

.banner::before,
.banner::after {
  content: '';
  position: absolute;
  z-index: -1;
  left: -70px;
  top: 24px;
  display: block;
  width: 40px;
  height: 0px;
  border: 30px solid #9b2;
  border-right: 20px solid #791;
  border-bottom-color: #94b81e;
  border-left-color: transparent;
  transform: rotate(-5deg);
}

.banner::after {
  left: auto;
  right: -70px;
  border-left: 20px solid #791;
  border-right: 30px solid transparent;
  transform: rotate(5deg);
}

@media (max-width: 475px) {

  .banner {
    height: 90px;
    line-height: 45px;
  }

  .banner::before,
  .banner::after {
    border-width: 45px;
    border-right-width: 30px;
  }

  .banner::after {
    border-left-width: 30px;
    border-right-width: 45px;
  }

}

链接到有效的代码笔:https://codepen.io/Adam0410/pen/QZOKdV

感谢您的帮助!

4 个答案:

答案 0 :(得分:1)

检查是否对您有帮助。

@import url(https://fonts.googleapis.com/css?family=Rye);

body {
  background: #eee;
}

.banner {
  position: absolute;
  left: 50%;
  transform: translate(-50%);
  display: block;
  margin: 100px 0;
  width: 400px;
  max-width: calc(100% - 150px);
  min-height: 60px;
}
.banner span {
  display: block;
  position: relative;
  z-index: 1;
  border: 1px solid #8a1;
  font: normal 30px/60px "Rye";
  text-align: center;
  color: #451;
  background: #9b2;
  border-radius: 4px;
  box-shadow: 0 0 30px rgba(0, 0, 0, 0.15) inset, 0 6px 10px rgba(0, 0, 0, 0.15);
}
.banner::before,
.banner::after {
  content: "";
  position: absolute;
  z-index: -1;
  left: -70px;
  bottom: -24px;
  display: block;
  width: 40px;
  height: 0px;
  border: 30px solid #9b2;
  border-right: 20px solid #791;
  border-bottom-color: #94b81e;
  border-left-color: transparent;
  transform: rotate(-5deg);
}

.banner::after {
  left: auto;
  right: -70px;
  border-left: 20px solid #791;
  border-right: 30px solid transparent;
  transform: rotate(5deg);
}

@media (max-width: 475px) {
  .banner {
    min-height: 90px;
    line-height: 45px;
  }

  .banner::before,
  .banner::after {
    border-width: 45px;
    border-right-width: 30px;
  }

  .banner::after {
    border-left-width: 30px;
    border-right-width: 45px;
  }
}
<h1 class="banner"><span>A Simple CSS Banner</span></h1>

答案 1 :(得分:0)

这样您可以保持顶部和底部边距:

left: 0; right: 0;
margin: 100px auto;

答案 2 :(得分:0)

您可以简单地使用Media查询并调整Width和margin。

类似:

media (max-width: 475px) {
  .banner{
    width: 200px; 
    margin:100px -100px;
  }
}

答案 3 :(得分:-1)

如果要将绝对或固定元素居中,则应:

  position: absolute;
  left: 0%;
  right:0%;
  margin: 0 auto;
相关问题