How do i crop an image using HTML and CSS? - html

I would like to add an image to my website like in this example -> Cropped image
How do I crop the image? If i am using a .png image, i'm still seeing the black/white background. Is there a way to have only the man without the background using HTML & CSS?

Here is an easy way to crop any image before uploading. https://codepen.io/githyoung/pen/LYEjwdO
// Start upload preview image
$(".gambar").attr("src", "https://user.gadjian.com/static/images/personnel_boy.png");
var $uploadCrop,
tempFilename,
rawImg,
imageId;
function readFile(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('.upload-demo').addClass('ready');
$('#cropImagePop').modal('show');
rawImg = e.target.result;
}
reader.readAsDataURL(input.files[0]);
}
else {
swal("Sorry - you're browser doesn't support the FileReader API");
}
}
$uploadCrop = $('#upload-demo').croppie({
viewport: {
width: 150,
height: 200,
},
enforceBoundary: false,
enableExif: true
});
$('#cropImagePop').on('shown.bs.modal', function(){
// alert('Shown pop');
$uploadCrop.croppie('bind', {
url: rawImg
}).then(function(){
console.log('jQuery bind complete');
});
});
$('.item-img').on('change', function () { imageId = $(this).data('id'); tempFilename = $(this).val();
$('#cancelCropBtn').data('id', imageId); readFile(this); });
$('#cropImageBtn').on('click', function (ev) {
$uploadCrop.croppie('result', {
type: 'base64',
format: 'jpeg',
size: {width: 150, height: 200}
}).then(function (resp) {
$('#item-img-output').attr('src', resp);
$('#cropImagePop').modal('hide');
});
});
// End upload preview image
label.cabinet{
display: block;
cursor: pointer;
}
label.cabinet input.file{
position: relative;
height: 100%;
width: auto;
opacity: 0;
-moz-opacity: 0;
filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);
margin-top:-30px;
}
#upload-demo{
width: 250px;
height: 250px;
padding-bottom:25px;
}
<div class="container">
<div class="row">
<div class="col-xs-12">
<label class="cabinet center-block">
<figure>
<img src="" class="gambar img-responsive img-thumbnail" id="item-img-output" />
<figcaption><i class="fa fa-camera"></i></figcaption>
</figure>
<input type="file" class="item-img file center-block" name="file_photo"/>
</label>
</div>
</div>
</div>
<div class="modal fade" id="cropImagePop" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">
<?=multiLanguage( "Edit Foto" , "Edit Photo" )?></h4>
</div>
<div class="modal-body">
<div id="upload-demo" class="center-block"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" id="cropImageBtn" class="btn btn-primary">Crop</button>
</div>
</div>
</div>
</div>

If I understood correctly, you want to use a picture without any background.
for this, you should use a transparent png like this:
body{
background:lightblue
}
<img src="https://sb.kaleidousercontent.com/67418/800x533/9e7eebd2c6/animals-0b6addc448f4ace0792ba4023cf06ede8efa67b15e748796ef7765ddeb45a6fb-removebg.png" width="500"/>
And if you want to add your voluntary color to the picture background like your sample:
img{
background: linear-gradient(45deg, #cb2d2d, #172aec);
}
body{
background:lightblue;
}
<img src="https://sb.kaleidousercontent.com/67418/800x533/9e7eebd2c6/animals-0b6addc448f4ace0792ba4023cf06ede8efa67b15e748796ef7765ddeb45a6fb-removebg.png" width="500"/>
But if your picture isn't transparent, I think you must make it transparent on photoshop at first.

To my knowledge, there isn't a way of doing this through CSS or HTML. There is no inbuilt function to do this. So the only way to achieve this is by removing the background of the image by a third-party service like remove.bg or apps like photoshop etc.. (preferred to use Adobe Photoshop and use the pen tool, Magic wand tool or Quick selection tools for removing the external background of the image.) and then downloading it as a png and then uploading to your website.
Here I will mention some usefull softwares and websites for removing backgrounds, which are top rated and which maintains the quality of your image.
Adobe Photoshop
Canva
PhotoScissors
Wondershare PixCut
HitPaw Online Background Remover
Remove.bg
Once you get the png image, you can directly put it into your website. If you still see some white background altering over the image, use background-color: transparent.

Related

How to change d-none class to d-block in jquery on hover?

I would show and hide a button on edit when hovering a div. Like WordPress.
How to display a div inside a div if hovered? I'm trying to do this but there's something wrong, nothing appears.
<div class="row d-flex align-items-center" id="company">
<div class="col-lg-6 mb-4">
<div class="p-5">
<h1>Company</h1>
<p class="text-justify">Description</p>
Start
</div>
</div>
<div class="col-lg-6 mb-4">
<img src="/images/layer.png" alt="Company" class="img-fluid lazy">
</div>
<div class="p-5">
<div id="menu">
<div class="btn-group">
<button type="button" class="btn btn-warning">وEdit</button>
</div>
</div>
</div>
</div
<script>
$("#company").hover(function(){
$("#menu").css("d-none", "d-block");
});
</script>
wordpress
You don't need Javascript for this. You can do it all in CSS. The first rule below defaults to hiding the menu. The second rule overrides the first rule to show the menu when you hover over the #company div.
#menu {
display: none; /* Default */
}
#company:hover #menu {
display: block;
}
If you want to do it with Javascript/jQuery, you must specify separate actions for the mouseenter and mouseleave events, to ensure the menu shows/hides accordingly:
$("#company").hover(
function() {
$("#menu").css("display", "block");
},
function() {
$("#menu").css("display", "none");
},
);
If you want to use purely Bootstrap classes rather than manipulating the CSS style rules directly:
$("#company").hover(
function() {
$("#menu").addClass('d-block').removeClass('d-none');
},
function() {
$("#menu").addClass('d-none').removeClass('d-block');
},
);
Hi your code is almost correct.In your Jquery part you have to use addClass() instead of css() because you are trying to change the class name of the element.Just try this one.Hope this works
<script>
$("#company").hover(function () {
$("#menu").addClass("d-none");
});
</script>
By default the css can be added to the "menu" id
#menu {
display: none;
}
using js:
$("#company").hover(function () {
$("#menu").css("display", "block");
});

Creating a list of items, assign them id and than make a option button with overlay with multiple options

I'm working on a list of items for my website.
I have a list of 5 items with a 'id'. When you click the button, an overlay must be shown with 2 buttons 'change to background to red' and than 'cancel'.
If you click the cancel, the specific 'div class="item"' with the specific id his background must become red.
But, the problem is I don't know how using jquery/javascript to know which button of the div what pressed (button of item id 1 or 2 or 3..)
And also when you click outside the buttons, the overlay must be removed.
Here's the code
$(document).ready(() => {
$('.options-btn').click(function ()
{
var id = $(this).parent().attr('id'); /* find <div class="item"> */
$('body').append('<div class="overlay"></div>');
var append = `
<div class="item-options-active">
<button class="feed-option-btn-number background-btn" tabindex="0">Background set to RED</button>
<button class="feed-option-btn-number cancel-btn" tabindex="0">Cancel</button>
</div>
`;
$(append).appendTo('.overlay');
});
$(document).click(function (e)
{
if (document.getElementsByClassName("overlay").length == 1)
{
if(document.getElementsByClassName("item-options-active").length == 1)
{
// this condition is not working when you click the specific button
if($(".background-btn").data('clicked'))
{
// how to get <div> of the button which was press to change the background??
$('.item').css('background', 'red');
}
}
}
});
})
body {
background: grey;
}
.item {
background: green;
border: 5px solid purple;
margin: 20px;
}
.item button {
margin-left: 10px;
}
.overlay {
position: fixed;
width: 100%;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0,0,0,.85);
z-index: 10000;
}
<body>
<div class="show-items">
<div class="item" id="1">
<button type="button" class="options-btn">check options</button>
</div>
<div class="item" id="2">
<button type="button" class="options-btn">check options</button>
</div>
<div class="item" id="3">
<button type="button" class="options-btn">check options</button>
</div>
<div class="item" id="4">
<button type="button" class="options-btn">check options</button>
</div>
<div class="item" id="5">
<button type="button" class="options-btn">check options</button>
</div>
</div>
</body>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
If you rewrite the .option-btn click event to something like this and delete the document click event it should work:
$('.options-btn').click(function ()
{
var id = $(this).parent().attr('id'); /* find <div class="item"> */
var button = $(this);
$('body').append('<div class="overlay"></div>');
var append = `
<div class="item-options-active">
<button class="feed-option-btn-number background-btn" tabindex="0">Background set to RED</button>
<button class="feed-option-btn-number cancel-btn" tabindex="0">Cancel</button>
</div>
`;
$(append).appendTo('.overlay');
$('.overlay').on('click', function(){
$(this).remove();
}).find('.cancel-btn').on('click', function(){
$(this).closest('.overlay').remove();
});
$('.overlay').find('.background-btn').on('click', function(){
button.closest('.item').css('background', 'red');
$(this).closest('.overlay').remove();
});
});
Working fiddle: https://jsfiddle.net/qntupzj6/

HTML/CSS- Button alignment

I'm new to HTML/CSS, so I'm still working out the tags and their uses... This is what my page looks like atm.
This is the goal- sorry for the bad paint drawing..
I want to increase the size of the buttons, "+" and "-", and make them closer to the dropdown, and to the right of the dropdown...Currently, I can only get it this near.
<button type="button" class="button6" id="add_service">+</button>
<button type="button" class="button6" id="delete_row">-</button>
This is the HTML code I have for it. I have a header in CSS to link to, and I've been playing around with it, but I can't get the alignment right...How can I get it to look similar to my paint drawing?
CSS :
.button6 {
text-align: right;
content: "\00a0 \00a0 ";
}
Here is my solution
Its not perfect, but it will give you starting point
.parent {
width: 150px;
}
select {
width: 100%;
margin-bottom:5px;
}
.btns {
text-align: right;
}
<div class="parent">
<select>
</select>
<div class="btns">
<button type="button" class="button6" id="add_service">+</button>
<button type="button" class="button6" id="delete_row">-</button>
</div>
</div>

Img src to a link

Is it possible to make this chili img to a "a href". The function of the pop up is working fine, but there is no "hand", when you hover the picture.
Link to my portfolio
HTML
<img
src="/images/thumbs/image1.jpg"
data-toggle="modal"
data-target="#myModal1"
alt="Trolltunga, Norway"
width="300px"
height="200px"
>
<div id="myModal1" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<img src="//placehold.it/1000x600" class="img-responsive">
</div>
</div>
</div>
</div>
JS
function centerModal() {
$(this).css('display', 'block');
var $dialog = $(this).find(".modal-dialog");
var offset = ($(window).height() - $dialog.height()) / 2;
// Center modal vertically in window
$dialog.css("margin-top", offset);
}
$('.modal').on('show.bs.modal', centerModal);
$(window).on("resize", function () {
$('.modal:visible').each(centerModal);
});
img:hover{
cursor: pointer;
}
Add this to your css
You could add this,
img{
cursor:pointer;
}
cursor:pointer indicates that as link. But actually in your codes there is no <a> tag around your image so you can change your cursor property.
Simple use this code in your css.
img:hover{
cursor: pointer;
}

Remove modal footer from twitter bootstrap modal

I want to remove the footer from my modal. But it is only removing the contents and not the space acquired by it.
Here is my code...
<div class="modal hide fade">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">× </button>
<h3>Modal header</h3>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
</div>
If you don't use '<div class="modal-footer">' in your modal template then bootstrap will made it for you. I think this is why you see the footer with your code.
My solution is hidding the footer in the template (last line) :
<div class="modal hide fade">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">× </button>
<h3>Modal header</h3>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer" style="display:none"></div>
</div>
Try this
<div class="modal-footer" hidden="true"></div>
I'm pretty sure the "extra space" is the <p> margin. Try this css :
.modal .modal-body p {
margin-bottom: 0;
}
I think its because of the padding given to modal-footer class. Try overriding it
.modal .modal-footer {
padding: 4px;
}
I have kept it as 4px to preserve round corners
You can override modal-header, modal-footer of the bootstrap modal.
Add the following code.
.modal-footer{display: none;}
This can be achievable using jQuery
require(
[
'jquery',
'Magento_Ui/js/modal/modal'
],
function(
$,
modal
) {
var options = {
type: 'popup',
responsive: true,
innerScroll: true,
buttons: []
};
$('#approvalsModal').modal(options,{
closed: function(){
// Do some action when modal closed
}
});
$(".approvalsModal").click(function(){
$("#approvalsModal").modal('openModal');
});
}
);