Adjust image size to fill screen without JS - html

When a user loads the page, I want an image to fill the entire screen. What I want is very similar to background-size:cover, but I want to be able to scroll down without the static background.
This is what I have so far:
body {
margin: 0;
}
#bg img {
z-index: -1;
position: absolute;
max-width: 100%;
}
The only problem with this, is that the image height is not restricted to the height of the browser window. height or max-height does not make any difference.
Is there any simple way of achieving this in pure CSS?

Use the vh and vw Viewport-Relative Length units.
#bg img {
height: 100vh;
width: 100vw; /* If you want it to be full width as well as height. */
}

Related

How to make 1920-1080px html page fit screen and remove whitespace

I am working on a website which is 1920-1080px but It doesn't fit the screen as it is too big. I have removed the margin and padding and it works but it is too big to fit the screen. there is white space on the right side of the page with no scrollbar. If I scale it to fit, It has a scroll bar and so it does not fit the screen
#Home_Page {
position: absolute;
box-sizing: border-box;
background: #E5E5E5;
width: 1920px;
height: 1080px;
background-color: rgba(255, 255, 255, 1);
overflow: hidden;
margin: 0;
padding: 0;
--web-view-name: Home Page;
--web-view-id: Home_Page;
--web-scale-to-fit: true;
--web-scale-on-resize: true;
--web-refresh-for-changes: true;
--web-enable-deep-linking: true;
}
I expected it to be full screen and scrollable.
I'm not sure I understand your problem here, but if you want your "Home_Page" div to be full screen, you should consider the vh and vw units, such as:
#Home_Page{
width: 100vw;
height: 100vh;
…
100vw being 100% of your viewport width, it dynamically adapt to the width of the browser's viewport width.
If you want to keep a minimal width of 1366px, then:
#Home_Page{
width: 100vw;
min-width: 1366px;
height: 100vh;
…
You can't fit a web page with static sizes everywhere becuase different users have different monitors/displays. Even if you achieved it in some way, the image would probably be a bit blury after upscaling it.
As DimNC pointed out earlier, you should consider creating a responsive page or set your element's height to 100vh and width to 100vw.
vh comes from vierport height and
vw from viewport width.
That way your page will "adapt" to various screen and browser sizes dynamically.
#Home_Page {
height: 100vh;
width: 100vw;
}

Really confused about how to set height and width as a percentage for my carousel

I am trying to set the width of my carousel to take up 100% of the width of the screen while the height takes up 35% of the screen from the top. I have tried numerous attempts to use % with height, but it just doesn't work.
This is my css:
html,body{
height: 100%; width: 100%; margin: 0;
}
.carousel-slide, .carousel-image, .carousel-inner{
height: 35%;
width: 100%;
}
/*carousel-image is the class for the images placed in the carousel*/
The reason why I dont want to use px or em is because I want the carousel to take up 35% of the screen regardless of the device the website is being used on (desktop browser, ipad, smartphone...)
Thanks in advance.
Try using 35 viewport height and 100 viewport width instead and see if that works.
.carousel-slide, .carousel-image, .carousel-inner{
height: 35vh;
width: 100vw;
}

Set a fixed height without width changing

I want to make an image stay at a certain height, but I also want it to stay at the width of the user's screen.
Here's the image's CSS:
#cafe {
max-width: 100%;
height: 700px;
}
Here's the output:
you can try this
#cafe {
width: 100%;
height: 100vh;
}
here the 'vh' means VIEWPORT HEIGHT which automatically takes the user's screen height.

responsive image resizing not working

img {
width: 100%;
height: auto;
}
The code above resizes the images width with the with of the page, while keeping the same ratio with the height.
Why doesn't
img {
width: auto;
height: 100%;
}
resize the image's height with the pages height, while keeping the same ratio with the width? Is there another way I can have that effect on my image?
May be height is not defined of parent of img. So please put any height to the parent of img then try.

CSS image constraints/resizing

I've been making a tumblr theme using html/css and it allows the user to input their own image that will show up in their sidebar on the theme.
I'm not sure how to constrain the image to a certain region/size on the screen. What I have right now for the css of the image (which is probably excessive)
.sidebaricon {
margin-left: auto;
margin-right: auto;
width: auto;
height: auto;
max-width: 500px;
max-height: 500px;
}
But when I upload differently sized photos they don't constrain to the same size. I want all the uploaded images to constrain to a 500x500px square region.
Please help! Thank you.
As far as I understand, you try to upload an image and set it a max width. There's some mistakes in your code and some things that can be confusing (e.g. : your sidebar and its image have the same class).
First you need to set a width to your sidebar:
.sidebar { /* I renamed the container to avoid any misunderstanding */
width: 30%; /* or whatever */
max-width: 500px;
}
Then, the image in the sidebar :
.sidebar .sidebaricon { /* this should be your img */
max-width: 100%;
height: auto;
}
The logic is quite simple : you let the image to fill 100% of its parent's width, so you just have to play with the parent's width.
A lot of CSS frameworks use the same technic to provide a quick way to make all images "responsive", like this :
img {
max-width: 100%;
height: auto;
}
Again, this means : all images in the document body should have a maximum width of 100% of its container. I added the automatic height as some old IE doesn't keep the ratio by default.