简体   繁体   English

将DIV右对齐

[英]Aligning DIV to the right

Im using a set of layers, with this CSS 我在此CSS中使用了一组图层

.left1 {
    float: left;
    left: 5px;
    width: 72px;
    height: 100px;
}
.left2 {
    height: 100px;
    background-color: red;
    margin-left: 186px;
}
.eventCat {
    float: right;
    width: 5px;
    height: 100px;
}

to make inline divs. 进行内联div。 however, when i add a layer that i wish to be align to the right, it seems to fall below (the green one .eventCat ). 但是,当我添加一个希望与右边对齐的图层时,它似乎会落在下面(绿色的.eventCat )。 It should be at the right hand side of the red box! 它应该在红色框的右侧! even with float:right; 即使使用float:right; what am i missing? 我想念什么?

I made a fiddle.. http://jsfiddle.net/7GBca/ to fiddle with :) 我做了一个小提琴.. http://jsfiddle.net/7GBca/来提琴:)

It is not floating correctly because .float2 is not floted, my guess is you want it to expand to fill all available width and that's why you didn't set an explicit width. 它没有正确浮动,因为.float2没有浮动,我想您要扩展它以填充所有可用宽度,这就是为什么未设置显式宽度的原因。 One solution to align .eventCat correctly would be to use position:absolute; 正确对齐.eventCat一种解决方案是使用position:absolute; .eventCat and use right:0; 并使用right:0;

.wrapper {
    position: relative; 
}
.eventCat {
    position: absolute;
    right: 0;
    top: 0;
    width: 5px;
    height: 100%;
}
.left2 {
    padding-right: 5px; /*set this to the width of .eventCat so it does not overlap*/
}

Example fiddle 小提琴的例子

The easiest way to fix this is set a negative top margin to your green div: 解决此问题的最简单方法是为绿色div设置负的上边距:

.eventCat {
  margin: -100px 0 0 0;
  float: right;
  width: 5px;
  height: 100px;
}

Example Fiddle 小提琴的例子

您缺少“ left2”的“ width:... px”和“ float:left”,以及“ wrapper”的“ width:... px”。

Your issue is fixed, please check the code here: 您的问题已解决,请在此处检查代码:

<style type="text/css">
.eventCat {
float: right;
width: 5px;
height: 100px;
}
.eventIMG {
float: left;
left: 0;
width: 100px;
height: 100px;
}
.left1 {
float: left;
left: 5px;
width: 72px;
height: 100px;
}
.left2 {
height: 100px;
background-color: red;
margin-left: 186px;
}
.set:hover {
background-color: #FFDEAD;
cursor: pointer;
cursor: hand;
}
#event.title {
font-size: 21px;
font-weight: bold;
color: black;
text-decoration: none;
}
#event {
color: black;
}
</style>

and here is your HTML 这是你的HTML

<div class="wrapper">
  <div class="eventIMG" style="background:url('http://hqhq.dk/beta/wp-content/uploads/2013/07/png-5.jpg') no-repeat scroll center center / 100% auto transparent"></div>
  <div class="left1">
    <div style="text-transform: uppercase;">WED 18.09</div>
    <div style="text-transform: uppercase;">kl.22.00</div>
  </div>
  <div class="eventCat" style="background-color:green;"></div>
  <div class="left2" id="event">
    <div id="event" class="title"><a class="url" href="http://hqhq.dk/beta/event/fuzz-manta-dron/" title="FUZZ MANTA + DRÖN" rel="bookmark">FUZZ MANTA + DRÖN</a></div>
    <div></div>
    <div class="">
      <p>something here</p>
      <a href="http://hqhq.dk/beta/event/fuzz-manta-dron/" class="" rel="bookmark">Find out more »</a> </div>
  </div>
</div>
<div class="wrapper">
  <div class="eventIMG" style="background:url('http://hqhq.dk/beta/wp-content/uploads/2013/07/png-5.jpg') no-repeat scroll center center / 100% auto transparent"></div>
  <div class="left1">
    <div style="text-transform: uppercase;">WED 18.09</div>
    <div style="text-transform: uppercase;">kl.22.00</div>
  </div>
  <div class="eventCat" style="background-color:green;"></div>
  <div class="left2" id="event">
    <div id="event" class="title"><a class="url" href="http://hqhq.dk/beta/event/fuzz-manta-dron/" title="FUZZ MANTA + DRÖN" rel="bookmark">FUZZ MANTA + DRÖN</a></div>
    <div></div>
    <div class="">
      <p>something here</p>
      <a href="http://hqhq.dk/beta/event/fuzz-manta-dron/" class="" rel="bookmark">Find out more »</a> </div>
  </div>
</div>

What you have to do there is to replace you div.eventCat with div.left2 that's it :-) 您要做的就是将div.eventCat替换为div.left2 :-)

Fiddle : http://jsfiddle.net/KRU9U/ 小提琴: http : //jsfiddle.net/KRU9U/

Cheers 干杯

eventCat (要浮动到右侧的元素)作为wrapper下的第一个元素。

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

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