html - 如何在浏览器窗口中并排放置两个 div,居中

标签 html css

有人可以告诉我如何将两个 div 水平并排放置在浏览器窗口的中心吗?

它们的宽度和高度必须完全相同,但它们之间的间隙“不多”。以及让他们响应。

我在堆栈中寻找答案,但没有一个答案涵盖我想要实现的目标。

最佳答案

最好的方法是使用 Viewport units: vw, vh, vmin, vmaxthe calc() CSS function 在一起。

*{box-sizing: border-box; padding: 0; margin: 0} /*reset all browser style*/

:root{background: black; min-height: 100vh; width: 100vw} /*set the root element to viewport*/

body{ text-align: center} /*ask the browser to set the box in the middle of the screen*/

article{
  background: white;
  width: calc(50vw - 40px);  /*reserve 20x2px for the margin*/
  height: calc(100vh - 20px);  /*reserve 10x2px for the margin*/
  display: inline-block;
  margin: 20px 10px   
}
<article></article>
<article></article>

但是你也可以使用百分比

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

:root{background: black; min-height: 100vh}

section{
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  text-align: center
}

article{
  background: white;
  width: 48%;
  height: 90%;
  display: inline-block;
  margin: 5% auto
}
<section>
  <article></article>
  <article></article>
</section>

关于html - 如何在浏览器窗口中并排放置两个 div,居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27741068/

相关文章:

html - 通过将选择框隐藏在文本框下来设置选择框的样式

javascript - CSS3 转换不起作用

jquery 否决 css

javascript - JQuery链接打开新窗口

java - Spring应用程序Thymeleaf中的css问题

css - 如何使用 css 制作边框布局?

html - 无滚动条的图像全宽和全高

javascript - 将图像和数据合并为 1 个 html 表单

html - 将下拉菜单标题与下拉菜单对齐

html - SVG <image> 元素渲染质量