我如何垂直对齐浮元素



我需要实现的目标是垂直对齐.btn p.btn img高度的中间。如果.btn p元素漂浮在右侧,这是否可以做到?我需要它们在同一条线上,但是当将其显示为display: inline block;时,它们会垂直堆叠(我不知道为什么)。

我知道我可以很容易地使用flexbox来完成,但是我需要由IE 9 浏览器支持,并且flexbox仅由IE 11。

html, body {
    margin: 0;
    padding: 0;
}
.slide1 {
    background-color: lightblue;
    height: 100vh;
    position: relative;
    text-align: center;
}
.slide1 h1 {
    display: block;
    width: 100%;
    padding: 0;
    margin: 0 auto;
    position: absolute;
    top: 4.75em;
}
.slider-btn {
    position: absolute;
    width: 100%;
    bottom: 120px;
    left: 50%;
    transform: translateX(-50%);
    text-align: center;
}
.slider-btn a {
    display: inline-block;
}
.slider-btn p {
    background-color: blue;
    color: white;
    margin: 0;
    padding: 0.5em;
    float: right;
}
.btn {
    display: inline-block;
}
/*# sourceMappingURL=main.css.map */
<!DOCTYPE html>
<html>
<head>
    <title> test </title>
    <link rel="stylesheet" type="text/css" href="main.css">
</head>
<body>
    <section class="heading">
        <div class="slide1">
            <h1> Lorem Ipsum sit dolor amet </h1>
        </div>
        <div class="slider-btn">
            <div class="btn">
                <a href="http://google.com">
                    <img src="http://placehold.it/81x81" />
                    <p> Slide no. 1 </p>
                </a>
            </div>
            <div class="btn">
                <a href="http://google.com">
                    <img src="http://placehold.it/81x81" />
                    <p> Slide no. 2 </p>
                </a>
            </div>
            <div class="btn">
                <a href="http://google.com">
                    <img src="http://placehold.it/81x81" />
                    <p> Slide no. 3 </p>
                </a>
            </div>
        </div>
    </section>
</body>
</html>

您可以做到这一点:

更改:位置,垂直空间,显示和HTML

CSS

html,
body {
  margin: 0;
  padding: 0;
}
.slide1 {
  background-color: lightblue;
  height: 100vh;
  position: relative;
  text-align: center;
}
.slide1 h1 {
  display: block;
  width: 100%;
  padding: 0;
  margin: 0 auto;
  position: relative;
  padding: 40px 0px;
}
.slider-btn {
  position: relative;
  width: 100%;

  text-align: center;
}
.slider-btn a {
  display: inline-block;
}
.slider-btn p {
  background-color: blue;
  color: white;
  margin: 0;
  padding: 0.5em;
   display: inline-block;
  vertical-align: middle;
}
.btn {
  display: inline-block;
}
.btn a img{
  display: inline-block;
  vertical-align: middle;
}

html

<section class="heading">
  <div class="slide1">
    <h1>Lorem Ipsum sit dolor amet</h1>
     <div class="slider-btn">
    <div class="btn">
      <a href="http://google.com">
        <img src="http://placehold.it/81x81" /><p>Slide no. 1</p>
      </a>
    </div>
    <div class="btn">
      <a href="http://google.com">
        <img src="http://placehold.it/81x81" /><p>Slide no. 2</p>
      </a>
    </div>
    <div class="btn">
      <a href="http://google.com">
        <img src="http://placehold.it/81x81" /><p>Slide no. 3</p>
      </a>
    </div>
  </div>
  </div>
</section>

演示此处

相关内容

  • 没有找到相关文章

最新更新