English 中文(简体)
是否有办法使儿童DIV的宽度比父母DIV使用国家社会保障局的宽广?
原标题:Is there a way to make a child DIV s width wider than the parent DIV using CSS?
  • 时间:2011-04-07 12:30:05
  •  标签:
  • css
  • html

是否有办法在比父母更宽的母集装箱DIV内安置儿童DIV。 儿童DIV需要与浏览器一样的宽度。

See example below: enter image description here

儿童DIVmust作为父母一方的子女居留。 我知道,我会给孩子们造成任意的消极的边缘,使其更加广泛,但我可以说明,如何从根本上使孩子的百分之十。

我知道我可以这样做:

.child-div{
    margin-left: -100px;
    margin-right: -100px;
}

但我需要孩子们与充满活力的浏览器一样。

Update

由于你的答复,现在最接近的答案似乎是将儿童DIV<代码>position:绝对,并将遗留下来的财产和适当财产定为0。

我的下一个问题是,父母有<条码>:相对,意思是,离开和正确的财产仍然与母系有关,而不是与浏览器有关,见以下例子:

我可以删除<代码>:相对,而不必再重述其他内容。

最佳回答

绝对定位

.child-div {
    position:absolute;
    left:0;
    right:0;
}
问题回答

我在很长一段时间内寻求解决这一问题。 理想的做法是,我们想要让孩子比父母多,但事先不知道父母的制约因素。

And I finally found a brilliant generic answer here. Copying it verbatim:

The idea here is: push the container to the exact middle of the browser window with left: 50%;, then pull it back to the left edge with negative -50vw margin.

.child-div {
  width: 100vw;
  position: relative;
  left: 50%;
  right: 50%;
  margin-left: -50vw;
  margin-right: -50vw;
}

根据你的建议原建议(确定负差),我尝试并提出了使用百分单位进行动态浏览器的类似方法:

<><><><>>><>>>>><>>>>>>

<div class="grandparent">
    <div class="parent">
        <div class="child">
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolore neque repellat ipsum natus magni soluta explicabo architecto, molestias laboriosam rerum. Tempore eos labore temporibus alias necessitatibus illum enim, est harum perspiciatis, sit, totam earum corrupti placeat architecto aut minus dignissimos mollitia asperiores sint ea. Libero hic laudantium, ipsam nostrum earum distinctio. Cum expedita, ratione, accusamus dicta similique distinctio est dolore assumenda soluta dolorem quisquam ex possimus aliquid provident quo? Enim tempora quo cupiditate eveniet aperiam.</p>
        </div>
    </div>
</div>

<>CSS:

.child-div{
   margin: 0 -100%;
   padding: 0 -100%;
}

.parent {
    width: 60%;
    background-color: red;
    margin: 0 auto;
    padding: 50px;
    position:relative;
}

.grandparent {
        overflow-x:hidden;
        background-color: blue;
        width: 100%;
        position:relative;
    }

不利幅度将使得内容从父母四肢解中流出。 因此,我确定了<条码>:100%;,将内容推回Chlid DIV的原始边界。

负差还将使child子的总宽度从浏览室中扩大,从而形成横向的滚动。 因此,我们需要通过向祖父母DIV(父母离异的父母)适用<条码>over-x:隐藏的<>/条码>,来揭穿外衣:

www.un.org/Depts/DGACM/index_spanish.htm 这里是JSfiddle

我已尝试过Nils Kaspersson sleft:calc(-50vw + 50%);它在 Chrome和amp做了精干的罚款;FF(不是确定IE, 还是),直到我发现Safaribrowsers没有适当做。 我希望他们在我实际上喜欢这一简单方法时就确定这一点。

This also may resolve your issue where the Parent DIV element has to be position:relative

这一工作方式的2个缺点是:

  • Require extra markup (i.e a grandparent element (just like the good ol table vertical align method isn t it...)
  • The left and right border of the Child DIV will never show, simply because they are outside of the browser s viewport.

Please let me know if there s any issue you find with this method ;). Hope it helps.

Flex Box可用于使子女比父母更宽泛,分三条线路。

Only the child’s display, margin-left and width need to be set. margin-left depends on the child’s width. The formula is:

margin-left: calc(-.5 * var(--child-width) + 50%);

基础设施服务变量可用于避免人工计算左边。

<>Demo #1: 人工计算<>

.parent {
  background-color: aqua;
  height: 50vh;
  margin-left: auto;
  margin-right: auto;
  width: 50vw;
}

.child {
  background-color: pink;
  display: flex;
}

.wide {
  margin-left: calc(-37.5vw + 50%);
  width: 75vw;
}

.full {
  margin-left: calc(-50vw + 50%);
  width: 100vw;
}
<div class="parent">
  <div>
    parent
  </div>
  <div class="child wide">
    75vw
  </div>
  <div class="child full">
    100vw
  </div>
</div>

www.un.org/Depts/DGACM/index_spanish.htm 第2号:使用CSS变量

.parent {
  background-color: aqua;
  height: 50vh;
  margin-left: auto;
  margin-right: auto;
  width: 50vw;
}

.child {
  background-color: pink;
  display: flex;
  margin-left: calc(-.5 * var(--child-width) + 50%);
  width: var(--child-width);
}

.wide {
  --child-width: 75vw;
}

.full {
  --child-width: 100vw;
}
<div class="parent">
  <div>
    parent
  </div>
  <div class="child wide">
    75vw
  </div>
  <div class="child full">
    100vw
  </div>
</div>

I used this:

传真

<div class="container">
 <div class="parent">
  <div class="child">
   <div class="inner-container"></div>
  </div>
 </div>
</div>

CSS

.container {
  overflow-x: hidden;
}

.child {
  position: relative;
  width: 200%;
  left: -50%;
}

.inner-container{
  max-width: 1179px;
  margin:0 auto;
}

我尝试了一些解决办法。 一、导 言

margin: 0px -100%;
padding: 0 100%;

迄今为止,这是最好的,因为我们不需要额外的小屏幕。

I had a similar issue. The content of the child element was supposed to stay in the parent element while the background had to extend the full viewport width.

I resolved this issue by making the child element position: relative and adding a pseudo element (:before) to it with position: absolute; top: 0; bottom: 0; width: 4000px; left: -1000px;. The pseudo element stays behind the actual child as a pseudo background element. This works in all browsers (even IE8+ and Safari 6+ - don t have the possibility to test older versions).

Small example fiddle:

使儿童在内容上更加广泛:

.child{
    position:absolute;
    left:0;
    overflow:visible;
    white-space:nowrap;
}

you can try position: absolute. and give width and height , top: y axis from the top and left: x-axis

除了Nils Kaspersson的解决方案外,我也正在解决垂直滚动的 w。 我正在使用<代码>16px,作为实例,从观察区转出。 这将避免出现横向滚动障碍。

width: calc(100vw - 16px);
left: calc(-1 * (((100vw - 16px) - 100%) / 2));

Assuming the parent has a fixed width, e.g #page { width: 1000px; } and is centered #page { margin: auto; }, you want the child to fit the width of browser viewport you could simply do:

#page {
    margin: auto;
    width: 1000px;
}

.fullwidth {
    margin-left: calc(500px - 50vw); /* 500px is half of the parent s 1000px */
    width: 100vw;
}
.parent {
    margin:0 auto;
    width:700px;
    border:2px solid red;
}
.child {
    position:absolute;
    width:100%;
    border:2px solid blue;
    left:0;
    top:200px;
}

然后解决办法如下。

<><><><>>><>>>>><>>>>>>

<div class="parent">
    <div class="child"></div>
</div>

<>CSS

.parent{
        width: 960px;
        margin: auto;
        height: 100vh
    }
    .child{
        position: absolute;
        width: 100vw
    }

body {
  margin: 0;
  overflow-x: hidden;
  /* This ensures that any content overflowing the viewport horizontally is hidden */
}

.parent-container {
  position: relative;
  width: 100%;
  max-width: 1200px;
  /* Set a maximum width for the parent container if desired */
  margin: 0 auto;
  /* Center the parent container */
  background-color: #f0f0f0;
}

.wider-child {
  position: absolute;
  top: 0;
  left: 50%;
  /* Position the left edge at the center of the viewport */
  transform: translateX(-50%);
  /* Adjust to center the child div */
  width: 100vw;
  /* 100% of the viewport width */
  height: 200px;
  /* Adjust the height as needed */
  background-color: #3498db;
  color: #fff;
  text-align: center;
  padding: 20px;
}
<div class="parent-container">
  <div class="wider-child">
    <p>This is a wider child div.</p>
  </div>
  <!-- Other content within the parent container goes here -->
</div>

这样做的一个途径是根本不使用任何儿童,仅仅制造两个单独的小区,将其定位在另一个地方。 你们只能使顶上的灰.有100伏。 你们也可以把这种方法带给他们。

#parent-div {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 60vw;
  height: 90vh;
  border: 3px solid black;
  background: red;
}
#child-div {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 90vw;
  height: 50vh;
  border: 3px solid black;
  background: grey;
}
<div id="parent-div">
  <div id="child-div">
  </div>
</div>

EDIT; for your example:

#parent-div {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 960px;
  height: 100vh;
  border: 3px solid black;
  background: red;
}
#child-div {
  position: fixed;
  top: 30%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 100vw;
  height: 33vh;
  border: 3px solid black;
  background: grey;
}
<div id="parent-div">
  <div id="child-div">
  </div>
</div>
Please go to full page because you said 960px so its larger than the small screen.




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签