Optimization in terms of page speed and page size.
First method is showing the image as a background of a div instead of in an img tag:
<div style="background:url("image-url.jpeg") no-repeat;background-size:cover;max-width:350px;max-height:350px;display:block;width:100%;height:100%;"></div>
Second is the normal img tag way:
<img src="image-url.jpeg" width=350 height=350 alt="" />
Or anyone has a better way to display image.
Thank you.
*Disregarding SEO benefits
An answer here : https://buildawesomewebsites.com/html-img-vs-css-background-image/
Imo, I'll go with the img tag, but I'll improve your code : If you want to improve perfs, you could use 'srcset' attributes with your img tag : https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images and load smaller images for smaller devices.
In addition, you can use lazy load with img tag easily
Related
How can I resize an image on mouse hover using PHP with $_GET? I know that this can easily be done with css or js but I want to know how to do it with php. I don't want to upload a file either. So let's say I have html code with an <img> tag and an image, how can I use PHP or $_GET to resize that image on hover?
EDIT:
What i mean is to put the image between <a> tags and then when clicked it refreshes the page but adds the the name of the image to the url so then I could use $get to access and echo a style to resize it.
Resize question on Stack Overflow
PHP is server side and you would have to refresh your page to display a resized image on hover(although I'm not sure if you can implement action on hover without js) and if you don't want to refresh you need to use AJAX(and that requires Javascript)
Going off of your edit, are you looking for something like this?
<?php
if(isset($_GET['image'])){
echo "<img src={$_GET['image']} style='height: 100px; width: 100px;' />";
}
?>
<img src="image_name.jpg" />
Hi I would like to give my clients who use a CMS for their website the ability to add their own background image. My CMS doesn't allow clients to edit the background-image directly so I would like to enable them to do it by using some php or other script. I would like them to be able to upload an img as normal (this image will be hidden from view on the webpage but will be visible in the CMS) and then use the script to take the img src and put it into the background-image:url('...');
Here is my code:
<div id="maincontentcontainer" style="background-image:url('...');background-size:100%;background- attachment:fixed;">
<img src="..." alt=""/>
Looking forward to any answers, thanks!
The js part, as I assume that's what you're asking for, is quite simple. You'll have to make sure your CSS is there to make sure it's not visible though. Also assuming jQuery...
var $image = $('.uploaded-image'),
src = $image.attr('src');
$image.remove();
$('#maincontentcontainer').css('background-image', 'url('+src+')');
Fiddle: http://jsfiddle.net/61znv2a3/
I'm editing a website that contains an image slider that displays two images side-by-side, however when i view the website in chrome the images are not inline with each other like so: http://i754.photobucket.com/albums/xx182/rache_R/Untitled-1_zps6f3014ef.jpg
I use php and html to get data from a database and display the images based on the data gathered. I have tried to target the images individually in my css but it either doesn't work or effects both images.
Is there a correct way of targeting the images individually or how can i fix this?
php code:
<?
$result = $mysqli->query("SELECT * FROM stock");
while($obj = $result->fetch_object())
{
if($obj->id&1)
echo "<u1>"; ?>
<li class="<?=$obj->orientation=='landscape'?'landscape':'portrait'?>">
<img src="./img/<?=$obj->description=='unframed'?$obj->poster_no:$obj->poster_no.'_frame'?>.jpg" alt="">
<h4><?= $obj->description=="unframed"?"Unframed Poster - 750mm x 500mm":"Framed Poster - 790mm x 540mm"?><br />£<?=$obj->price?> each</h4></li>
so i managed to solve it:
#media screen and (-webkit-min-device-pixel-ratio:0){
#mi-slider li {
vertical-align: top;
}}
Place the < ul> having a width so much that it can hold both images side by side and give style='display:inline' for the ul.. You will get the images inline
This is more of a css question.
You have one of to options, using an inline list or a float.
personally i line inline list, you could also use a table but that is considered bad practice.
You could also use java script to calculate the position, which is also bad practice if used improperly.
W3schools.com should be a good reference if you dont know how to implement these properties in css.
Next time please replace your php with static information so it looks better :) cheers
I'm making a blog, that has specific html5 structure for img:
<figure>
<img />
<figcaption></figcaption>
</figure>
Is there a way to customize, how the_content() handles img rendering, adding my own template for img specificly (plus adding the_post_thumbnail_caption() template)?
Or is there a way to make your own template for WYSIWYG. I'm asking because i'd like to allow user, to apply one out of three possible img displays, that are bound to specific class of figure element (for example )
Thanks in advance
You could use get_the_content() and then use preg_match to look for the img tag and replace with your own code see how to replace img that with preg_match here https://stackoverflow.com/a/1107203/1287608
and the get_the_content() function here http://codex.wordpress.org/Function_Reference/get_the_content
i have problem in displaying an image in textarea
please help me to solve it
thanks
Textareas can only hold plain text. Use contenteditable or a fully functional editor, like TinyMCE to save you the troubles.
As anothershrubery already mentioned, textarea can only hold plain text.
You can instead use a div element with the contenteditable attribute set to true.
<div contenteditable="true">
You can edit this text and also add images! <img src="smiley.jpg" alt=":)" />
</div>
There are also many different JavaScript WYSIWYG editors which uses the above method or an iframe in designmode.
Here's a couple of them:
Aloha Editor
MarkItUp
TinyMCE
Lightweight RTE
CKEditor
You cannot display images in a textarea. Textarea's just accept plain text.
Textarea support text only, if you want to display image/ link or any thing else, use WYSIWYG HTML Editor
You can try this, a really good one ;)
http://premiumsoftware.net/cleditor/
You can try this:
HTML:
<div id="image_txtarea" style="width:auto">
<img src="image/Example.jpg" id="image" style="position:absolute"/>
<textarea id="some" style="position:absolute;display:none"></textarea>
</div>
JQUERY
$('#image').click(function(){
var img = $('#image');
$(this).hide().next('textarea').show().css({'background':'url('+ img.attr('src') +') no-repeat center center','width':img.width(),'height':img.height()});
});
The answer is yes, image can be placed in textarea.
I placed an animated gif in textarea (worked in ie-6 and ff-13.0.1) with this technique:
Convert any image into a base64 string. (free online converters available, there is a limit to the base 64 file size - around 30k image size); a simple url() background statement did not show image for me.
The online converters usually output both an <img> tag and a CSS background property with the correct values from the uploaded image. Just copy and paste the CSS output into your own file. use url data statement - url(data:image/ext.....)
use the base64 css as a background in the style statement.
to change images, you'll have to change div id="" or class where you have base 64 defined as background-image in the class or id.
time consuming to do conversions so make sure that you really want an image in a textarea before you start.