shape-outside

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.

* Some parts of this feature may have varying levels of support.

Thuộc tính shape-outside của CSS xác định một hình dạng — có thể không phải hình chữ nhật — mà nội dung nội tuyến liền kề sẽ bao quanh. Theo mặc định, nội dung nội tuyến bao quanh hộp lề của phần tử; shape-outside cung cấp một cách để tùy chỉnh việc bao quanh này, giúp có thể bao văn bản xung quanh các đối tượng phức tạp thay vì các hộp hình chữ nhật.

Try it

shape-outside: circle(50%);
shape-outside: ellipse(130px 140px at 20% 20%);
shape-outside: url("/shared-assets/images/examples/round-balloon.png");
shape-outside: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
<section class="default-example" id="default-example">
  <div class="example-container">
    <img
      class="transition-all"
      id="example-element"
      src="/shared-assets/images/examples/round-balloon.png"
      width="150" />
    We had agreed, my companion and I, that I should call for him at his house,
    after dinner, not later than eleven o'clock. This athletic young Frenchman
    belongs to a small set of Parisian sportsmen, who have taken up "ballooning"
    as a pastime. After having exhausted all the sensations that are to be found
    in ordinary sports, even those of "automobiling" at a breakneck speed, the
    members of the "Aéro Club" now seek in the air, where they indulge in all
    kinds of daring feats, the nerve-racking excitement that they have ceased to
    find on earth.
  </div>
</section>
.example-container {
  text-align: left;
  padding: 20px;
}

#example-element {
  float: left;
  width: 150px;
  margin: 20px;
}

Cú pháp

css
/* Giá trị từ khóa */
shape-outside: none;
shape-outside: margin-box;
shape-outside: content-box;
shape-outside: border-box;
shape-outside: padding-box;

/* Giá trị hàm */
shape-outside: circle();
shape-outside: ellipse();
shape-outside: inset(10px 10px 10px 10px);
shape-outside: polygon(10px 10px, 20px 20px, 30px 30px);

/* Hộp hình dạng kết hợp với hình dạng cơ bản */
shape-outside: circle() border-box;
shape-outside: margin-box ellipse();

/* Giá trị <url> */
shape-outside: url("image.png");

/* Giá trị <gradient> */
shape-outside: linear-gradient(45deg, white 150px, red 150px);

/* Giá trị toàn cục */
shape-outside: inherit;
shape-outside: initial;
shape-outside: revert;
shape-outside: revert-layer;
shape-outside: unset;

Thuộc tính shape-outside được chỉ định bằng các giá trị từ danh sách dưới đây, xác định vùng float cho các phần tử float. Vùng float xác định hình dạng mà nội dung nội tuyến (phần tử float) bao quanh.

Giá trị

none

Vùng float không bị ảnh hưởng. Nội dung nội tuyến bao quanh hộp lề của phần tử, như thông thường.

<shape-box>

Vùng float được tính toán theo hình dạng của các cạnh của phần tử float (như được xác định bởi mô hình hộp CSS). Có thể là margin-box, border-box, padding-box, hoặc content-box. Hình dạng bao gồm bất kỳ đường cong nào được tạo bởi thuộc tính border-radius (hành vi tương tự như background-clip).

margin-box

Xác định hình dạng được bao bọc bởi cạnh lề ngoài. Bán kính góc của hình dạng này được xác định bởi các giá trị border-radiusmargin tương ứng. Nếu tỷ lệ border-radius / margin1 hoặc lớn hơn, thì bán kính góc hộp lề là border-radius + margin. Nếu tỷ lệ nhỏ hơn 1, thì bán kính góc hộp lề là border-radius + (margin * (1 + (ratio - 1) ^ 3)).

border-box

Xác định hình dạng được bao bọc bởi cạnh viền ngoài. Hình dạng tuân theo các quy tắc tạo bán kính viền thông thường cho phía ngoài của viền.

padding-box

Xác định hình dạng được bao bọc bởi cạnh đệm ngoài. Hình dạng tuân theo các quy tắc tạo bán kính viền thông thường cho phía trong của viền.

content-box

Xác định hình dạng được bao bọc bởi cạnh nội dung ngoài. Mỗi bán kính góc của hộp này là giá trị lớn hơn trong 0 hoặc border-radius - border-width - padding.

<basic-shape>

Vùng float được tính toán dựa trên hình dạng được tạo bởi hàm inset(), circle(), ellipse(), hoặc polygon(); các hàm <basic-shape> khác không hợp lệ. Nếu một <shape-box> cũng được cung cấp, nó xác định hộp tham chiếu cho hàm <basic-shape>. Nếu không, hộp tham chiếu mặc định là margin-box.

<image>

Vùng float được trích xuất và tính toán dựa trên kênh alpha của <image> được chỉ định như được xác định bởi shape-image-threshold.

Note: Nếu hình ảnh không hợp lệ, hiệu ứng sẽ như thể từ khóa none đã được chỉ định. Ngoài ra, hình ảnh phải được phục vụ với tiêu đề CORS cho phép sử dụng nó.

Định nghĩa chính thức

Initial valuenone
Applies tofloats
Inheritedno
Computed valueas defined for <basic-shape> (with shape-box following, if supplied), the <image> with its URI made absolute, otherwise as specified.
Animation typeyes, as specified for <basic-shape>, otherwise no

Cú pháp chính thức

shape-outside = 
none |
[ <basic-shape> || <shape-box> ] |
<image>

<basic-shape> =
<basic-shape-rect> |
<circle()> |
<ellipse()> |
<polygon()> |
<path()> |
<shape()>

<shape-box> =
<visual-box> |
margin-box |
half-border-box

<image> =
<url> |
<image()> |
<image-set()> |
<cross-fade()> |
<element()> |
<gradient>

<basic-shape-rect> =
<inset()> |
<rect()> |
<xywh()>

<circle()> =
circle( <radial-size>? [ at <position> ]? )

<ellipse()> =
ellipse( <radial-size>? [ at <position> ]? )

<polygon()> =
polygon( <'fill-rule'>? [ round <length> ]? , [ <length-percentage> <length-percentage> ]# )

<path()> =
path( <'fill-rule'>? , <string> )

<shape()> =
shape( <'fill-rule'>? from <position> , <shape-command># )

<visual-box> =
content-box |
padding-box |
border-box

<image()> =
image( <image-tags>? [ <image-src>? , <color>? ]! )

<image-set()> =
image-set( <image-set-option># )

<cross-fade()> =
cross-fade( <cf-image># )

<element()> =
element( <id-selector> )

<inset()> =
inset( <length-percentage>{1,4} [ round <'border-radius'> ]? )

<rect()> =
rect( <top> , <right> , <bottom> , <left> )

<xywh()> =
xywh( <length-percentage>{2} <length-percentage [0,∞]>{2} [ round <'border-radius'> ]? )

<radial-size> =
<radial-extent> |
<length [0,∞]> |
<length-percentage [0,∞]>{2}

<position> =
<position-one> |
<position-two> |
<position-four>

<fill-rule> =
nonzero |
evenodd

<length-percentage> =
<length> |
<percentage>

<shape-command> =
<move-command> |
<line-command> |
close |
<horizontal-line-command> |
<vertical-line-command> |
<curve-command> |
<smooth-command> |
<arc-command>

<image-tags> =
ltr |
rtl

<image-src> =
<url> |
<string>

<image-set-option> =
[ <image> | <string> ] [ <resolution> || type( <string> ) ]?

<cf-image> =
[ <image> | <color> ] &&
<percentage [0,100]>?

<id-selector> =
<hash-token>

<border-radius> =
<length-percentage [0,∞]>{1,4} [ / <length-percentage [0,∞]>{1,4} ]?

<radial-extent> =
closest-corner |
closest-side |
farthest-corner |
farthest-side

<position-one> =
left |
center |
right |
top |
bottom |
x-start |
x-end |
y-start |
y-end |
block-start |
block-end |
inline-start |
inline-end |
<length-percentage>

<position-two> =
[ left | center | right | x-start | x-end ] && [ top | center | bottom | y-start | y-end ] |
[ left | center | right | x-start | x-end | <length-percentage> ] [ top | center | bottom | y-start | y-end | <length-percentage> ] |
[ block-start | center | block-end ] && [ inline-start | center | inline-end ] |
[ start | center | end ]{2}

<position-four> =
[ [ left | right | x-start | x-end ] <length-percentage> ] && [ [ top | bottom | y-start | y-end ] <length-percentage> ] |
[ [ block-start | block-end ] <length-percentage> ] && [ [ inline-start | inline-end ] <length-percentage> ] |
[ [ start | end ] <length-percentage> ]{2}

<move-command> =
move <command-end-point>

<line-command> =
line <command-end-point>

<horizontal-line-command> =
hline [ to [ <length-percentage> | left | center | right | x-start | x-end ] | by <length-percentage> ]

<vertical-line-command> =
vline [ to [ <length-percentage> | top | center | bottom | y-start | y-end ] | by <length-percentage> ]

<curve-command> =
curve [ [ to <position> with <control-point> [ / <control-point> ]? ] | [ by <coordinate-pair> with <relative-control-point> [ / <relative-control-point> ]? ] ]

<smooth-command> =
smooth [ [ to <position> [ with <control-point> ]? ] | [ by <coordinate-pair> [ with <relative-control-point> ]? ] ]

<arc-command> =
arc <command-end-point> [ [ of <length-percentage>{1,2} ] && <arc-sweep>? && <arc-size>? && [ rotate <angle> ]? ]

<command-end-point> =
to <position> |
by <coordinate-pair>

<control-point> =
<position> |
<relative-control-point>

<coordinate-pair> =
<length-percentage>{2}

<relative-control-point> =
<coordinate-pair> [ from [ start | end | origin ] ]?

<arc-sweep> =
cw |
ccw

<arc-size> =
large |
small

Ví dụ

Kênh hóa văn bản

HTML

html
<div class="main">
  <div class="left"></div>
  <div class="right"></div>
  <p>
    Sometimes a web page's text content appears to be funneling your attention
    towards a spot on the page to drive you to follow a particular link.
    Sometimes you don't notice.
  </p>
</div>

CSS

css
.main {
  width: 530px;
}

.left,
.right {
  background-color: lightgray;
  height: 12ex;
  width: 40%;
}

.left {
  clip-path: polygon(0 0, 100% 100%, 0 100%);
  float: left;
  shape-outside: polygon(0 0, 100% 100%, 0 100%);
}

.right {
  clip-path: polygon(100% 0, 100% 100%, 0 100%);
  float: right;
  shape-outside: polygon(100% 0, 100% 100%, 0 100%);
}

p {
  text-align: center;
}

Kết quả

Thông số kỹ thuật

Specification
CSS Shapes Module Level 1
# shape-outside-property

Khả năng tương thích trình duyệt

Xem thêm