Positioning caption within image border while maintaining aspect ratio - html

I'm dealing with images that may or may not have a caption.
I've created a polaroid effect around each image via the following:
<div style="text-align:center">
<img src="daenerys.png" style="border: 10px solid #FAFAFA;border-bottom: 45px solid #FAFAFA;-webkit-box-shadow: 3px 3px 3px #9E9E9E; -moz-box-shadow: 3px 3px 3px #9E9E9E;box-shadow: 3px 3px 3px #9E9E9E;">
</div>
Results look acceptable:
But this can't support captions the way I want it to. I need to write a caption under the image, within the white border of the polaroid effect.
I changed my code to:
<div style="text-align:center;background-color: #ffffff;display: inline-block;padding: 10px;-webkit-box-shadow: 3px 3px 3px #9E9E9E; -moz-box-shadow: 3px 3px 3px #9E9E9E;box-shadow: 3px 3px 3px #9E9E9E;">
<img src="daenerys.png" style="display: block;margin: 0 auto;margin-bottom: 20px;width: 100%;">
<div class="cxl cgy"></div>
</div>
This gives me an image that is stretched:
How do I keep my image resolution and aspect ration intact, while ensuring the text dynamically grows the white space under the image as required? I've taken a look at this SO post for help, but to no avail.
The overall goal is to keep the polaroid look (even for images that have no caption), and to use the white space below the image to display text for images that do have captions.

I found a solution with keeping aspect ration:
<div style="text-align:center;background-color: #ffffff;display: inline-block;padding: 10px;-webkit-box-shadow: 3px 3px 3px #9E9E9E;-moz-box-shadow: 3px 3px 3px #9E9E9E;box-shadow: 3px 3px 3px #9E9E9E;">
<div style="display: inline-table;">
<img src="daenerys.png" style="display: block;margin: 0 auto;margin-bottom: 20px;width: 100%;">
<div class="cxl cgy" style="display: table-caption;width: 100%;caption-side: bottom;">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Quae necessitatibus architecto quis non error placeat illo enim illum quo corrupti molestias id sint recusandae animi obcaecati, laudantium nostrum fugit eaque.
</div>
</div>
</div>
Here is preview link : https://codepen.io/ziruhel/pen/RjwmQg

For creating how you want to create the polariod, there is a new html tag that can have a caption and and image inside one element.
The Structure is this :
-- Figure
---- img
---- figcaption
I have made a jsfiddle keeping in mind your structure and the code also.
You can style the figure tag with your card stylings and also keep the text for caption if you want.
The jsfiddle link: https://jsfiddle.net/vmt5w1u8/
SCSS code:
figure {
text-align: center;
background-color: #ffffff;
display: inline-block;
padding: 10px;
height: 100%;
-webkit-box-shadow: 3px 3px 3px #9e9e9e;
-moz-box-shadow: 3px 3px 3px #9e9e9e;
box-shadow: 3px 3px 3px #9e9e9e;
img {
display: block;
margin: 0 auto;
margin-bottom: 20px;
width: 100%;
height: 100%;
}
figcaption {
text-align: center;
}
}
Figure tag reference link: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/figure

The reason you are getting image stretch is because you are not setting a fix/max width for your div, without width specified text will stretch the image as the width gets longer
Try adding:
div{
/*width:300px; use max-width if responsive*/
max-width: 300px; /*maximum width the image will stretch*/
box-sizing: border-box; /*include padding as part of the specified max-width above*/
}
Example:
div {
max-width: 300px;
box-sizing: border-box;
}
<div style="text-align:center;background-color: #ffffff;display: inline-block;padding: 10px;-webkit-box-shadow: 3px 3px 3px #9E9E9E; -moz-box-shadow: 3px 3px 3px #9E9E9E;box-shadow: 3px 3px 3px #9E9E9E;">
<img src="https://dummyimage.com/300x250/4168a3/fff" style="display: block;margin: 0 auto;margin-bottom: 20px;width: 100%;">
<div class="cxl cgy">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,
sem. Nulla consequat massa quis enim.
</div>
</div>

You don't have to set max-width, just set .image width to width: 100% and height: auto
Fiddle: http://jsfiddle.net/MBLZx/2312

Setting max width to your image might help. Image will be resized to fit the aspect ratio without stretching beyond required width. And also the text content will be wrapped accordingly.
CSS:
.img-wrapper {
max-width: 500px;
padding: 10px;
background: #fff;
text-align: center;
position: relative;
box-sizing: border-box;
}
.image {
width: 100%;
}
.text {
word-wrap: break-word;
}
Here is the Fiddle.
Hope this helps :)

Related

Color border inverted [duplicate]

This question already has answers here:
How to align content of a div to the bottom
(29 answers)
Closed 4 years ago.
I would like to create a border with 2 two colors inside. The first color will be in blue and the next in white.
In my code, there is the colors which are inverted... The white before the blue... I don't understand why ?
.border-color-blue{
height: 182px;
width: 260px;
background-color: blue;
}
.border-white{
border: 1px solid #e0e0e0;
background: white;
width: 260px;
}
<div class="border-color-blue">
<div class="border-white">Lorem ipsum dolor sit amet, ei cum option deserunt, sed cu dicta albucius dissentias.</div>
</div>
This is all regular.
The whole container is blue.
Inside there is another container, with a white background.
To illustrate, added the opacity rule to border-white.
The one in forefront is hiding.
You can adjust which one end on top with the z-index rule.
.border-color-blue{
height: 182px;
width: 260px;
background-color: blue;
}
.border-white{
border: 1px solid #e0e0e0;
background: white;
width: 260px;
opacity: 0.7
}
<div class="border-color-blue">
<div class="border-white">Lorem ipsum dolor sit amet, ei cum option deserunt, sed cu dicta albucius dissentias.</div>
</div>

How to set background image to the left side of division?

The div below shows error messages now I want to add a image that would appear before error message. It will be just small error image which I want to appear on the left side of div so that it appears before error text. I tried to do it with background image now text is overlapping image as image is background for text. Is there any way in which error message instead of overlapping follows image.
<div id='er'><?php echo $er; ?></div>
<style>
#er{font-size: 14px;color:blue; background: url(img_stop.gif);
background-size: 35px 35px;
background-repeat: no-repeat;}
</style>
Something like this JS Fiddle
#er{
font-size: 14px;
color:blue;
min-height:45px; /* so that the image will always be shown even for short error messages */
background: transparent url(http://www.willowsigns.com/images/products/reflective-stop-sign-circular-icon-600mm-dia-Ehwf.png) no-repeat;
background-position: 5px center; /* image positioned 5px of the 0 left and centered top */
background-size: 35px 35px;
border:1px solid red;
padding:5px 5px 5px 45px; /*giving left padding as 45, 35px for the image, and 5px on each side */
}
<div id='er'>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Asperiores soluta accusamus dolorum ut odit voluptatem, autem sint!
</div>

Strange behavior with em widths

I'm currently trying to learn adaptive/mobile web-design, which involves media queries for different platforms and resolutions, using em rather than px in designs etc. I am currently trying to place two em-width elements next to each other; a navigation/info bar next to my content.
I have set the info bar to be 16em wide (translates into 16px per em according to font-size) and the content to be calc(100% - 17em) wide. I'd assume this should leave 1em of margin between the menu and the content, no matter how much I zoom and resize my window, but the end result disagrees:
100% zoom
25% zoom
friend's screen
The space between the elements changes vastly by zoom level, although everything universally uses 'em' units and the font-size is not changed between relevant elements. What could possibly be the issue?
Info: I'm using a media query to transition the navigation from horizontal alignment to a sidebar. It's the queried version that is acting up. Keep this in mind when looking over the CSS. It might be part of the problem, though I seriously doubt it...
#contentwrap {
margin-top: 1em;
border: 1px solid #000;
border-radius: 8px;
}
#content {
border-radius: 8px;
padding: 2em;
#navbar {
margin-top: 1em;
border: 1px solid #000;
border-radius: 8px;
width: 100%;
display: table;
font-family: 'Cabin', sans-serif;
}
.navelement {
font-size: 0.8em;
width: 25%;
padding: 1em;
text-align: center;
display: table-cell;
}
#nav4 {
}
#media (min-width:1580px) {
#navbar {
border: 1px dashed red;
padding: 0px;
width: 16em;
float: left;
background-image: none;
}
.navelement {
font-size: 0.8em;
background-image: url('../img/navbg.png');
background-size: 100% 100%;
width: 20em;
display: inherit;
border: 1px solid;
border-color: #303030 #101010 #101010 #101010;
border-radius: 8px;
margin-bottom: 1.25em;
padding: 1.25em;
}
#nav4 {
background-image: url('../img/navbg.png');
background-size: 100% 100%;
margin-bottom: 0em;
}
#contentwrap {
float: right;
width: -moz-calc(100% - 17em);
width: -webkit-calc(100% - 17em);
width: calc(100% - 17em);
}
}
HTML code:
<div id="navbar">
<div id="nav1" class="navelement"><b>Current news post:</b><br/>"Welcome to usp3!"<br/>by ividyon</div>
<div id="nav2" class="navelement"><b>Current MOTW:</b><br/>"Some Map"<br/>by some guy</br>[ Link to thread ]</div>
<div id="nav3" class="navelement"><b>Recent additions:</b><br/>- "Some map" review by Delacroix<br/>- Article: "Blah blah blah.." by ividyon<br/>- "Some other map" review by ividyon</div>
<div id="nav4" class="navelement">Recent forum posts:</br>- "This design is not good!"<br/>by A Dude</br>- "Too lazy to type filler..."<br/>by ividyon</br>- "Too lazy to type filler..."<br/>by ividyon</br>- "Too lazy to type filler..."<br/>by ividyon</div>
</div>
<div id="contentwrap">
<div id="content">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In ut purus tortor. Maecenas ut semper dui, ac convallis libero. Vivamus molestie mauris a mauris pretium, et dignissim mauris dictum. Vivamus et interdum ipsum, vitae facilisis massa. In auctor convallis feugiat. Nulla sit amet accumsan ipsum. Sed risus felis, sodales ornare nisl a, scelerisque fringilla neque.</p>
</div>
</div>
The content of your #navbar is wider than the width you have given to the #navbar itself.
#navbar would be 16em+2px = 258px (at a font size of 16px), while the content would be 20em+2.5em+2px = 290px (at a font size of 12.8px).
And since the #navbar has display:table, its own width will adjust itself to the width of its children rather than let the content overflow out of its boundaries. Tables do that.
So, calc(100%-17em) for the remainder doesn't make it. Either calculate a smaller width, or, since the navbar has float:left anyway, you can leave out the width altogether! Simply set the left margin if desired and you're done.
See Fiddle.

Troubles crafting a flexible CSS Layout for displaying user comments in a text balloon

I'm building a website that displays a vertically stacked list of comments that are placed by users.
The text should appear in a text balloon that basically displays the name of the user, there under the text and finally in the text balloon footer, it shows two links and floated to the right, a time stamp.
Since design/layouts are not my thing, it took me some painful days to achieve this in pure CSS (requirement) and I managed to make the list appear very neatly. For that I have tried to study the CSS that Google and Twitter use to show resp their video's and Tweets and try to extract some useful stuff from it. However, I noticed their CSS's and HTML are huge and I'm questioning if they did it the "right" way or if they found out that was the only possibility in order to make it display well on all types of devices. (Can somebody shed some light on that perhaps?) Conclusion is that it was not very useful for me.
However, the result doesn't feel good and is very "touchy" (not flexible at all); for instance, when I resize my window or open the page on my tablet, it just looks disgusting; text block wrapped and displayed under the avatar image...
Question 1: as I mentioned, I have been looking /studying a lot by looking how the big sites (such as YouTube, Twitter and FaceBook) doing similar things and the HTML/CSS looks a bit messy in my opinion. Anybody sharing that thought/opinion?
Question 2: can someone provide me a with good starting point, i.e. HTML/CSS Example (preferably in a JSFiddle or so) for the following:
Some remarks:
No images should be used (expect from the avatar image offcourse)
No tables should be used; only Div's and/or HTML-5 sementics (such as header, footer, article, and so on)
The CSS/HTML layout should be that flexible that it adjust itself properly. On the image you can how I would like to have it displayed in different scenarios.
Should display well in latest version in IE, FireFox, Safari and Chrome.
Given the following mark-up:
<div class="wrap">
<img src="http://davidrhysthomas.co.uk/img/dexter.png" />
<div class="comment" data-owner="Dexter">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque lacus lacus, blandit sit amet iaculis sodales, eleifend ut massa. Mauris arcu felis, facilisis sed bibendum et, tristique tincidunt dolor. Cras a hendrerit nisl. Maecenas accumsan, urna at aliquam blandit, ipsum erat pellentesque urna, et interdum mauris lacus et tellus.</p>
<ol class="postscript"> <!-- links and timestamp -->
<li>link 1</li>
<li>link 2</li>
<li class="date">3 days ago</li>
</ol>
</div>
</div>​
And the following CSS:
div.wrap {
width: 90%;
margin: 0 auto 1em auto;
position: relative; /* the image will be absolutely-positioned relative to this */
}
div.wrap:first-child {
margin-top: 1em; /* just for aesthetic reasons, adjust or remove, to taste */
}
div.comment {
font-size: 1em;
position: relative; /* the arrow on the left side of the div positioned relative to this element */
margin-left: 60px; /* allows a 10px gutter for the arrow to fit into */
border-radius: 0.75em 0.75em 0.75em 0.75em;
background-color: #ccc;
line-height: 1.4em;
font-family: Helvetica; /* or whatever... */
}
div.comment::before { /* requires a fairly modern browser */
content: attr(data-owner); /* displays the name of the comment-owner */
border-radius: 0.75em 0.75em 0 0;
background-color: #ccc;
display: block;
text-indent: 10%; /* adjust to taste */
border-bottom: 3px solid #999;
}
div.comment::after { /* again, requires a fairly modern browser */
content: ''; /* this property is necessary, even if only an empty string */
position: absolute;
top: 50%;
left: 0;
border: 10px solid transparent;
border-right: 10px solid #ccc; /* forms the 'arrow' */
margin: -10px 0 0 -20px;
}
div.comment p { /* or whatever, adjust to taste */
width: 80%;
margin: 0 auto 1em auto;
padding-bottom: 1em;
}
img {
position: absolute;
top: 50%;
width: 50px;
float: left;
border-radius: 10px;
margin-top: -25px;
}​
p + ol.postscript {
width: 80%;
font-size: 0.8em;
margin: -0.5em auto 0 auto;
}
ol.postscript::after {
content: '';
height: 0.5em;
display: block;
clear: both;
}
ol.postscript li {
float: left;
margin-right: 0.5em;
}
ol.postscript li.date {
float: right;
margin-right: 0;
}
.wrap a:link,
.wrap a:visited {
color: #333;
text-decoration: none;
border-bottom: 1px solid #333;
}
.wrap a:hover,
.wrap a:active,
.wrap a:focus {
color: #f00;
border-bottom: 1px solid #f00;
}​
JS Fiddle demo.
Edited in response to the valid comments, left below:
I don't think screen readers reads attributes which means it would probably be better to put the content of data-owner inside its own element, instead of an attribute.
One quibble (as noted above too) [Screenreaders will not read CSS generated content](One quibble (as noted above too) Screenreaders will not read CSS generated content and the comment author seems to me to be an essential bit of content that should be accessible to screenreader users.) and the comment author seems to me to be an essential bit of content that should be accessible to screenreader users.
Given the sound advice, I've replaced the .comment::before element, adding a discrete h2:
<div class="wrap">
<img src="http://davidrhysthomas.co.uk/img/dexter.png" />
<div class="comment" data-owner="Dexter">
<h2 class="owner">Dexter</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque lacus lacus, blandit sit amet iaculis sodales, eleifend ut massa. Mauris arcu felis, facilisis sed bibendum et, tristique tincidunt dolor. Cras a hendrerit nisl. Maecenas accumsan, urna at aliquam blandit, ipsum erat pellentesque urna, et interdum mauris lacus et tellus.</p>
<ol class="postscript">
<li>link 1</li>
<li>link 2</li>
<li class="date">3 days ago</li>
</ol>
</div>
</div>
and appended the following CSS (in place of the original .comment::before):
div.comment p {
width: 80%;
margin: 0 auto 1em auto;
}
Revised JS Fiddle.

position element over image

I am having some weird problems trying to position the h3 tag over the image .featured-image(Sitting on top of the image). When I try position: absolute; .. If the content in <p> is more or less, the h3 will move from its position. If I try position: relative; theres a big gap from the image and if I tried to margin <p> closer or farther, the position also moves. If I wrap another div around the h3 tag with position relative; and then leave h3 as position: absolute;. It fixes it but that seems such a hack.
HTML:
<section class="featured">
<h1><img src="images/icon-featured.png" width="24px" height="23px" alt="Featured Site Icon"> Featured Site </h1>
<div class="image-wrap">
<a href="#">
<img class="featured-image" src="images/content-images/image.jpg" width="399px" height= "37px" alt="" />
</a>
<h3> Lorem Ipsum </h3>
</div> <!-- image-wrap -->
<p>
Lorem Ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor.
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud.
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
</p>
</section><!-- .featured -->
CSS
article h1 img { margin: 0 5px 0 0; }
article .featured { }
article .featured h1 { }
article .featured .image-wrap { margin: 27px 0 0 0; text-align: center; }
article .featured .image-wrap img { border: 1px solid #8e8d8e; -webkit-box-shadow: 0 0 14px rgba(0, 0, 0, .4); -moz-box-shadow: 0 0 14px rgba(0, 0, 0, .4);
-o-box-shadow: 0 0 14px rgba(0, 0, 0, .4); box-shadow: 0 0 14px rgba(0, 0, 0, .4); }
article .featured .image-wrap h3 { background: rgba(23, 23, 23, .5); bottom: 66px; font-size: 23px; padding: 10px; position: relative; left: 190px;
text-align: right; width: 230px; }
article .featured p { margin: 25px auto; padding: 10px; width: 380px; }
Position .image-wrap relatively, but without any left/right/top/bottom properties. Then, position the h3 absolutely and move it to where you want it.
The problem you're encountering is that absolutely positioned elements have to be contained in a non-statically positioned ancestor element, otherwise it defaults to positioning it relative to the html element (that's probably why it was affected by the following paragraph). Relatively positioned elements are still part of the page flow, so the space relatively positioned elements would originally have taken up is preserved. That's the extra space you're seeing.
The w3c has a nice intro to css positioning: http://www.w3schools.com/css/css_positioning.asp, as does this page: http://www.barelyfitz.com/screencast/html-training/css/positioning/