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

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

html - How to prevent flexbox from flexing vertically?

So I have the following in my page:

enter image description here

and they're basically two divs side by side coloured blue and orange which is made by making the outer container a flexbox.

What i want to do now is to prevent it from flexing vertically meaning if i adjust the height of the browser window, I want to prevent this:

enter image description here

as shown, they are becoming thinner height wise when adjusting the height of browser. How do i prevent this? Instead I want them to retain their height but just brings up a scroll bar at the side so it is scrollable height wise.

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    height: 100%;
    font-size: 1vw;
}

body {
    height: 100%;
}

#container {
    display: flex;
    width: 100%;
}

#box1 {
    border: 1px solid black;
    width: 50%;
    height: 90vh;
    background: lightblue;
}

#box2 {
    border: 1px solid black;
    width: 50%;
    height: 90vh;
    background: orange;
}
<!DOCTYPE html>
<html lang="en">  
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href = "style.css">
  <title>Document</title>
</head>
<body>
  <div id = "container">
    <div id = "box1"></div>
    <div id = "box2"></div>
  </div>
</body>
</html>
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

As I already wrote in the commentary, you can use javascript. If I understand correctly, then in the code I get the height in pixels for the current screen size, provided that the height is set to 90vh. Further, the blocks (#box1, #box2) are assigned this height.

I don’t know, why you didn’t assign a height of 90vh to the #container div. And instead they assigned each block separately. Perhaps you need it more. And according to your css, I made the code in javascript.

window.onload = function() {
let box1 = document.querySelector('#box1');
let box2 = document.querySelector('#box2');
  
  box1.style.height = box1.offsetHeight + 'px';
  box2.style.height = box2.offsetHeight + 'px';
}
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    height: 100%;
    font-size: 1vw;
}

body {
    height: 100%;
}

#container {
    display: flex;
    width: 100%;
}

#box1 {
    border: 1px solid black;
    width: 50%;
    height: 90vh;
    background: lightblue;
}

#box2 {
    border: 1px solid black;
    width: 50%;
    height: 90vh;
    background: orange;
}
<!DOCTYPE html>
<html lang="en">  
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href = "style.css">
  <title>Document</title>
</head>
<body>
  <div id = "container">
    <div id = "box1"></div>
    <div id = "box2"></div>
  </div>
</body>
</html>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...