如何垂直对齐浮动元素?

时间:2017-03-02 13:22:46

标签: html css vertical-alignment centering

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

我知道我可以非常轻松地使用flexbox,但我需要IE 9+浏览器支持它,flexbox只有partially-supported by 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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

你可以这样做:

更改:位置,垂直对齐,显示和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>

<强> DEMO HERE