background-position not centering w/ clip-path not working - html

I am having trouble centering a background image. At first I thought there were underlying CSS rules causing it not to work so I created a demo with minimal markup and CSS.
From all examples on SO & elsewhere it seems like I'm doing everything right. Could someone please help to see what I'm missing? I mentioned clip-path: in the title of the question, but playing with these properties doesn't seem to make a difference. Thanks in advance.
Codepen Demo:
https://codepen.io/dickkirkland/pen/rNGbqMr
Markup:
<!-- a demo of positioning dynamic images stored in a database that can vary in size for
an avatar-like presentation in applications -->
<div class="container">
<div class="member-avatar">
<img src="https://memdata-dev.herokuapp.com/images/display_image/H0266" />
</div>
</div>
CSS/SCSS:
.container {
display: flex;
justify-content: center;
background-color:#ccc;
padding:33px;
}
// clip the background to create a round shape
.member-avatar {
-webkit-clip-path: circle(50% at 50% 50%);
clip-path: circle(50% at 50% 50%);
width: 176px;
height: 176px;
background-position: center center;
}
// scale up images that are small in the database
.member-avatar img {
transform: scale(1.5);
}

There is no background image for the background position to affect.
There is an img element but that is not a background in the CSS sense.
You need to position the image so it is at the top but centered on the circle.
This snippet does this with a background and uses size cover which ensures you get things as required (whatever the size of the original image, without needing further scaling, as long as the subject is well placed similarly to the face in this one.
.container {
display: flex;
justify-content: center;
background-color: #ccc;
padding: 33px;
}
.member-avatar {
-webkit-clip-path: circle(50% at 50% 50%);
clip-path: circle(50% at 50% 50%);
width: 176px;
height: 176px;
background-position: center top;
background-image: url(https://memdata-dev.herokuapp.com/images/display_image/H0266);
background-size: cover;
}
<div class="container">
<div class="member-avatar">
</div>
</div>

Related

HTML/CSS section with a specific values and shape

I'm thinking about a website design, kinda new to HTML/CSS and JS. My issue is related only to HTML and CSS. I'd like to make a sort of polygon shape that would be either transparent or white in which I can put text in and it would wrap the text according to the shape of the box. I'm attaching an image of how I'm thinking of making it but I can't seem to come to conclusion. I was thinking of making a normal polygon shape just like that and then separate the text into different classes and then wrap them and position them as they are but that seems kinda messy. Is there any better simpler way of doing this? Thanks in advance.enter image description here
I tried making it like this with a polygon shape, and then making different classes for text and then wrapping it so it fits into the shape. That honestly feels a bit messy. Any insights of how could I do that pls?
<section>
<div class="container">
<div class="clip-path-inset-square"></div>
</div>
</section>
<style>
.body {
margin: 0;
padding: 0;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
}
.container {
width: 500px;
height: 450px;
border: 5px solid lightgrey;
background: blue;
position: relative;
margin: 5px;
display: flex;
justify-content: center;
align-items: center;
}
.clip-path-inset-square {
width: 250px;
height: 250px;
background-color: brown;
clip-path: polygon(
60% 70%,
60% 30%,
100% 30%,
100% 0%,
2% 1%,
0% 100%,
100% 100%,
100% 70%
);
}
</style>

Gradient on background using CSS

I want to create something like this for the top section of my one-page website.
repeating background image with a gradient
I have figured out how to repeat a background image, but I was wondering if there is a way I can specify opacity for each time the image gets repeated.
This is the CSS code I've used for the section:
section{
width: 100%;
float: left;
height: 100vh;
background-image: url("img/bgflower.jpg");
background-repeat: repeat-x;
background-size: contain;
}
Please suggest any methods I can use to achieve the same, thank you!
If you want to have true gradient instead of visible opacity regions, you can do something like my code below. Unfortunately this does not really apply opacity to your image and works only with one color (like in your example picture you have white).
#background {
/* place at the top of your page */
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
/* set background image */
background: url(https://pyry.info/stackoverflow/flower.png);
background-repeat: repeat-x;
background-size: contain;
}
/* create the white gradient */
#gradientLayer {
width: 100%;
height: 100%;
background: linear-gradient(to right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1));
}
<!-- place this below everything else -->
<div id="background">
<div id="gradientLayer"></div>
</div>
I'm not sure if the section you made is responsive or if it sits within another container that has a fixed width. With the codes below, a fixed width will render a better result. However, I made something up in codepen to help you move along. https://codepen.io/jennift/pen/qBRJOYd?editors=1100. I've included some comments in the code below:
<section>
<div class="extended">
<div class="first">first</div>
<div class="second">second</div>
<div class="third">third</div>
<div class="fourth">4th</div>
</div>
</section>
section {
width: 100%;
height: 100vh;
background-image: url("https://placeimg.com/200/480/nature");
background-repeat: repeat-x;
background-size: contain;
}
/* again I'm not sure if you will use the same image bg. However, if you intend to change, remember to change the aspect ratio here as well so that the white layers on top will lay somewhat nicely aligned with the bg */
:root {
--aspectratio: 0.416; /* divide bg image width with bg image height of bg image > 200 / 480 */
}
.extended { /*this extends the container box so the divs stays in a row instead of breaking into a new line as the screen gets resized to something smaller */
width:500%;
height: 100vh;
overflow:hidden;
}
.first, .second, .third, .fourth {
background-color: #fff;
height: 100vh;
float: left;
width: calc(100vh * var(--aspectratio)); /*using the aspect ratio, you can then calculate the width of each white section
}
.first {
opacity:0;
}
.second {
opacity: 0.3;
}
.third {
opacity: 0.6;
}
.fourth {
opacity: 0.9;
}
With the codes above, if your section gets wider than this, you probably need to put in a fifth div, and probably javascript will be easier solution to auto-create divs as the screen gets wider/smaller. But if your width is fixed, this way works well.

How do I pin one image to the background-image (responsive)?

I need your help! I have a background image and I need one part of that image to move (rotate or zoom) whenever you hover it. This image is just an example but it's pretty similar what i have, except mine is more centered.
I want the little sticker on the bowl to move a little (rotate like 20 degrees or any sort of animation to be honest) whenever you mouse over it.
I've already cropped the sticker so now I have two images. I'm using the background as my div's bg. Here's my code:
HTML:
<header class="background-image">
<div class="sticker"></div>
</header>
CSS:
.background-image {
background-image: url(../img/bg-principal.png) !important;
background: white;
height: 90%;
background-position: center;
background-size: contain;
background-repeat: no-repeat; }
I've managed to place the sticker where it's supposed to be, but it's not responsive. When the screen is resized, the little sticker moves and I can't seem to find a solution for that. Basically what I need is to pin the sticker to the background image, but I have no idea if that's possible. I looked around and couldn't find anything that worked.
Does anyone have any suggestions? Is there any way that I can do this using only CSS? I only have basic knowledge of jQuery so I'd like to avoid that but if it's the only solution I'd be fine with that too.
Thanks!!
EDIT: I tried using a transparent png sticker the same size as the background, but since I need the hover effect, I end up having the same problem.
If you use vw as a unit of measurement, the sticker can track based on the size of the element
.background-image {
background-image:
url(http://images.all-free-download.com/images/graphiclarge/butterfly_flower_01_hd_pictures_166973.jpg) !important;
height: 40vw;
background-position: center;
background-size: contain;
background-repeat: no-repeat; }
.sticker{
width:20px; height:20px; position:absolute;
top: 20vw; left:40vw;
background:red;
}
<header class="background-image">
<div class="sticker"></div>
</header>
I finally found a solution!
This is my final code:
HTML:
<div class="background">
<img src="..."/>
<div id="pin" class="sticker">
</div>
</div>
CSS:
.background {
margin:10px;
position: relative;
display: inline-block;
}
.map img {
max-width:100%;
display: block;
}
.box {
width:20%;
height:20%;
background-image: url(...);
background-position: top center;
background-repeat: no-repeat;
background-size: contain;
position: absolute;
}
#pin {
top:60%;
left:46%;
}

How to create simple circle in css3 where I can move/resize the background image?

I tried with
.mask {
width: 200px;
height: 200px;
border-radius: 50%;
-webkit-border-radius: 50%;
-moz-border-radius: 50%;
background: url(http://lapeniche.net/wp-content/uploads/2013/12/Chat.jpg) no-repeat;
background-position: -50px -50px; /* this allows to move the image inside */
/* but how to resize the CAT picture ???? */
}
<img class="mask">
which is almost good: I can move the pic with backgound-position, but I CANNOT resize the background picture.
I tried with css3 masks, but this works only on chrome
-webkit-clip-path: circle(100px at 150px 150px);
-moz-clip-path: circle(50%, 50%, 30%);
clip-path: circle(50%, 50%, 30%);
Any clue ?
The fiddle is: http://jsfiddle.net/nnr64y3b/
The goal is to create a rounded circle where I can move the position of the inside image and the dimensions of the of inside image
You can use:
background-size: x% x%;
https://developer.mozilla.org/en-US/docs/Web/CSS/background-size
Your fiddle: http://jsfiddle.net/nnr64y3b/2/

CSS triangle containing text

I have a weird problem at hand and I am struggling to find a solution.
I have created a triangle <div> "container" using only CSS but what I would like now is to insert some text inside the container.
The solution I am aiming for has to contain the text within the boundaries of the triangle no matter how much text is inserted as I am looking to create thumbnails.
An example can be found here [note; this example is very basic and only shows the way I have chosen to create the triangle]
Pushing it a little further, I want to create one triangle facing up and one facing down and the text has to be at the base of each one, so for the 1st triangle the text will be at the bottom and for the 2nd at the top, plan B is just to center the text within the triangle both vertically and horizontally.
CSS:
.up {
text-align:right;
width: 0px;
height: 0px;
border-style: inset;
border-width: 0 100px 173.2px 100px;
border-color: transparent transparent #007bff transparent;
float: left;
transform:rotate(360deg);
-ms-transform:rotate(360deg);
-moz-transform:rotate(360deg);
-webkit-transform:rotate(360deg);
-o-transform:rotate(360deg);
}
HTML:
<div class="up">
<p>some information text goes here<p>
</div>
For your plan B (to center the text within the triangle both vertically and horizontally), which I prefer as solution, you could add this css rule:
.up p {
text-align: center;
top: 80px;
left: -47px;
position: relative;
width: 93px;
height: 93px;
margin: 0px;
}
Try it here:
.up {
width: 0px;
height: 0px;
border-style: inset;
border-width: 0 100px 173.2px 100px;
border-color: transparent transparent #007bff transparent;
float: left;
transform: rotate(360deg);
-ms-transform: rotate(360deg);
-moz-transform: rotate(360deg);
-webkit-transform: rotate(360deg);
-o-transform: rotate(360deg);
}
.up p {
text-align: center;
top: 80px;
left: -47px;
position: relative;
width: 93px;
height: 93px;
margin: 0px;
}
<div class="up">
<p>some information text goes here
<p>
</div>
View on JSFiddle
How can you fit text inside the triangle, no matter how much text there is? As far as I know, it is not possible with CSS alone. The text that can't fit in will overflow, and you'd need to use Javascript to adjust the font size accordingly to fit all of them.
But suppose that you want a reasonable amount of text to fit inside a right triangle (base is on the left, pointing to the right), here is an approach:
create a container with fixed width and height to hold the text, and the shapes.
inside the container, create two divs floated to the right. Each has width 100% and height 50%, shape-outline and clip-path as polygon.
give these divs background color similar to the background of the rendered page.
The idea is that the part outside these two divs will take the shape of a triangle we are looking for.
In CSS, elements are rectangles, where you realize it or not. It's not about drawing a triangle. It's about creating neighboring elements that suggest a triangle. Hope that makes sense.
.main {
width: 400px;
height: 200px;
position: relative;
background: peachpuff;
}
.top, .bottom {
width: 100%;
height: 50%;
background: white;
}
.top {
-webkit-shape-outside: polygon(0% 0, 100% 0%, 100% 100%);
shape-outside: polygon(0% 0, 100% 0%, 100% 100%);
float: right;
-webkit-clip-path: polygon(0% 0, 100% 0%, 100% 100%);
clip-path: polygon(0% 0, 100% 0%, 100% 100%);
}
.bottom {
height: 50%;
float: right;
bottom: 0;
clip-path: polygon(0% 100%, 100% 100%, 100% 0%);
shape-outside: polygon(0% 100%, 100% 100%, 100% 0%);
}
<div class="main">
<div class="top"></div>
<div class="bottom"></div>
<p>
When should one use CSS versus SVG? Use CSS for simple shapes. HTML elements are rectangles, so all you are doing is creating an illusion of shapes. Sometimes this can become a deep rabbit hole. Instead, use SVG for complex shapes.
</p>
</div>