سؤال

I'm having an issue with some text that doesn't want to create a line break: when narrowing the browser window, it makes the text go on top of itself. How is it possible to fix this?

See the h1 and p text here:

http://jsfiddle.net/wWJqm/ and try resizing down your browser window.

HTML:

<div class="meteor">
    <img width="599" height="400" src="http://imageshack.com/a/img197/9543/2v8z.jpg" />
        <h1><a href="" title="Nulla eu purus et orci">Nulla eu purus et orci</a></h1>

    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
</div>

CSS:

.meteor h1 {
    position: absolute;
    margin: 0;
    top: 20px;
    left: 30px;
    font-size:24px;
    line-height:1px;
    text-align: center;
    text-shadow: 1px 1px 0 #000;
    padding: 2.3%;
    background: #000;
}
.meteor p {
    background: #000;
    top: 50px;
    left: 30px;
    color: #fff;
    line-height:1px;
    margin: 0;
    padding: 2%;
    position: absolute;
    text-align: center;
    text-shadow: 1px 1px 1px #000;
}
هل كانت مفيدة؟

المحلول

Try this

.meteor p {
background: #000;
min-height:inherit;
max-height:auto;

top: 50px;
left: 30px;
color: #fff;

margin: 0;
padding: 2%;
position: absolute;
text-align: center;
word-wrap: break-word;
text-shadow: 1px 1px 1px #000;
}

your line height was set to 1px, it was moving, but it was only moving down one pixel which is almost unnoticeable. get rid of the line-height, or change it. Add in a word wrap, and a min/max-width and height is optional too.

نصائح أخرى

I think it's the line-height and the fact that your wrapper doesn't seem to have position:relative

JSFiddle Demo

CSS

.meteor {
    background-color: lightgrey;
    position: relative;
    font-size:16px;
}

.meteor img {
    max-width: 100%;
    height:auto;
    position: absolute;
    top:0;
}

.meteor h1 {
    position: absolute;
    margin: 0;
    top: 20px;
    left: 30px;
    line-height: 1em;
    font-size:24px;
    text-align: center;
    text-shadow: 1px 1px 0 #000;
    padding: 2.3%;
    background: #000;
}
.meteor p {
    background: #000;
    top:100px;
    left: 30px;
    color: #fff;
    line-height:1em;
    margin: 0;
    padding: 2%;
    position: absolute;
    text-align: center;
    text-shadow: 1px 1px 1px #000;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top