简体   繁体   English

如何删除页面上的滚动条

[英]How to remove scroll bar on page

Working on a webpage that has one "div" which is aligned as center. 使用一个以“ div”为中心对齐的网页。 I went through a bunch of methods to try and get it working the way that I want it to, and finally found the closest one. 我经历了一堆方法来尝试使其以我想要的方式工作,最后找到了最接近的方法。 I have a working demo over here 我在这里有一个工作演示

FIDDLE 小提琴

My only problem with it right now is that even when the content doesn't take up the whole content of the page, it still adds a scroll bar. 我现在唯一的问题是,即使内容没有占据页面的全部内容,它仍然会添加滚动条。 I'm wondering if there's anyway to remove that? 我想知道是否可以删除它? I believe it has to do with the jQuery method of centering the "div". 我相信这与将“ div”居中的jQuery方法有关。

Javascript: Javascript:

$(function() {
  jQuery.fn.center = function() {
    this.css("position", "fixed");
    this.css("top", ($(window).height() / 2) - (this.outerHeight() / 2));
    this.css("left", ($(window).width() / 2) - (this.outerWidth() / 2));
    return this;
  }

  $('#myDiv').center();
  $(window).resize(function() {
    $('#myDiv').center();
  });
});

CSS 的CSS

* {
  margin: 0;
  padding: 10px;
}

html,
body {
  height: 100%;
}

body {
  font-family: 'Josefin Sans';
  text-align: center;
}

p {
  font-size: 18px;
  margin: 0 0 0.5em;
}

.centered-wrapper {
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-height: 100%;
}

.centered {
  background: #ccc;
  border-radius: 8px;
  margin: 0 auto;
  padding: 20px;
  width: 85%;
}
body{
    overflow: hidden;
}

Vertical only: 仅垂直:

body{
    overflow-y: hidden;
}

Horizontal only: 仅水平:

body{
    overflow-x: hidden;
}

Possible duplicate: Hiding the scrollbar on an HTML page 可能重复: 将滚动条隐藏在HTML页面上

html,
body {
  position: fixed;
  top:00;
  left:0;
  bottom:0;
  right:0;
}

Here is your updated demo 这是您更新的演示

Setting overflow to hidden may solve the scrollbar not appear but in that case it may not be fully vertically centered. 将溢出设置为隐藏可能会解决滚动条不出现的问题,但在这种情况下,滚动条可能不会完全垂直居中。

Instead you can not set padding on all elements, like you do with 相反,您不能像对那样对所有元素设置填充

* {
  margin: 0;
  padding: 10px;
}

It makes your body element and .content-wrapper to have extra padding of 10px that adds to its height and makes the scrollbar appear, if you set this padding to 0 the scrollbar disapears, check on your fiddle. 它会使您的body元素和.content-wrapper具有额外的10px填充,从而增加其高度并显示滚动条,如果将此填充设置为0,则滚动条会消失,请检查小提琴。

So for your example you could set your css like 因此,对于您的示例,您可以将CSS设置为

body, html {
  margin: 0;
  padding: 0;
}

p, h1, h2, h3, h4, span, br, hr {
  margin: 0px;
  padding: 10px;
} 

html,
body {
  height: 100%;
  margin: 0;
}

body {
  font-family: 'Josefin Sans';
  text-align: center;
}

p {
  font-size: 18px;
  margin: 0 0 0.5em;
}

.centered-wrapper {
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-height: 100%;
}

.centered {
  background: #ccc;
  border-radius: 8px;
  margin: 0 auto;
  padding: 20px;
  width: 85%;
}

/*
Button
*/

.btn {
  -webkit-border-radius: 8;
  -moz-border-radius: 8;
  border-radius: 8px;
  font-family: Arial;
  color: black;
  font-size: 18px;
  background: #ccc;
  padding: 10px 20px 10px 20px;
  text-decoration: none;
  outline: none;
}

.btn:hover {
  background: #bbb;
  text-decoration: none;
  }

In that case scrollbar won't appear. 在这种情况下,滚动条将不会出现。

Instead of setting margins and paddings on all elements with * use something like css style reset. 不要在所有元素上使用*设置边距和填充,而应使用CSS样式重置之类的方法。 Google for it. Google。 Then apply padding only on those elements you really need. 然后,仅对您真正需要的元素应用填充。

set overflow to hidden; 将溢出设置为隐藏; this may solve it. 这可以解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM