Html CSS 垂直填充屏幕 50% 50%

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23956910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:55:00  来源:igfitidea点击:

CSS to fill screen 50% 50% vertically

htmlcss

提问by asdf

enter image description here

在此处输入图片说明

What's the proper CSS to achieve this for most browsers?

对于大多数浏览器来说,实现这一目标的正确 CSS 是什么?

  • 2 Divs - 50% and 50% vertically fill the entire screen.
  • Each div has 50% and 50% horizontally to fill 1600px width.
  • 2 个 Divs - 50% 和 50% 垂直填充整个屏幕。
  • 每个 div 有 50% 和 50% 水平填充 1600px 宽度。
<!-- TOP 50% -->
<div class="top">
   <div class="left">img</div>
   <div class="right">txt</div>
</div>

<!-- BOT 50% -->
<div class="bot">
   <div class="left">text</div>
   <div class="right">img</div>
</div>

采纳答案by Andy Mercer

Goal:

目标:

You want a 2 x 2 grid of boxes. Each box is to be 50% of the window in height and width. This is actually much easier than you'd think. You don't need .leftor .right, you don't need .top.bot. All you need is a single class called .row.

你想要一个 2 x 2 的盒子网格。每个框的高度和宽度应为窗口的 50%。这实际上比你想象的要容易得多。你不需要.left或者.right,你不需要.top.bot。您只需要一个名为.row.

EDIT: You mentioned in comments that you want the width fixed at 1600px;We just need to add widthto body.

编辑:您在评论中提到您希望将宽度固定在1600px;We 只需要添加widthbody.

Code

代码

HTML:

HTML:

<!-- TOP 50% -->
<div class="row">
   <div>img</div>
   <div>txt</div>
</div>

<!-- BOT 50% -->
<div class="row">
   <div>text</div>
   <div>img</div>
</div>

CSS:

CSS:

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

body {
    width:1600px;
}

.row {
    width:100%;
    height:50%;
}
.row div {
    width:50%;
    height:100%;
    float:left;
}

Screenshot

截屏

This is from the example below, but I've added colors to make it easier to see.

这是来自下面的示例,但我添加了颜色以使其更易于查看。

Edit: The Fiddle has changed to include width. My screenshot is before the width, to demonstrate. It'll look a lot wider, because of the fixed width.

编辑:小提琴已更改为包括宽度。我的截图是在宽度之前,以演示。由于宽度固定,它看起来会更宽。

enter image description here

在此处输入图片说明

Working Example:

工作示例:

jsFiddle

js小提琴

回答by Samih

There are a few ways to achieve this. I tend to favour this one:

有几种方法可以实现这一点。我倾向于支持这个:

http://jsfiddle.net/YyBW7/

http://jsfiddle.net/YyBW7/

.top, .bot {
    height: 50%;
    border: 1px solid black;
    box-sizing: border-box;
}

.left, .right {
    display: inline-block;
    width: 50%;
    height: 100%;
    margin-right: -4px;
    border: 1px solid red;
    box-sizing: border-box;
}

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

回答by Narong

I think there were some things missing from Samih's answer. Probably floatSee if this gets you better results: jsFiddle

我认为萨米的回答中遗漏了一些东西。可能float看看这是否能让你得到更好的结果: jsFiddle