วิธีตัดข้อความรอบ ๆ รูปภาพโดยใช้ HTML / CSS


106

ฉันต้องการออกแบบบล็อกข้อความดังภาพต่อไปนี้:

ป้อนคำอธิบายภาพที่นี่

ถามว่าเป็นไปได้ไหม?



คุณกำลังพยายามตัดข้อความรอบ ๆ รูปภาพเช่นเดียวกับใน<p>แท็กปกติเป็นต้น แต่คุณยังพูดถึง<textarea>ซึ่งอาจเป็นปัญหาที่แตกต่างไปจากเดิมโดยสิ้นเชิง ทำไมคุณไม่โพสต์ HTML ของคุณถ้าคุณมี ขอบคุณ.
Marc Audet

1
ใช่คุณเพียงแค่ลอยภาพ php ไปทางซ้ายและข้อความจะล้อมรอบ :-)
Jack Tuck

ความคิดเห็นทั้งหมดนี้และไม่มีคำตอบที่ยอมรับ ...
Dave Kanter

คำตอบ:


111

คุณต้อง floatคอนเทนเนอร์รูปภาพของคุณดังนี้:

HTML

<div id="container">
    <div id="floated">...some other random text</div>
    ...
    some random text
    ...
</div>

CSS

#container{
    width: 400px;
    background: yellow;
}
#floated{
    float: left;
    width: 150px;
    background: red;
}

ซอ

http://jsfiddle.net/kYDgL/


51

ด้วยCSS Shapesคุณสามารถไปได้ไกลกว่าเพียงแค่ลอยข้อความรอบ ๆ รูปภาพสี่เหลี่ยม

จริงๆแล้วคุณสามารถตัดข้อความให้เป็นรูปร่างของขอบของรูปภาพหรือรูปหลายเหลี่ยมที่คุณพันไว้รอบ ๆ

DEMO FIDDLE (ปัจจุบันทำงานบน webkit - caniuse )

.oval {
  width: 400px;
  height: 250px;
  color: #111;
  border-radius: 50%;
  text-align: center;
  font-size: 90px;
  float: left;
  shape-outside: ellipse();
  padding: 10px;
  background-color: MediumPurple;
  background-clip: content-box;
}
span {
  padding-top: 70px;
  display: inline-block;
}
<div class="oval"><span>PHP</span>
</div>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
  survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
  software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley
  of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing
  Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy
  text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised
  in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

นอกจากนี้นี่คือรายการที่ดีนอกเหนือจากบทความเกี่ยวกับ CSS Shapes


1
มีตัวเลือกใดบ้างที่จะห่อวัตถุ "AROUND" เพื่อให้วัตถุอยู่ตรงกลางได้หรือไม่?
redestructa

@redestructa ตรวจสอบการยกเว้น CSS ดูโพสต์ของฉัน: stackoverflow.com/a/19895616/703717ปัจจุบันรองรับเฉพาะใน IE - caniuse.com/#feat=css-exclusions
Danield

20

นอกเหนือจากคำตอบของBeNdErR :
องค์ประกอบ "TEXT อื่น ๆ " ควรมีfloat:noneเช่น:

    <div style="width:100%;">
        <div style="float:left;width:30%; background:red;">...something something something  random text</div>
        <div style="float:none; background:yellow;"> text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text  </div>
    </div>


นี่เป็นวิธีเดียวที่ฉันจะทำให้มันใช้งานได้ .. และวิธีแก้ปัญหาที่สะอาดที่สุด
andygoestohollywood

float: ไม่มีอะไรช่วยฉันได้! พยายามลอยอยู่เรื่อย ๆ : ทำให้องค์ประกอบที่สองไม่มีประโยชน์
bkunzi01

11

หากขนาดรูปภาพเป็นตัวแปรหรือการออกแบบตอบสนองนอกเหนือจากการตัดข้อความคุณสามารถตั้งค่าความกว้างขั้นต่ำสำหรับย่อหน้าเพื่อหลีกเลี่ยงไม่ให้แคบเกินไป
ให้องค์ประกอบหลอก CSS ที่มองไม่เห็นพร้อมความกว้างย่อหน้าต่ำสุดที่ต้องการ ถ้ามีพื้นที่ไม่เพียงพอที่จะใส่องค์ประกอบหลอกนี้มันจะถูกดันลงไปข้างใต้ภาพโดยใช้ย่อหน้าด้วย

#container:before {
  content: ' ';
  display: table;
  width: 10em;    /* Min width required */
}
#floated{
    float: left;
    width: 150px;
    background: red;
}

ฉันคิดว่าคำตอบของคุณอาจช่วยฉันได้บ้าง ฉันมีปัญหากับภาพลอยด้านซ้ายที่ซ้อนกันในการออกแบบที่ตอบสนอง ด้วยความกว้างของรูปภาพเริ่มต้นของฉันคือ 30% และรูปภาพทั้งสองด้านบางครั้งฉันอาจมีคอลัมน์ข้อความกว้างเพียง 4 อักขระ
Sherwood Botsford
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.