简体   繁体   English

两个DIVS,一种流体,一种固定的,固定的div(不考虑高度)在内部具有垂直居中的元素

[英]two DIVS, one fluid, one fixed, fixed div regardless of height has vertically centered element inside

This is as far as I got. 据我所知。 So far I am unable to vertically centre the icon in the right DIV regardless of the height. 到目前为止,无论高度如何,我都无法在右侧DIV中垂直居中图标。 I am also unable to get the text in the left DIV to break when it hits it's max length. 当它达到最大长度时,我也无法使左DIV中的文本中断。

Here is what I have: http://jsfiddle.net/SpSjL/1685/ 这是我所拥有的: http : //jsfiddle.net/SpSjL/1685/

both boxes should remain the same height, if there are a few lines of text in the left, the right should adjust as well 两个框应保持相同的高度,如果左侧有几行文字,右侧也应调整

expected: 预期: 在此处输入图片说明

HTML: HTML:

  <div class="container">

    <div class="right">
        <div class="someIcon"></div>
    </div> 
    <div class="left">
aaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbcccdcccccccccccccccccccccccccccccdccccccccddccccc
    </div>

</div>

CSS: CSS:

    .left {
    overflow: hidden;

    border: 1px solid #FF0000;
    /* word-wrap: break-word;
    word-break: break-all;
    text-wrap: normal; 
    white-space: normal;*/
    word-break: break-all;
}

.container {
    display: block;
    position: relative;
    overflow: hidden;
}

.someIcon {
    background: url(http://www.adiumxtras.com/images/thumbs/adiumtwit_twitters_icon_for_adium_1_31971_7892_thumb.png);
    background-size: 20px;
    width: 20px;
    height: 20px;
    margin: 0 auto;
    position: absolute;
    top: 50%:
}

.right {
    float: right;
    width: 50px;
    min-height: 50px;
    border: 1px solid #000000;
    display:table-cell; 
    vertical-align:middle;
}

I simplified your layout by using the background properties position attributes to keep it centered vertically and to the right. 我通过使用背景属性position属性使布局保持垂直居中和向右居中来简化了布局。 You shouldn't need to do any floating. 您不需要进行任何浮动。 I try and keep my layouts as simple as possible. 我尝试使布局尽可能简单。

CSS CSS

.container {
    border:1px solid black;
    background: url(http://www.adiumxtras.com/images/thumbs/adiumtwit_twitters_icon_for_adium_1_31971_7892_thumb.png) center right 15px no-repeat;
    position: relative;
    margin: 5px;
    padding: 15px 75px 15px 15px;
}

wrapword class pulled from CSS: How do I wrap text with no whitespace inside a <td>? CSS提取的wrapword类:如何在<td>内包装没有空格的文本?

.wrapword{
white-space: -moz-pre-wrap !important;  /* Mozilla, since 1999 */
white-space: -pre-wrap;      /* Opera 4-6 */
white-space: -o-pre-wrap;    /* Opera 7 */
white-space: pre-wrap;       /* css-3 */
word-wrap: break-word;       /* Internet Explorer 5.5+ */
word-break: break-all;
white-space: normal;
}

HTML HTML

<div class="container wrapword">
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
</div>

<div class="container wrapword">
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sit amet facilisis tellus. Aliquam eu mi in augue faucibus egestas sed convallis lorem. Nulla rutrum purus ac dolor dictum viverra.
</div>

<div class="container wrapword">
    a short sentence
</div>

update : removed unecessary div, support for one really long word (although I imagine that isn't necessary) http://jsfiddle.net/SpSjL/1708/ 更新 :删除不必要的div,支持一个非常长的单词(尽管我认为这不是必需的) http://jsfiddle.net/SpSjL/1708/

you can adjust the padding on the background and the container to fit your image. 您可以调整背景和容器上的填充以适合您的图像。

First, the icon, it's quite simple : 首先,图标非常简单

Remove the .someIcon element, and put the icon as a centered background of the .right div : 删除.someIcon元素,并将图标作为.right div的居中背景:

.right {


   background: url(http://www.adiumxtras.com/images/thumbs/adiumtwit_twitters_icon_for_adium_1_31971_7892_thumb.png) center center no-repeat;
    background-size: 20px;
    float: right;
    width: 50px;
    min-height: 50px;
    border: 1px solid #000000;
    display:table-cell; 
    vertical-align:middle;
    }

The layout problem needs more time, I come with that soon.

EDIT : 编辑:

Here you go :), working demo : http://jsfiddle.net/SpSjL/1709/ 在这里,您可以:), 正在运行的演示http : //jsfiddle.net/SpSjL/1709/

You have to use display: table cell; 您必须使用display:table cell; on both divs : 在两个div上:

    <div class="container">


        <div class="left">
    aaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbcccdcccccccccccccccccccccccccccccdccccccccddccccc
        </div>
         <div class="right">
            <div class="someIcon"></div>
        </div> 
        <div class="clear"></div>
    </div>

CSS : CSS:

.left {
display: table-cell;
    margin-right: 52px;
    border: 1px solid #FF0000;
    /* word-wrap: break-word;
    word-break: break-all;
    text-wrap: normal; 
    white-space: normal;*/
    word-break: break-all;

}

.container {
    display: block;
    position: relative;
    height: 100px;
}



.right {
     background: url(http://www.adiumxtras.com/images/thumbs/adiumtwit_twitters_icon_for_adium_1_31971_7892_thumb.png) center center no-repeat;
    background-size: 20px;
    display: table-cell;
    width: 50px;
    min-height: 50px;
    border: 1px solid #000000;

}

And I cleaned up all your code. 然后我清理了所有代码。

check this FIDDLE 检查此FIDDLE

Simply remove float from your right div, and place it after the left div. 只需从右div删除float ,然后将其放在左div之后即可。

Updated FIDDLE with minimal CSS 用最少的CSS更新了FIDDLE

Well, if you know your icon is always going to be fixed in size, and you are willing to resort to absolute positioning, what's stopping you from using the negative margins? 好吧,如果您知道图标的大小总是固定的,并且愿意采用绝对定位,那么是什么阻止您使用负边距呢?

.someIcon {
    background: url(http://www.adiumxtras.com/images/thumbs/adiumtwit_twitters_icon_for_adium_1_31971_7892_thumb.png);
    background-size: 20px;
    width: 20px;
    height: 20px;
    margin: -10px;
    left: 50%;
    top: 50%;
    position: absolute;
}

Here is the updated fiddle . 这是更新的小提琴

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

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