I'm doing a tool to help in a game. The original game looks like this:
I was able to do the elixir number on the purple drop on the top-left. The transparent label to show the Level. (still need to work with the right colors based in card rarity).
But cant make the label center on the bottom and if possible looks like inside the border instead of above the whole picture.
I was trying following this question CSS center text (horizontally and vertically) inside a div block but haven't be able to make it work.
.elixir {
position: absolute;
top: 5px;
left: 15px;
color: white;
font-weight: bold;
font-size: 50px;
}
.level {
color: white;
font: bold 24px/45px Helvetica, Sans-Serif;
letter-spacing: -1px;
background: rgb(0, 0, 0);
/* fallback color */
background: rgba(0, 0, 0, 0.7);
padding: 10px;
text-align: center;
}
#header {
position: relative;
background: yellow;
width: 200px;
}
#header-content {
position: absolute;
bottom: 0;
color: Violet ;
font: bold 24px/45px Helvetica, Sans-Serif;
letter-spacing: -1px;
background: rgb(0, 0, 0);
/* fallback color */
background: rgba(0, 0, 0, 0.7);
padding: 10px;
/* width: 100%; */
line-height: 40px;
}
<div id="header">
<img src="https://vignette.wikia.nocookie.net/clashroyale/images/4/46/DarkPrinceCard.png/revision/latest?cb=20160702201038" />
<span class="elixir"> 4 </span>
<span id="header-content"> Level 2 </span>
</div>
There are a few things you need to take into consideration.
Centralising this element
To address your issue of centralising this element with a couple of methods:
Option 1.
You can make the entire span width 100% and center the text within it by adding this to #header-content:
width: 100%;
display: block;
text-align:center;
As shown here: https://codepen.io/anon/pen/RjmMjQ
Option 2.
You can remove the absolute positioning, use display:inline-block, and, adding text-align:center; on the parent element like so:
https://codepen.io/anon/pen/wPbmrQ
Option 3.
If the element is position:absolute; you can also use:
left: 50%;
transform: translateX(-50%);
Positioning the element relative to an image:
You also said you would like this element to be positioned inside the border. Because this border is set on the image itself, you will need to position your element so it is relative to the image.
To make sure everything lines up correctly with padding we need to set the following, otherwise padding & css borders are not included in any width you set.:
*{
box-sizing: border-box;
}
Then you need to get the exact position right for aligning you element with the image, these seem to work right:
width: 170px;
bottom: 18px;
left:50%;
transform: translateX(-50%);
border-radius: 0 0 12px 12px;
This is done for you here:
https://codepen.io/anon/pen/YEbMYB
First you need to make the image max-width:100% to avoid overflow, then simply adjust left/right/bottom values since your element is absolute position and add text-align:center :
.elixir {
position: absolute;
top: 5px;
left: 15px;
color: white;
font-weight: bold;
font-size: 50px;
}
.level {
color: white;
font: bold 24px/45px Helvetica, Sans-Serif;
letter-spacing: -1px;
background: rgb(0, 0, 0);
/* fallback color */
background: rgba(0, 0, 0, 0.7);
padding: 10px;
text-align: center;
}
#header {
position: relative;
background: yellow;
width: 200px;
}
#header img {
max-width:100%;
}
#header-content {
position: absolute;
bottom: 5px;
left:5px;
right:5px;
text-align:center;
color: Violet ;
font: bold 24px/45px Helvetica, Sans-Serif;
letter-spacing: -1px;
background: rgb(0, 0, 0);
/* fallback color */
background: rgba(0, 0, 0, 0.7);
padding: 10px;
/* width: 100%; */
line-height: 40px;
}
<div id="header">
<img src="https://vignette.wikia.nocookie.net/clashroyale/images/4/46/DarkPrinceCard.png/revision/latest?cb=20160702201038" />
<span class="elixir"> 4 </span>
<span id="header-content"> Level 2 </span>
</div>
By using left:0; right:0; and appropirate margin value, you should be able to center it. You could also set the width of the #header-content and set margin:0 auto;, it would achieve the same effect.
I also fit the image inside parent and added a curve to the label, you can see everything commented in css.
Working snippet:
.elixir {
position: absolute;
top: 5px;
left: 15px;
color: white;
font-weight: bold;
font-size: 50px;
}
.level {
color: white;
font: bold 24px/45px Helvetica, Sans-Serif;
letter-spacing: -1px;
background: rgb(0, 0, 0);
/* fallback color */
background: rgba(0, 0, 0, 0.7);
padding: 10px;
text-align: center;
}
#header {
position: relative;
background: yellow;
width: 200px;
}
#header img {width:100%; /* sets image to fit parent */}
#header-content {
position: absolute;
bottom: 0;
color: Violet ;
font: bold 24px/45px Helvetica, Sans-Serif;
letter-spacing: -1px;
background: rgb(0, 0, 0);
/* fallback color */
background: rgba(0, 0, 0, 0.7);
padding: 10px;
/* width: 100%; */
line-height: 40px;
right:0; left:0; /*center*/
margin:0 18px 24px 18px; /* set margin to appropriate values top right bottom left*/
border-bottom-left-radius:13px; border-bottom-right-radius:13px; /* curved edges */
}
<div id="header">
<img src="https://vignette.wikia.nocookie.net/clashroyale/images/4/46/DarkPrinceCard.png/revision/latest?cb=20160702201038" />
<span class="elixir"> 4 </span>
<span id="header-content"> Level 2 </span>
</div>
Not too sure, but looks like you are just missing coordonates and text-align.
update example for #header-content:
bottom: 30px;
left:24px;
right:24px;
text-align:center;
and eventually
border-radius:0 0 10px 10px;
text-shadow:1px 1px white;
Demo below:
.elixir {
position: absolute;
top: 5px;
left: 15px;
color: white;
font-weight: bold;
font-size: 50px;
}
.level {
color: white;
font: bold 24px/45px Helvetica, Sans-Serif;
letter-spacing: -1px;
background: rgb(0, 0, 0);
/* fallback color */
background: rgba(218, 21, 225);
padding: 10px;
text-align: center;
}
#header {
position: relative;
background: yellow;
width: 200px;
}
#header-content {
position: absolute;
bottom: 30px;
left:24px;
right:24px;
text-align:center;
border-radius:0 0 10px 10px;
text-shadow:2px 2px black, -1px -1px black;;
color: Violet ;
font: bold 24px/45px Helvetica, Sans-Serif;
letter-spacing: -1px;
background: rgb(0, 0, 0);
/* fallback color */
background: rgba(218, 21, 225,0.5);
/* padding:10px;
width: 100%; */
line-height: 40px;
}
<div id="header">
<img src="https://vignette.wikia.nocookie.net/clashroyale/images/4/46/DarkPrinceCard.png/revision/latest?cb=20160702201038" />
<span class="elixir"> 4 </span>
<span id="header-content"> Level 2 </span>
</div>
Related
I just need to style two-tone color background like this.
I tried and successfully did using linear gradient
background: linear-gradient(172deg, #ECB034 50%, #BE883C 50%);
But it has a problem when resizing web page. It just not well aligned to its corners. Showing weirdly. Like this
is there any way to did this?
.cta{
padding: 60px 0;
background: linear-gradient(172deg, #ECB034 50%, #BE883C 50%);
text-align:center;
}
.cta h3{
font-size: 58px;
margin-bottom: 30px;
font-weight: 700;
}
.cta a{
padding: 16px 49px;
border: 2px solid #000;
color: inherit;
font-size: 20px;
text-transform: uppercase;
display: inline-block;
font-weight: 500;
}
<div class="cta text-center">
<div class="container">
<h3>Let’s talk about your project.</h3>
Get Started >
</div>
</div>
This is a very simple fix with an SVG (as I mentioned in the comments) you simply need to add preserveAspectRatio="none" to the SVG tag and run it through a URL encoder. This one will even generate the CSS for you which is quite nice.
.cta{
padding: 60px 0;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' height='100%25' width='100%25' preserveAspectRatio='none' viewBox='0 0 10 10'%3E%3Crect width='10' height='10' fill='%23ECB034' /%3E%3Cpath d='m0 10 h10 v-10' fill='%23BE883C' /%3E%3C/svg%3E");
text-align:center;
}
.cta h3{
font-size: 58px;
margin-bottom: 30px;
font-weight: 700;
}
.cta a{
padding: 16px 49px;
border: 2px solid #000;
color: inherit;
font-size: 20px;
text-transform: uppercase;
display: inline-block;
font-weight: 500;
}
<div class="cta text-center">
<div class="container">
<h3>Let’s talk about your project.</h3>
Get Started >
</div>
</div>
You don't need anything complex. Simply change the angle with to bottom right
.cta{
padding: 60px 0;
background: linear-gradient(to bottom right, #ECB034 50%, #BE883C 50%);
text-align:center;
}
.cta h3{
font-size: 58px;
margin-bottom: 30px;
font-weight: 700;
}
.cta a{
padding: 16px 49px;
border: 2px solid #000;
color: inherit;
font-size: 20px;
text-transform: uppercase;
display: inline-block;
font-weight: 500;
}
<div class="cta text-center">
<div class="container">
<h3>Let’s talk about your project.</h3>
Get Started >
</div>
</div>
You can use pseudo and clip-path combination.
.box{
position:relative;
background-color: #ecae20;
text-align:center;
margin: 30px;
padding: 30px;
z-index:1;
}
.box .content{
position:relative;
z-index:3;
}
.box:before {
content: "";
position:absolute;
left:0;
bottom:0;
width: 100%;
height: 100%;
background-color: #BE883C;
clip-path: polygon(0 99%, 100% 0, 100% 99%, 0 100%);
opacity: 0.7;
z-index:2;
}
<div class="box">
<div class="content">
<p> Let's talk about your project </p>
<button> GET STARTED > </button>
</div>
</div>
As the angle needed changes with the aspect ratio it is not possible to do this with linear-gradient without some recalculation on every resize. (This is incorrect, see better suggestion from #temaniAfif using to bottom left etc.)
However, it is possible to create a triangle with its hypoteneuse being a diagonal by using clip-path and a polygon.
There is no need to inline an SVG if you put the two colors as backgrounds to the before and after pseudo elements, the after also having a clip-path.
.cta {
padding: 60px 0;
text-align: center;
position: relative;
}
.cta::before,
.cta::after {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
}
.cta::before {
background-color: #ECB034;
}
.cta::after {
background-color: #BE883C;
clip-path: polygon(0 100%, 100% 0, 100% 100%);
}
.cta h3 {
font-size: 58px;
margin-bottom: 30px;
font-weight: 700;
}
.cta a {
padding: 16px 49px;
border: 2px solid #000;
color: inherit;
font-size: 20px;
text-transform: uppercase;
display: inline-block;
font-weight: 500;
}
<div class="cta text-center">
<div class="container">
<h3>Let’s talk about your project.</h3>
Get Started >
</div>
</div>
You can use pseudo element for the same.
#import "https://cdn.jsdelivr.net/gh/KunalTanwar/normalize/css/normalize.inter.min.css";
body {
height: 100%;
display: grid;
place-items: center;
}
.container {
--container-width: 400px;
--container-height: 200px;
width: 100%;
background-color: #ecb034;
height: var(--container-height);
max-width: var(--container-width);
}
.container::after {
content: "";
position: absolute;
width: 0;
height: 0;
user-select: none;
pointer-events: none;
inset: auto 0 0 auto;
border-bottom: var(--container-height) solid #be883c;
border-left: var(--container-width) solid transparent;
}
<div class="container"></div>
Make sure to keep border-left-width and width of the container same. And same for height.
I'm trying to stack the on top of each other vertically but they are overlapping for some reason and not sure if it has to do with the positioning. I'm not very good at CSS. Also, is there an easier way to align the datetime span to the right side instead of using padding-left: 1140px? Thank you
<div class="content-box">
<span class="name">John Doe</span>
<span class="datetime">May 2022</span><br><br>
<span class="content">Lorem ipsum</span>
</div>
<div class="content-box">
<span class="name">Jane Doe</span>
<span class="datetime">June 2022</span><br><br>
<span class="content">Lorem ipsum</span>
</div>
.content-box {
justify-content: center;
top: 600px;
position: absolute;
width: 75%;
border: none;
outline: none;
font-family: 'Roboto Mono', monospace;
font-size: 18px;
transform: translate(0);
background-image: linear-gradient(45deg, #4568DC, #B06AB3);
padding: 20px 40px;
border-radius: 5px;
box-shadow: 0 22px 44px rgba(128, 128, 128, 0.1);
transition: box-shadow .25s;
padding: 60px;
}
.content-box .name {
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-image: linear-gradient(45deg, #4568DC, #B06AB3);
font-size: 30px;
}
.content-box .content {
color: #4568DC;
font-size: 20px;
}
.content-box .datetime {
padding-left: 1140px;
color: black;
font-size: 18px;
}
.content-box .content {
color: #4568DC;
font-size: 20px;
}
.content-box:after {
content: '';
border-radius: 4px;
position: absolute;
margin: 1px;
top: 0;
left: 0;
bottom: 0;
right: 0;
z-index: -1;
background: white;
}
.content-box:hover {
background-image: linear-gradient(-45deg, #00FFFF, #ff1a1a);
box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
transition: .5s;
}
.content-box:hover .name {
background-image: linear-gradient(-45deg, #4568DC, #B06AB3);
}
The problem here is that both div has content-box class; this class applies position: absolute; , which is the main cause of overlapping. Removing this css property you should be able to se both div.
Regarding date time span you could change the class .content-box .datetime { color: black; font-size: 18px; } (you have to remove the padding)
And add the following properties to .content-box class to display datetime correctly without padding:
display: flex;
flex-flow: column;
There is a button with a border: 3px solid #E82929; what technology can be used to add additional lines like in the photo?
.btn {
position: relative;
width: 362px;
height: 71px;
color: #FFFFFF;
background-color: #000000;
border: 3px solid #E82929;
font-family: 'Flamenco';
font-style: normal;
font-weight: 400;
font-size: 24px;
line-height: 25px;
text-align: center;
}
<button class="btn">Забронировать столик</button>
Use gradient
.btn {
position: relative;
padding: 20px 50px;
color: #FFFFFF;
border: 3px solid #E82929;
font-family: 'Flamenco';
font-style: normal;
font-weight: 400;
font-size: 24px;
line-height: 25px;
text-align: center;
background: linear-gradient(90deg, #E82929 40px,#0000 0 calc(100% - 40px), #E82929 0) 50%/100% 3px no-repeat;
background-color: #000000;
}
<button class="btn">Забронировать столик</button>
You do not need extra markup, because it can be done with the ::before and ::after pseudo elements.
Assuming your 2 lines at the left and right should have a width of 30px and a left and right padding of 10px, you could add this to your already existing CSS:
.btn {
position: relative;
width: 362px;
height: 71px;
color: #FFFFFF;
background-color: #000000;
border: 3px solid #E82929;
font-family: 'Flamenco';
font-style: normal;
font-weight: 400;
font-size: 24px;
line-height: 25px;
text-align: center;
}
/* extra code comes here */
.btn {
padding: 0 40px; /* 30px line width + 10px padding */
}
.btn::before,
.btn::after {
background-color: #E82929; /* border color */
content: ''; /* content is mandatory for the element to show up */
height: 3px; /* 3px border width */
position: absolute;
top: 50%; /* 50% from the top */
transform: translateY(-50%); /* half of its height back to the top */
width: 30px; /* 30px line width */
}
.btn::before {
left: 0;
}
.btn::after {
right: 0;
}
<button class="btn">Забронировать столик</button>
Change the width and padding according to your needs.
What it does: It adds the ::before and ::after pseudo elements with no text content on the left and right and positions them vertically centered.
If you have any questions regarding details, feel free to ask.
I have a sticky top navbar that I want to stay visible and above all other content when scrolling. I have content on the page that I have set to position: relative so that I can position other elements around it. When I do this, the relative content ignores the navbar when scrolling and overlaps it. Is there any way for my to have my page content positioned relative and still have it observe the sticky navbar?
I've tried giving the relative content a top margin equal to the height of the navbar.
.nav-bar {
position: sticky;
top: 0px;
font-family: Arial, Helvetica, sans-serif;
border-bottom: solid rgb(179, 173, 173) 1px;
background-color: rgb(255, 255, 255);
}
.nav-bar #title {
margin: 0;
font-size: 2em;
padding-left: 2%;
}
.test-class #test-content {
width: 500px;
height: 500px;
background-color: rgb(70, 67, 67);
position: absolute;
}
<div class="nav-bar">
<p id="title">Title</p>
</div>
<div class="test-class">
<p id="test-content"></p>
</div>
Expected: sticky header stays above all other content.
Actual: Content overlaps header when its position is set to relative.
If you want your navbar stay always visible just give it a z-index bigger than your content
.nav-bar{
position:sticky;
top:0px;
font-family: Arial, Helvetica, sans-serif;
border-bottom:solid rgb(179, 173, 173) 1px;
background-color: rgb(255, 255, 255);
z-index: 1
}
Try this. Remove position:absolute from .test-class #test-content class. It works fine as you want.
.nav-bar{
position:sticky;
top:0px;
font-family: Arial, Helvetica, sans-serif;
border-bottom:solid rgb(179, 173, 173) 1px;
background-color: rgb(255, 255, 255);
}
.nav-bar #title{
margin:0;
font-size: 2em;
padding-left: 2%;
}
.test-class #test-content {
width:500px;
height:500px;
background-color:rgb(70, 67, 67);
}
<body>
<div class="nav-bar">
<p id="title">Title</p>
</div>
<div class="test-class">
<p id="test-content"></p>
</div>
</body>
you can use this code
body {
margin: 0;
padding: 0;
}
.nav-bar {
overflow: hidden;
background-color: #333333;
position: sticky;
top: 0;
width: 100%;
}
.nav-bar #title {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 20px;
text-decoration: none;
font-size: 25px;
margin: 0;
}
.test-class {
padding: 16px;
margin-top: 0px;
height: 1500px;
}
.test-class #test-content {
width: 500px;
height: 500px;
background-color: rgb(70, 67, 67);
margin: 0;
}
I am working currently on my website and wanted to ask best solution for this situation.
Biggest struggle is that I need to put gradient over img + text and arrow icon.
I tried 2 different ways:
I was able to put gradient over image using :after, but now I don't know how to put text over image and that left arrow to the right of image.
I used <figure> and <figcaption> I think this solution isn't the best, using negative margins to put text over image. But on this case I am not able to use :after solution.
I included photo — there you can see how it should look like. Also I am starting my coding career and if you notice bad practises please let me know!
figure {
display: inline-block;
margin: 15px 12px;
box-shadow: 0 20px 30px rgba(0, 0, 0, 0.2);
}
figcaption {
margin-top: -80px;
}
figcaption p {
margin-top: 0px;
margin-bottom: 0px;
padding-left: 44px;
text-align: left;
color: #ffffff;
font-family: "Proxima Nova";
font-size: 25px;
font-weight: 700;
line-height: 35px;
}
.cs-text {
margin-top: -10px;
color: #b9b8b8;
font-family: "Proxima Nova";
font-size: 12px;
font-weight: 700;
letter-spacing: 1px;
text-transform: uppercase;
}
figcaption img {
position: relative;
float: right;
padding: 10px 35px 50px 0px;
}
.cs-item {
position: relative;
display: inline-block;
max-width: 430px;
max-height: 254px;
}
.cs-item:after {
content: '';
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: linear-gradient(0deg, rgba(0, 0, 0, 0.5) 0%, rgba(0, 0, 0, 0.11) 36%, rgba(0, 0, 0, 0) 46%);
}
<div class="case-study-items">
<div class="cs-item">
<img src="https://www.upload.ee/image/7272952/case-studies-item.png" alt="">
<p class="solgu">Melb Lashes</p><img src="https://www.upload.ee/image/7272954/left-arrow.png" /></p>
<p class="cs-text">Case-study</p>
</div>
<figure>
<img src="https://www.upload.ee/image/7272952/case-studies-item.png" alt="">
<figcaption>
<p>Melb Lashes<img src="https://www.upload.ee/image/7272954/left-arrow.png" /></p>
<p class="cs-text">Case-study</p>
</figcaption>
</div>
Welcome to StackOverflow. Be sure to search the site for answers, prior to asking a question. The question is actually two questions, and has been answered in other posts.
For gradients over images, see here.
By structuring the HTML properly, and simplifying the CSS, the texts and arrow image can float over the image and gradient, by being nested within the parent element that has the image and the gradient.
<div class="case-study-items">
<div class="cs-item"> <!-- image and gradient here -->
<p class="cs-title">Melb Lashes <span class="cs-left-arrow"></span></p>
<p class="cs-text">Case-study</p>
</div>
</div>
I've created a Codepen example to demonstrate.
You can try to assign cs-item class to Figure, and set figcaption with z-index:1 and position: relative
figure {
display: inline-block;
margin: 15px 12px;
box-shadow: 0 20px 30px rgba(0, 0, 0, 0.2);
}
figcaption {
margin-top: -80px;
position:relative;
z-index:2;
}
figcaption p {
margin-top: 0px;
margin-bottom: 0px;
padding-left: 44px;
text-align: left;
color: #ffffff;
font-family: "Proxima Nova";
font-size: 25px;
font-weight: 700;
line-height: 35px;
}
.cs-text {
margin-top: -10px;
color: #b9b8b8;
font-family: "Proxima Nova";
font-size: 12px;
font-weight: 700;
letter-spacing: 1px;
text-transform: uppercase;
}
figcaption img {
position: relative;
float: right;
padding: 10px 35px 50px 0px;
}
.cs-item {
position: relative;
display: inline-block;
max-width: 430px;
max-height: 254px;
}
.cs-item:after {
content: '';
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: linear-gradient(0deg, rgba(0, 0, 0, 0.5) 0%, rgba(0, 0, 0, 0.11) 36%, rgba(0, 0, 0, 0) 46%);
}
<figure class="cs-item">
<img src="https://www.upload.ee/image/7272952/case-studies-item.png" alt="">
<figcaption>
<p>Melb Lashes<img src="https://www.upload.ee/image/7272954/left-arrow.png" /></p>
<p class="cs-text">Case-study</p>
</figcaption>
</figure>