CSS help (figure, figcaption, fluid images)

Open discussion about any topic, as long as you abide by the rules of course!
Post Reply
obsidian
Posts: 10970
Joined: Mon Feb 04, 2002 8:00 am

CSS help (figure, figcaption, fluid images)

Post by obsidian »

Here's the test page:

https://dl.dropbox.com/u/20123341/figure.html

Code: Select all

<html>
	<head>
		<style>
			body {
				font-family: verdana;
			}
			figure {
				max-width: 900px;
				background-color: #888;
				margin: 2em auto;
				box-shadow: 0 4px 8px 0 #888;
				border-radius: 8px;
			}
			figure img {
				display: block;
				max-width: 100%;
				margin: 0 auto;
				border-top-left-radius: 8px;
				border-top-right-radius: 8px;
			}
			figcaption {
				max-width: 100%;
				padding: 8px 16px;
				font-size: x-small;
				color: #eee;
				background-color: #444;
				border-bottom-left-radius: 8px;
				border-bottom-right-radius: 8px;
			}
		</style>
	</head>
	<body>
		<figure>
			<img src="https://assets.mozillalabs.com/Brands-Logos/Firefox/logo-wordmark/firefox_logo-wordmark-horiz_RGB.png">
			<figcaption>Test 1: Firefox logo and wordmark. High resolution 32-bit lossless PNG file.</figcaption>
		</figure>
		<figure>
			<img src="http://mozorg.cdn.mozilla.net/media/img/styleguide/identity/firefox/wordmark-logo.png">
			<figcaption>Test 2: Firefox logo and wordmark. Small image, 32-bit lossless PNG file.</figcaption>
		</figure>
	</body>
</html>
Test 1 works as expected because the image width is greater than 900px. Test 2 does not. I would like the container (figure) and sibling (figcaption) to shrink to fit the image, while keeping the image fluid so that it can scale down on smaller screens (scale the browser width down).

Ideas?
[size=85][url=http://gtkradiant.com]GtkRadiant[/url] | [url=http://q3map2.robotrenegade.com]Q3Map2[/url] | [url=http://q3map2.robotrenegade.com/docs/shader_manual/]Shader Manual[/url][/size]
4days
Posts: 5465
Joined: Tue Apr 16, 2002 7:00 am

Re: CSS help (figure, figcaption, fluid images)

Post by 4days »

think you'd need to use display:table or display:inline-table if you didn't want to use javascript - otherwise the container will need a width specifying.
obsidian
Posts: 10970
Joined: Mon Feb 04, 2002 8:00 am

Re: CSS help (figure, figcaption, fluid images)

Post by obsidian »

Yes, but then the image width is no longer fluid (unless I'm missing something).
[size=85][url=http://gtkradiant.com]GtkRadiant[/url] | [url=http://q3map2.robotrenegade.com]Q3Map2[/url] | [url=http://q3map2.robotrenegade.com/docs/shader_manual/]Shader Manual[/url][/size]
Dark Metal
Posts: 5496
Joined: Sun Feb 20, 2000 8:00 am

Re: CSS help (figure, figcaption, fluid images)

Post by Dark Metal »

obsidian wrote:Yes, but then the image width is no longer fluid (unless I'm missing something).
You started the thread because you're missing something.
[WYD]
User avatar
GONNAFISTYA
Posts: 13369
Joined: Sun Jan 23, 2005 8:20 pm

Re: CSS help (figure, figcaption, fluid images)

Post by GONNAFISTYA »

Goddamned nerds.

Where's my bat?
Tsakali
Posts: 7175
Joined: Thu Mar 02, 2000 8:00 am

Re: CSS help (figure, figcaption, fluid images)

Post by Tsakali »

obsidian wrote:Yes, but then the image width is no longer fluid (unless I'm missing something).
the second image still looks fluid to me with 'inline-table' / 'table' :confused:
User avatar
Eraser
Posts: 19174
Joined: Fri Dec 01, 2000 8:00 am

Re: CSS help (figure, figcaption, fluid images)

Post by Eraser »

Stackoverflow3world.com :)
User avatar
shaft
Posts: 12473
Joined: Fri Dec 10, 1999 8:00 am

Re: CSS help (figure, figcaption, fluid images)

Post by shaft »

T&T :arrow:
User avatar
MKJ
Posts: 32581
Joined: Fri Nov 24, 2000 8:00 am

Re: CSS help (figure, figcaption, fluid images)

Post by MKJ »

figure is a block element by default and will therefore take up the entire space available. while you changed the available space to 900 instead of fullscreeen with max-width, it will still stretch out to 900 regardless of content.
change it's display to inline-block and see the magic.
SoM
Posts: 8489
Joined: Fri Dec 03, 1999 8:00 am

Re: CSS help (figure, figcaption, fluid images)

Post by SoM »

shaft wrote:T&T :arrow:
that's dead
[color=red][WYD][/color]S[color=red]o[/color]M
Deathshroud
Posts: 2103
Joined: Tue Feb 22, 2005 6:22 pm

Re: CSS help (figure, figcaption, fluid images)

Post by Deathshroud »

http://webdesignerwall.com/tutorials/5- ... ive-design

See: "Min & Max Width"
User avatar
MKJ
Posts: 32581
Joined: Fri Nov 24, 2000 8:00 am

Re: CSS help (figure, figcaption, fluid images)

Post by MKJ »

the problem he's having is not related to the img tho, but to the figure container. my solution only works one way but not the other.

the solution on that site kinda cheats, as he sets the body width to 850px. suppose you could give that a maxwidth so that it scales down..
bitWISE
Posts: 10704
Joined: Wed Dec 08, 1999 8:00 am

Re: CSS help (figure, figcaption, fluid images)

Post by bitWISE »

MKJ wrote:figure is a block element by default and will therefore take up the entire space available. while you changed the available space to 900 instead of fullscreeen with max-width, it will still stretch out to 900 regardless of content.
change it's display to inline-block and see the magic.
:up:
User avatar
MKJ
Posts: 32581
Joined: Fri Nov 24, 2000 8:00 am

Re: CSS help (figure, figcaption, fluid images)

Post by MKJ »

well, looks like you're SOL. even with a fluid container it won't scale the figure down when its content is smaller than itself.
pretty strange that a figure is a block element actually. you'd think you'd want it to contain the img/svg/canvas and nothing else.

it's about 3 lines of javascript to fix, but unfortunately not the most semantic or clean solution.
User avatar
MKJ
Posts: 32581
Joined: Fri Nov 24, 2000 8:00 am

Re: CSS help (figure, figcaption, fluid images)

Post by MKJ »

I take that back.
my initial solution was correct, it's just that the absolute maxwidth fucks it up.

give the figure a display inline-block and it's maxwidth to something relative, like 100%, and you're set.
if you want to influence the actual width of the figure you must wrap it in a container, in this case one with a max-width of 900px.

Code: Select all

<!doctype html>	
<html>
	<head>
		<style>
			body {
				font-family: verdana;
			}
			div.figContainer {
				max-width: 900px; 
				text-align: center;	
				margin: 0 auto;
			} 
			figure {
				max-width: 100%;
				background-color: #888;
				margin: 2em auto;
				box-shadow: 0 4px 8px 0 #888;
				border-radius: 8px;
				display: inline-block;
			}
			figure img {
				display: block;
				max-width: 100%;
				margin: 0 auto;
				border-radius: 8px 8px 0 0;
			}
			figcaption {
				max-width: 100%;
				padding: 8px 16px;
				font-size: x-small;
				color: #eee;
				background-color: #444;
				border-radius: 0 0 8px 8px;
			}
		</style>
	</head>
	<body>
		<div class="figContainer">
			<figure>
				<img src="https://assets.mozillalabs.com/Brands-Logos/Firefox/logo-wordmark/firefox_logo-wordmark-horiz_RGB.png">
				<figcaption>Test 1: Firefox logo and wordmark. High resolution 32-bit lossless PNG file.</figcaption>
			</figure>
		</div>
	
		<div class="figContainer"> 
			<figure>
				<img src="http://mozorg.cdn.mozilla.net/media/img/styleguide/identity/firefox/wordmark-logo.png">
				<figcaption>Test 2: Firefox logo and wordmark. Small image, 32-bit lossless PNG file.</figcaption>
			</figure>
		</div>
		
	</body>
</html>
User avatar
FragaGeddon
Posts: 3229
Joined: Sun Sep 17, 2000 7:00 am

Re: CSS help (figure, figcaption, fluid images)

Post by FragaGeddon »

MKJ wrote:I take that back.
my initial solution was correct, it's just that the absolute maxwidth fucks it up.

give the figure a display inline-block and it's maxwidth to something relative, like 100%, and you're set.
if you want to influence the actual width of the figure you must wrap it in a container, in this case one with a max-width of 900px.

Code: Select all

<!doctype html>	
<html>
	<head>
		<style>
			body {
				font-family: verdana;
			}
			div.figContainer {
				max-width: 900px; 
				text-align: center;	
				margin: 0 auto;
			} 
			figure {
				max-width: 100%;
				background-color: #888;
				margin: 2em auto;
				box-shadow: 0 4px 8px 0 #888;
				border-radius: 8px;
				display: inline-block;
			}
			figure img {
				display: block;
				max-width: 100%;
				margin: 0 auto;
				border-radius: 8px 8px 0 0;
			}
			figcaption {
				max-width: 100%;
				padding: 8px 16px;
				font-size: x-small;
				color: #eee;
				background-color: #444;
				border-radius: 0 0 8px 8px;
			}
		</style>
	</head>
	<body>
		<div class="figContainer">
			<figure>
				<img src="https://assets.mozillalabs.com/Brands-Logos/Firefox/logo-wordmark/firefox_logo-wordmark-horiz_RGB.png">
				<figcaption>Test 1: Firefox logo and wordmark. High resolution 32-bit lossless PNG file.</figcaption>
			</figure>
		</div>
	
		<div class="figContainer"> 
			<figure>
				<img src="http://mozorg.cdn.mozilla.net/media/img/styleguide/identity/firefox/wordmark-logo.png">
				<figcaption>Test 2: Firefox logo and wordmark. Small image, 32-bit lossless PNG file.</figcaption>
			</figure>
		</div>
		
	</body>
</html>
I put it up on codepen.
http://codepen.io/FragaGeddon/pen/BvtcK
obsidian
Posts: 10970
Joined: Mon Feb 04, 2002 8:00 am

Re: CSS help (figure, figcaption, fluid images)

Post by obsidian »

Cool link.

It doesn't look like it's possible. In MKJ's example, figcaption no longer wraps if it is longer than the image width. Since img and figcaption are siblings, there doesn't seem to be a way to make one affect the other.

That sucks, I guess I'll have to hack something together with a bit of Javascript, but had hoped to avoid it.
[size=85][url=http://gtkradiant.com]GtkRadiant[/url] | [url=http://q3map2.robotrenegade.com]Q3Map2[/url] | [url=http://q3map2.robotrenegade.com/docs/shader_manual/]Shader Manual[/url][/size]
User avatar
MKJ
Posts: 32581
Joined: Fri Nov 24, 2000 8:00 am

Re: CSS help (figure, figcaption, fluid images)

Post by MKJ »

theres a bit of a hack to make the figcap wrap around, but then the lot won't shrink anymore when resizing.

so yea. figcaption.style.width = img.width.
bitWISE
Posts: 10704
Joined: Wed Dec 08, 1999 8:00 am

Re: CSS help (figure, figcaption, fluid images)

Post by bitWISE »

http://cdpn.io/sDuqG

How about that?
obsidian
Posts: 10970
Joined: Mon Feb 04, 2002 8:00 am

Re: CSS help (figure, figcaption, fluid images)

Post by obsidian »

Images are no longer fluid (resize your browser window to be narrow).

Looks like it's one or the other: fluid image or wrapping text in figcaption. Since images can't have children there's no way to use the image as a container to set the same width for figcaption. Silly HTML. :disgust:
[size=85][url=http://gtkradiant.com]GtkRadiant[/url] | [url=http://q3map2.robotrenegade.com]Q3Map2[/url] | [url=http://q3map2.robotrenegade.com/docs/shader_manual/]Shader Manual[/url][/size]
User avatar
MKJ
Posts: 32581
Joined: Fri Nov 24, 2000 8:00 am

Re: CSS help (figure, figcaption, fluid images)

Post by MKJ »

It's a weird and mostly unexpected behavior of the figcaption element.
Good thing the specifications aren't final yet ey.
diego
Posts: 1379
Joined: Mon Jan 24, 2005 12:25 pm

Re: CSS help (figure, figcaption, fluid images)

Post by diego »

lol MKJ is nerding out yet again :olo:
User avatar
MKJ
Posts: 32581
Joined: Fri Nov 24, 2000 8:00 am

Re: CSS help (figure, figcaption, fluid images)

Post by MKJ »

set diego.upset = true;
User avatar
GONNAFISTYA
Posts: 13369
Joined: Sun Jan 23, 2005 8:20 pm

Re: CSS help (figure, figcaption, fluid images)

Post by GONNAFISTYA »

lol
Post Reply