html - CSS:如何设置与浏览器屏幕相关的内容的固定宽度

标签 html css width

我是 CSS 和 HTML 的新手,对此我很迷茫。我想将屏幕分成两半,其中一个的宽度等于最大化浏览器屏幕的 50%。而另一个将是灵活的。所以当你将浏览器宽度设置为一半时,其中一个内容将完全可见,而另一个内容将完全隐藏。当然,我希望每个分辨率都相同。

与四方相同:https://foursquare.com/explore?mode=url&near=New%20York%2C%20NY%2C%20United%20States&nearGeoId=72057594043056517 (在浏览器宽度小于 50% 之前, map 被隐藏,条形图信息不受影响)。

示例如下:

body {
  width: 100vw;
  height: 100vh;
}

/*width must be 50% of browser's maximum width, same as bars on foursquare*/

.lefthalf {
  float: left;
  background-color: black;
  width: 50%;
  height: 100%; 
}

/*width is between 0-50% of browser's maximum width, same as map on foursquare*/

.righthalf {
  float: right;
  background-color: red;
  height: 100%;
  width: 50%;
}
<div class="lefthalf"></div>
<div class="righthalf"></div>	

最佳答案

你可以用 Flexbox 做这样的事情:

html, body { /* modified */
  width: 100vw;
  height: 100vh;
  margin: 0; /* recommended */
}

body {
  display: flex; /* displays children inline by default */
}

.lefthalf {
  /*float: left;*/
  background: black;
  flex: 0 0 750px; /* adjust to your needs / doesn't grow nor shrink with the initial width of 750px */
  /*height: 100%;*/
  overflow-x: auto; /* optional / enables horizontal scrolling if the content is wider than 750px (in this case) */
}

.righthalf {
  /*float: right;*/
  background: red;
  /*height: 100%;*/
  /*width: 50%;*/
  flex: 1; /* takes the remaining horizontal space */
}

@media screen and (max-width: 1000px) { /* adjust to your needs */
  .lefthalf {flex: 3} /* always three times wider than the .righthalf between 601px and 1000px screen width */
  .righthalf {flex: 1}
}

@media screen and (max-width: 600px) { /* adjust to your needs */
  body {
    flex-direction: column; /* recommended / stacks children vertically */
  }
  .lefthalf,
  .righthalf {
    flex: 1; /* optional & recommended / each takes half of the body height */
  }
}
<div class="lefthalf"></div>
<div class="righthalf"></div>

.lefthalf 需要在 px 中有一个静态的 width,它不会与定义的 50%< 一起工作50vw 因为它的 width 会根据浏览器的 width 而改变。

关于html - CSS:如何设置与浏览器屏幕相关的内容的固定宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47739399/

相关文章:

html - 使用 Div 元素向左或向右设置 CSS 内容(图像和文本)

javascript - 单击链接时如何保持事件状态

javascript - 当 overflow-x 设置为 hidden 时,处理可变高度图像的最佳方法是什么

css - 选择:焦点{宽度:自动;}

python GTK3限制标签宽度

javascript - URL 不显示请求参数

javascript - JQuery 检查是否需要输入

html - 一个简单的问题 : what html element should be used as a list paging?

html - 过渡延迟不适用于边界

CSS 嵌套跨度和宽度属性