将图片与 div 完美地并排放置

3

如何在不同的屏幕尺寸下完美地放置图片?

img

.background-login {
  background: url(../img/login.png) no-repeat;
  background-size: 100%;
  z-index: -2;
  position: absolute;
  top: 8%;
  min-height: 400px;
  right: 2%;
  height: 100%;
}

.background-login-create {
  background: url(../img/login.png) no-repeat;
  background-size: 100%;
  z-index: -2;
  position: absolute;
  top: 17%;
  min-height: 400px;
  right: 2%;
  height: 100%;
}

.background-login-create-profile {
  background: url(../img/login.png) no-repeat;
  background-size: 100%;
  z-index: -2;
  position: absolute;
  top: 13%;
  min-height: 400px;
  right: 2%;
  height: 100%;
}

.sellfie-start-text {
  font-size: 15px;
  padding: 145px;
  text-align: center;
  font-weight: 200;
  position: relative;
  top: 8%;
  vertical-align: middle;
  color: #FFF;
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container div-login">
  <div class="row">
    <div class="col-sm-5 col-md-offset-1">
      <div class="center">
        <h2 class="login-title"></h2>
        <h4></h4>
        <p class=""></p>
      </div>
      <div class="account-wall">
        <p class="profile-name">Hello!<span style="color:red">:)</span></p>
        <p class="profile-desc">We require only your mobile number for both login and signup</p>
        <br>
        <div class="form-signin">
          <div class="md-form">
            <input type="text" placeholder="Enter Mobile Number" value="" name="mobile_number" id="mobile_number" class="mobile_number form-control">
          </div>
          <button class="btn btn-main btn-continue" id="continue">
            CONTINUE</button>
          <div class="form-signin-bottom">By clicking on Continue, you agree to our <a href="">Privacy Policy</a>
            & <a href="">Terms</a>
          </div>
        </div>
      </div>
    </div>
    <div class="col-sm-6 background-login">
      <div class="sellfie-logo">
        <img src="img/sellfie_white.png"/>
      </div>    
      <div class="sellfie-start-text">
        Sellfie is the easiest way to sell online and collect payments for your products & services on social networks
        and online channels.
      </div>    
    </div>  
  </div>
</div>

如果左侧容器更大,我希望右侧的图像与左侧容器具有相同的高度,该怎么办?
   .background-login {
    background: url(../img/login.png) no-repeat;
    background-size: cover;
    z-index: -2;
    position: absolute;
    margin-top: 50px;
    right: 2%;
    min-height:400px;
    height: 100%;

}

enter image description here


你需要这样的东西吗:链接如下:https://css-tricks.com/perfect-full-page-background-image/ - Razia sultana
我制作了这个简单的 fiddle,它使用相对和绝对定位。您的容器应该是相对的,左右内容应该是绝对的。尝试在我的 fiddle 中更改 wrapper 的宽度和高度,您会发现内容也会随之改变。有用的示例 - Roy123
1
我很困惑为什么这里需要任何绝对的东西? - Tom
列等高的解决方案:https://dev59.com/tWIj5IYBdhLWcg3w_5vl - Nhan
2个回答

1
我已经在background-login元素中添加了wrapper,并添加了一些CSS使其居中对齐。
.background-login的CSS中,设置background-size: cover(这将使图像覆盖整个元素)。
我还删除了topright值,在Bootstrap列上不要设置位置值,因为它们会破坏布局。

body {
  background-color: #ddd !important;
}

.form-left,
.background-login {
  height: 400px;
}

.form-left {
  background-color: white;
}

.background-login {
  position: relative;
  background: url(https://unsplash.it/600/400) no-repeat;
  background-position: center center;
  background-size: cover;
}

.background-login .wrapper {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  padding: 30px;
  text-align: center;
}

.sellfie-start-text {
  font-size: 15px;
  text-align: center;
  font-weight: 200;
  position: relative;
  top: 8%;
  vertical-align: middle;
  color: #FFF;
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container div-login">
  <div class="row">
    <div class="col-sm-5 col-md-offset-1 form-left">
      <div class="center">
        <h2 class="login-title"></h2>
        <h4></h4>
        <p class=""></p>
      </div>
      <div class="account-wall">
        <p class="profile-name">Hello!<span style="color:red">:)</span></p>
        <p class="profile-desc">We require only your mobile number for both login and signup</p>
        <br>
        <div class="form-signin">
          <div class="md-form">
            <input type="text" placeholder="Enter Mobile Number" value="" name="mobile_number" id="mobile_number" class="mobile_number form-control">
          </div>
          <button class="btn btn-main btn-continue" id="continue">
            CONTINUE</button>
          <div class="form-signin-bottom">By clicking on Continue, you agree to our <a href="">Privacy Policy</a>
            & <a href="">Terms</a>
          </div>
        </div>
      </div>
    </div>
    <div class="col-sm-6 background-login">
      <div class="wrapper">
        <div class="sellfie-logo">
          <img src="img/sellfie_white.png"/>
        </div>    
        <div class="sellfie-start-text">
          Sellfie is the easiest way to sell online and collect payments for your products & services on social networks
          and online channels.
        </div>
      </div>
    </div>
  </div>
</div>


如何使左侧容器和右侧图片的高度相同? - vini

1
#details { 
    display: inline-block; 
    vertical-align:middle;
    border:solid black 1px; 
    width: 300px; 
 } 
.photo { 
   display: inline-block; 
   vertical-align:middle;
   width: 300px; 
   height: 300px; 
   border: 1px solid #d1c7ac; 
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接