简体   繁体   English

如何在整个屏幕宽度上并排两个div?

[英]How do I span two divs side-by-side for the full screen width?

There are a lot of questions regarding side-by-side divs. 关于并排div有很多问题。 I didn't miss those. 我没有错过那些。 But I need something that spans the whole width of the screen. 但是我需要一些可以覆盖整个屏幕宽度的东西。 This is the situation: 这种情况:

I need three divs positioned side-by-side. 我需要三个并排放置的div。 The left, middle, and right divs we'll call them. 我们将其称为左,中和右div。 The middle div holds the header contents of the site and is a fixed width (800px). 中间的div包含网站的标题内容,并且是固定宽度(800px)。 I want the left and right div to span the rest of the screen width on either side. 我希望左右div能够跨越屏幕两侧的其余宽度。 So.. 所以..

<-LEFT-> | <-LEFT-> | MIDDLE | 中| <- RIGHT -> <-右->

The reason I want to do it this way is because the middle (content holding) div has a backgrond that is a gradient. 我之所以要这样做是因为中间(内容保存)div的背景是渐变的。 Let's say the left side of the gradient is white and the right side is black. 假设渐变的左侧为白色,右侧为黑色。 I need the Left div to be white so it is a continuation and the Right div to be black. 我需要Left div为白色,以便它是延续,Right div为黑色。 This way it looks like one fluid heading that spans the whole width of the screen. 这样,它看起来就像一个横跨屏幕整个宽度的流体航向。

Thanks. 谢谢。

A solution for this problem I once implemented was using 2 div elements, absolutely positioned, with the center div as an overlay. 我曾经实现的此问题的解决方案是使用2 div元素(绝对定位),以div为中心。 I have a working example here: 我在这里有一个工作示例:

jsFiddle solution jsFiddle解决方案

This way, it doesn't matter how wide the screen is: The div's span 50% of your screen, and the middle part is behind the centered div. 这样,屏幕的宽度就无关紧要了:div的跨度为屏幕的50%,中间部分位于居中的div后面。

Note that you might have to use a javascript workaround for the height-issues. 请注意,您可能必须对高度问题使用javascript解决方法。

Do you want content in the left or right divs? 您要在左或右div中显示内容吗? If not, Simply stick with your one center div, give it a width and position it using margin: 0 auto; 如果没有,只需坚持您的一个中心格,给它一个宽度,并使用margin: 0 auto;定位它margin: 0 auto; in your css. 在你的CSS。 You can then set the background image of the body tag with an image (say 1px by 2400px) that is half white and half black. 然后,您可以使用一半白色和一半黑色的图像(例如1px x 2400px)设置body标签的背景图像。

If you want that effect just behind your header, then you could create a div the same height as the heading and give it the following css properties: 如果您希望该效果位于标题的后面,则可以创建与标题高度相同的div并为其提供以下css属性:

position: absolute;
width: 100%;
z-index: -1;

that way it should sit behind your container (middle) div. 这样,它应该位于容器(中间)div的后面。

You should consider having just one centered div and on the body put a background-image of 1px height and large enough width and centered. 您应该考虑只有一个居中的div,并在主体上放置1px高,足够大的宽度和居中的背景图像。 That image will have the left half white and the right one black. 该图像的左半部分为白色,右半部分为黑色。

Hope this helps, Alin 希望这会有所帮助,Alin

...WWWWW| DIV |BBBBB...

Anyway I don't think it's possible without using a table. 无论如何,我认为不使用表格是不可能的。 Usually floatting div are size-fixed and center div is fluid. 通常,浮动div是固定大小的,而中心div是可变的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不指定宽度和大量数据或文本的情况下并排浮动两个div? - How do I float two divs side-by-side without specifying a width and large amount of data or text? 如何在不指定宽度的情况下并排浮动两个div? - How do I float two divs side-by-side without specifying a width? 如何将两个div并排放置,其背景跨全宽,但其内容包含在换行中? - How to have two divs side-by-side with their backgrounds spanning full width, but their content contained inside a wrap? 如何在不指定宽度的情况下并排浮动两个div,同时保持正确的div最小? - How do I float two divs side-by-side without specifying a width while keeping the right div minimal? 如何并排获得这两个div? - How to get these two divs side-by-side? 如何在移动设备上堆叠两个div,但在桌面上并排放置呢? - How do I stack two divs on mobile but arrange side-by-side on desktop? 如何使两个元素在不同宽度的容器中并排对齐? - How do I make two elements align side-by-side in a different width container? 将两个100%宽度的div并排放置 - Put two 100% width divs side-by-side 如何使div并排固定为一个固定宽度,而其余为屏幕 - How to make divs side-by-side with one fixed width and one the rest of the screen 如何使用 div 创建两个并排的全尺寸文本区域 - How to create two side-by-side full-sized textareas using divs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM