Pergunta

I am trying to surround div with position: absolute by text. Always text going under div.

CSS which I am using:

.all {
    display: block;
    width: 250px;
    min-height: 180px;
    height: auto;
    position: relative;
    background: #fa65fc;
}
.abs {
    position: absolute;
    top:  80px;
    left: 200px;
    float: right;
    width: 50px;
    height: 100px;
    background: #4542df;
}

Two divs:

<div class="all">
    <div class="abs">ABS</div>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
</div>

Here is link to: JSFiddle

I would like to do something like this:

enter image description here

Thanks in advance.

Foi útil?

Solução

You can achieve the result you are looking for by using css to insert a dummy element. This method means you do not need to position your <div class="abs"> within the middle of the content of that div. This may be of use if you are not able to control what the content is (in the case of content being entered in a cms).

HTML:

<div class="all">
    <div class="abs">ABS</div>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
</div>

CSS:

.all {
    display: block;
    width: 250px;
    min-height: 180px;
    height: auto;
    position: relative;
    background: #fa65fc;
}

.all:before { 
    content: "";
    float: right;
    height: 80px;
    width: 0;
}

.abs {
    clear: both;
    float: right;
    width: 50px;
    height: 100px;
    background: #4542df;
}

Link to JSFiddle.

.all:before inserts a dummy element which is floated right, no width and 80px high at the very beginning of <div class="all">.

Because .abs is floated right (but not positioned absolute), it will now try and stay floated right at the top of the div. Adding clear: both forces it drop below any other floated elements, so it ends up moving 80px down to clear the dummy float before it.

Outras dicas

You cannot do that with position absolute. However you can achieve what you show on your image using static position and float: right; with some margins.

Here is an updated jsFiddle: http://jsfiddle.net/U5Pg5/2/

HTML:

<div class="all">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
<div class="abs">ABS</div>
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum
</div>

CSS:

.all {
    display: block;
    width: 250px;
    min-height: 180px;
    height: auto;
    position: relative;
    background: #fa65fc;
}
.abs {
    float: right;
    width: 50px;
    height: 100px;
    background: #4542df;
    margin-left: 15px;
    margin-top: 10px;
    margin-bottom: 10px;
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top