I have a parent div that contains a child that is aligned to the bottom right corner of the parent. The child has text within it that I am trying to get to display correctly.
As it is currently set up, the contents of the child has placed the text to the right side instead of within.
CSS:
.container {
background-color: red;
height: 200px;
width: 400px;
position:relative;
}
.gradeTriangle{
width: 0px;
height:0px;
border-bottom: 50px solid #000;
border-left: 50px solid transparent;
bottom: 0;
right: 0;
position: absolute;
color: green
}
HTML:
<div class="container">
<div class="gradeTriangle">
$25
</div>
</div>
Fiddle:
http://jsfiddle.net/vh7m8gey/1/
Output:
I am trying to get the $25 to be centered in the black triangle that is on the bottom right of the child.
How should i approach this?
I created a container for the amount with absolute position right 3px and bottom -45px.
.container {
background-color: red;
height: 200px;
width: 400px;
position:relative;
}
.gradeTriangle{
width: 0px;
height:0px;
border-bottom: 50px solid #000;
border-left: 50px solid transparent;
bottom: 0;
right: 0;
position: absolute;
color: green
}
.amountContainer{
position:absolute;
padding:1%;
bottom:-45px;
right:3px;
}
<div class="container">
<div class="gradeTriangle">
<div class="amountContainer">$25</div>
</div>
</div>
Look at this. You need to insert for example span inside gradeTriangle, and position It using css.
.gradeTriangle span {
position: absolute;
bottom: -40px;
right: 0px;
}
HTML:
<div class="container">
<div class="gradeTriangle">
<span>$25</span>
</div>
</div>
You can easily create the triangle as background of the main container:
.container {
background:
linear-gradient(to bottom right,transparent 49.8%,#000 50%) bottom right/50px 50px no-repeat,
red;
height: 200px;
width: 400px;
position: relative;
}
.gradeTriangle {
bottom: 5px;
right: 5px;
position: absolute;
color: green
}
<div class="container">
<div class="gradeTriangle">
$25
</div>
</div>
Need to add some style to get the design, added the following style for gradeTriangle
.gradeTriangle{
position:absolute;
right:0;
bottom:0;
width: 60px;
height: 60px;
border-bottom: solid 30px black;
border-right: solid 30px black;
box-sizing: border-box;
color:#fff;
border-left: solid 30px transparent;
border-top: solid 30px transparent;
}
please check the sample code.
Related
How to get triangle layout of 'mission' section within the container only without using "polygon". I don't want it to flow outside the container.
You can make triangles using border property of css.
Check out this link for more shapes : https://css-tricks.com/examples/ShapesOfCSS/
.container {
position: relative;
}
.mission {
position: absolute;
width: 0;
height: 0;
border-left: 0px solid transparent;
border-right: 100px solid transparent;
border-top: 150px solid red;
border-bottom: 0px solid transparent;
}
.mission + div {
position: absolute;
}
.content {
position: absolute;
width: 350px;
height: 200px;
background: lightgrey;
padding-left: 100px;
}
<div class="container">
<div class="content">
Content
</div>
<div class="mission">
</div>
<div>
Mission
</div>
</div>
I created a profile dialog body like this:
<div style="background-color: white;
color: black;
border-radius: 2px;
position: absolute;
top: 52px;
right: 10px;
padding: 2px">
<div style="width: 0;
height: 0;
border-style: solid;
border-width: 0 10px 15px 10px;
border-color: transparent transparent #ffffff transparent;
position: relative;
top: -10px;
right: -165px">
</div>
</div>
I want to place child little triangle div always 10-20px far away from right end of parent div. How I can make second little triangle div take its start position on right from the end of parent div?
Just make the parent div's position relative and the child's position absolute
Something like this in your example:
<div style="background-color: black;color: black;border-radius: 2px; position: relative; padding: 2px ; width:1000; height:1000;">
<div style="
position: absolute;
width: 200;
height: 200;
border-style: solid;
border-color: transparent transparent #ffffff transparent;
top: 10px;
right: 16px;
background-color: blue">
</div>
The width of the inner DIV is 20px (left border + right border). Add another 2px for the border of the outer DIV and you have 22px as your zero position for the starting of the triangle.
Therefore, if you want the triangle to start 20px from the right side of the outer DIV, it is 22px + 20px:
right:-42px;
jsFiddle Demo
div{position:relative;}
#one{position:absolute;top:52px;right:200px;padding:2px;background-color:white;color:black;border-radius: 2px; }
#two{width:0;height:0;top:-10px;right:-22px;
border-style:solid;
border-width:0 10px 15px 10px;
border-color: transparent transparent #ffffff transparent;
}
body{background:darkcyan;}
<div id="one"><div id="two"></div></div>
Here is 2 good solutions that will allow the div and its content to flow properly with other page content.
If all you need is that "triangle", use a pseudo element instead
#one {
background-color: #ddd;
color: black;
border-radius: 2px;
position: relative;
padding: 2px;
width:200px;
height:80px;
padding-right: 15px; /* make up for the triangle so inner content does not overlap */
}
#one:after {
content: ' ';
position: absolute;
top: 0;
right: 0;
width: 0;
height: 0;
border-style: solid;
border-width: 0 10px 15px 10px;
border-color: transparent transparent #f00 transparent;
}
<div id="one">
</div>
More content out side the `one` div
This one use float, also have it stay either at top/right or bottom/right.
#one {
background-color: #ddd;
color: black;
border-radius: 2px;
top: 52px;
right: 10px;
padding: 2px;
clear: both;
}
.inner {
height: 30px;
background: green;
}
#two {
float: right;
width: 0;
height: 0;
border-style: solid;
border-width: 0 10px 15px 10px;
border-color: transparent transparent #f00 transparent;
}
<div id="one">
<div id="two"></div>
Now, anything you write here, whether it is text, or another element
<div class="inner"></div>
the "two" div will always stay at the right top<br><br>
</div>
More content out side the `one` div
<br>
<br>
<div id="one">
Now, anything you write here, whether it is text, or another element
<div class="inner"></div>
the "two" div will always stay at the right top<br><br>
This one has it stay at the right bottom
<div id="two"></div>
</div>
More content out side the `one` div
I am trying to display a few words inside of a CSS styled arrow. I have figured out how to create an arrow with CSS which works fine. however, when I place the arrow within <h2>, complete arrow is not being displayed.
The source code is as follows
HTML
<div style="background-color: yellow;">
<h2><span style="background: green;">This is what I want</span><span class="arrow-right"></span><span style="margin-left: 50px;">is this what you want?</span></h2>
</div>
STYLE
<style>
.arrow-right::after{
content: "";
width: 0;
height: 0;
border-top: 15px solid transparent;
border-bottom: 15px solid transparent;
border-left: 15px solid green;
}
</style>
The output is as follows
The arrow pointer is not being displayed completely. Am I using the elements wrongly? I will need the div / h2 height to be bigger later, but at least that is not my concern right now since the arrow itself is not being displayed as desired.
Edit:
Sorry for my bad drawing. This sample below is what I want but of course the arrow would be lots nicer I just used paints to give it a quick draw.
Is this what you're looking for?
http://jsfiddle.net/61tc5em9/2/
HTML
<div id="container">
<div id="arrow">text text text</div>
<div id="content">text text text text</div>
</div>
CSS
#container {
height: 75px;
background-color: black;
position: relative;
white-space: nowrap;
}
#arrow {
width: 30%;
background-color: red;
text-align: center;
font-size: 1.5em;
line-height: 75px;
}
#arrow::after {
content: "";
border-top: 37px solid transparent;
border-bottom: 38px solid transparent;
border-left: 50px solid red;
position: absolute;
left: 30%;
}
#content {
color: yellow;
font-size: 1.5em;
position: absolute;
left: 50%;
top: 25px;
}
Hope this helps. Let me know if you need any changes.
You need font-size:0; for the arrow.
.arrow-right::after {
content: "";
width: 0;
height: 0;
border-top: 30px solid transparent;
border-bottom: 30px solid transparent;
border-left: 30px solid green;
font-size: 0;
position: relative;
top: -8px;
}
span{
display: inline-block;
}
<div style="background-color: yellow;">
<h2><span style="background: green;">This is what I want</span><span class="arrow-right"></span><span style="margin-left: 50px;">is this what you want?</span></h2>
</div>
Recommendations for improving your code and make it more dynamic:
Use :after in the statement element itself (this way you will avoid
the extra code in html and you can position the arrow relative to the element).
Align it to the right using left: 100% (so it is always position to
the right regardless of the width of the arrow).
Use top: 50% and margin-top: -(height/2)px to center it vertically.
Just like this:
.wrapper {
padding: 2px 0;
background: yellow;
}
.statement {
position: relative;
background: green;
}
.statement:after {
content:"";
border-top: 15px solid transparent; /*change the border width to set the desired hieght of the arrow*/
border-bottom: 15px solid transparent;
border-left: 15px solid green; /*change the border width to set the desired width of the arrow*/
position: absolute;
left: 100%;
top: 50%;
margin-top: -15px; /*the element has height= 30px (border-top + border-bottom) to center it -height /2 */
}
h2{
margin: 0;
}
<div class="wrapper">
<h2>
<span class="statement">This is what I want</span>
<span style="margin-left: 50px;">is this what you want?</span>
</h2>
</div>
Note that in this way you have a more semantic code because you don't have dummy element in your html and if you want more statement it will put the arrow behind automatically like this:
.wrapper {
padding: 2px 0;
background: yellow;
margin-bottom: 10px;
}
.statement {
position: relative;
background: green;
}
.statement:after {
content:"";
border-top: 15px solid transparent;
border-bottom: 15px solid transparent;
border-left: 15px solid green;
position: absolute;
left: 100%;
top: 50%;
margin-top: -15px; /*the element has height= 30px (border-top + border-bottom) to center it -height /2 */
}
h2{
margin: 0;
}
<div class="wrapper">
<h2>
<span class="statement">One statement</span>
<span style="margin-left: 50px;">Good</span>
<span class="statement">Two statement</span>
<span style="margin-left: 50px;">Great</span>
</h2>
</div>
<div class="wrapper">
<h2>
<span class="statement">Where is the arrow?</span>
<span style="margin-left: 50px;">Do not worry about it</span>
</h2>
</div>
I've got this shape made:
https://jsfiddle.net/5vue1buj/1/
However, the way I'm doing this is by inserting:
<br /><br /><br /><br />
in between the top and bottom. How do I do this more elegantly?
Remove all inline styling.
DEMO
HTML
<div>
<div id="top">
<div class="triangle-down-right">
<!--empty-->
</div>
<div class="triangle-down-left">
<!--empty-->
</div>
</div>
<div id="bottom">
<div class="triangle-up-right">
<!--empty-->
</div>
<div class="triangle-up-left">
<!--empty-->
</div>
</div>
</div>
Then add this CSS:
#top, #bottom {
float: none;
overflow: hidden;
}
#top {
margin-bottom: 10px;
}
By using css styles margin/padding you can achieve this.
In your case you have to clear the space between the two containers [top and bottom]. By default div elements are left aligned. I have added an empty divwhich will remove the space in between the two container [using clear:both. height and overflow is added for Cross browser compatibility]
please check this Fiddle.
By using minimal of html and css
You can use only two div and two its psuedo elements :after and :before
.bottom {
position:absolute;
width:210px;
top:180px;
}
.upper {
position:absolute;
width:210px;
top:20px;
}
.upper:before {
content:'';
position:absolute;
left:0;
width: 0;
height: 0;
border-bottom: 100px solid #4679BD;
border-left: 100px solid transparent;
}
.upper:after {
content:'';
position:absolute;
right:0;
width: 0;
height: 0;
border-bottom: 100px solid #4679BD;
border-right: 100px solid transparent;
}
.bottom:before {
content:'';
position:absolute;
left:0;
width: 0;
height: 0;
border-top: 100px solid #4679BD;
border-left: 100px solid transparent;
}
.bottom:after {
content:'';
position:absolute;
right:0;
width: 0;
height: 0;
border-top: 100px solid #4679BD;
border-right: 100px solid transparent;
}
<div class="upper"></div>
<div class="bottom"></div>
Here's another way, with much less CSS...
#top, #bottom {
height: 200px;
position: relative;
}
.right, .left {
height: 0;
width: 0;
display: inline-block;
}
#top {
margin-bottom: 10px;
}
.left {
margin-right: 10px;
}
#top .left {
border-top: 200px solid transparent;
border-right: 200px solid #4679bd;
}
#top .right {
border-top: 200px solid transparent;
border-left: 200px solid #4679bd;
}
#bottom .left {
border-bottom : 200px solid transparent;
border-right: 200px solid #4679bd;
}
#bottom .right {
border-bottom: 200px solid transparent;
border-left: 200px solid #4679bd;
}
<div>
<div id="top">
<div class="left"></div><div class="right"></div>
</div>
<div id="bottom">
<div class="left"></div><div class="right"></div>
</div>
</div>
Just for fun, here another example.
It uses pseudo elements and some new css3 properties to minimize the html markup down to only one div. This div is relatively positioned, but could as well be positioned absolutely for easily placing it wherever you like on the page.
A sophisticated jsfiddle can be found here where you can play around if the values easily (using Sass).
#shape{
position:relative;
background:#4679BD;
width:200px;height:200px;
transform:rotate(45deg);
margin-top:50px;margin-left:50px;
overflow:hidden;
}
#shape::before,#shape::after{
content:"";display:block;
position:absolute;
width:300px;height:10px;
background:white;
transform:rotate(45deg);
transform-origin:5px 5px;
left:-5px;top:-5px;
}
#shape::after{
transform:rotate(-45deg);
bottom:-5px;top:auto;right:-5px;
}
<div id="shape"></div>
Size is easily adjustable by adjusting the width of the pseudo elements like: (dim of shape + 5) * 1,414 and the height determines the gap between the triangles.
I am trying to make a simple chatbox with a header in css, I am trying to align the chat to the bottom of the page, and to the right of the page. I have tried using
float: right;
bottom: 0;
position: absolute;
It aligns it to the bottom of the page but not to the right. Here is my full code
CSS
#chatbox {
height: 360px;
width: 320px;
border-left: 1px solid #000;
border-right: 1px solid #000;
border-bottom: 1px solid #000;
float: right;
bottom: 0;
position: absolute;
}
.chatheader {
font-family:'PT Sans';
background: #999;
width: 322px;
height: 36px;
color: #fff;
text-align: center;
padding-top: 15px;
}
HTML
<div class="chatheader">chatboxheader</div>
<div id="chatbox">
</div>
Here is a demo of the code DEMO
This is just a very simple script because I am trying to get it aligned first, later On I will make it look better. Thanks in advance to anyone who can help me!
Since you want both chatheader and chatbox on the bottom right of the page. I have modified code little bit. I have wrapped both of them in a div.
HTML:
<div id="chat-container">
<div class="chatheader">chatboxheader</div>
<div id="chatbox"></div>
</div>
CSS:
#chat-container {
right :0;
bottom: 0;
position: absolute;
}
right:0 will keep the element on extreme right.
Updated fiddle here.
DEMO
HTML
#chatbox {
height: 360px;
width: 320px;
border-left: 1px solid #000;
border-right: 1px solid #000;
border-bottom: 1px solid #000;
}
.chatheader {
font-family:'PT Sans';
background: #999;
width: 322px;
height: 36px;
color: #fff;
text-align: center;
padding-top: 15px;
float:right;
}
.chatMain {
position:absolute;
bottom:0;
right:0;
}