png icon with circle border - CSS - html

I've got a PNG image icon with un-even sides (64px x 42px) and I'd like to create a circle border around it.
My html looks like this:
<span class="cat_circle">
<div class="cat_icon">
<img src="https://cdn.pbrd.co/images/GNK97WG.png">
</div>
</span>
I created a circle border around the image, but I just can't get the icon to the exact middle of the circle. The icon sits on the bottom right of the circle.
Here is my CSS for the circle border:
.cat_circle {
border: 3px solid #7E9CC2;
border-radius: 50%;
width: 25px;
height: 25px;
overflow: hidden;
position: absolute;
padding: 30px;
left: 10px;
top: 10px;
text-align: center;
vertical-align: middle;
}
I then started fiddling with the actual png image and I gave it negative margins like this:
.cat_icon {
margin-top: -10px;
margin-left: -18px;
}
I mean it seems to work and I have my icon in the middle of the circle, but is this the right way I should be approaching this??
Here is my fiddle: https://jsfiddle.net/ox0anvL7/

You can do that a lot simpler. I added three flexbox properties for the centering, but erased one HTML wrapper and quite a few of the (too complicated) CSS settings:
.cat_circle {
border: 3px solid #7E9CC2;
border-radius: 50%;
width: 70px;
height: 70px;
display: flex;
justify-content: center;
align-items: center;
}
.cat_circle img {
width: 80%;
height: auto;
}
<span class="cat_circle">
<img src="https://cdn.pbrd.co/images/GNK97WG.png">
</span>

This is a pretty good way to do it. This works for all cases too when trying to center something inside a div.
It can be better than using flex as I believe that flex is only available in versions of IE 10 onwards (source here).
.cat_circle {
position: absolute;
display: inline-block;
border: 3px solid #7E9CC2;
border-radius: 50%;
width: 70px;
height: 70px;
}
.cat_circle img {
position: absolute;
top: 50%; left: 50%;
width: 80%;
transform: translate(-50%, -50%);
}
<span class="cat_circle">
<img src="https://cdn.pbrd.co/images/GNK97WG.png">
</span>

You Can Try this way !!
<div class="image-circle">
<div>
<img class="img" src="https://cdn.pbrd.co/images/GNK97WG.png">
</div>
</div>
.image-circle {
width:25%;
}
.image-circle:after {
content: "";
background: #4679BD;
padding-bottom: 100%;
border-radius: 50%;
display: block;
width: 100%;
height:0;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
}
.image-circle div {
float:left;
width:100%;
line-height:1em;
margin-top:-0.5em;
padding-top:40%;
text-align:center;
}

I'd suggest flexbox, simplifies and reduces the need of another class.
.cat_circle {
border: 3px solid #7E9CC2;
border-radius: 50%;
width: 25px;
height: 25px;
overflow: hidden;
position: absolute;
padding: 30px;
left: 10px;
top: 10px;
/* new */
display: flex;
justify-content: center;
align-items: center;
}
<span class="cat_circle">
<img src="https://cdn.pbrd.co/images/GNK97WG.png">
</span>

Related

how to move SVG icon into div?

I want to make that white check mark into the green.
I set the check's size into 100px for now.
.approvalContainer {
align-self: flex-end;
width: 24px;
height: 24px;
background-color: var(--color-background-success-dark);
vertical-align: flex-end;
}
.approvalIcon {
width: 100px;
height: 100px;
}
dom structure
<div style=approvalContainer>
<svg style=approvalIcon/>
</div>
Plenty of ways to skin a cat here. This could be one, noting that I swapped the svg to another div purely for this example. Also note that the html you provided is not syntactically correct.
.approvalContainer {
align-self: flex-end;
position: relative;
display: block;
width: 48px;
height: 48px;
background-color: #0f0;
border-radius: 50%;
vertical-align: flex-end;
}
.approvalIcon {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: block;
color: #fff;
}
<div class="approvalContainer">
<div class="approvalIcon">√</div>
</div>

Display 'SALE' tag before an image

I am looking to display a 'SALE' tag just before an image, I have tried to do this using the 'before' pseudo element however nothing seems to be displaying on the screen.
I am trying to create the 'SALE' tag inside a circle with black background.
Below is the code that I have used
<span class"bag-image">
<img src="https://images-na.ssl-images-
amazon.com/images/I/71lDa7EbWSL._UY395_.jpg" class="image">
</span>
.bag-image::before{
background-color: #red;
content: 'SALE';
border-radius: 500px;
display: inline-block;
width: 100px;
height: 100px;
}
For visual reference:
LIKE THIS
https://codepen.io/anon/pen/rgLPdp
Make the bag-image class position: relative;
Make the bag-image:before position: absolute; and position it with top/left or margins and set the line-height to vertically center the SALE text.
You can give the pseudo-class a lower z-index so that only the top half is visible, e.g. z-index: -1;
You can use margin-top: -2.5em; margin-left: 175px; in the pseudo-code to position it.
div.bag-image {
display: inline-block;
/* just so that we can see in the example */
margin-top: 3em;
}
div.bag-image:before {
position: absolute;
background-color: #ff0000;
content: 'SALE';
text-align: center;
color: #ffffff;
margin-top: -2.5em;
margin-left: 175px;
/* optionally make it a circle */
border-radius: 9999px;
height: 3em;
padding: 1em;
line-height: 3em;
}
/* just for clarity */
img.image {
border: 1px solid #ccc;
}
<div class="bag-image">
<img src="https://images-na.ssl-images-amazon.com/images/I/71lDa7EbWSL._UY395_.jpg" class="image">
</div>
I think you want this. Also check the codepen.Codepen
<style>
.bag-image{
text-align:center;
}
.bag-image::before{
content: 'SALE';
border-radius: 50px;
background: red;
display: inline-block;
width: 100px;
height: 100px;
position: absolute;
line-height: 100px;
color: #fff;
}
</style>
In summary, you need to add the position: relative property to the span, and the position: absolute property to the ::after element. Something like this:
.bag-image {
position: relative;
display: block;
}
.bag-image::after {
background-color: red;
content: 'SALE';
border-radius: 500px;
display: block;
width: 100px;
height: 100px;
position: absolute;
top: 0px;
text-align: center;
left: 100px;
}
<span class="bag-image">
<img src="https://images-na.ssl-images-amazon.com/images/I/71lDa7EbWSL._UY395_.jpg" class="image">
</span>
Play with the left (and maybe the top) property to place the text in the desired position.
You can try this code:
your code background-color: #red; is the wrong to declared #red it's instant of only red.
the sale is a position by my self parent relatively, you can learn about more position https://www.w3schools.com/css/css_positioning.asp .
here also write a shadow element related to some code for needed your answer.
maybe it solves your problem.
=== Thanks ===
.bag-image {
margin-top: 50px;
position: relative;
border: 1px solid red;
}
.bag-image img {
display: block;
max-width: 100%;
height: auto;
margin: 0 auto;
}
.bag-image::before{
background-color: red;
content: 'SALE';
border-radius: 500px;
display: inline-block;
width: 100px;
height: 100px;
text-align: center;
line-height: 100px;
color: #fff;
position:absolute;
top: 0;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="bag-image">
<img src="https://images-na.ssl-images-amazon.com/images/I/71lDa7EbWSL._UY395_.jpg" class="image">
</div>

How to arrange correctly?

I'm trying to create two banners with cropped corners, but I'm having problems with my code:
https://codepen.io/Jeerjmin/pen/VdBVKL
.container {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
background-color: white;
}
.banner-1 {
min-width: 200px;
max-width: 200px;
height: 300px;
margin: 70px;
background-color: white;
border: solid #01579B 4px;
border-radius: 15px;
overflow: hidden;
}
.banner-1:before {
position: absolute;
content: '';
min-width: 38px;
height: 60px;
top: 39px;
left: 303px;
background-color: white;
border-bottom: solid #01579B 4px;
transform: rotate(-45deg);
}
.banner-2 {
min-width: 200px;
max-width: 200px;
height: 300px;
margin: 70px;
background-color: white;
border: solid #01579B 4px;
border-radius: 15px;
overflow: hidden;
}
.banner-2:before {
position: absolute;
content: '';
min-width: 38px;
height: 60px;
top: 39px;
left: 652px;
background-color: white;
border-bottom: solid #01579B 4px;
transform: rotate(-45deg);
}
img {
position: relative;
top:100px;
left: 25px;
width: 200px;
height: 150px;
}
.dot {
height: 50px;
width: 50px;
background-color: deepskyblue;
border-radius: 50%;
position: relative;
left: -50px;
bottom: 100px;
text-align:center;
}
p {
text-align:center;
}
<div class="container">
<div class="banner-1">
<p>Card-1</p>
<img src="https://img2.goodfon.ru/original/1366x768/6/5e/koshka-kot-meyn-kun-belyy-fon-5567.jpg"></img>
<span class="dot dot-1">
<h1>0,5</h1>
<h5>кг</h5>
</span>
</div>
<div class="banner-2">
<p>Card-2</p>
<img src="https://img2.goodfon.ru/original/1366x768/6/5e/koshka-kot-meyn-kun-belyy-fon-5567.jpg"></img>
<span class="dot dot-2">
<h1>0,5</h1>
<h5>кг</h5>
</span>
</div>
</div>
Problem 1:
I'm trying to crop the top left corners of the banners, but the cards move around while the crops stay where they are.
Problem 2:
The span with class="dot" should form a colored circle, but does not.
Not sure what you meant in Problem 1. As for problem 2, you need to add display: block to your .dot class and you also can remove left property form it.
I had to make several changes to your code to get this working, so see the code snippet below for all the changes. However, here are a few key points:
To position your ::before pseudo-elements in correct relation to the banners, add relative positioning to the banners.
To make your blue dot work, and to align its contents correctly, Flexbox layout is a good solution.
To simplify your code, it's preferable to style similar items together. For that reason, I've given your banners shared styles.
<img> elements should not have a corresponding closing tag (the technical term for this kind of element is "self-closing"). That said, I changed your image to be a background image. The reason is that you can't both have overflow: hidden on your banners and put the clipped corner over its borders.
.container {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
background-color: white;
}
/* Target both banners together,
since they share styles */
.banner-1,
.banner-2 {
min-width: 200px;
max-width: 200px;
height: 300px;
margin: 70px;
border: 4px solid #01579B;
border-radius: 15px;
/* Relatively position the banner,
so that the clipped corners,
can be properly placed */
position: relative;
/* To have the image clip to
the banner, make it a
background image */
background: white no-repeat 10px bottom / auto 150px
url("https://img2.goodfon.ru/original/1366x768/6/5e/koshka-kot-meyn-kun-belyy-fon-5567.jpg");
}
.banner-1::before,
.banner-2::before {
content: "";
width: 22px;
height: 38px;
position: absolute;
left: 0;
top: -1px;
background-color: white;
border-right: 4px solid #01579B;
/* Adjust your transform origin,
to make the rotation easier */
transform-origin: top right;
transform: rotate(45deg);
}
.dot {
/* Use Flexbox to align the
content in your dot */
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 80px;
width: 80px;
background-color: deepskyblue;
border-radius: 50%;
position: absolute;
left: 20px;
bottom: 100px;
}
h1, h5 {
/* Remove margins on
dot content */
margin: 0;
}
p {
text-align: center;
}
<div class="container">
<div class="banner-1">
<p>Card-1</p>
<span class="dot dot-1">
<h1>0,5</h1>
<h5>кг</h5>
</span>
</div>
<div class="banner-2">
<p>Card-2</p>
<span class="dot dot-2">
<h1>0,5</h1>
<h5>кг</h5>
</span>
</div>
</div>

CSS Make a div curved at top and bottom but straight at the left and right sides

Hey guys I've been wondering if it was possible to do this, I've tried with border-radius but it only makes curves to the left and right sides apparently...
Here's what I need:
This would be the working div:
#mainbox {
width: 115px;
height: 24px;
background-color: gray;
border: 1px solid #000000;
text-align: center;
}
<div id="mainbox">
<div id="secondbox">test</div>
</div>
Any possible ideas?
Something like this can be achieved but it's troublesome. SVG will be better for this.
Referenced from this question on SO.
body {
width: 100%;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
#mainbox {
width: 200px;
height: 130px;
overflow: hidden;
position: relative;
display: flex;
align-items: flex-end;
justify-content: center;
}
#mainbox::before,
#mainbox::after {
content: "";
display: block;
position: absolute;
height: 100px;
/* equal to inner curvedbox */
border-left: 5px solid black;
bottom: 0;
z-index: 1;
}
#mainbox::before {
left: 0;
}
#mainbox::after {
right: 0;
}
#curvedbox {
position: relative;
width: 100%;
height: 100px;
display: flex;
align-items: center;
justify-content: center;
}
#curvedbox::before,
#curvedbox::after {
display: block;
content: "";
width: 140%;
height: 200%;
border: solid 5px #000;
border-color: #000 transparent transparent transparent;
border-radius: 100%;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
#curvedbox::before {
top: -30%;
}
#curvedbox::after {
top: 69%;
}
#secondbox {
transform: translateY(-140%);
}
<div id="mainbox">
<div id="curvedbox">
<div id="secondbox">test</div>
</div>
</div>
A few possible ideas:
You can try using
border-top-left-radius: 30%;
border-top-right-radius: 30%;. The problems with this are twofold: You'll see that it does end up curving the corners too when the percentages get higher (which isn't what you want), and it also doesn't support inverted, so you won't be able to use it for the bottom of the div.
Or, you can mess around with the clip-path property, like this: clip-path: polygon(0 0, 100% 0, 100% 96%, 0 100%); This article https://www.viget.com/articles/angled-edges-with-css-masks-and-transforms talks about how he uses that css property to make slanted divs.
Take a look at the methods presented in Invert rounded corner in CSS?. The point there was to try to make an inverted rounded corner, and a lot of the solutions presented pretty creative ways to manipulate the borders of a div.
I know this isn't a complete answer, but I hope it helps anyways :)

Circles inside circle and vertical centering text

I am trying to draw 2 outer circle around a circle and keeping the text as vertically centered.
I am able to draw a circle outside a circle but not the 3rd one.
Html
<div id="content">
<div id="outer-circle">
<p>text</p>
</div>
</div>
Demo : http://jsfiddle.net/squidraj/7vusbo0v/1/
The text is also not centering horizontally.
Any help is highly appreciated. Thanks in advance.
Here is my solution, based on your code:
Creating a "3rd circle" by using the parent #container
centering the text by using the display:table-cell(which allows you to vertical align elements)
#content {
border-radius: 50%;
height: 320px;
width: 320px;
position: relative;
box-shadow: 0 0px 0 10px green;
margin: 10px;
}
#outer-circle {
background: #385a94;
border-radius: 50%;
height: 300px;
width: 300px;
position: absolute;
box-shadow: 0 0px 0 10px black;
top: 10px;
left: 10px;
display: table;
}
#outer-circle p {
display: table-cell;
vertical-align: middle;
text-align: center;
}
<div id="content">
<div id="outer-circle">
<p>text</p>
</div>
</div>
I was editing my answer by the time it got accepted and received comments on, but no matter what I'm giving the other solution i was typing at the time:
Applying the border property to your #outer-circle would do the "3rd circle" since you are using box-shadow on it.
to vertical align the text, same solution as the 1st one.
#outer-circle {
background: #385a94;
border-radius: 50%;
height: 300px;
width: 300px;
position: absolute;
box-shadow: 0 0px 0 10px green;
top: 10px;
left: 10px;
display: table;
border: 10px solid black;
margin:10px;
}
#outer-circle p {
display: table-cell;
vertical-align: middle;
text-align: center;
}
<div id="content">
<div id="outer-circle">
<p>text</p>
</div>
</div>
There is nothing different with them being in a circle, so follow the normal centering rules
The specifics are dependant on what browsers you need to support
Patrick's reference is correct. Give the following a try:
#outer-circle p {
text-align: center;
width: 100%;
position: absolute;
top: 50%;
transform: translateY(-50%);
margin-top: -5px;
}
Note that I've added a negative top margin, which accounts for your border width.
set you css
#outer-circle p {
bottom: 0;
left: 0;
/* position: absolute; */
right: 0;
text-align: center;
padding: 50% 0;
top: 0;
}
see https://jsfiddle.net/fwzfoncy/