将屏幕中心的两个图像之间的文本居中

时间:2016-04-08 04:55:31

标签: html css

我在尝试拍摄一段文字,将其置于页面中心,并在其左侧和右侧有一个图像时遇到问题。

请记住,我只允许更改CSS代码进行定位。 HTML完全正确。

这里有html代码:

<div id="container">
    <div>
        <img src="../logo.png" id="header">
    </div>
    <div>
        <img src="../barbecue01.jpg" id="pic_1">
        <div id="aboutus">
            <h1>About Us</h1>
            <p>
               Our restaurant has the best barbecue that you can find at Philadelphia. 
               We have an amazing team just to serve you, your family, and your friends.
            </p>
            <h1>Try It Now!</h1> 
        </div>
        <img src="../barbecue02.jpg" id="pic_2">
    </div>
</div>

这是我的CSS

#container {
  width: 75%;
  margin: 15px auto 15px auto;
}

* {
  background-color: tan;
}

#pic_1 {
  position: absolute;
  display: inline-block;
  float: left;
}

#pic_2 {
  position: absolute;
  display: inline-block;
  float: right;
}

#aboutus {
  position: relative;
  text-align: center;
  height: 275px;
  width: 200px;
  color: white;
  display: inline-block;
  left: 275px;
}

div {
  border: solid 2px black;
}

我遇到的问题是第一张图片在正确的位置,我只是想让第二张图片在右侧。出于某种原因,它只是没有它。文本应该居中。

任何帮助都会非常感激

2 个答案:

答案 0 :(得分:0)

我建议您使用flex而不是float,因为float实际上不适用于布局。

Stack snippet

&#13;
&#13;
#container {
  width: 75%;
  margin: 15px auto;
}

* {
  background-color: tan;
}

#container > div:nth-child(2) {
  display: flex;
}

#pic_1 {
  flex: 1;
}

#pic_2 {
  flex: 1;
}

#aboutus {
  flex: 1 1 200px;
  text-align: center;
  height: 275px;
  color: white;
}

div {
  border: solid 2px black;
}
&#13;
<div id="container">
  <!-- ADD NEW CODE HERE... -->

  <div>
    <img src="../logo.png" id="header">
  </div>


  <div>
    <img src="../barbecue01.jpg" id="pic_1">
    <div id="aboutus">
      <h1>About Us</h1>
      <p>Our restaurant has the best barbecue that you can find at Philadelphia. We have an amazing team just to serve you, your family, and your friends.  </p>
      <h1>Try It Now!</h1> 
    </div>
    <img src="../barbecue02.jpg" id="pic_2">
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

让它们显示块并向左浮动

{{1}}