简体   繁体   English

HTML / CSS布局浮动吗?

[英]HTML/CSS Layout Floating?

I am trying to layout something that initially needs to look like: 我正在尝试布局最初需要看起来像的东西:

http://screencast.com/t/xbCY636Pi1d9

And then, I need to add 2 divs via jQuery to make it look like: 然后,我需要通过jQuery添加2个div使其看起来像:

http://screencast.com/t/xL485PQT

I am trying to put everything into a div container, and then creating other divs for: 我试图将所有内容放入div容器,然后为以下对象创建其他div:

  • the main square, 大广场
  • the rectangle at the bottom, 底部的矩形
  • and then when required a div that contains 2 other divs (the 2 squares). 然后在需要时再包含另外2个div(2个正方形)的div。

When the 2 squares are not there, the main square and the bottom rectangle must take the whole width of the container. 如果不存在2个正方形,则主正方形和底部矩形必须占据容器的整个宽度。

I've tried floating left the div containing the 2 squares and some other stuff but I fail. 我试着向左浮动包含2个正方形和其他一些东西的div ,但是我失败了。

HTML looks like: HTML看起来像:

<div id="container">
    <div id="videos">
        <video autoplay muted id="localVideo"></video>
        <br>
        <video autoplay id="remoteVideo"></video>
    </div>
    <div id="chatMessagesWindow"></div>
    <input autofocus="autofocus" placeholder="Enter a message" id="chatInput">
</div>

CSS looks like: CSS看起来像:

#container {
  width: 850px;
  border: 1px solid black;
  height: 580px;
}

#chatMessagesWindow {
  width: 70%;
  height: 100%;
  border: 9px solid gray;
  margin-bottom: 7px;
  overflow: auto;
  float: right;
}

#chatInput {
  width: 100%;
  height: 35px;
  border: 1px solid gray;
  float: right;
}


#videos {
  display: none;
  width: 30%;
  float: left;
}

#videos video {
  border: 1px solid black;
}

In JavaScript I show .show() the #videos element. 在JavaScript中,我显示.show() #videos元素。

Fiddle: http://jsfiddle.net/67WLG/1/ 小提琴: http : //jsfiddle.net/67WLG/1/

I have made this example in jsfiddle 我在jsfiddle中做了这个例子

try this link 试试这个链接

JS: JS:

$('#container').on('click', function () {
    $('#videos').show();
});

CSS: CSS:

#right_blk {  width:auto;display:table; }
#container {
  width: 850px;
  border: 1px solid black;
  height: 580px;
}

#chatMessagesWindow {
  width: 98%;
  height: 562px;
  border: 9px solid gray;
  margin-bottom: 7px;
  overflow: auto;
  float: left;
  display:table;
}

#chatInput {
  width: 100%;
  height: 35px;
  border: 1px solid gray;
  float: right;
}


#videos {
  display: none;
  width: 30%;
  float: left;
}

#videos video { width:99.1%;
  border: 1px solid black;
}

HTML: HTML:

<div id="container">
    <div id="videos">
        <video autoplay muted id="localVideo"></video>
        <br>
        <video autoplay id="remoteVideo"></video>
    </div>

    <div id="right_blk">
    <div id="chatMessagesWindow">
     asadad asdasdasd asds dsadasds asdasd asadad asdasdasd asds dsadasds asdasd asadad asdasdasd asds dsadasds asdasd asadad asdasdasd asds dsadasds asdasd asadad asdasdasd asds dsadasds asdasd asadad asdasdasd asds dsadasds asdasd asadad asdasdasd asds dsadasds asdasd asadad asdasdasd asds dsadasds asdasd asadad asdasdasd asds dsadasds asdasd 
        </div>
        <input autofocus="autofocus" placeholder="Enter a message" id="chatInput">
    </div>
</div>

perhaps this will help you! 也许这对您有帮助!

html: HTML:

<div class="page-wrap">
    <div class="existed-content">
        <div class="main-content">this is a big div</div>
        <div class="main-footer">this is your footer content</div>
    </div>
</div>

css: CSS:

.page-wrap {
    clear: both;
}
.existed-content {
}
.js-added-content {
    float: left;
    width: 130px; //this can be defined by yourself!
}

js(with jQuery): js(使用jQuery):

$(function() {
    var STR = "<div class='js-added-content'><div>this is left top box!</div><div>this is left bottom box!</div></div>";
    var $pageW = $('.page-wrap');
    $pageW.prepend(STR);
});

This sounds like a job for CSS-Tables : all of the handy layout features of HTML Tables with none of the content pollution! 这听起来像是CSS-Tables的工作:HTML Tables的所有便捷布局功能都不会造成内容污染! Or at least minimal content pollution. 或至少内容污染最小。 I did need to add a div to serve as a container for your chat messages and chat input elements: 我确实需要添加一个div作为您的聊天消息和聊天输入元素的容器:

<div id="container">
    <div id="videos">
        <video autoplay muted id="localVideo"></video>
        <br>
        <video autoplay id="remoteVideo"></video>
    </div>
    <div id="chatContainer">
        <div id="chatMessagesWindow"></div>
        <input autofocus="autofocus" placeholder="Enter a message" id="chatInput">
    </div>
</div>

Changes to your CSS basically involved adding in the new chatContainer id, getting rid of any floats, and adding in some display: lines. 对CSS的更改基本上涉及添加新的chatContainer id,摆脱任何浮点数以及添加一些display:行。 The box-sizing lines at the beginning are to make sure that any borders are taken into account when determining the width and height of elements. 开头的框大小调整线用于确保确定元素的宽度和高度时考虑任何边界。 For some reason, the default is to ignore the borders and base sizes only on the interior of an element. 由于某些原因,默认设置是仅忽略元素内部的边框和基本尺寸。 You can get rid of those lines if you don't end up using any borders. 如果您最终不使用任何边框,则可以摆脱这些线条。

div {
    box-sizing: border-box;
    -moz-box-sizing: border-box;
}

#container {
    width: 850px;
    border: 1px solid black;
    height: 580px;
    display: table;
}

#chatContainer {
    border: 1px solid red;
    display: table-cell;
}

#chatMessagesWindow {
    width: 100%;
    height: 530px;
    border: 9px solid gray;
    margin-bottom: 7px;
    overflow: auto;
}
#chatInput {
    width: 100%;
    height: 35px;
    border: 1px solid gray;
}
#videos {
    border: 2px solid green;
    display: none;
}

#videos video {
    border: 1px solid black;
}

The videos div can be hidden in JavaScript with this syntax: <object>.style.display="none" and shown with <object>.style.display="table-cell" , where is of course the videos div. 可以使用以下语法在JavaScript中隐藏<object>.style.display="none" div: <object>.style.display="none"并显示为<object>.style.display="table-cell" ,当然,其中还有<object>.style.display="table-cell" div。

The video elements themselves probably still need some positioning work though and some other elements may need sizing tweaks as well. 视频元素本身可能仍然需要进行一些定位工作,而其他一些元素可能还需要调整大小。

In my own scenario, first, I would create a global-wrapper that will take the 850px width . 在我自己的场景中,首先,我将创建一个global-wrapper ,其850px width850px width

It would contain the #videos ( by default - display:none; ) and the chat #container which would be floated left and inheriting the width of global-wrapper . 它包含#videos (默认情况下为display:none; )和chat #container ,该#videos将向左浮动并继承global-wrapper的宽度。

On #container 's click() : #containerclick()

  1. set the #videos 's width 1/3rd (for instance) and float: left . 设置#videos的宽度1 / 3rd(例如)并进行float: left
  2. set the #container 's width to remaining 2/3rd and float it left . #container的宽度设置为剩余的2 / 3rd并将float it left
  3. call the show() method on #videos #videos上调用show()方法

Instead of using jQuery's css() method, use predefined CSS classes and add them dynamically. 不要使用jQuery的css()方法,而要使用预定义的CSS类并动态添加它们。

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

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