简体   繁体   English

将100%宽度的div放入WordPress的页面内容容器中

[英]Putting 100% width div inside page content's container in WordPress

I guess this question equally applies to CSS and WordPress. 我想这个问题同样适用于CSS和WordPress。 I'm using Bootstrap but it's not necessarily relevant to the issue. 我正在使用Bootstrap,但这与问题不一定相关。

I have a design that calls for a full width section (a full width image with text) in the middle of a WordPress page. 我有一个设计,要求在WordPress页面的中间有一个全角部分(带有文本的全角图像)。 This diagram basically outlines the layout: 该图基本上概述了布局:

http://postimg.org/image/xls5kg9yf/ http://postimg.org/image/xls5kg9yf/

The header and footer in gray are full width. 灰色的页眉和页脚为全角。 The page content (in yellow) sits within a Boostrap container, with a max-width of 1170px. 页面内容(黄色)位于Boostrap容器中,最大宽度为1170px。 The red section represents the full width div with an image and text which need to be inserted in the middle of the page content. 红色部分表示全幅div,其中需要在页面内容的中间插入图片和文字。

What is the best way to make this happen? 做到这一点的最佳方法是什么? The page's content has to sit inside of a container class for obvious reasons. 由于明显的原因,页面的内容必须位于容器类的内部。 The client wants the full width section to sit in the middle of the page's content, as the text above and below it needs to be editable via the dashboard. 客户希望全宽部分位于页面内容的中间,因为它上方和下方的文本需要通过仪表板进行编辑。 So I can't just edit the page template and put the full width image after the container which encloses the page content. 因此,我不能只编辑页面模板并将全角图像放在包含页面内容的容器之后。

position: absolute; won't work, because the image needs to remain within the flow of the page. 将无法使用,因为图像需要保留在页面流中。 Additionally, Boostrap columns use position: relative so it would just position itself relative to the parent column, which sits inside a container, and therefore wouldn't be 100% width. 另外,Boostrap列使用position: relative因此它只会相对于位于容器内部的父列进行自身定位,因此宽度不会为100%。

I could use margin-left: -9999px & margin-right: -9999px (or whatever number) to make it 100% width, but if those margins aren't exactly equal to window width - container width then the background image is going to stretch well past 100%. 我可以使用margin-left: -9999pxmargin-right: -9999px (或任意数字)将其设置为100%宽度,但是如果这些边距与窗口宽度-容器宽度不完全相等,则背景图片将变为延伸到100%以上

The only possible solution I can think of is to use jQuery to get the window width, then set the section's width to the same as the window, with a negative left margin equal to (window width - container width)/2. 我能想到的唯一可能的解决方案是使用jQuery获取窗口宽度,然后将节的宽度设置为与窗口相同,负的左边距等于(窗口宽度-容器宽度)/ 2。

So for example, if the window was 1400px wide and the container 1170px then the full width section would be set using a script to: 因此,例如,如果窗口的宽度为1400px,容器的宽度为1170px,则可以使用脚本将全宽部分设置为:

.full-width-section {
  width: 1400px;
  margin-left: -115px;
}

Of course the script would need to run when the window is resized as well. 当然,当调整窗口大小时,脚本也需要运行。 Overall this seems like a very convoluted solution. 总体而言,这似乎是一个非常复杂的解决方案。 Any simple solution using just CSS would be amazing. 仅使用CSS的任何简单解决方案都将是惊人的。

Here's the relevant CSS and HTML for reference: 以下是相关的CSS和HTML供参考:

HTML: HTML:

<div class="container">
  <div class="row">
    <div class="col-xs-12">
      [page content, <?php the_content(); ?> is here on the page template]
      <div class="full-width-section"></div>
      [more page content]
    </div>
  </div>

CSS: CSS:

.container {
    width: 1170px;
}

.col-xs-12 {
    width: 100%;
    position: relative;
}

Jquery might be the solution. jQuery可能是解决方案。 How does it go with margin-left:-100%;? 剩余利润率如何:-100%;?

Else you might be able to close the .container class, sett your slider image thingy and then start the .container again... so you can calcualte from full window with with the slider. 否则,您也许可以关闭.container类,设置滑块图像为问题,然后再次启动.container ...,以便可以使用滑块从整个窗口进行计算。

I ended up just closing the divs before the full width section, and then opening them again afterwards. 我最后只是在全宽部分之前关闭了div,然后在之后再次打开它们。 I'm actually using a shortcode for the section so I just added the HTML to close and then re-open the tags to the shortcode's output. 我实际上是在该部分使用简码,所以我只是添加了HTML以使其关闭,然后将标签重新打开到简码的输出中。

It certainly doesn't seem like best practice, but between that or a complicated jQuery alternative it was the easier way to go. 当然,这似乎不是最佳实践,但是在这之间或一个复杂的jQuery替代方法中,这是更简单的方法。

If you don't need to support IE8, there's an alternative solution. 如果您不需要支持IE8,则有另一种解决方案。 This does not involve any modification on the markup, using absolute positionning or jQuery. 使用绝对定位或jQuery,这不涉及对标记的任何修改。

Instead, you can use the css calc() function and viewport units to apply the right negative margin and padding. 相反,您可以使用css calc()函数和视口单位来应用右负边距和填充。

I made an example @ http://codepen.io/manuszep/pen/pvqqwp/ 我做了一个例子@ http://codepen.io/manuszep/pen/pvqqwp/

The negative left margin is calculated like so : 负的左边距的计算方法如下:

margin-left: calc(((-100vw + 960px) / 2) - 20px);

where 960px is the width of the page and 20px is the padding of the containers 其中960px是页面的宽度,而20px是容器的填充

To center again the content to the size of the wrapper, you can reapply a padding: 要将内容再次居中包装的大小,可以重新应用填充:

padding-right: calc(((100vw - 960px) / 2) + 20px);
padding-left: calc(((100vw - 960px) / 2) + 20px);

That way, your div background is 100% width of the viewport but it's content is exactly as wide as your container. 这样,您的div背景就是视口的100%宽度,但是其内容恰好与容器一样宽。

I tried this with a container width set in % but it did not work correctly. 我尝试使用以%设置的容器宽度进行此操作,但无法正常工作。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM