How to fit background image to parent element? - css

I'm struggling to fit the background image to the parent div if the screen size getting wider.
If screen size for mobile, the result is correct what I expect. But if the screen size getting wider the background image will cut off by the parent element.
While for desktop screen, the background image will cut off by parent element:
So far my code like this:
index.html
<div id="hero">
<div className="weekend">
<h1 className="text-center">WEEKEND FROM HOME</h1>
<h6 className="text-center">Stay active with a little workout.</h6>
</div>
<div className="position-relative text-center d-flex justify-content-center">
<img src={robot} alt="" />
<button className="btn btn-light lets-go text-center">
Let's Go
</button>
</div>
</div>
style.css
#hero {
background-image: url("../../assets/shapes/bg-1.svg");
background-position: center;
background-repeat: no-repeat;
background-size: cover;
background-color: var(--pinky-50);
padding-top: 134px;
padding-bottom: 20px;
padding-left: 24px;
padding-right: 23px;
}
So far my solution is adjust padding and height for different screen size but it's not best practice, because sometime i missed for several screen size.
info: resolution of background image is 1366px x 732px

Related

how to use horizontal over-flow in css

please i'm trying to use horizontal overflow using css , if i use just images i get the horizontal scroll this how i use it with images
<div class="d-flex overflow-hidden overflow-scroll-x ">
<img
class="max-width-200 "
src="../../../../assets/images/Creditrd.svg"
/>
<img
class="max-width-200 "
src="../../../../assets/images/Creditard.svg"
/>
</div>
if i use background images i dont get anything here's how i do it
<div class="d-flex overflow-hidden overflow-scroll-x">
<div class="account-wallet-imag max-width-200 border-radius-16">
heheh
</div>
<div class="account-wallet-imag max-width-200 border-radius-16">
eheehhh
</div>
</div>
this is my css
.account-wallet-imag {
background-repeat: no-repeat; /* Do not repeat the image */
background-size: cover; /* Resize the background image to cover the entire container */
width: 100%;
height: 100%;
background-image: url("#/assets/images/card.png");
}
please how can i go about this

Responsive images with percentage

I have a layout that has a lot of images and in order to make my layout responsive, I am using percentage values for the width and height. Each image is nested into a div tag and the images are given a width and height of 100% of their parent's width and height.
Using media queries, depending on the screen size, I am changing the width of the divs that contain each images as follows:
When only 2 images can fit on the screen: 50% width each div that contains an image
When only 3 images can fit on the screen: 33.33% width each div that contains an image
When only 4 images can fit on the screen: 25% width each div that contains an image
When only 5 images can fit on the screen: 20% width each div that contains an image
etc..
But those images look pixelated when they get bigger, they loose quality... How do I make them not loose quality and not look pixelated when they go from having a 20% width to having a 50% width?
Do I use the srcset technique? What responsive image technique do I use to allow my images to scale to any size without getting pixelated?
If I understand correctly flex-grow will do the job for you.
Whenever you have 1 image it will force it to be 100% width, 2 images 50% and so on and so forth. You don't need a media query for this, unless you want to achieve different layouts per your wish, of course.
Here is an example on codepen, you can try adding/removing images to see how it fits.
.flexbox .item {
flex-grow: 1;
}
.flexbox {
display: flex;
border: 2px solid #86835f;
margin-bottom: 2em;
max-width: 100%;
}
.item {
box-sizing: border-box;
background: #86835f;
color: #ece69c;
margin: 0.25em;
}
img{
max-width: 100%;
}
<div class="flexbox flexbox-grow-1">
<div class="item ">
<img src="https://fox26medford.com/wp-content/uploads/2020/04/AprilShoe-720x399-1.jpg" alt=""/>
</div>
<div class="item ">
<img src="https://fox26medford.com/wp-content/uploads/2020/04/AprilShoe-720x399-1.jpg" alt=""/>
</div>
<div class="item ">
<img src="https://fox26medford.com/wp-content/uploads/2020/04/AprilShoe-720x399-1.jpg" alt=""/>
</div>
<div class="item ">
<img src="https://fox26medford.com/wp-content/uploads/2020/04/AprilShoe-720x399-1.jpg" alt=""/>
</div>
<div class="item ">
<img src="https://fox26medford.com/wp-content/uploads/2020/04/AprilShoe-720x399-1.jpg" alt=""/>
</div>
</div>
If you want to retain the original sizes of the images and make it responsive, there is a cool way to do this!
-> You can use a flexbox. (That is the secret)
Run the following code below and open the code in full screen. Then change your screen width by minimizing the screen and watch how responsive the images are using flexbox.
The magic down below is caused by a CSS property known as flex-wrap: wrap; Which will wrap all the images in a responsive container!
You can make something like this:-
.image {
height: auto;
width: 150px;
margin: 10px;
}
.container {
display: flex;
flex-wrap: wrap;
}
<div class="container">
<img class="image" src="https://www.befunky.com/images/wp/wp-2014-08-milky-way-1023340_1280.jpg?auto=webp&format=jpg&width=1184" />
<img class="image" src="https://www.befunky.com/images/wp/wp-2014-08-milky-way-1023340_1280.jpg?auto=webp&format=jpg&width=1184" />
<img class="image" src="https://www.befunky.com/images/wp/wp-2014-08-milky-way-1023340_1280.jpg?auto=webp&format=jpg&width=1184" />
<img class="image" src="https://www.befunky.com/images/wp/wp-2014-08-milky-way-1023340_1280.jpg?auto=webp&format=jpg&width=1184" />
<img class="image" src="https://www.befunky.com/images/wp/wp-2014-08-milky-way-1023340_1280.jpg?auto=webp&format=jpg&width=1184" />
<img class="image" src="https://www.befunky.com/images/wp/wp-2014-08-milky-way-1023340_1280.jpg?auto=webp&format=jpg&width=1184" />
</div>
If you resize the image they will align and be responsive!

Bootstrap image not adjusting height according to parent

I have some buttons with images. The images inside of the button are too big. I want the images to resize with the button element, either when the height of the button is changed or text-size is changed. I'm using the Bootstrap-4 class ("img-fluid") but it doesn't like it's working properly as the image height isn't changing.
My HTML code:
<div class="order-type-buttons col-12 d-flex" aria-label="First group">
<button type="button" class="btn order-type-btn-blue col-4 d-flex justify-content-center align-items-center">
<div class="col-8 button-text">Online Pick Up</div>
<div class="col-4 span-img">
<img class="img-fluid" src="images/icon%20files/online-pickup-icon.png" alt="">
</div>
</button>
<button type="button" role="button" class="btn order-type-btn-blue col-4 d-flex justify-content-center align-items-center">
<div class="col-8 button-text">Online Take out</div>
<div class="col-4 span-img">
<img class="img-fluid" src="images/icon%20files/online-take-out-icon.png" alt="">
</div>
</button>
<button type="button" class="btn order-type-btn-blue col-4 d-flex justify-content-center align-items-center">
<div class="col-8 button-text">Pick up</div>
<div class="col-4 span-img">
<img class="img-fluid" src="images/icon%20files/pick-up-icon.png" alt="">
</div>
</button>
Here is my CSS code:
.btn{
white-space: normal;
}
.button-text{
padding-left: 0;
padding-right: 0;
}
.order-type-btn-blue{
background-color: #0082d5;
font-size:14px;
color: white;
text-transform: uppercase;
margin: 2px 2px 2px 2px;
padding-right: 0;
padding-left: 0;
}
.order-type-buttons {
padding-right: 0;
padding-left: 0;
}
.span-img{
padding-right: 0;
padding-left: 0.313em;
}
Here is how my buttons look:
Here is how my buttons look after I change the button height to 46px and font-size to 9px.
Here an image of the buttons with text div and image div with borders:
just set the width and set height to auto cause in many cases the bootstrap take height as auto with defined with
or
use percentage , vh scale for height for responsiveness.
The image is responsive to the width, not the height. So as you reduce the button height, since the width is not reducing, neither is the image height.
You'd need to add some code to make it responsive to the height.
This is one way that works in some cases. Its probably not the best way. If you set a height on the image container, and set a max height on the img
.span-img {
height: 100%;
/* optional for right align */
display: flex;
justify-content: flex-end;
}
.img-fluid {
max-height: 100%;
}
That makes the image responsive to the height as well.
See it in action with bootstrap
https://codepen.io/arthurrandom/pen/wRwMvw
The extra flex settings on the .span-img are to hard align to the right, and not necessary for the responsive height image.

WordPress Bakery - Setting row to full height of image background (no content)

I have a row using WP Bakery (Visual Composer formerly) which has two columns that are just background images. There is no content. However, when I set the row for Full Height in the Front end editor from WordPress, the row still doesn't stretch vertically to accommodate the full image height. It shows maybe 1/4 of the image at the top and then the rest is cut off.
<div style="position: relative; left: -45px; box-sizing: border-box; width:
1256px;" data-vc-full-width="true" data-vc-full-width-init="true" data-vc-
stretch-content="true" class="normal_height vc_row wpb_row vc_row-fluid
vc_row-o-full-height vc_row-o-columns-stretch vc_row-o-equal-height vc_row-
flex">
<div class="wpb_column vc_column_container vc_col-sm-6 vc_col-has-fill">
<div class="vc_column-inner vc_custom_1517014250070">
<div class="wpb_wrapper">
</div>
</div>
</div>
<div class="wpb_column vc_column_container vc_col-sm-6 vc_col-has-fill">
<div class="vc_column-inner vc_custom_1517014312168">
<div class="wpb_wrapper"></div>
</div>
</div>
</div>
CSS for left image:
.vc_custom_1517014250070 {
background-image: url(/uploads/2018/01/hbar-chart.jpg?id=2944)
!important;
background-position: 0 0 !important;
background-repeat: no-repeat !important;
}
CSS for right image:
.vc_custom_1517014312168 {
background-image: url(/uploads/2018/01/f-mix.jpg?id=2943) !important;
background-position: 0 0 !important;
background-repeat: no-repeat !important;
}
Don't use a background image, but insert an image with full width.
A page element will never adjust its size to a to a background image, it's only possible (not the rule) the other way round. So just use an img element with your image
You can use raw html widget into the column with background image.
Firstly, choose background style as "cover" than
<h2><br><br><br><br></h2> <!--how many space as you need to visualize background image-->
Than it will be cover the height of the row

Keep Text In <DIV> When Re-Sizing

I have text that is overlayed on an image which is set as a background (main-logo). It works well for desktop but when scaling down to tablet or mobile devices the text remains a larger font size and just cuts off.
I'd like the background to expand and the text to stay inside the background (some scaling down of the text is fine). This is what I have:
<div class="full-logo" id="top">
<div class="col-lg-12 main-logo img-responsive">
<img src="\img\long.png" class="" alt="">
<br>
<h1 class="text-center">
<div class="col-md-8 col-centered-headline">WHO WE ARE</div>
</h1>
<h2 class="text-center">
<div class="col-md-8 col-centered-headline">
Our conference is one of the best technology conferences on the planet as voted on by readers of Industry Magazine. We take a different approach.
And that difference works for our attendees, ranging from Fortune 500 companies to the most exciting startups in the world.
Our speakers are world class, but our networking is “simply legendary”.
</div>
<br>
</h2>
</div>
</div>
And CSS contains this info:
/* Full Width Logo*/
.full-logo {
width: 100%;
height: 400px;
background-image: url('/img/bluesky.jpg');
background-size: cover;
margin-top: -9px;
}
.col-centered-headline {
display: inline-block;
float: none;
text-align: center;
margin-right: -4px;
color: white;
}
First, I would direct your toward Media Queries. But in your case, can you get away with just setting min-height on .full-logo?
.full-logo {
width: 100%;
min-height: 400px; /* <-- change 'height' to 'min-height' */
background-image: url('/img/bluesky.jpg');
background-size: cover;
margin-top: -9px;
}

Resources