Create angled elements using html and css only - html

How to create the offer div with a background color using html and css only ?
What I'm asking is width and height of the offer div should be same as in picture.
I created this using html and CSS. However part of div is hidden in my solution because offer div has a larger width and height.
Code I have written :
.offer-text-wrapper {
display: inline-block;
position: absolute;
top: -39px;
left: -36px;
color: #FFF;
height: 72px;
width: 75px;
background: #f44336;
transform: rotate(-43deg);
}
.offer-text {
-moz-transform: rotate(-40deg);
-o-transform: rotate(-40deg);
display: inline-block;
margin-left: 21px;
line-height: 124px;
}
<div class="offer-text-wrapper">
<span class="offer-text"> Offer </span>
</div>
Edit :
Because width and height of offer-text-wrapper is larger, I'm having below issue when I added that to the bootstrap grid.

Have a look at the code snippet, it's basically just changing dimensions and position until you're happy with it.
Is this what you mean?
.offer-container { /* added container to show effect better */
width: 200px;
height: 100px;
overflow: hidden;
position: relative;
border: 1px solid #ccc;
border-radius: 12px;
margin: 40px;
}
.offer-text-wrapper {
display: inline-block;
position: absolute;
top: -29px;
left: -40px;
color: #FFF;
height: 72px;
width: 95px;
background: #f44336;
transform: rotate(-45deg);
text-align: center;
}
.offer-text {
display: inline-block;
line-height: 114px;
}
<div class="offer-container">
<div class="offer-text-wrapper">
<span class="offer-text">Offer</span>
</div>
</div>

Related

Create Button with toggle and text with a single element

I'm trying to create a button out of a single html element. The button needs to have a toggle slider and the text needs to be aligned vertically and horizontally. So I thought I can make use of :before element to help me make that happen. Here is what I have tried:
div {
width: 140px;
height: 40px;
background-color: #B3B3B3;
color: #FFF;
float: left;
clear: both;
border-radius: 5px;
font-size: 12px;
}
div:before {
content: '';
display: block;
width: 20px;
height: 36px;
background-color: #4D4D4D;
position: relative;
left: 2px;
top: 2px;
float: left;
border-radius: 5px;
}
<div>Text Value</div>
I have 2 problems with the above code:
I can't position the text how I want and I have tried using text-align and position to move it around.
I am using a float, which means that it will affect behavior of other elements around it, and I really don't want that.
Is what I want possible with a single element?
Here is the JS Fiddle: https://jsfiddle.net/m3q5Lcjy/
EDIT: The centered text should not be centered on the whole element, but on the light gray area.
This is how I would do this:
Array.from(document.querySelectorAll('.toggler')).forEach((item) => {
item.addEventListener('click', e => {
item.classList.toggle('active');
})
});
.toggler {
display: flex;
align-items: center;
justify-content: center;
position: relative;
box-sizing: border-box;
padding-left: 24px;
width: 140px;
min-height: 40px;
background-color: #B3B3B3;
color: #FFF;
border-radius: 5px;
font-size: 12px;
text-align: center;
cursor: pointer;
transition: padding .25s ease;
}
.toggler.active {
padding: 0 24px 0 0;
}
.toggler:before {
content: '';
display: block;
width: 20px;
background-color: #4D4D4D;
position: absolute;
bottom: 2px;
left: 2px;
top: 2px;
border-radius: 5px;
/* transition to make it look smoother */
transition: left .4s ease;
z-index: 1;
}
.toggler.active:before {
left: calc(100% - 22px);
}
<div class="toggler">Text Value</div>
<hr />
<div class="toggler active">Text Value realllllyy long</div>
<hr />
<div class="toggler">Text Value really far too long for this tiny, tiny, ohhh so tiny button. I recommend using shorter text though, but it won't break not even if you have like 10 or more lines.</div>
If anything about this implementation is unclear, feel free to ask.
Use flexbox to center your text vertically and horizontally. Then use absolute positioning on your pseudo element. Make sure parent element has relative positioning applied so absolute positioned pseudo stays within the parent.
div {
display: flex;
align-items: center;
justify-content: center;
position: relative;
box-sizing: border-box;
padding-left: 24px; /* 20px for :before width, 4px for :before offset */
width: 140px;
height: 40px;
background-color: #B3B3B3;
color: #FFF;
border-radius: 5px;
font-size: 12px;
}
div:before {
content: '';
display: block;
width: 20px;
height: 36px;
background-color: #4D4D4D;
position: absolute;
left: 2px;
top: 2px;
border-radius: 5px;
}
<div>Text Value</div>
You could place the text in a paragraph.
<div class="thediv">
<p class="theText">
enter text here
</p>
</div>
.thediv{
Your own style.
}
.theText{
position: absolute;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
}
I don't see why you would want it be in one element.
If you do want that, you should give the div a padding.
div{
background-color: #B3B3B3;
color: #FFF;
float: left;
border-radius: 5px;
font-size: 12px;
padding: 20px 70px;
}

Why doesn't the css `rotateY` property work on safari

Below is my html and css code. It is pretty simple. I want to show a square \F1FC on a red react.
.parent::before {
position: absolute;
content: '';
width: 40px;
height: 40px;
background-color: red;
border-radius: 10px 0px 0px 10px;
transform: perspective(5px) rotateY(-2deg);
z-index: -1;
}
p::before {
content: "\F1FC";
color: black;
margin-left: 15px;
}
<div class="parent">
<p></p>
</div>
This code works fine on chrome but doesn't work on safari. Half of the square is hidden by the p::before. Below is a screen shot on safari:
Below is the screenshot for chrome:
This is some kind of weird bug in Safari and pseudo elements. My solution for this issue was to counter the transform: rotateY(-2deg); on the parent pseudo element since it was causing the issue.
Add display: block; and transform: rotateY(1deg); to p::before. The small rotation doesn't seem to affect how the square looks and it fixes it for me in Safari.
.parent::before {
position: absolute;
content: '';
width: 40px;
height: 40px;
background-color: red;
border-radius: 10px 0px 0px 10px;
transform: perspective(5px) rotateY(-2deg);
z-index: -1;
}
p::before {
content: "\F1FC";
color: black;
display: block;
margin-left: 15px;
transform: rotateY(1deg);
}
<div class="parent">
<p></p>
</div>

How to resize div based on text within div

I have different div's which looks like this:
<div class="marker marker-availability" style="left: 975.516px; top: 346.265px;">
<span class="marker-label">Tenten comfort</span>
<div style="background-color:#7ba1bc" class="cluster-background">
<span class="marker-id">81</span>
</div>
</div>
<div class="marker marker-availability">
<span class="marker-label">Standaard kampeerplaatsen</span>
<div style="background-color:#d99200" class="cluster-background">
<span class="marker-id">81</span>
</div>
</div>
But now I have an issue because I set an :after with an image to the bottom of the image which looks like this:
Now you see the issue very clear, I tried to set the height to auto and set an min-height but this will not solve the problem.
I have recreated a jsfiddle: jsfiddle
Here is my less code:
&.marker-availability {
display: block;
width: 120px;
height: 23px;
color: #fff;
background-color: #6f6926;
border: 2px solid #fff;
border-radius: 2px;
margin-left: -60px;
margin-top: -26px;
.marker-label {
margin-top: 1px;
margin-left: 1px;
font-size: 12px;
font-weight: 500;
color: #fff;
}
.cluster-background {
.square(25px);
background-color: black;
color: #fff;
margin-top: -30px;
margin-left: -12px;
border-radius: 50%;
&:after {
.retina-image('/img/map/clustermarker-point.png', '/img/map/clustermarker-pointx2.png', 184px, 55px);
.pos-b-l(-26px, 50%);
.translate(-50%, -50%);
content: "";
display: block;
width: 120px;
height: 20px;
background-repeat: no-repeat;
}
}
.marker-id {
padding-top: 1px;
padding-left: 1px;
font-size: 15px;
}
}
Thereby, my question is it possible to make it look like this:
Or is it not possible because of the position of the :after image
The problem was primarily your negative margins which should be avoided if possible.
I've updated your example, you just need to adjust the paddings:
https://jsfiddle.net/txsv0ha5/
removed:
margin-top: -30px;
margin-left: -12px;
Also your bottom background shouldn't be an :after Element of your colored circles but rather of the whole marker itself.
You have some trouble with your css.
The main problem is the negative margin. If you do so, all the height of the parent is reduce. So, you need to add position:absolute.
Change the :after element to the parent so it will relative to the parent and not connected to the cluster-background.
.marker-availability {
display: block;
width: 120px;
min-height: 23px;
color: #fff;
background-color: #6f6926;
border: 2px solid #fff;
border-radius: 2px;
margin-top: 26px;
position:absolute;
}
.marker-availability .marker-label {
margin-top: 1px;
margin-left: 1px;
font-size: 12px;
font-weight: 500;
color: #fff;
}
.marker-availability .cluster-background {
width: 25px;
height: 25px;
background-color: black;
color: #fff;
margin-top: -50px;
margin-left: -12px;
border-radius: 50%;
position:absolute;
}
.marker-availability:after {
background: url('http://i65.tinypic.com/bhytdd.png');
position: absolute;
bottom: -26px;
left: 50%;
transform: translate(-50%, -50%);
content: "";
display: block;
width: 120px;
height: 20px;
background-repeat: no-repeat;
}
.marker-availability .marker-id {
padding-top: 1px;
padding-left: 1px;
font-size: 15px;
}
<body style="background-color: black">
<div class="marker marker-availability" style="left: 975.516px; top: 346.265px;"><span class="marker-label">Tenten comfort</span><div style="background-color:#7ba1bc" class="cluster-background"><span class="marker-id">81</span></div></div>
<div class="marker marker-availability"><span class="marker-label">Standaard kampeerplaatsen</span><div style="background-color:#d99200" class="cluster-background"><span class="marker-id">81</span></div></div>
</body>

How to position an absolute label, underneath a button and centered in the div

I'm trying to achieve this layout:
I have this:
<div class="button-wrapper">
<button>
<i></i>
</button>
<label>Carregamento Telemóvel</label>
</div>
My label has to be absolute positioned because it can't take space in the div. The div has to be the width of the button.
Any ideas?
EDIT: Sorry guys, my question was done in a little bit of hurry, so it was incomplete. I will now explain detailed.
I need all the div's to be responsive and don't have a fixed width.
The button itself it's contained in a row with others button's with the same layout.
The button's have to have the same margin between them, that's why I can't have the label with with because the length of the text it's never the same so the div's will have different sizes, and then I can't give the same margin between them.
Solution:
http://plnkr.co/edit/NYOb3uQnYrNFkfO6RaIG?p=preview
The plunker just works in -webkit- but it's all I need.
The trick to centering the label is a CSS3 transform
.button-wrapper label {
position: absolute;
top:100%;
left: 50%;
transform:translateX(-50%);
color: orange;
font-weight: bold;
}
body {
text-align: center;
}
.button-wrapper {
display: inline-block;
margin: 1em;
position: relative;
}
.button-wrapper i {
font-size: 72px;
background: orange;
line-height: 1.4em;
width: 1.4em;
border-radius:50%;
display: block;
}
.button-wrapper label {
position: absolute;
top:100%;
left: 50%;
transform:translateX(-50%);
color: orange;
font-weight: bold;
}
<div class="button-wrapper">
<i>K</i>
<label>Carregamento Telemóvel</label>
</div>
.button-wrapper {
display: inline-block;
max-width: 80px;
text-align: center;
}
.button-wrapper > button {
border-radius: 50%;
height: 40px; width: 40px;
background-color: orange;
position: relative;
border: 0 none;
display: inline-block;
}
.button-wrapper > button > i {
display: block;
height: 10px;
width: 10px;
background-color: #ffffff;
margin-top: -5px;
margin-left: -5px;
left: 50%;
top: 50%;
position: absolute;
}
.button-wrapper > label {
display: block;
font-size: 11px;
color: orange;
}
<div class="button-wrapper">
<button>
<i></i>
</button>
<label>Carregamento Telemóvel</label>
</div>
Without seeing your code, it's difficult to help you. But you can center an absolute positioned block element by combining margin:0 auto; left:0; right:0;
JSFiddle - http://jsfiddle.net/vfs3n68e/
Here's a very simple way to do it that I've created quickly: https://jsfiddle.net/rn8ysg5q/
HTML:
<div class="button-wrapper">
<button>
<i></i>
</button>
<br />
<label>Carregamento Telemóvel</label>
</div>
CSS:
.button-wrapper
{
/*border: 1px dotted red;*/
color: orange;
width: 100px;
position: relative;
text-align: center;
}
/* Reset button layout */
.button-wrapper button
{
background: none;
border: 0;
color: inherit;
/* cursor: default; */
font: inherit;
line-height: normal;
overflow: visible;
padding: 0;
-webkit-appearance: button; /* for input */
-webkit-user-select: none; /* for button */
-moz-user-select: none;
-ms-user-select: none;
}
.button-wrapper button
{
width: 60px;
height: 60px;
background-color: orange;
border-radius: 50%;
}
If that's what you're looking for I'll update it once I'm home in an hour and make some improvements.

CSS transform porperty Safari

I am trying to get following output (a large slash line between two numbers):
Following code works on Firefox and Chrome but doesn't work on Safari. Would there be any workaround for that?
Here's the code:
HTML:
<div class="wrap">
<div class="top">4</div>
<div class="bottom">15</div>
</div>
CSS:
.top {
display: block;
float: left;
font-size: 60px;
font-weight: 700;
}
.bottom {
display: block;
float: left;
font-size: 38px;
font-weight: 700;
margin-top: 70px;
position: relative;
width: 28px;
}
.bottom:before {
border-left: 1px solid;
content: "";
height: 66px;
position: absolute;
right: 0;
top: -35px;
transform: skew(-45deg);
transform-origin: left top 0;
width: 0;
}
JSFiddle Demo:
http://jsfiddle.net/pg4sxrc1/
Certain versions of Safari still require the use of the -webkit- prefix for transform and transform-origin, try adding the following definitions to your .bottom:before CSS:
-webkit-transform: skew(-45deg);
-webkit-transform-origin: left top 0;
jsFiddle Demo