Hello World

Set size of HTML page and browser window

I have a div element with . I want to set this element to be the size of the viewable area in the browser window. I am using the following JS to do so.

var container= document.getElementById("container"); container.style.height=(window.innerHeight); container.style.width=window.innerWidth; 

However, the browser window size is larger than the viewable size and horizontal and vertical scroll bars appear. How do I make it so that my HTML page fits into the browser window without a need for scroll bars? EDIT: I am basically asking, how can I make the document size the same as the viewport size?

5 Answers 5

    html, body < width: 100%; height: 100%; margin: 0; padding: 0; background-color: green; >#container < width: inherit; height: inherit; margin: 0; padding: 0; background-color: pink; >h1  

Hello World

The background colors are there so you can see how this works. Copy this code to a file and open it in your browser. Try playing around with the CSS a bit and see what happens.

The width: inherit; height: inherit; pulls the width and height from the parent element. This should be the default and is not truly necessary.

Try removing the h1 < . >CSS block and see what happens. You might notice the layout reacts in an odd way. This is because the h1 element is influencing the layout of its container. You could prevent this by declaring overflow: hidden; on the container or the body.

I’d also suggest you do some reading on the CSS Box Model.

Источник

Code Sample for Modifying HTML Window Width

To meet the minimum width of 330px and minimum height of 400px, the CSS code can be written using position fixed along with the top, right, and bottom properties. An example of this can be seen in the bootstrap Getting Started guide. Another solution is to set the min-width property for the body tag in the CSS file. Additionally, the class can be changed to achieve the desired result.

How to set html elements width to resize upon browser window resize?

Utilize Bootstrap’s grid classes for your project, as it may be beneficial. Consider the following code snippet: col-md-2 col-sm-6 col-xs-12 .

Take a glance at the link provided: https://getbootstrap.com/docs/3.3/css/#grid

Grid system

Bootstrap features a fluid grid system that is responsive and prioritizes mobile devices. The grid automatically scales up to 12 columns as the size of the viewport or device increases. It offers predefined classes for convenient layout options and comes with robust mixins that can generate more semantic layouts.

 | Extra small devices Phones (<768px)| Small devices Tablets (≥768px) |Medium devices Desktops (≥992px) |Large devices Desktops (≥1200px) __________________________________________________________________________________________________________________________________________________________ Class prefix| .col-xs- | .col-sm- | .col-md- | .col-lg- __________________________________________________________________________________________________________________________________________________________ 

As evidenced by my example, I eliminated all the width and height CSS references present in your code, resulting in its successful execution.

Bootstrap Getting Started

Additionally, I modified the category to container in order to help you accomplish your objective.

Читайте также:  Php array contains no values

Set size of HTML page and browser window, var container= document.getElementById("container"); container.style.height=(window.innerHeight); container.style.width=window.innerWidth; However, the browser window size is larger than the viewable size and horizontal and vertical scroll bars appear. How do I make it so that my HTML page fits into the browser window without a need for scroll Code sampleFeedback

Min width in window resizing

To establish a minimum width for the body tag using CSS, it is possible to set the min-width property. However, considering that this specific property is not compatible with IE6, it may be necessary to use an alternative approach such as:

body < min-width:1000px; // Suppose you want minimum width of 1000px _width: expression( document.body.clientWidth >1000 ? "1000px" : "auto" ); /* sets max-width for IE6 */ > 

Your answer lies in giving the containing element a min-width in your CSS. To support IE6, you can utilize the min-width trick.

This will provide a minimum width of 800px in both IE6 and modern browsers.

Use @media in CSS

Examine the CSS at-rule identified as @media .

Utilize @media screen and (max-width: . px) and @media screen and (max-height: . px) to specify the width of your HTML component, ensuring that your page maintains a consistent appearance. If desired, grant users access to the concealed content on your page by implementing overflow:auto .

Although you cannot stop the user from resizing their browser below certain values, rest assured that your web page will always remain above the selected values.

To illustrate, if a minimum width of 330px and a minimum height of 400px are desired, the CSS code may appear as follows:

body < width: 100%; height: 100%; >@media screen and (max-width: 330px) < html< width: 330px; overflow: auto; >> @media screen and (max-height: 400px) < html< height: 400px; overflow: auto; >> 

Scaling HTML layout to the screen size, If you mean the actual browser window, Javascript has screen.width and screen.height which return the current resolution; see examples of use. On a particular webpage, you could set a script to run on a timer to check whether these values have changed, and resize the window if they have.

To change panel size according to the screen size

Utilize the properties of position fixed along with top, right, and bottom to achieve the desired outcome.

Css - How do I control the size of html window?, I would like to control the size (width and height) of an html window page. I would like the page to be small like a popup window. I would like the properties in the same html document to control the size of the browser without having to make a javascript link in another page..

CSS Change Content Based on Screen Size

Place the CSS code labeled @media below the CSS code labeled .notice .

.notice < display: none; visibility: hidden; >@media screen and (max-width:900px), screen and (max-height:500px) < .wrapper < display: none !important; >.notice < display: block; visibility: visible; >> 

Assuming your HTML looks like this:

 
viewable screen size is too small

You can add the following css.

// Show content for bigger viewpoints @media screen and (min-width:900px), screen and (min-height:500px) < .wrapper.content .wrapper.notice > // Show notice for smaller viewpoints @media screen and (max-width:900px), screen and (max-height:500px) < .wrapper.content .wrapper.notice > 

I hope this either proves useful or provides you with some valuable perspective.

Источник

How do I get the window size in css?

I am currently writing my header for my website, and I have a friend who is helping me. We have two very different computers (PC and Mac) and used different browsers to ensure that it is multiplatform and everything, and we found that the header doesn't fit on his screen. So I realized it would be easier to just get the windows width and divide that by four and make that the size of the buttons. How would I going about doing this? Thanks so much! EDIT: Here is what I am using:

 div.horizontal < position: relative; width: 100%; height:45px; >div.horizontal ul < list-style-type:none; margin:0; padding:0; >div.horizontal li < float:left; >div.horizontal a < display:block; background: -moz-linear-gradient(top, rgba(255,255,255,0) 0%, rgba(255,255,255,0.10) 52%, rgba(255,255,255,0) 100%), url("../img/truefactzheader.png"); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(255,255,255,0)), color-stop(52%,rgba(255,255,255,0.10)), color-stop(100%,rgba(255,255,255,0))), url("../img/truefactzheader.png"); background: -webkit-linear-gradient(top, rgba(255,255,255,0) 0%,rgba(255,255,255,0.10) 52%,rgba(255,255,255,0) 100%), url("../img/truefactzheader.png"); background: -o-linear-gradient(top, rgba(255,255,255,0) 0%,rgba(255,255,255,0.10) 52%,rgba(255,255,255,0) 100%), url("../img/truefactzheader.png"); background: -ms-linear-gradient(top, rgba(255,255,255,0) 0%,rgba(255,255,255,0.10) 52%,rgba(255,255,255,0) 100%), url("../img/truefactzheader.png"); background: linear-gradient(to bottom, rgba(255,255,255,0) 0%,rgba(255,255,255,0.10) 52%,rgba(255,255,255,0) 100%), url("../img/truefactzheader.png"); filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#00ffffff', endColorstr='#00ffffff',GradientType=0 ); width:25%; height:35px; >div.horizontal a:link,div.horizontal a:visited < font-weight:bold; color:#FFFFFF; text-align:center; vertical-align:middle; padding:4px; text-decoration:none; text-transform:uppercase; >div.horizontal a:hover,div.horizontal a:active < height:45px; background: -moz-linear-gradient(top, rgba(0,0,0,0.25) 0%, rgba(0,0,0,0.25) 100%), url("../img/truefactzheader.png"); background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0.25)), color-stop(100%,rgba(0,0,0,0.25))), url("../img/truefactzheader.png"); background: -webkit-linear-gradient(top, rgba(0,0,0,0.25) 0%,rgba(0,0,0,0.25) 100%), url("../img/truefactzheader.png"); background: -o-linear-gradient(top, rgba(0,0,0,0.25) 0%,rgba(0,0,0,0.25) 100%), url("../img/truefactzheader.png"); background: -ms-linear-gradient(top, rgba(0,0,0,0.25) 0%,rgba(0,0,0,0.25) 100%), url("../img/truefactzheader.png"); background: linear-gradient(to bottom, rgba(0,0,0,0.25) 0%,rgba(0,0,0,0.25) 100%), url("../img/truefactzheader.png"); filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#99000000', endColorstr='#99000000',GradientType=0 ); a:link a:visited > 

This is the wrong way to approach the question. General solutions: 1) Combine CSS properties like width:25%; min-width:100px; on the buttons to make them size dynamically, 2) use CSS3 Media Queries to apply different styles depending on the screen width (e.g., make the buttons stack vertically on a small screen), or 3) use JavaScript to do the same thing as #2.

Источник

Set a css property to screen width using css

Is there any way to set a css property to the screen width?

What I want to achieve is something close to this:

Edit 1: I would like to use the class to exactly size other elements with the current width as i choose with css. Edit 2: I don't believe it can be done without scripting of some sort but what do I know. Edit 3: Final Thoughts A post about the widow re-sizing script using JavaScript is here in Alex V's answer. You'd just add in the script flavor you want. In my case it'd be setting a class property. However I believe setting visual attributes with JavaScript can be bad practice/undesirable in certain uses.

I want an exact number. Using @media screen I can set it to set sizes but, I want it exact if possible.

Are you saying you want to size other elements in proportion to the size of the div that hypothetically is equal to your screen width? In other words, you want all other elements to be proportional to screen width?

So let's say for a child div, you want it to be width 50% and height 25% of the root div === screen dimensions. How does width: 50vw; and height: 25vh not work for you? These units are based on the screen viewport dimensions.

3 Answers 3

Try width: 100vw; or as the above comment suggests, width: 100%; .

You may also want to set the meta tag in the HTML if it applies:

Edit:

If the isn't fitting 100% of the screen width, perhaps you need to have the default margin/padding reset:

You guys are very correct. When something goes wrong, which it has, %100 doesn't work properly. This and a few other reasons I want to pass out the class or affect a whole tag that contains the exact current screen width in it. While doing it efficient ofcourse.

Its beyond this question but yes i did. I do believe there is a bug with dynamic content but i need to do more research before i ask that one.

The 100vw did actually work where the 100% did not. It isn't the exact answer I was hoping for because it was so easy, but thank you because sometimes the best answers are! it does what I need it to. As it turns out it is not possible to set a class and property inside css.

Generally, a div, if it's set to display:block (which is the default in most browsers, I believe) will expand to the full width of it's parent. If you want it to be the full width of the screen, it really depends on the way your page is configured.

If the div is within another element that is only set to width:500px or any other size, the div will only be the width of the parent. But if div's parent is the html body, then it should be the full width of the screen.

I've been smacking my head around on html and css a lot lately, and the best tool I've found to figure out CSS issues like this is Chrome's developer tools. You can actually right-click and "inspect" the div you are looking at. Then you can try out all the different css settings you want and Chrome will show you in real-time what effect those things will have.

Источник

How can I change the CSS style of a page depending on the screen's width?

I'm making a website and I need the website to be responsive, so I need to change the CSS depending on the width of the screen. I have 3 stylesheets, a main, normal and mobile version, that are present in the HTML:

When I use this, I always make sure that it works by changing the width of the page, and I see that it works. However, when I use Chrome's Emulation mode (inspect element -> enable emulation), and the screen is below the maximum width for a certain stylesheet, the webpage conservs its "normal" mode and my responsive design doesn't have any effect. Should I be using this in the CSS instead (as one big file):

 @media only screen and (max-width : 851px) <> 

Normal (on big screen): normal When I change the width (big screen): mobile on big emulation mode (ipad): glitch.

3 Answers 3

Your method, although not wrong, is less than ideal. I usually have the same problem when I come across websites that employ this method.

Instead, make 2 stylesheets

Make sure the responsive.css is below the main.css so that it overwrites the main css if needed.

Do all your responsive breakpoints in that stylesheet.

It is probably a good idea to use a responsive framework (Bootstrap) this minimizes the amount of responsive code you will be doing as long as you make use of the bootstrap classes that are available to you. And it is much easier to maintain.

If Bootstrap doesn't have the breakpoints you are looking for, I'd suggest you look into SASS + Susy link With Susy, you can map your breakpoints in anyway you want and you can nest your media code within the element you want to behave responsively. Neatest method and the future of front end design.

Источник

Оцените статью