简体   繁体   English

是否可以使用CSS将元素放置在其容器的边界之外?

[英]Is it possible to position an element outside the bounds of it's container using CSS?

Hey guys I have an interesting set up going on. 大家好,我正在进行有趣的设置。 I'm working on creating SOME mobile support for an existing site. 我正在为现有网站创建一些移动支持。 Basically when the window is brought to a certain size or the page is opened up on a phone I want to the header to do something different. 基本上,当窗口达到某个大小或在电话上打开页面时,我希望页眉执行其他操作。 That part is easy the only thing I'm running into is this. 那部分很容易,我遇到的唯一问题就是这个。

The basic structure of my header is this 标头的基本结构是这样

[logo][user-stuff][right-side][1][2][3][/right-side] [标志] [用户东西] [右侧] [1] [2] [3] [/右侧]

These elements are all in a nice line in my header. 这些元素都位于标题的漂亮行中。 My problem is that in mobile I need one of the elements from inside the containing div on the right to float underneath the header. 我的问题是,在移动设备中,我需要右侧包含div内的元素之一浮在标头下方。 So I either need it to pop outside of its container or I need its container to take up with the width of the screen. 因此,我要么需要它弹出其容器之外,要么需要它的容器占用屏幕的宽度。 The idea is that it will end up looking like this. 这个想法是最终看起来像这样。

[logo][user-stuff][right-side][1][2][/right-side] [标志] [用户东西] [右侧] [1] [2] [/右侧]
[ 3 ] [3]

any ideas how this can be done? 任何想法如何做到这一点? If I have to use some Javascript to make this possible that's fine, but the markup needs to be minimal as per my bosses instruction. 如果我必须使用一些Javascript来做到这一点就可以了,但是按照我的老板的指示,标记必须最小。 Just a little stumped on the direction. 方向上只有一点点绊脚石。

current html 当前的html

<div id="header">
  <div id="logo"></div>
  <div id="user-stuff"></div>

  <div id="right-side">
    <div id="1" class="right-side-section"></div>
    <div id="2" class="right-side-section"></div>
    <div id="3" class="right-side-section"></div>
  </div>
</div>

current css 当前的CSS

#header {
  height: 48px;
  width: 100%;
}

#logo {
  display: inline-block;
  vertical-align: top;
}

#user-stuff {
  display: inline-block;
  vertical-align: top;
}

#right-side {
  display: block;
  float: right;
}

.right-side-section {
  display: inline-block;
  vertical-align: top;
}

Of course this is just a little bit of mockup code to give you an idea of the structure i'm working with and how everything is laid out. 当然,这只是一小段样机代码,可以让您大致了解我正在使用的结构以及所有内容的布局方式。 I just need to figure out a way to have div#3 drop underneath everything and take up the width of the screen when the screen is a certain size. 我只需要找出一种方法,使div#3放到所有东西下面,并在屏幕为特定大小时占用屏幕的宽度。 Not sure how to have it breaks it's flow. 不知道如何破坏它的流程。

Since the header has a defined height this will be easy. 由于集管具有确定的高度,因此这很容易。 Just add position: relative so that you can absolutely position child elements relative to itself. 只需添加position: relative以便您可以相对于其自身绝对定位子元素。

Then you can set the css for div#3 to use absolute positioning as in the following example. 然后,您可以将div#3的css设置为使用绝对定位,如以下示例所示。

#header {
  height: 48px;
  width: 100%;
  position: relative;
}

#3 {
    position: absolute;
    top: 48px;
    left: 0;
    width: 100%;
    text-align: center;
}

See working Demo here: http://jsfiddle.net/Cce9n/ 在此处查看工作示例: http : //jsfiddle.net/Cce9n/

Please note that it is not valid to assign an ID starting with a number. 请注意,分配以数字开头的ID是无效的。

You may use Javascript to edit other div definitions, 您可以使用Javascript编辑其他div定义,

EG changing the text-align style EG更改文本对齐样式

document.getElementById("right-side").style.text-align = "center";

暂无
暂无

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

相关问题 CSS:位置嵌套元素稍微超出父元素的边界 - CSS: position nested element slightly outside of parent element's bounds 如果元素在容器之外,是否可以相对于容器进行绝对定位? - Is it possible to position an element with absolute positioning relative to a container if it is OUTSIDE the container? CSS 定位绝对元素,自动宽度超出父边界? - CSS Positioned Absolute Element, automatic width outside of parent's bounds? Css:位置元素相对于它的容器顶部的底部 - Css: Position element by it's bottom relative to it's container's top 即使通过CSS / CSS3滚动时,是否有可能将具有绝对位置的元素“固定”到其父容器的底部? - Is it possible to have an element with absolute position to be 'fixed' to the bottom of it's parent container, even when scrolling via CSS/CSS3? 像容器一样放置绝对元素的宽度(在CSS中) - Position absolute element's width like it's container (in css) CSS输入元素的宽度超出容器 - CSS input element width going outside container 使用CSS将元素移动到使用溢出的容器之外 - With CSS move an element outside of a container that uses overflow 是否可以选择容器中的第一个元素,否则纯文本而不使用纯CSS中的类或标识符? - Is it possible to select the very first element within a container that's otherwise pure text without using classes or identifiers in pure CSS? CSS来调整元素的位置 - A css to adjust an element's position
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM