Prevent hero image from stretching on larger screens - html

I need help preventing my hero image from stretching on larger screens. It's probably a simple fix but I can't seem to make it happen.
It works fine when I make the screen smaller and on mobile as it reduces with flexbox.
Here's a screenshot of the image:
.hero {
width: 100%;
height: 360px;
background-color: #222222;
display: flex;
justify-content: center;
align-items: center;
position: relative;
z-index: 1000;
/* padding: 20px 50px; */
padding-bottom: 10px;
gap: 50px;
/* background-image: url('../images/elvis.jpeg'); */
/* opacity: 50%; */
}
.movie-details {
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
gap: 20px;
z-index: 1000;
padding: 20px;
}
.bg-image {
position: absolute;
width: 100%;
height: 100%;
}
.banner-img {
opacity: 40%;
height: 100%;
width: 100%;
object-fit: cover;
aspect-ratio: 16 / 9;
}
h1 {
padding-top: 20px;
font-size: 30px;
}
p {
font-size: 14px;
}
<div class="hero">
<div class="bg-image">
<img src="https://via.placeholder.com/800" class="banner-img" />
</div>
<div class="movie-details">
<h1>Movie Title</h1>
<p>Movie overview.</p>
</div>
</div>

Add object-position to .banner-image to control the image's anchor point around which it scales. I think you probably want object-position: center top; or maybe object-position: left top;
.banner-img {
opacity: 40%;
height: 100%;
width: 100%;
object-fit: cover;
aspect-ratio: 16 / 9;
object-position: center top;
}
(Original Answer)
Add a `max-width` and set horizontal margins to `auto` to center it.
.hero {
max-width: 800px;
margin: 0 auto;
}

Related

How to set image size dynamically in ReactJS?

I have structure like this:
img {
width: auto;
height: 200px;
}
.cards {
display: flex;
justify-content: center;
margin-top: 2em;
width: 80%;
flex-wrap: wrap;
}
.card {
margin: 1em;
box-shadow: 0 0 6px #000;
object-fit: cover;
}
.info {
padding: 1em;
border-top: none;
}
<div class='cards'>
<div class="card">
<img src="https://picsum.photos/id/1004/5616/3744" alt="1004" />
<div class="info">
<h3>Greg Rakozy</h3>
<div><small>https://unsplash.com/photos/SSxIGsySh8o</small></div>
</div>
</div>
</div>
on computers with long width image is rendered a little wrong.
how can I fix this so that it displays correctly, i.e. sticks to the '.card' block?
First you need to limit the width of you main container:
.cards {
display: flex;
justify-content: center;
margin-top: 2em;
width: 80%;
flex-wrap: wrap;
max-width: 1440px; /* whatever you desire */
margin-left: auto; /* center the container */
margin-right: auto; /* center the container */
}
Then each image should take 100% for it's container:
.card {
margin: 1em;
box-shadow: 0 0 6px #000;
object-fit: cover;
flex: 0 0 25%; /* each card will be 25% width */
}
img {
width: 100%;
height: 200px;
object-fit: cover;
}
Adding those to .card class
width: 100%;
height: auto;
Google how to make image responsive with css, it's not related to React.
.card {
width: 100%;
height: auto;
}
You can either put those images in a div.img-container and set the div width & height like this.
.img-container {
width: 100%;
height: // as you want;
}
and then put that image inside .img-container and set the image width to 100%.
.container {
width: 350px;
height 350px;
border: 2px solid black;
border-radius: 5px;
}
.container .img-container {
width: 100%;
height: 200px;
}
.container .img-container img {
width: 100%;
height: 100%;
}
.container .card-info {
width: 100%;
height: auto;
}
<div class="container">
<div class="img-container">
<img src="https://picsum.photos/200">
</div>
<div class="card-info">
<h5>Title</h5>
<small>Your link here</small>
</div>
</div>
and either set image width 100% and height auto.

[CSS Challenge]: Creating a resizable textbox that can stretch vertically and horizontally without warping corner elements

I am creating a resizable textbox that can stretch vertically and horizontally without warping graphical corner elements. To do so, I am using three vertical sections (top, center, bottom) and three horizontal sections (left, middle, right) within the top and bottom vertical sections. This way, the 'top-middle' and 'bottom-middle' sections can stretch horizontally and the center section can stretch vertically & horizontally, while the corner sections (top-left, top-right, bottom-left..) stay the same width and height to avoid warping.
The problem is: positioning elements so that they line up with one another. Specifically, I seem to be getting some cut-off on the right sides of my corner elements.
Here's a screenshot of the issue:
https://postimg.cc/Xp4dRDrQ
Here is the HTML:
<div className='textbox-container'>
<div className='top-block'>
<div className='left-block' />
<div className='middle-block'>
<p>
Top Block
</p>
</div>
<div className='right-block' />
</div>
<div className='center-block'>
<p>
Center Block
</p>
</div>
<div className='bottom-block'>
<div className='left-block' />
<div className='middle-block'>
<p>
Bottom Block
</p>
</div>
<div className='right-block' />
</div>
</div>
And here is the CSS I am using:
.textbox-container {
display: flex;
flex-flow: column;
align-items: center;
width: 75%;
margin: 0 auto;
}
.top-block {
display: inline-flex;
flex-direction: row;
float: left;
width: 100%;
height: 80px;
margin:0 auto;
margin-top: 10vh;
}
.top-block .left-block {
background-image: url('/src/images/Textbox-Top-Left.png');
background-repeat: no-repeat;
background-size: cover;
width: 150px;
height: 80px;
margin:0 auto;
}
.top-block .middle-block {
width: 100%;
height: 80px;
line-height: 80px;
text-align: right;
vertical-align: middle;
background-image: url('/src/images/Textbox-Top-Middle.png');
color: #fff;
margin:0 auto;
}
.top-block .right-block {
float:right;
background-image: url('/src/images/Textbox-Top-Right.png');
background-repeat: no-repeat;
background-size: cover;
width: 150px;
height: 80px;
margin:0 auto;
}
.center-block {
display: flex;
flex-flow: column;
align-items: center;
justify-content: center;
width: 100%;
height: 800px;
background-image: url('/src/images/Textbox-Center.png');
background-repeat: repeat-y;
background-size: contain;
margin: 0;
}
.bottom-block {
display: inline-flex;
flex-direction: row;
float: left;
width: 100%;
height: 80px;
margin-bottom: 20vh;
}
/* Close but no cigar with magic numbers in bot sections: */
.bottom-block .left-block {
width: 220px;
height: 80px;
background-image: url('/src/images/Textbox-Bottom-Left.png')
}
.bottom-block .middle-block {
width: 100%;
line-height: 80px;
text-align: right;
padding-right: 5vw;
vertical-align: middle;
height: 80px;
background-image: url('/src/images/Textbox-Bottom-Middle.png');
color: #fff;
margin: none;
}
.bottom-block .right-block {
float:right;
width: 220px;
height: 80px;
background-image: url('/src/images/Textbox-Bottom-Right.png')
}
Fiddle (currently not working):
https://jsfiddle.net/edmundw/6xku4qwa/6/
Fiddle collaborate invite:
https://jsfiddle.net/edmundw/6xku4qwa/4/#&togetherjs=4VMz2rGNAo
Any solutions would be greatly appreciated as I am stumped.
Many thanks for reading this far,
Betty.
Hm, I got something working by using actual img elements (which have an inherent width and height) for the individual sections, along with flexbox (specifically flex-basis, flex-grow and flex-shrink).
The only problem I can see is that the center element's background's borders are blurry. Not sure how to fix that, but other than that, it works. No border cutoff.
* {
padding: 0px;
margin: 0px;
box-sizing: border-box;
}
.textbox-container {
width: min-content;
height: auto;
overflow: hidden;
display: flex;
flex-direction: column;
flex-wrap: nowrap;
justify-content: flex-start;
align-items: center;
}
.textbox-vertical-block {
width: 100%;
max-width: 100%;
height: 80px;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: flex-start;
align-items: center;
}
.textbox-vertical-block>* {
width: auto;
height: 100%;
object-fit: fill;
}
.textbox-vertical-block>*:nth-child(2) {
flex-basis: auto;
flex-grow: 1;
flex-shrink: 0;
}
.textbox-center-block {
height: auto;
align-items: stretch;
background-image: url(https://i.postimg.cc/d3p7Nt38/Textbox-Center.png);
background-size: 100% 100%;
background-repeat: repeat;
}
.textbox-center-block.textbox-vertical-block>* {
height: initial;
}
.textbox-center-block>.textbox-block {
width: 6px;
}
.textbox-center-block>textarea {
width: auto;
height: auto;
flex-basis: auto;
flex-grow: 1;
flex-shrink: 1;
margin: 16px;
padding: 16px;
}
<div class="textbox-container">
<div class="textbox-vertical-block">
<img class="textbox-block" src="https://i.postimg.cc/jDn9G5wH/Textbox-Top-Left.png" />
<img class="textbox-block" src="https://i.postimg.cc/qtfY0Ty9/Textbox-Top-Middle.png" />
<img class="textbox-block" src="https://i.postimg.cc/8FhYz0bs/Textbox-Top-Right.png" />
</div>
<div class="textbox-vertical-block textbox-center-block">
<img class="textbox-block" src="https://i.postimg.cc/xcrz8TS0/Textbox-Center-Right.png" />
<textarea></textarea>
<img class="textbox-block" src="https://i.postimg.cc/xcrz8TS0/Textbox-Center-Right.png" />
</div>
<div class="textbox-vertical-block">
<img class="textbox-block" src="https://i.postimg.cc/kD70BqzC/Textbox-Bottom-Left.png" />
<img class="textbox-block" src="https://i.postimg.cc/CdJW89pq/Textbox-Bottom-Middle.png" />
<img class="textbox-block" src="https://i.postimg.cc/dL1gjnJS/Textbox-Bottom-Right.png" />
</div>
</div>
EDIT #1: Removed the blur by using the same method used in the top and bottom sections (extra border and center images (the center image is 1x1 though, so could be easily replaced by background-color)). More flexbox foolery too

Vertically align image inside a sticky div

I know this is a question that has been answered before but for some reason, I can't get anything to work. I have an image inside a div that is sticky. The image is currently aligned in the center horizontally but I can't get it to align in the center vertically. The image is stuck to the top of the div.
This is my HTML code:
<section>
<div class="stickyImg img-1">
<img src="MainImgCrop.jpeg" class="stickyImg">
</div>
</section>
And this is my CSS code:
section {
display: flex;
flex-direction: row;
}
section div.stickyImg {
height: 92vh;
background-color: purple;
background-size: cover;
background-position: center;
position: sticky;
position: -webkit-sticky;
top: 8vh;
width: 50vw;
margin: 0;
}
.stickyImg {
width: 90%;
height: auto;
display: block;
margin-right: auto;
margin-left: auto;
margin: auto;
vertical-align: middle;
top: 50%;
}
Any help would be appreciated, thank you!
section {
display: flex;
flex-direction: row;
}
section div.stickyImg {
height: 92vh;
background-color: purple;
background-size: cover;
background-position: center;
position: sticky;
position: -webkit-sticky;
top: 8vh;
width: 50vw;
margin: 0;
display:flex;
flex-direction:column;
justify-content:center;
align-items:center;
}
.stickyImg {
width: 90%;
height: auto;
display: block;
margin-right: auto;
margin-left: auto;
margin: auto;
vertical-align: middle;
top: 50%;
}
<section>
<div class="stickyImg img-1">
<img src="MainImgCrop.jpeg" class="stickyImg">
</div>
</section>

images and buttons are not horizontally aligned

I have met some problems while doing a image-viewer project. The problem is that my buttons and the image are not following justify-content property, which they don't distributed equally inside my div block, how could it be solved? Also the image is not centered as the title does despite I set the align item property. I dow know how to fix that. I've searched over the website for solutions but none of them seems working.
Could anyone help me, please? Thanks in advance.
Here are the html and css code:
<div class="image-viewer__container">
<div class="image-viewer__title">Image Viewer</div>
<div class="image-viewer__main">
<div class="image-viewer__button"><img src="./images/back.png" id="previous" /></div>
<div class="image-viewer__display" style="background-image: url(./images/loading.gif);background-repeat: no-repeat; background-position: center;">
<img src="https://scontent.ftpe7-2.fna.fbcdn.net/v/t1.0-0/p640x640/119893827_3212042898922322_5684339818610522875_o.jpg?_nc_cat=104&_nc_sid=730e14&_nc_ohc=fGG3wRqLaLEAX8MrIY-&_nc_ht=scontent.ftpe7-2.fna&tp=6&oh=36c5e163223a1e8abca79a2b3892c915&oe=5F976AFF" id="display">
<div class="image-viewer__display-source-wrapper">
<span><a href="https://scontent.ftpe7-2.fna.fbcdn.net/v/t1.0-0/p640x640/119893827_3212042898922322_5684339818610522875_o.jpg?_nc_cat=104&_nc_sid=730e14&_nc_ohc=fGG3wRqLaLEAX8MrIY-&_nc_ht=scontent.ftpe7-2.fna&tp=6&oh=36c5e163223a1e8abca79a2b3892c915&oe=5F976AFF" target="_blank">
https://scontent.ftpe7-2.fna.fbcdn.net/v/t1.0-0/p640x640/119893827_3212042898922322_5684339818610522875_o.jpg?_nc_cat=104&_nc_sid=730e14&_nc_ohc=fGG3wRqLaLEAX8MrIY-&_nc_ht=scontent.ftpe7-2.fna&tp=6&oh=36c5e163223a1e8abca79a2b3892c915&oe=5F976AFF</a>
</span>
</div>
</div>
<div class="image-viewer__button"><img src="./images/next.png" id="next" /></div>
</div>
</div>
.image-viewer__container {
height: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-evenly;
}
.image-viewer__title {
font-size: 5rem;
font-weight: 600;
color: #615dec;
margin: 0;
margin-top: 2rem;
}
.image-viewer__main {
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
margin: auto;
}
.image-viewer__button {
display: inline;
background: none;
border: none;
border-radius: 50%;
}
.image-viewer__button img {
width: 80px;
height: 80px;
border: 1px solid transparent;
border-radius: 50%;
cursor: pointer;
}
.image-viewer__display {
position: relative;
padding: 15px;
margin: 3rem;
max-width: 80rem;
display: flex;
flex-direction: column;
flex-wrap: wrap;
align-items: center;
font-size: 0.6rem;
}
.image-viewer__display-source-wrapper {
position: absolute;
font-size: 12px;
left: 50%;
margin-right: 50%;
transform: translate(-50%, -50%);
min-width: 100em;
text-align: center;
bottom: 0;
}
#display {
object-fit: contain;
width: 50rem;
height: 30rem;
margin-bottom: 1rem;
}
#source {
display: inline;
color: black;
}
This is because you've set a fixed width to your image. By setting the main image to 100% the image will fit and fill up the remaining space so the 3 elements are always distributed equally.
main image size = full width - both your arrows
current
#display {
object-fit: contain;
width: 50rem; /*fixed width*/
height: 30rem; /*fixed width*/
margin-bottom: 1rem;
}
amended
#display {
margin-bottom: 1rem;
width: 100%; /*was added*/
height: auto; /*was added*/
}
jsFiddle
Add css float:"right" in css button.

Keeping P Element Inside A Div

I would like to make this div responsive and keep the text in the center of it the entire time. As of now, it looks fine on mobile since I added media queries to it, but on desktop the p element in my .info-p-div goes into the div below and looks awful. These divs are side-by-side on the page. I will post my HTML and CSS code relating to this section of the page so you can see what I mean.
HTML
<div class="info">
<div class="info-img-div">
<img src="images/owner.jpg" />
<p><font color="#F5F5F5">Text here</font></p>
</div>
<div class="info-p-div">
<p><font color="#F5F5F5">Text here</font></p>
</div>
</div>
<div class="parallax-3"></div>
CSS
.info {
text-align: center;
min-height: 250px;
width: 100%;
background-color: #000;
}
.info-img-div {
position: left;
width: 35%;
border-right-style: solid;
border-color: #F5F5F5;
}
.info-img-div img {
border-radius: 50%;
height: 25%;
width: 25%;
min-height: 100px;
min-width: 100px;
margin-top: 20px;
}
.info-p-div {
height: 25%;
width: 50%;
max-width: 65%;
text-align: center;
position: relative;
float: right;
font-size: 12px;
word-wrap: break-word;
vertical-align: middle;
}
.parallax-3 {
background-image: url("images/background3.jpg");
height: 400px;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
#media only screen and (max-width: 600px) {
.info {
text-align: center;
float: none;
height: 50%;
}
.info-img-div {
float: none;
border-right-style: none;
position: relative;
width: 100%;
}
.info-p-div {
float: none;
text-align: center;
width: 100%;
max-width: 100%;
}
}
I think you are looking for below solution.
I posted an working example.
.box {
display: flex;
justify-content: center;
flex-direction: row;
align-items: center;
width: 100%;
height: 300px;
background: tomato;
}
.box p {
flex: 1 0 0;
text-align: center;
}
#media screen and (max-width:600px) {
.box {
flex-direction: column;
}
.box p {
display: flex;
justify-content: center;
align-items: center;
}
}
<div class="box">
<p>One</p>
<p>Two</p>
</div>