이미지 아래에 캡션을 쓰는 방법은 무엇입니까?


126

인라인으로 유지해야하는 두 개의 이미지가 있습니다. 각 이미지 아래에 캡션을 쓰고 싶습니다.

<center>
   <a href="http://example.com/hello">
       <img src="hello.png" width="100px" height="100px">
   </a>
   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
   <a href="http://example.com/hi">
       <img src="hi.png" width="100px" height="100px">
   </a>
</center>

어떻게 구현할 수 있습니까?


8
@ceejayoz &nbsp는 여백을 설정하거나 다른 CSS 레이아웃 도구를 사용하는 것과는 반대로 모든 것을 언급하지 않습니다 .
jzworkman

이것은 본질적으로 대답되었지만 인라인 이미지의 너비에 맞는 캡션을 얻는 유일한 방법은 래퍼 또는 캡션 자체에 너비 속성을 하드 코딩하는 것입니다.
MyNameIsKo 2014

2
@McGarnagle은 다른 사람의 게시물에서 의미하는 코드를 편집하지 않습니다. 포맷은 괜찮지 만 (파이썬이 아니라면) 모든 코드 내용이 중요합니다! 편집 덕분에 jxworkmans 게시물은 의미가 없어 보였습니다. 그리고 정말, 고정 잘못된 코드해당하는 완전한 넌센스입니다. 그렇다면 대답의 요점은 무엇입니까?
Tomáš Zato-Monica 복원

답변:


275

FigureFigcaption 태그 :

<figure>
    <img src='image.jpg' alt='missing' />
    <figcaption>Caption goes here</figcaption>
</figure>

HTML5를 좋아해야합니다.


샘플보기


1
하지만 대부분의 IE에는 HTML5 shim이 필요합니다.
ceejayoz

10
이러한 태그는 어떤 식 으로든 도움이되지 않습니다. 그것들은 단지 더미 마크 업이며, 모든 작업은 CSS (및 / 또는 다른 HTML 태그)에서 수행해야합니다. 그리고 이전 버전의 IE를 더미 태그로 인식하도록하려면 추가 작업이 필요합니다. 그래서 사용하기 간단 divspan.
Jukka K. Korpela 2012

15
@ JukkaK.Korpela이 태그는 매우 중요합니다. stackoverflow.com/a/17272444/756329html5doctor.com/lets-talk-about-semantics
Joseph Hansen

3
문제는 이것입니다 : [figures] can be moved to another page or to an appendix without affecting the main flow. <figure>그리고 <figcaption>하지 자막과 이미지에 대한 범용 교체입니다. 그들은 단지를 그림에 적용됩니다. 예를 들어 캡션이있는 사진과 혼합 된 단락으로 구성된 단계별 방법이있는 경우 이미지가 html에 정의 된 것과 동일한 단락 구분에 표시되는 것이 중요 할 수 있습니다. 따라서 <figure>여기에는 적용되지 않습니다 (내가 이해하는대로).
Ponkadoodle

이 선이 반드시 이미지 아래에 그림 캡션을 배치하는 것은 아닙니다 . 여기에 제공된 HTML은 내용을 설명하지만 모양은 CSS의 영향을 받거나 결정됩니다.
jvriesem

21

CSS

#images{
    text-align:center;
    margin:50px auto; 
}
#images a{
    margin:0px 20px;
    display:inline-block;
    text-decoration:none;
    color:black;
 }

HTML

<div id="images">
    <a href="http://xyz.com/hello">
        <img src="hello.png" width="100px" height="100px">
        <div class="caption">Caption 1</div>
    </a>
    <a href="http://xyz.com/hi">
        <img src="hi.png" width="100px" height="100px"> 
        <div class="caption">Caption 2</div>
    </a>
</div>

바이올린이 여기 있습니다.


7
<div style="margin: 0 auto; text-align: center; overflow: hidden;">
  <div style="float: left;">
    <a href="http://xyz.com/hello"><img src="hello.png" width="100px" height="100px"></a>
    caption 1
  </div>
 <div style="float: left;">
   <a href="http://xyz.com/hi"><img src="hi.png" width="100px" height="100px"></a>
   caption 2                      
 </div>
</div>

7

링크 안에 이미지를 넣습니다. 너비가 140px라고 가정 해 보겠습니다.

<a><img src='image link' style='width: 140px'></a>

다음으로, 캡션을 a에 넣고 중앙에 배치하면서 이미지보다 너비를 줄입니다.

<a>
  <img src='image link' style='width: 140px'>
  <div style='width: 130px; text-align: center;'>I just love to visit this most beautiful place in all the world.</div>
</a>

그런 다음 링크 태그에서 더 이상 링크처럼 보이지 않도록 링크 스타일을 지정합니다. 원하는 색상을 지정할 수 있지만 링크에 표시 될 수있는 텍스트 장식 만 제거하면됩니다.

<a style='text-decoration: none; color: orange;'>
  <img src='image link' style='width: 140px'>
  <div style='width: 130px; text-align: center;'>I just love to visit this most beautiful place in all the world.</div>
</a>

어떤 텍스트도 캡션을 밀어 낼 수 없도록 이미지를 링크에 캡션으로 감쌌습니다. 캡션은 링크로 그림과 연결되어 있습니다. 예 : http://www.alphaeducational.com/p/okay.html


1
같은 <div>인라인 태그에는 같은 블록 태그 가 허용되지 않습니다 . 캡션에 <a>사용해야 <span>합니다.
mindplay.dk

@TheAlpha의 예에서 <div>를 <span>으로 바꾸면 완전히 실패합니다. <div>가 필요한 것 같습니다.
Pierre

@ mindplay.dk a태그의 콘텐츠 모델이 투명하기 때문에 부모 태그가 할 수있는 모든 자식을 받아 들일 수 있다고 생각합니다 (대화 형 콘텐츠 제외). 틀 렸으면 말해줘? 배우고 싶습니다. w3.org/TR/html5/text-level-semantics.html#the-a-element
Chris Bier

2

CSS는 당신의 친구입니다. 센터 태그 (상당히 감가 상각 된 것은 말할 것도없고)도 필요하지 않으며 과도한 비 깨지지 않는 공간도 없습니다. 다음은 간단한 예입니다.

CSS

.images {
    text-align:center;
}
.images img {
    width:100px;
    height:100px;
}
.images div {
    width:100px;
    text-align:center;
}
.images div span {
    display:block;
}
.margin_right {
    margin-right:50px;
}
.float {
    float:left;
}
.clear {
    clear:both;
    height:0;
    width:0;
}

HTML

<div class="images">
    <div class="float margin_right">
        <a href="http://xyz.com/hello"><img src="hello.png" width="100px" height="100px" /></a>
        <span>This is some text</span>
    </div>
    <div class="float">
        <a href="http://xyz.com/hi"><img src="hi.png" width="100px" height="100px" /></a>
        <span>And some more text</span>
    </div>
    <span class="clear"></span>
</div>

2

반응 형 이미지 용. 그림 태그 내에 그림 및 소스 태그를 추가 할 수 있습니다.

<figure>
  <picture>
    <source media="(min-width: 750px)" srcset="images/image_2x.jpg"/>
    <source media="(min-width: 500px)" srcset="images/image.jpg" />
    <img src="images.jpg" alt="An image">
  </picture>
  <figcaption>Caption goes here</figcaption>
</figure>

0

더 의미 적으로 정확하고 OPs를 나란히 정렬하는 것에 대한 원래 질문에 대답하려면 다음을 사용합니다.

HTML

<div class="items">
<figure>
    <img src="hello.png" width="100px" height="100px">
    <figcaption>Caption 1</figcaption>
</figure>
<figure>
    <img src="hi.png" width="100px" height="100px"> 
    <figcaption>Caption 2</figcaption>
</figure></div>

CSS

.items{
text-align:center;
margin:50px auto;}


.items figure{
margin:0px 20px;
display:inline-block;
text-decoration:none;
color:black;}

https://jsfiddle.net/c7borg/jLzc6h72/3/


0

<figcaption>HTML5 의 태그를 사용하면 이미지에 텍스트를 입력 할 수 있습니다. 예를 들면 다음과 같습니다.

<figcaption>
Your text here
</figcaption>.

그런 다음 CSS를 사용하여 이미지에서 텍스트가 있어야하는 위치에 텍스트를 배치 할 수 있습니다.


-3
<table>
<tr><td><img ...><td><img ...>
<tr><td>caption1<td>caption2
</table>

원하는 스타일.


6
나는 결코 이것을하지 않을 것이다. 그 사람은 콘텐츠를 표 형식으로 표시하는 방법이 아니라 프레젠테이션 조언을 요청하고 있습니다.
Mike Kormendy 2014
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.