简体   繁体   English

将图像放在较小容器内的页面宽度的一半

[英]Making an image half the page width inside of a smaller container

I'm trying to set the photo to 50vw (half the page width), always left-aligned with the screen, using only CSS -- no restructuring the HTML. 我正在尝试将照片设置为50vw(页面宽度的一半),始终仅使用CSS左对齐屏幕,而不用重组HTML。 I want the other half of the flexbox to remain centered inside the container. 我希望flexbox的另一半在容器内保持居中。

Essentially, if your screen is 1600px wide, Nick Cage should begin at 0px on the left and stretch to 800px (the center), and then the .hero-text should begin at 800px and end at 1400px, as it stays confined to the 1200px width container. 本质上,如果您的屏幕为1600px宽,则尼克·凯奇(Nick Cage)应该从左侧的0px开始并延伸到800px(中央),然后.hero-text应该以800px开始并以1400px结束,因为它会限制在1200px宽度的容器。

I've commented out a line that I think is key, but I can't get the math right. 我评论了一条我认为是关键的线,但是我无法正确地进行数学计算。 I just need to offset the image to always be on the left edge of the screen no matter the resolution. 无论分辨率如何,我只需要将图像偏移到始终位于屏幕的左侧即可。

 .container { width: 100%; border: 1px solid blue; height: 500px; } .inner { width: 1200px; height: 100%; margin: 0 auto; border: 1px solid red; } .hero { width: 100%; height: 100%; border: 1px solid green; display: flex; flex-direction: row; align-items: center; justify-content: center; text-align: center; } .hero > div { width: 50%; } .hero-image { width: 50vw; height: 100%; /* margin-left: calc((100vw - 1200px) * -1); */ background: url('https://www.placecage.com/c/500/400'); background-repeat: no-repeat; background-size: cover; } 
 <div class="container"> <div class="inner"> <div class="hero"> <div class="hero-image"> </div> <div class="hero-text">Here is Nick Cage, everybody! </div> </div> </div> </div> 

https://jsfiddle.net/cqu6xf90/1/ https://jsfiddle.net/cqu6xf90/1/

No need to use any calculation. 无需使用任何计算。

Use relative & absolute positioning to achieve this. 使用相对和绝对定位可实现此目的。 See here for additional reading. 请参阅此处以获取其他阅读内容。 https://css-tricks.com/absolute-positioning-inside-relative-positioning/ https://css-tricks.com/absolute-positioning-inside-relative-positioning/

You can position any child element "absolutely" with a relative parent. 您可以将任何子元素“绝对”定位为相对的父元素。

In this case, setting the container element to have position: relative; 在这种情况下,将容器元素设置为具有以下position: relative; & setting the hero image to have position: absolute; left: 0; 并设置英雄形象的position: absolute; left: 0; position: absolute; left: 0; Will in fact set place it on the left hand side of the element. 实际上将其放置在元素的左侧。

Full Css 全CSS

.container {
  width: 100%;
  border: 1px solid blue;
  height: 500px;
/*  Relative Parent  */
  position: relative;
}

.inner {
  width: 1200px;
  height: 100%;
  margin: 0 auto;
  border: 1px solid red;
}

.hero {
  width: 100%;
  height: 100%;
  border: 1px solid green;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  text-align: center;
}
.hero > div {
  width: 50%;
}
.hero-text {
/*  Margin to place the text div  */
  margin-left: 50%;
}
.hero-image {
/*  Positions the hero image where you want it   */
  position: absolute;
  left: 0;
  width: 50vw;
  height: 100%;
  /* margin-left: calc((100vw - 1200px) * -1); */
  background: url('https://www.placecage.com/c/500/400');
  background-repeat: no-repeat;
  background-size: cover;
}

Check out this codepen http://codepen.io/anon/pen/NbwVmy to see it working. 请查看此codepen http://codepen.io/anon/pen/NbwVmy以查看其工作原理。

Hope this is what you're looking for! 希望这就是您要寻找的! ( without messing with the markup of course ) (当然不要弄乱标记)

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

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