获取带有文本的图像到页面中心

时间:2021-05-19 17:32:34

标签: html css

嗨,我正在为学校项目制作一个网站,我需要将图片居中放置在页面中间,并附上文字。现在图像和文本被推到左侧。我需要图像在页面中居中并展开,文字仍在下方。

HTML

<section class="middle">

        <div class="rowone">
            <img src="images/logofootball.png" height="200" width="200">
            <div class="text">Text</div>
        </div>
        <div class="rowone">
            <img src="images/logofootball.png" height="200" width="200">
            <div class="text">Text</div>
        </div>
        <div class="rowone">
            <img src="images/logofootball.png" height="200" width="200">
            <div class="text">Text</div>
        </div>
        <div class="rowone">
            <img src="images/logofootball.png" height="200" width="200">
            <div class="text">Text</div>
        </div>
    </section>

CSS

.middle {
    position: relative;
    overflow: hidden;
}

.rowone {
    float: left;
    width: 200px;
    margin: 1% 1% 45px 1%;
}

.text {
    position: absolute;
    bottom: 0px;
    margin: 30px;
    background-color: gray;
}

.rowone img {
    width: 100%;
    display: block;
}

3 个答案:

答案 0 :(得分:0)

这应该可以解决问题。

HTML

    <section class="middle">
<center>
  <br>
  <br>
  <br>
  <br>
        <div class="rowone">
            <img src="images/logofootball.png" height="200" width="200">
            <div class="text">Text</div>
        </div>
        <div class="rowone">
            <img src="images/logofootball.png" height="200" width="200">
            <div class="text">Text</div>
        </div>
        <div class="rowone">
            <img src="images/logofootball.png" height="200" width="200">
            <div class="text">Text</div>
        </div>
        <div class="rowone">
            <img src="images/logofootball.png" height="200" width="200">
            <div class="text">Text</div>
        </div>
  </center>
    </section>

CSS

.middle {
    position: relative;
    overflow: hidden;
}

.rowone {
  padding-left: 100px;
    float: left;
    width: 200px;
    margin: 1% 1% 45px 1%;
}

.text {
    position: absolute;
    bottom: 0px;
    margin: 30px;
    background-color: gray;
}

.rowone img {
    width: 100%;
    display: block;
}

答案 1 :(得分:0)

看看下面的例子。这对您来说可能是一个舒适的解决方案。图像和文本由 .wrapperflex: 1; 基本上是说,.rowone 中的所有包装元素都应该具有相同的大小。

.middle {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  overflow: hidden;
}

.rowone {
  display: flex;
}

.wrapper {
  display: inline-block;
  text-align: center;
  max-height: 200px;
  flex: 1;
}

.text {
  background-color: gray;
}

img {
  display: inline-block;
}
<section class="middle">
  <div class="rowone">
    <div class="wrapper">
      <img src="https://via.placeholder.com/150">
      <div class="text">Text</div>
    </div>
    <div class="wrapper">
      <img src="https://via.placeholder.com/150">
      <div class="text">Text</div>
    </div>
    <div class="wrapper">
      <img src="https://via.placeholder.com/150">
      <div class="text">Text</div>
    </div>
    <div class="wrapper">
      <img src="https://via.placeholder.com/150">
      <div class="text">Text</div>
    </div>
  </div>

</section>

答案 2 :(得分:0)

我使用了 flex 来展开图像。我认为这是将一个部分分成几列的最佳方式。 以及用于居中文本的简单文本对齐中心。

JSFiddle

HTML

<section class="middle">
    <div class="rowone">
        <img src="https://via.placeholder.com/200" height="200" width="200">
        <div class="text">Text</div>
    </div>
    <div class="rowone">
        <img src="https://via.placeholder.com/200" height="200" width="200">
        <div class="text">Text</div>
    </div>
    <div class="rowone">
        <img src="https://via.placeholder.com/200" height="200" width="200">
        <div class="text">Text</div>
    </div>
    <div class="rowone">
        <img src="https://via.placeholder.com/200" height="200" width="200">
        <div class="text">Text</div>
    </div>
</section>

CSS

.middle {
    /*position: relative;
    overflow: hidden;*/
    display: flex;
    justify-content: space-between;
}

.rowone {
    /*float: left;
    width: 200px;
    margin: 1% 1% 45px 1%;*/
    text-align: center;
}

.text {
    /*position: absolute;
    bottom: 0px;
    margin: 30px;*/
    background-color: gray;
    display:inline-block;
}

.rowone img {
    width: 100%;
    display: block;
}
相关问题