简体   繁体   English

如何将背景图像设置为仅屏幕的左半部分

[英]How to set background image to only left half of screen

My goal is to create a page where there's an image on the left-hand side of the screen and a white text area on the right.我的目标是创建一个页面,其中屏幕左侧有一个图像,右侧有一个白色文本区域。 If the text area is too long for the screen, only that area should scroll.如果文本区域对于屏幕来说太长,则只有该区域应该滚动。 I've been researching this for a bit and can't find any examples on how to do so.我已经对此进行了一段时间的研究,但找不到任何有关如何执行此操作的示例。

I tried a few different things with CSS grid which doesn't seem to work well: https://codepen.io/aysong/pen/JjXoebx?editors=1100我用 CSS 网格尝试了一些不同的东西,似乎效果不佳: https://codepen.io/aysong/pen/JjXoebx?editors=1100

The issue with this seems to be that the height of the column is dependent on the这个问题似乎是柱的高度取决于

p class=item2

I also tried just using CSS and fiddling with background-size.我还尝试使用 CSS 并摆弄背景大小。 But without setting the height (ex below), the image doesn't appear.但是如果不设置高度(例如下面),图像就不会出现。

height:1000px;

https://codepen.io/aysong/pen/poyvQEW https://codepen.io/aysong/pen/poyvQEW

I'm using this site as inspiration where you can see there's an image on the left and text on the right.我正在使用这个网站作为灵感,您可以在其中看到左侧的图像和右侧的文本。 If there were enough text on the right, I would only want that side to scroll and keep the left side as is, which I know is doable with the below code, I just haven't gotten the background image working yet.如果右侧有足够的文本,我只希望那一侧滚动并保持左侧不变,我知道可以使用下面的代码,我只是还没有让背景图像工作。

overflow: auto;

Here is a method using CSS Flexbox: https://codepen.io/xenvi/pen/JjXoeWz这是使用 CSS Flexbox 的方法: https://codepen.io/xenvi/pen/JjXoeWz

What I did was make the background box and the text box separate divs and wrapped them both in a parent container.我所做的是将背景框和文本框分开,并将它们都包装在父容器中。 Adding 'display: flex' to the parent container automatically wraps children in a row format (so our 2 divs will now be next to each other).向父容器添加 'display: flex' 会自动以行格式包装子容器(因此我们的 2 个 div 现在将彼此相邻)。 I also defined the height on the parent div to cover the full screen with '100vh'.我还定义了父 div 的高度,以使用“100vh”覆盖全屏。

For the children, to make each box take up half the page, I added "flex: 1" to both divs.对于孩子们,为了使每个框占据页面的一半,我在两个 div 中都添加了“flex: 1”。 This means that both children will take up the full width of the parent container equally, or 50%.这意味着两个孩子将平等地占据父容器的整个宽度,即 50%。 You can look up more about flexbox classes for reference.您可以查找有关 flexbox 类的更多信息以供参考。

Finally, add overflow: auto to your text div so only the text box will scroll if there is an overflow of content.最后,将 overflow: auto 添加到您的文本 div 中,这样如果内容溢出,则只有文本框会滚动。

 body { box-sizing: border-box; padding: 0; margin: 0; }.container { display: flex; height: 100vh; }.bg-image { /* The image used */ background-image: url("https://mdbootstrap.com/img/Photos/Horizontal/Nature/full page/img(11).jpg"); /* Center and scale the image nicely */ background-repeat: no-repeat; background-position: left top; background-size: 100% 100%; flex: 1; height: 100%; }.content { flex: 1; height: 100%; overflow: auto; }
 <section class="container"> <div class="bg-image"> </div> <div class="content"> <p>This example creates a full page background image. Try to resize the browser window to see how it always will cover the full screen (when scroled to top), and that it scales nicely on all screen sizes.</p> <p>This example creates a full page background image. Try to resize the browser window to see how it always will cover the full screen (when scroled to top), and that it scales nicely on all screen sizes.</p> <p>This example creates a full page background image. Try to resize the browser window to see how it always will cover the full screen (when scroled to top), and that it scales nicely on all screen sizes.</p> <p>This example creates a full page background image. Try to resize the browser window to see how it always will cover the full screen (when scroled to top), and that it scales nicely on all screen sizes.</p> <p>This example creates a full page background image. Try to resize the browser window to see how it always will cover the full screen (when scroled to top), and that it scales nicely on all screen sizes.</p> <p>This example creates a full page background image. Try to resize the browser window to see how it always will cover the full screen (when scroled to top), and that it scales nicely on all screen sizes.</p> <p>This example creates a full page background image. Try to resize the browser window to see how it always will cover the full screen (when scroled to top), and that it scales nicely on all screen sizes.</p> <p>This example creates a full page background image. Try to resize the browser window to see how it always will cover the full screen (when scroled to top), and that it scales nicely on all screen sizes.</p> </div> </section>

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

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