2010-03-16 95 views
2

我有以下代碼:寬度可變,並與背景圖像標題飄來圖像

<!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/

問題: 圖像可以有2個不同的寬度(3:2比率 - 水平或垂直)。如您所見,該標題具有背景圖像,該圖像位於浮動圖像的後面。

是否有css解決方案使背景圖像開始標題文本開始的地方 - 以便它不會被浮動圖像重疊?左圓角需要可見。

P.S .:你也可以指點我的JavaScript解決方案,但我更願意用css解決這個問題。

+0

我真的不知道你在問什麼。也許你可以通過「使標題文本開始的地方開始背景圖像」來創建你的意思的模型? – Robusto 2010-03-16 16:39:21

+0

您是否能夠在顯示頁面之前檢測到頁面上的圖像大小? – Brant 2010-03-16 18:25:49

+0

已經有一個連接到問題的演示鏈接 - 問題可以在那裏看到。我設法通過應用overflow:隱藏到h1來正確地創建標題背景圖像位置。然而,另一個竅門是段落仍然計算邊界後面的填充和邊距。如果您添加了overflow:隱藏到p元素,則它們將在圖像下方被清除,這會留下空白區域,直到p標記結束。 – easwee 2010-03-17 09:07:39

回答

0

如果能夠在吐出頁面之前確定哪個圖像比例(3:2與2:3),則可以將類分配給h1,然後使用CSS邊距處理它。

<!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> 

然後,只需切換<h1 class='wide'><h1 class='narrow'>之間

+0

是的,但這將需要JavaScript來讀取圖像寬度和分配類。 – easwee 2010-03-17 09:12:47

+0

這就是爲什麼我問上面是否可以事先檢測它。如果您使用某種模板,則該類可以分配給服務器端。 – Brant 2010-03-17 12:47:16

+0

我可能必須使用javascript來完成它,因爲它與工作相關,日期並不好玩。但我仍然想知道這個問題是否可以用普通的CSS來解決。問題在於,如果您還將填充應用於p,則它也不會顯示,因爲它是從圖像後面計算得出的。並設置保證金會抵消所有段落,我不能這樣做:第一個孩子,因爲我不知道有多少p將在開始包裝圖像。到目前爲止,溢出:隱藏在H1和P做了最好的結果,但仍然不完全是我所需要的。 – easwee 2010-03-17 16:03:06