Add line on right and left of text [duplicate] - html

This question already has answers here:
CSS technique for a horizontal line with words in the middle
(34 answers)
Closed 3 years ago.
I have the following HTML and CSS:
body {
text-align: center;
}
div {
border: 1px solid black;
margin: 0 auto;
width: 200px;
}
p {
border: 1px solid red;
line-height: 0.5;
margin: 20px;
text-align: center;
}
span {
display: inline-block;
position: relative;
}
span:before,
span:after {
content: "";
position: absolute;
height: 5px;
border-bottom: 1px solid black;
top: 0;
width: 100%;
}
span:before {
right: 100%;
margin-right: 20px;
}
span:after {
left: 100%;
margin-left: 20px;
}
<div>
<p class="strike"><span>Phrase</span></p>
</div>
I added a line on left and right of text but with 2 problems:
The line gets outside of the P border;
The P does not fill the entire width off the container DIV.
How can I solve these problems?

I've left your original CSS in but commented much of it out. FlexBox is a good way to achieve what you want (as opposed to position: absolute and position: relative:
/*body {
text-align: center;
}*/
div {
border: 1px solid black;
margin: 0 auto;
width: 200px;
}
p {
border: 1px solid red;
/*line-height: 0.5;*/
/*margin: 20px;*/
/*text-align: center;*/
}
span {
display: flex;
/*position: relative;*/
/*width: 100%;*/
align-items: center;
}
span:before,
span:after {
content: "";
/*position: absolute;*/
/*height: 5px;*/
border-bottom: 1px solid black;
/*top: 0;*/
width: 100%;
}
span:before {
/*right: 100%;*/
margin-right: 20px;
}
span:after {
/*left: 100%;*/
margin-left: 20px;
}
<div>
<p class="strike"><span>Phrase</span></p>
</div>

use left:0; and right:0 to make sure the lines stay within the borders
The margins you have on the p is what's stopping it from filling the entire width of the container.
Also the span is not really needed.
body {
text-align: center;
}
div {
border: 1px solid black;
margin: 0 auto;
width: 200px;
}
p {
border: 1px solid red;
line-height: 0.5;
/* margin: 20px; to span full width*/
text-align: center;
position: relative;
}
p:before,
p:after {
content: "";
position: absolute;
height: 1px;
background:black;
top: 50%;
transform:translateY(-50%);
width: 20%;
}
p:before {
left: 0;
}
p:after {
right: 0;
}
<div>
<p class="strike">Phrase</p>
</div>

Related

How to place a triangle on my div to make it look like a speech bubble?

I created a simple div for my comments section.
I would like to give it the appearance of a speech bubble by having a triangle on the left or any other effect that would make it look like a speech bubble coming from the left.
How can I achieve that without using an image ?
image
html
<div class='comment'></div>
css
.comment {
margin-left: 10px;
height: 80px;
display: inline-block;
color: white;
width: 400px;
border: 1px solid white;
padding: 10px;
border-radius: 5px;
overflow: hidden;
}
Try this
.comment {
margin-left: 10px;
height: 80px;
display: inline-block;
color: white;
width: 400px;
border: 1px solid white;
padding: 10px;
border-radius: 5px;
position: relative;
background-color: #fff;
border:1px solid #000;
}
.comment::before{
content:"";
position: absolute;
top:20px;
left:-12px;
margin:auto;
height: 20px;
width: 20px;
border:1px solid #fff;
transform:rotate(45deg);
background-color: #fff;
border-bottom:1px solid #000;
border-left:1px solid #000;
}
<div class='comment'></div>
style accordingly,
hope this helps...
I hope to help you:
.comment {
position: relative;
margin-left: 50px;
margin-top: 50px;
height: 50px;
display: inline-block;
width: 200px;
padding: 10px;
border-radius: 5px;
background: skyblue;
color: #FFF;
}
.comment:before, .comment:after {
content: '';
border-radius: 100%;
position: absolute;
width: 50px;
height: 50px;
z-index: -1;
}
.comment:after {
background-color: #fff;
bottom: -30px;
left: 55px;
}
.comment:before {
background-color: skyblue;
bottom: -20px;
left: 70px;
}
<div class='comment'>Hello,World!</div>
I like Nicholas Gallagher's work best, see his demo page.
This is lifted off his page and is not my own work.
<style>
/* Bubble with an isoceles triangle
------------------------------------------ */
.triangle-isosceles {
position: relative;
padding: 15px;
margin: 1em 0 3em;
color: #000;
background: #f3961c;
border-radius: 10px;
background:linear-gradient(#f9d835, #f3961c);
}
/* creates triangle */
.triangle-isosceles:after {
content: "";
display: block; /* reduce the damage in FF3.0 */
position: absolute;
bottom: -15px;
left: 50px;
width: 0;
border-width: 15px 15px 0;
border-style: solid;
border-color: #f3961c transparent;
}
</style>
<p class="triangle-isosceles">This is a quote. Hello world. text goes here.</p>

Aligning ::before and ::after pseudo elements

Can anyone explain me what I am doing wrong in this example? I am trying to create div which has lines on both sides.
.bottom-logo {
width: 50px;
height: 50px;
border-radius: 100%;
background-color: orange;
margin: 0 auto;
position: relative;
}
.bottom-logo::before {
content: "";
margin-right: 50px;
margin-top: 20px;
border-bottom: 4px solid black;
display: inline-block;
width: 100px;
float: right;
}
.bottom-logo::after {
content: "";
display: inline-block;
border-bottom: 4px solid black;
width: 100px;
margin-left: 50px;
}
<div class="bottom-logo"></div>
I would suggest to use absolute position for the pseudo elements. Also updated to use percentage values to make it more flexible.
.bottom-logo {
width: 50px;
height: 50px;
border-radius: 100%;
background-color: orange;
margin: 0 auto;
position: relative;
}
.bottom-logo::before,
.bottom-logo::after {
content: "";
border-bottom: 4px solid black;
width: 100px;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
.bottom-logo::before {
right: 100%;
}
.bottom-logo::after {
left: 100%;
}
<div class="bottom-logo"></div>
Or, you can add a <span> tag then use inline block with vertical align.
.bottom-logo {
text-align: center;
}
.bottom-logo span {
display: inline-block;
vertical-align: middle;
width: 50px;
height: 50px;
border-radius: 100%;
background-color: orange;
}
.bottom-logo::before,
.bottom-logo::after {
display: inline-block;
vertical-align: middle;
content: "";
border-bottom: 4px solid black;
width: 100px;
}
<div class="bottom-logo"><span></span></div>
Another way is to use flexbox with a <span> tag or so.
.bottom-logo {
display: flex;
justify-content: center;
align-items: center;
}
.bottom-logo span {
width: 50px;
height: 50px;
border-radius: 100%;
background-color: orange;
}
.bottom-logo::before,
.bottom-logo::after {
content: "";
border-bottom: 4px solid black;
width: 100px;
}
<div class="bottom-logo"><span></span></div>
Please add float:left;
.bottom-logo::after {
content: "";
display: inline-block;
border-bottom: 4px solid black;
width:100px;
margin-left:50px;
float:left;
}

Center align line below text

I have a line below my h2 element.
It is left aligned, but I'd like to center align this directly below the text.
Can someone help resolve this?
Demo: https://jsfiddle.net/4es6ugjn/
h2 {
display:block
}
h2:after {
border-bottom: 4px solid #ae263d;
content: "";
display: block;
margin-top: 10px;
width: 50px;
}
<h2 class="text-center">Welcome to our Website</h2>
Try this
h2 {
display: inline-block;
position:relative;
}
h2:after {
border-bottom: 4px solid #ae263d;
content: "";
display: block;
width: 50px;
left: 0;
right: 0;
margin: 0 auto;
position:absolute;
}
Best way would be to use display: inline-block on the parent & margin: auto on the child.
h2 {
display:inline-block
}
h2:after {
border-bottom: 4px solid #ae263d;
content: "";
display: block;
width: 50px;
margin: 10px auto 0 auto;
}
<h2 class="text-center">Welcome to our Website</h2>
Alternatively, instead of using margin as suggested by others, you can set left: 50%; and transform: translateX(-50%);
This method of centering can come in handy when you are unable to use margin, or you have a margin set for another reason.
h2 {
display: inline-block;
position:relative;
}
h2:after {
border-bottom: 4px solid #ae263d;
content: "";
display: block;
width: 50px;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
<h2 class="text-center">Welcome to our Website</h2>
Try this:
h2 {
display:inline-block;
}
h2:after {
border-bottom: 4px solid #ae263d;
content: "";
display: block;
margin-top: 10px;
width: 50px;
margin-left: auto;
margin-right: auto;
}
Try this code for this issue
h2 {
display: block;
text-align: center;
}
h2:after {
border-bottom: 4px solid #ae263d;
content: "";
display: block;
margin: 10px auto 0px auto;
width: 50px;
}
<h2 class="text-center">Welcome to our Website</h2>

Drawing specific lines in html

I'm putting design into html+css - the question is - how do i draw curved lines on the sides, so that it would be scalable and responsive? What's the best way to do it
you can try this one:
section{
margin: 0 auto;
width: 600px;
text-align: center;
}
h1 {
position: relative;
//margin-top: 20px;
}
h1.one {
margin-top: 0;
}
h1.one:before {
content: "";
display: block;
border: solid 1px black;
width: 100%;
height: 150px;
position: absolute;
top: 50%;
z-index: 1;
border-radius:5px;
}
h1.one span {
background: #fff;
padding: 0 20px;
position: relative;
z-index: 5;
}
/* method 2*/
button
{
padding:8px;
background-color: gray
color:white;
border: 2px solid;
border-radius: 15px;
margin-top:2px;
position: relative;
z-index: 5;
}
.circle
{
border:4px solid red;
height: 70px;
width: 70px;
border-radius: 40px;
position:relative;
margin:10px auto;
display:inline-block;
}
.row
{
height: 100px;
width: 700px;
margin: 10px;
text-align:center;
position:relative;
}
DEMO FIDDLE
Check as per the description this answer get your perfectly needs.
body {
background:#007DAD;
}
section {
text-align: center;
}
h1.one {
position: relative;
margin-top: 0;
}
h1.one:before {
content: "";
display: block;
border: solid 1px #FFF;
width: 100%;
height: 150px;
position: absolute;
top: 50%;
z-index: 1;
border-radius:5px;
}
h1.one span {
background: #007DAD;
padding: 0 20px;
position: relative;
z-index: 5;
}
.row {
height: 100px;
margin: 10px;
}
.circle {
border:4px solid red;
height: 70px;
width: 70px;
border-radius: 40px;
margin:10px auto;
display:inline-block;
}
button {
padding:10px;
background-color: gray;
color:white;
border: 2px solid;
border-radius: 5px;
margin-top:-10px;
position: relative;
z-index: 5;
}
<section>
<h1 class="one">
<span>It Has Naver Been Easier</span>
</h1>
<div class="row">
<div class="circle"><p>1</p></div>
<div class="circle"><p>2</p></div>
<div class="circle"><p>3</p></div>
</div>
<button type="button">Order Now</button>
</section>

Heading with line afterwards, with 2 different sizes

I am trying to achieve the following, with pure CSS and no images:
As you can see, its a heading with a line afterwards. The problem is, that the line should has 2 different colors and more important, 2 different heights.
The first parts color is orange, has a height of 3px and a fixed width of 100px (padding-left: 15px)
The sedond parts color is #E1E1E1 and should fill the rest of the line.
My first try was this:
<h1><span>OUR ARTICLES</span></h1>
<style>
h1 {
overflow: hidden;
}
h1 span {
display: inline-block;
position: relative;
}
h1 span:after {
content: "";
position: absolute;
height: 1px;
top: 45%;
width: 999px;
background: #E1E1E1;
border-left: 100px solid orange;
left: 100%;
margin-left: 15px;
}
</style>
See http://jsfiddle.net/oyxmxoLs/
But as you can see, I can't make the orange part thicker than the grey one.
Any ideas?
Another way: Flexbox
With display: flex you don't have to give the line a certain width and you can make sure it is always responsive.
We are going here with an progressive enhancement approach. We'll make a cut after IE8 by using ::before instead of :before. In IE9 only the grey line will be shown (underneath the title).
h1 {
align-items: center;
color: #444;
display: flex;
font: 18px/1.3 sans-serif;
margin: 18px 15px;
white-space: nowrap;
}
h1::before {
background-color: orange;
content: "";
height: 4px;
margin-left: 10px;
order: 2;
width: 100px;
}
h1::after {
background-color: #E1E1E1;
content: "";
display: block;
height: 2px;
order: 3;
width: 100%;
}
<h1>Our articles</h1>
Do not forget to add vendor-prefixes!
You can solve this by using :before and :after
http://jsfiddle.net/oyxmxoLs/1/
h1 {
overflow: hidden;
}
h1 span {
display: inline-block;
position: relative;
}
h1 span:before {
content: "";
position: absolute;
height: 1px;
top: 45%;
width: 999px;
background: #E1E1E1;
left: 100%;
margin-left: 15px;
}
h1 span:after {
content: "";
position: absolute;
height: 3px;
top: 45%;
width: 100px;
background: orange;
left: 100%;
margin-left: 15px;
margin-top:-1px;
}
<h1><span>OUR ARTICLES</span></h1>
You can also use the :before pseudo-element to add the orange line.
h1 {
overflow: hidden;
}
h1 span {
display: inline-block;
position: relative;
}
h1 span:after, h1 span:before {
content: "";
position: absolute;
height: 1px;
left: 100%;
top: 45%;
margin-left: 15px;
}
h1 span:after {
width: 999px;
background: #E1E1E1;
}
h1 span:before {
height: 3px;
z-index: 1;
margin-top: -1px;
border-radius: 2px;
width: 100px;
background: orange;
}
<h1><span>OUR ARTICLES</span></h1>