简体   繁体   English

如何使用背景图像在div中垂直居中放置div

[英]How to vertically center a div within a div with a background image

I am simply trying to center my bg-text within the outer div, therefore having a picture in the background with the h1 tag in the center for all devices. 我只是试图将我的bg文本居中在外部div中,因此对于所有设备,背景中的图片带有h1标签。 I originally had this where the margin-top was a fixed pixel. 我最初在边缘顶部是固定像素的情况下使用此功能。 I need it to be a percentage so it stays in the center. 我需要将其设置为一定百分比,以使其位于中心。 However, when i replaced the margin top with a percentage, as a minimize my screen the h1 element would slide upwards. 但是,当我用百分比替换边距顶部时,作为最小化我的屏幕,h1元素将向上滑动。 I want it to stay margin-top: 50% of my background picture at all times. 我希望它始终保持在最高水平:始终是我背景图片的50%。

HTML HTML

  <div class= 'bg'>
      <div class='bg-text'>
        <h1>Text</h1>
      </div>
    </div>

CSS CSS

.

.bg {
  display: table; 
  width: 100%;
  height: 50%;
  background-image: image-url('pic.jpg');
  background-attachment: scroll;
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
}

.bg-text{
  color: #fff;
  text-align: center;
  text-shadow: 1px 1px 12px rgba(0,0,0,0.5);
  margin-top: 150px;
}

.bg-text h1{
    font-size: 50px;
    font-weight: 700;
}

Use flex property for align the inner div in center 使用flex属性将内部div对准中心

.bg {
  display: flex; 
  align-items:center;
  width: 100%;
  height: 300px;
  background-image: url('pic.jpg');
  background-attachment: scroll;
  background-color: red;
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
}

.bg-text{
  color: #fff;
  text-align: center;
  text-shadow: 1px 1px 12px rgba(0,0,0,0.5);
}

.bg-text h1{
    font-size: 50px;
    font-weight: 700;
}

have a look here https://jsfiddle.net/pteus556/ 在这里看看https://jsfiddle.net/pteus556/

Supports in browsers. 在浏览器中支持。

在此处输入图片说明

A second way to potential do that is to use transform: translateY(-50%); 另一种可能的方法是使用transform: translateY(-50%); . Check out the fiddle for a little demo: https://jsfiddle.net/y58hu6tv/2/ . 查看小提琴以获取一些演示: https : //jsfiddle.net/y58hu6tv/2/

.bg {
    width: 100%;
    height: 500px;
    background-color: rgba(0, 0, 0, .7);
}

.bg-text {
   position: relative;
   top: 50%;
   margin: 0 auto;
   transform: translateY(-50%);
   text-align: center;
   background-color: rgba(255, 255, 255, .7);
}

Again though with this technique browser support is so-so. 同样,尽管有了这种技术,浏览器的支持还是一样。

在此处输入图片说明

Reference: http://caniuse.com/#feat=transforms2d 参考: http : //caniuse.com/#feat=transforms2d

  • Create a <div> with fixed dimensions and add your background to it. 创建具有fixed dimensions<div>并为其添加背景
  • Adjust the size of the <div> according to your needs. 根据需要调整 <div> 的大小
  • Put your text inside that <div> 将您的文字放入 <div>

  • Position the text inside the <div> and give it position:absolute - in relation to its parent 将文本放在 <div> ,并赋予其position:absolute相对于其父级

  • Use the transform property as well as top / left / right / bottom 使用transform属性以及top / left / right / bottom

 .mycontent { width: 100vw; max-width: 100%; } .mytext { top: 50%; left: 50%; transform: translate(-50%, -50%); position: absolute; background: rgba(255, 255, 255, .6); border: 1px solid #777; padding: .2em; color: #000; } .topcontent { height: 100vh; width: 2560px; max-width: 100%; background: url(http://supersocl.com/wp-content/uploads/2016/09/2560X1440-Wallpaper-Elegant-6G0.jpg); background-size: cover; } .othercontent { background: #010101; padding: 30px; } body { max-width: 100%; padding: 0; margin: 0 auto; color: #999; } p { margin: 0 auto; padding: 20px; text-align: justify; text-justify: inter-word; } 
 <body> <div class="mycontent"> <div class="topcontent"> <span class="mytext">your text goes here</span> </div> <div class="othercontent"> <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.</p> <p>Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur?</p> <p>Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p> <p>At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et vo</p> </div> </div> </body> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM