最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is {{ width: "100vw", height: "100vh"}} bigger than my browser window?(marg

programmeradmin4浏览0评论

I made a map with {{ width: "100vw", height: "100vh"}}, and it is bigger than my browser window.

<div id="map" style={{ width: "100vw", height: "100vh", margin: "0", padding: "0"}}></div>

I added border: "1px solid black" to see what is the problem, and this is what I saw:

As you can see, there is black border line around the map, and then white space surrounding it too. Why does margin still exist when I explicitly applied it to 0?

Whole code as reference:

export default function Home(props){
  useEffect(() => {
    let container = document.getElementById('map');
    let options = {
      center: new window.kakao.maps.LatLng(33.450701, 126.570667),
      level: 3
    };
  
    let map = new window.kakao.maps.Map(container, options);
  
  }, [])
  return (
    <>
    <div id="map" style={{ width: "100vw", height: "100vh", margin: "0", padding: "0", border: "1px solid black"}}></div>
    </>
  )
}

I made a map with {{ width: "100vw", height: "100vh"}}, and it is bigger than my browser window.

<div id="map" style={{ width: "100vw", height: "100vh", margin: "0", padding: "0"}}></div>

I added border: "1px solid black" to see what is the problem, and this is what I saw:

As you can see, there is black border line around the map, and then white space surrounding it too. Why does margin still exist when I explicitly applied it to 0?

Whole code as reference:

export default function Home(props){
  useEffect(() => {
    let container = document.getElementById('map');
    let options = {
      center: new window.kakao.maps.LatLng(33.450701, 126.570667),
      level: 3
    };
  
    let map = new window.kakao.maps.Map(container, options);
  
  }, [])
  return (
    <>
    <div id="map" style={{ width: "100vw", height: "100vh", margin: "0", padding: "0", border: "1px solid black"}}></div>
    </>
  )
}
Share Improve this question edited Jun 22, 2020 at 4:59 Uwe Keim 40.7k61 gold badges187 silver badges302 bronze badges asked Jun 21, 2020 at 23:01 newnew 3771 gold badge3 silver badges14 bronze badges 1
  • The map exceeded browser window before I used that style. I added it to debug after. – new Commented Jun 21, 2020 at 23:18
Add a ment  | 

4 Answers 4

Reset to default 8

Browser default styles include spacing for those elements, so you have to remove margin and padding from the <html> and <body> elements in your Css:

html,
body {
   margin: 0;
   padding: 0;
}
<div id="map" style={{ ... border: "1px solid black"}}></div>

Your borders 1px, it means +2px on each side. You can try to add box-sizing to your div:

box-sizing: border-box;

https://developer.mozilla/en-US/docs/Web/CSS/box-sizing

This problem was also irritating me. Then I tried "width: 100%;" except "width: 100vw;" and it worked. So try it and let me know if it works for you laso.

<div id="map" style="width: 100%; height: 100vh;"></div>

The accepted answer does not cover the case of

{position: fixed, height: 100vh}

however the above snippet seems to cover only the screen, the height of the browser navBar and device navigation bar will impact the visible part of viewport leading to overflow.

If an element is required that takes the whole screen but doesn't overflow use this:

{position: fixed, top:0, bottom:0, left:0, right:0}

This will lay out the element while respecting any OS element shrinking the visible part of the viewport.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论