Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am using CSS bootstrap 4 with multiple columns per row inside of a container.

(我正在使用CSS引导程序4,容器内每行有多列。)

Depending on the screen size, those columns can stack on top of each other and make the container much taller and sometimes goes beyond what can be displayed on screen and requires scrolling.

(根据屏幕大小,这些列可以彼此堆叠,并使容器更高,有时甚至超出屏幕上显示的内容,需要滚动。)

I cannot seem to figure out a way so that both of these conditions are met:

(我似乎无法找出一种方法来满足这两个条件:)

  • If the container is shorter than the browser window height, center the container in the middle of the screen

    (如果容器短于浏览器窗口的高度,则将容器居中放在屏幕中间)

  • If the container is taller than the browser window height, do not center the container (so that it does not go off screen)

    (如果容器的高度高于浏览器窗口的高度,请不要使容器居中(以使其不会离开屏幕))

I have the code to center the container below, but when the number of lines of text becomes taller than the screen, it simply goes off screen due to .centered, and cannot be scrolled like a normal page

(我有将容器居中的代码,但是当文本行数比屏幕高时,由于。居中,它只是离开了屏幕,无法像普通页面一样滚动)

Here is my HTML:

(这是我的HTML:)

<div class="container centered" style="width: 100%">
    <div class="row justify-content-center">
            <div class="col-8" id="main">
                Text<br>
                Text<br>
                Text<br>
                <!-- Any number of more lines may be added here -->
            </div>
    </div>
</div>

And my CSS:

(而我的CSS:)

html,
body {
  width: 100%;
}

body {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-align: center;
  align-items: center;
  padding-top: 40px;
  padding-bottom: 40px;
  background-color: rgb(0, 0, 0);
}

.centered {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

#main {
  background-color: rgb(30,29,38.3);
  border-radius: 6px;
  box-shadow: 0 0 30px rgb(25, 25, 35);
}
  ask by cg7 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
448 views
Welcome To Ask or Share your Answers For Others

1 Answer

The fixed position on centered is removing it from the the normal page flow, which makes the page not scroll.

(centered的固定位置将其从常规页面流中删除,这使页面无法滚动。)

It would be easier to center it using:

(使用以下方法更容易将其居中:)

body {
  display: -ms-flexbox;
  display: flex;
  -ms-flex-align: center;
  align-items: center;
  background-color: rgb(0, 0, 0);
  justify-content: center;
  min-height: 100vh;
}

https://codeply.com/p/tUejIBKi12

(https://codeply.com/p/tUejIBKi12)

Or, use Bootstrap classes for the same result:

(或者,使用Bootstrap类获得相同的结果:)

<body class="d-flex align-items-center justify-content-center min-vh-100">
    <div class="container">
        I'm centered...
    </div>
</body>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...