简体   繁体   English

CSS Div内容定位

[英]CSS Div Positioning With Content

I want to position 2 DIVs in a row like { [DIV 1] [DIV 2] }, but the problem I have is that some part of the div(the background) won't move with the other parts(text). 我想像{[DIV 1] [DIV 2]}那样连续放置2个DIV,但是我遇到的问题是div(背景)的某些部分不会与其他部分(文本)一起移动。 What can I do to get it fixed? 我该如何解决?

This is the CSS of the DIVs: 这是DIV的CSS:

   .container {
       position:relative;

   }

   .content {
       position:relative;
       color:White;
       z-index:5;
   }

   .background {
       position:absolute;
       top:0px;
       left:0px;
       width:35%;
       height:100%;
       background-color:Black;
       z-index:1;
       -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
       filter: alpha(opacity=50);
       opacity:.5;
   }

   .container1{
       position:relative;
   }

   .content1 {
       position:relative;
       color:White;
       z-index:5;
   }

   .background1 {
       position:absolute;
       top:0px;
       left:0px;
       width:15%;
       height:100%;
       background-color:Black;
       z-index:1;
       -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
       filter: alpha(opacity=50);
       opacity:.5;
  }

And this is the HTML part: 这是HTML部分:

<div class="container">
  <div class="content">
    Random Content Here 1234556678
  </div>
  <div class="background"></div>
</div>

<div class="container1">
  <div class="content1">
    Other Random Content 1423563262
  </div>
  <div class="background1"></div>
</div>

Your objective is a little unclear but I'll take a shot anyway. 您的目标尚不清楚,但是我还是会尝试一下。 From what you have originally posted you have more markup (HTML) than you need and the same goes for the CSS for what you are asking for. 从您最初发布的内容来看,您拥有的标记(HTML)超出了您的需要,而CSS的需求也是如此。 There might be a reason but it's not defined for us. 可能是有原因的,但尚未为我们定义。

First off, float your DIVs to the left so they will line up next to each other. 首先,将DIV浮动到左侧,以便它们彼此并排排列。 Then simply apply a class to each DIV so you can control which background image gets applied to each. 然后只需将一个类应用于每个DIV,以便您可以控制将哪个背景图像应用于每个DIV。

http://jsfiddle.net/p3Hb5/ http://jsfiddle.net/p3Hb5/

CSS 的CSS

#wrapper {
     margin: 0 auto;
     width: 900px;
}
div {
     font: bold 16px/1.5 Arial, sans-serif;
}
.first,
.second {
     float: left;
     padding: 10px;
     color: #FFFFFF;
}
.first {
     background-image: url('http://lorempixel.com/200/200/abstract/');
}
.second {
     background-image: url('http://lorempixel.com/200/200/city/');
}

HTML 的HTML

<div id="wrapper">
     <div class="first">
          <p>Some content content content content content</p>
     </div>
     <div class="second">
          <p>Some content content content content content</p>
     </div>
</div>

I don't understand the point of your background div class but this floats your content divs in a line like you're asking for...please let me know if you're looking for additional help, your question isn't very clear 我不了解您的背景div类的要点,但是这会使您的内容div像您要的那样浮动在一行中...请让我知道您是否在寻找其他帮助,您的问题不是很清楚

<div class="container">
     <div class="content">
        <p>Random Content Here 1234556678</p>
            <div class="background">
            </div>
    </div>

        <div class="content">
            <p>Other Random Content 1423563262</p>
                <div class="background">
                </div>
        </div>
</div>    


.container {
  margin: auto;
  width: 100%;
  padding: 1em;
}
.content {
  margin: auto;
  padding: 1em;
  float: left;
  background: #666;
  color: #FFF;
}

EDIT: If the background divs are an attempt to set the background of "content" divs, then this would be the code: 编辑:如果背景div试图设置“内容” div的背景,那么这将是代码:

    <div class="container">
     <div class="content" id="one">
        <p>Random Content Here 1234556678</p>
    </div>

        <div class="content" id="two">
            <p>Other Random Content 1423563262</p>
        </div>
    </div>    



.container {
  margin: auto;
  width: 100%;
  padding: 1em;
}
.content {
  margin: auto;
  padding: 1em;
  float: left;
  color: #FFF;
}  
#one {
  background: #09F;
}
#two {
  background: #666;
}

http://jsfiddle.net/2kq3r/ http://jsfiddle.net/2kq3r/

If you want the two divs to display in a row into a container, you can set 如果您希望两个div在容器中连续显示,则可以设置

clear:both

for the container, and 用于容器,以及

float: left //or right

for the divs 对于div

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

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