浮动图像与可变宽度和标题与背景图像 [英] Floated image with variable width and heading with background image

查看:235
本文介绍了浮动图像与可变宽度和标题与背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Test</title>
    <style type="text/css">
        body {font-family:Arial, Helvetica, sans-serif;font-size:12px;}
        .article {width:600px;}
        .image_container {float:left;margin-right:10px;}
        h1 {height:50px;line-height:50px;background:url(left.png) no-repeat 0 0;}
        h1 span {display:block;padding:0 10px;background:url(right.png) no-repeat top right;}
    </style>    
    </head>
    <body>
        <div class="article">
            <div class="image_container">
                <img src="test.jpg" alt="test_img" />
            </div>
            <h1><span>Test heading</span></h1>
            <p>
                There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
            </p>
            <p>
                There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
            </p>
            <h1><span>Test heading</span></h1>
            <p>
                There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
            </p>        
        </div>
    </body>
</html>

您可以在这里查看演示: http://easwee.net/floated_img_article/

You can check out a demo here: http://easwee.net/floated_img_article/

问题:
图像可以有2种不同的宽度(3:2比例 - 水平或垂直)。

Problem: The image can have 2 different widths (3:2 ratio - horizontal or vertical). The heading has a background image, as you can see, which goes behind the floated image.

有一个 css解决方案可以让背景图片图像开始标题文本开始 - 这样它不会被浮动的图像重叠?

Is there a css solution to make the background image start where heading text starts - so that it won't get overlapped by the floated image? The left rounded corners need to be visible.

PS:你也可以指向一个javascript解决方案,但我想用css解决这个问题。

P.S.: You can also point me to a javascript solution but I'd prefer to solve this with css.

推荐答案

如果您能够在分页之前确定哪个图片比例(3:2与2:3)类到h1,然后用CSS边距处理它。

If you are able to determine which image ratio (3:2 vs. 2:3) before spitting out the page, you can assign a class to the h1, then handle it with CSS margins.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Test</title>
    <style type="text/css">
        body {font-family:Arial, Helvetica, sans-serif;font-size:12px;}
        .article {width:600px;}
        .image_container {float:left;}
        h1 {height:50px;line-height:50px;background:url(left.png) no-repeat 0 0;}
        h1.wide{margin-left:260px}
        h1.narrow{margin-left:385px}
        h1 span {display:block;padding:0 10px;background:url(right.png) no-repeat top right;}
    </style>    
    </head>
    <body>
        <div class="article">
            <div class="image_container">
                <img src="test.jpg" alt="test_img" />
            </div>
            <h1 class='wide'><span>Test heading</span></h1>
            <p>
                There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
            </p>

            <p>
                There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
            </p>
            <h1><span>Test heading</span></h1>
            <p>
                There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
            </p>        
        </div>
    </body>

code>< h1 class ='wide'> 和< h1 class ='narrow'>

Then, just switch between <h1 class='wide'> and <h1 class='narrow'>

这篇关于浮动图像与可变宽度和标题与背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆