Basically I have a div with an image and a div inside of it with content. I need to make the background image be the size of whatever the content is, so grow/shrink accordingly.
Right now, it only shows the full size of the image which results in a lot of space being left where there is no content.
Here is my current styling, it is required I used flexbox.
.section {
height: 100%;
.section_image {
position: relative;
height: 100%;
display: flex;
flex: 1 1 100%;
img {
object-fit: cover
}
}
.content {
position: absolute;
display: flex;
flex: 1 1 100%;
}
}
<div className="section>
<div className="section_image">
<img src={Image} alt="" />
</div>
<div className="content">
<h1>Title</h1>
<p>Body Text</p>
</div>
</div>
.section {
height: 100%;
}
.section_wrapper {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
overflow: hidden
}
.section_image {
position: relative;
height: 100%;
display: flex;
flex: 1 1 100%;
}
img {
object-fit: cover;
width: 100%;
}
.content {
position: relative;
display: flex;
flex: 1 1 100%;
overflow: hidden;
width: fit-content
}
.content__elements {
display: flex;
z-index: 2;
color: white
}
<div class="section">
<div class="content">
<div class="section_wrapper">
<div class="section_image">
<img src="https://www.gardendesign.com/pictures/images/675x529Max/site_3/cosmos-pink-flower-pixabay_11886.jpg" alt="" />
</div>
</div>
<div class="content__elements">
<h1>Title</h1>
<p>Body Text</p>
</div>
</div>
</div>
Related
My HTML is:
<div class="event-presentation-featured">
<div class="event-presentation-featued__image-container">
<a href="...">
<img class="event-presentation-featued__image" src="...">
</a>
</div>
<div class="event-presentation-featured__content">
...
</div>
<div class="event-presentation-featured__actions">
...
</div>
</div>
I want the image to NOT expand the height of the containing div. Presently it looks like:
The relevant classes are:
.event-presentation-featured {
align-items: stretch;
background-color: var(--color-white);
display: flex;
width: 100%;
}
.event-presentation-featued__image-container {
flex-shrink: 1;
max-width: 457px;
position: relative;
}
.event-presentation-featued__image {
top: 0;
left: 0;
display: block;
object-fit: cover;
object-position: center;
width: 100%;
height: 100%;
}
I am trying to have a header/footer and a scrollable div#middle. Within the scrollable div, another (inner) footer div#middle-bottom should be placed at the bottom of div#used-for-swipe-animation and always be visible.
This is my current code using flex-container (using flex is not a requirement):
html, body {
margin: 0
}
.wrapper {
display: flex;
flex-direction: column;
height: 100vh;
width: 100%;
overflow-y: auto;
}
.top {
background: lightgreen;
}
.middle {
flex-grow: 1;
overflow: auto;
}
.middle-bottom {
background: red;
}
.bottom {
background: lightblue;
}
<body>
<div class="wrapper">
<div class="top">top</div>
<div id="used-for-swipe-animation">
<div class="middle">
middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle
</div>
<div class="middle-bottom">
middle-bottom<br>middle-bottom<br>middle-bottom<br>middle-bottom<br>
</div>
</div>
<div class="bottom">bottom</div>
</div>
</body>
Problem: Without the div#used-for-swipe-animation it works as expected. However, as the id suggests, div#used-for-swipe-animation is needed to perform some animation.
Nice-to-Have: Is it possible to have the scrollbar of div#middle to be displayed over full height of div#wrapper?
As per I understand your Que, you need fixed Header & Footer and scroll div in middle
html,
body {
margin: 0
}
.wrapper {
display: flex;
flex-direction: column;
height: 100vh;
width: 100%;
overflow-y: auto;
}
.top {
position: fixed;
top: 0;
width: 100%;
height: 30px;
background: lightgreen;
}
.main_center {
margin: 30px 0;
}
.middle {
flex-grow: 1;
overflow: auto;
}
.middle-bottom {
background: red;
}
.bottom {
position: fixed;
bottom: 0;
width: 100%;
height: 30px;
background: lightblue;
}
<body>
<div class="wrapper">
<div class="top">top</div>
<div id="used-for-swipe-animation" class="main_center">
<div class="middle">
middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle<br>middle
</div>
<div class="middle-bottom">
middle-bottom<br>middle-bottom<br>middle-bottom<br>middle-bottom<br>
</div>
</div>
<div class="bottom">bottom</div>
</div>
</body>
Replay can lead perfection :)
MWE:
https://jsfiddle.net/zjgc9dfx/
Suppose I've got this layout:
<div class="slider">
<span>button here</span>
<div class="slide">
<img src="https://loremflickr.com/1000/1000" alt="Bacn">
</div>
<span>button here</span>
</div>
The slider is a flex container. The two buttons are inline-block, and I want the slide img to be limited to the viewport's height without extra space.
How can I achieve this?
* {
margin: 0;
padding: 0;
}
.slider {
display: flex;
justify-content: center;
align-items: center;
background-color: red;
}
.slider .slide img {
width: 100%;
max-height: 100%;
}
<div class="slider">
<span>asd</span>
<div class="slide">
<img src="https://loremflickr.com/1000/1000" alt="Bacn">
</div>
<span>asd</span>
</div>
Maybe something like this will work:
.slider {
display: flex;
align-items: center;
height: 100vh;
background-color: red;
}
.slider>span {
flex-shrink: 0; /* disables shrinking feature */
}
.slider>.slide {
flex: 1; /* consume all free space */
height: 100%;
}
.slider .slide img {
width: 100%;
max-height: 100%;
object-fit: cover;
vertical-align: bottom; /* https://stackoverflow.com/a/31445364/3597276 */
}
* {
margin: 0;
padding: 0;
}
<div class="slider">
<span>button here</span>
<div class="slide">
<img src="https://loremflickr.com/1000/1000" alt="Bacn">
</div>
<span>button here</span>
</div>
jsFiddle demo
I have tried using overflow: hidden; for each element but it does not seem to be doing anything
The html looks like this, the display is to have the 2 divs side by side (stacks ontop for smaller screens). The left side div will also be on top of the right side div. I have a screen shot and fiddle too.
.sec {
background-color: red;
min-height: 100vh;
overflow: hidden;
}
.sec2 {
background-color: blue;
min-height: 100vh;
overflow: hidden;
}
.img1 {
max-width: 100%;
position: absolute;
}
.img1 {
z-index: 1;
}
.leftCol {
z-index: 10;
width: 50%;
}
.info-row {
display: flex;
flex-direction: row;
margin-left: 10vw;
margin-right: 200px;
}
.rightCol {
width: 50%;
}
<section class="sec">
<div class="info-row">
<div class="leftCol info-column">
<h1>haheaheh</h1>
<p>teataetetat</p>
</div>
<div class='rightCol info-column'>
<img class="img1" src='https://kasonbloom.files.wordpress.com/2017/08/lamb-2.jpg' />
</div>
</div>
</section>
<section class="sec2">
<div class="info-row">
<div class="leftCol info-column">
<h1>asdfasdfasdf</h1>
<p>basfbasdfbasdfba</p>
</div>
<div class='rightCol info-column'>
</div>
</div>
</section>
https://jsfiddle.net/gtrnrd9r/2/ keep result view at a point where the image breaks through the section
Add position:relative; to your outer section .sec and it will work fine.
.sec {
background-color: red;
min-height: 100vh;
overflow: hidden;
position:relative;
}
.sec2 {
background-color: blue;
min-height: 100vh;
overflow: hidden;
}
.img1 {
max-width: 100%;
position: absolute;
}
.img1 {
z-index: 1;
}
.leftCol {
z-index: 10;
width: 50%;
}
.info-row {
display: flex;
flex-direction: row;
margin-left: 10vw;
margin-right: 200px;
}
.rightCol {
width: 50%;
}
<section class="sec">
<div class="info-row">
<div class="leftCol info-column">
<h1>haheaheh</h1>
<p>teataetetat</p>
</div>
<div class='rightCol info-column'>
<img class="img1" src='https://kasonbloom.files.wordpress.com/2017/08/lamb-2.jpg' />
</div>
</div>
</section>
<section class="sec2">
<div class="info-row">
<div class="leftCol info-column">
<h1>asdfasdfasdf</h1>
<p>basfbasdfbasdfba</p>
</div>
<div class='rightCol info-column'>
</div>
</div>
</section>
.rightCol needs his own width and height if i'm not mistaken. overflow doesn't work with the parent element.
Here is my demo to show what I mean:
* {
box-sizing: border-box;
}
body, html {
height: 100%;
width: 100%;
}
body {
background: #333;
margin: 0;
padding: 0;
}
.content {
display: flex;
height: 100%;
padding: 20px;
}
.panel {
flex: 0 0 200px;
display: flex;
margin-left: 20px;
}
.widget {
background: white;
width: 100%;
height: 100%;
}
.videoContainer {
display: flex;
flex: 1 1 100%;
flex-wrap: wrap;
}
.video {
height: 50%;
width: 50%;
padding: 2px;
position: relative;
}
.videoCover {
position: absolute;
background: red;
opacity: 0.4;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
img {
width: 100%;
height: 100%;
object-fit: contain; /* Default for videos */
}
<div class="content">
<div class="videoContainer">
<div class="video">
<div class="videoCover"></div>
<img width="600" height="400" src="http://thatgrapejuice.net/wp-content/uploads/2016/03/rihanna-thatgrapejuice-mariah-carey-billboard-600x400.jpg">
</div>
<div class="video">
<img width="600" height="400" src="http://www.radioandmusic.com/sites/www.radioandmusic.com/files/images/entertainment/2015/09/28/rihanna-%2812%29.jpg">
</div>
<div class="video">
<img width="600" height="400" src="http://m.buro247.com.au/thumb/600x960_0/images/640-rihanna-charity1.jpg">
</div>
<div class="video">
<img width="600" height="400" src="http://hjb.hu/wp-content/uploads/2014/02/rihanna2.jpg">
</div>
</div>
<div class="panel">
<div class="widget"></div>
</div>
</div>
Resizing the browser vertically or horizontally, the problem can be seen clearly. So basically I would like to place the red cover exactly onto the images. So I assume I need a div, which is exactly the same size as the image.
It seems object fit does its job well, but because of this, I cannot place over the red div.
Is it can be done pure css? How should I modify the dom and the css?
Thank you very much!
Clarifying what I would like to achive is:
The best I can come up with is this, where I wrapped all in a cover and then used a pseudo for the red cover.
I also added a few media queries, as one need to control the width of the video's, so they don't become wider than their ratio height, and if, one make them less wide.
You might need to elaborate with these settings, maybe one or two more queries will be needed, but I think one can do this pretty good, and be able to avoid the need of script.
And by cutting out object-fit, you also get a better cross browser solution.
As a side note, here is an answer I participated in, which shows what it takes to achieve what you want: Scale element proportional to Background Cover/Contain. It has a script and a CSS version
* {
box-sizing: border-box;
}
body, html {
height: 100%;
width: 100%;
}
body {
background: #333;
margin: 0;
padding: 0;
}
.content {
display: flex;
height: 100%;
padding: 20px;
}
.panel {
flex: 0 0 200px;
display: flex;
margin-left: 20px;
}
.widget {
background: white;
width: 100%;
height: 100%;
}
.videoContainer {
display: flex;
flex: 1 1 100%;
flex-wrap: wrap;
align-items: center;
}
.video {
height: 50%;
width: 50%;
padding: 2px;
box-sizing: border-box;
display: flex;
flex-direction: column;
justify-content: center;
}
.videoCover {
position: relative;
overflow: hidden;
}
.video:nth-child(1) .videoCover::after {
content: '';
position: absolute;
background: red;
opacity: 0.4;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
img {
display: block;
width: 100%;
height: auto;
}
#media (min-aspect-ratio: 600/400) {
.video:nth-child(1) .videoCover::after {
width: 80%;
}
img {
width: 80%;
}
}
#media (min-aspect-ratio: 800/400) {
.video:nth-child(1) .videoCover::after {
width: 60%;
}
img {
width: 60%;
}
}
#media (min-aspect-ratio: 1000/400) {
.video:nth-child(1) .videoCover::after {
width: 40%;
}
img {
width: 40%;
}
}
<div class="content">
<div class="videoContainer">
<div class="video">
<div class="videoCover">
<img width="600" height="400" src="http://thatgrapejuice.net/wp-content/uploads/2016/03/rihanna-thatgrapejuice-mariah-carey-billboard-600x400.jpg">
</div>
</div>
<div class="video">
<div class="videoCover">
<img width="600" height="400" src="http://www.radioandmusic.com/sites/www.radioandmusic.com/files/images/entertainment/2015/09/28/rihanna-%2812%29.jpg">
</div>
</div>
<div class="video">
<div class="videoCover">
<img width="600" height="400" src="http://m.buro247.com.au/thumb/600x960_0/images/640-rihanna-charity1.jpg">
</div>
</div>
<div class="video">
<div class="videoCover">
<img width="600" height="400" src="http://hjb.hu/wp-content/uploads/2014/02/rihanna2.jpg">
</div>
</div>
</div>
<div class="panel">
<div class="widget"></div>
</div>
</div>