17
loading...
This website collects cookies to deliver better user experience
<div>Hello world!</div>
.divider {
display: flex;
align-items: center;
text-align: center;
}
/* To show the lines on right
and left sides of the text */
.divider::after,
.divider::before {
content: "";
border: 1px solid black;
flex: 1;
}
/* Space on left and right sides of text */
.divider:not(:empty)::before {
margin-right: 0.25em;
}
.divider:not(:empty)::after {
margin-left: 0.25em;
}
<div>
HTML tag and put the content which we need to show in the center of the divider inside the tag like this,<div>Hello world!</div>
class
name to the div
so that we can reference this in the CSS. it can be done like this,<div class="divider">Hello world!</div>
div
into a flex element. It can be done like this,.divider {
display: flex;
}
div
tag, so to do that we can use the align-items
flex property and set its value to center
. it can be done like this,.divider {
display: flex;
align-items: center;
}
text-align
property and set its value to center
like this,.divider {
display: flex;
align-items: center;
text-align: center;
}
div
tag.Hello world!
text, we can make use of the ::before
and ::after
pseudo-elements in CSS.1px
black color border to both of the div
tag's ::after
and ::before
pseudo-elements. We also need to apply flex: 1
to the pseudo-elements. So that we will have a long line to the right and left side of the text..divider {
display: flex;
align-items: center;
text-align: center;
}
/* To show the lines on right
and left sides of the text */
.divider::after,
.divider::before {
content: "";
border: 1px solid black;
flex: 1;
}
Hello world!
doesn't have much space on both its right and left sides. to set some space we can add this CSS also,.divider {
display: flex;
align-items: center;
text-align: center;
}
/* To show the lines on right
and left sides of the text */
.divider::after,
.divider::before {
content: "";
border: 1px solid black;
flex: 1;
}
/* Space on left and right sides of text */
.divider:not(:empty)::before {
margin-right: 0.25em;
}
.divider:not(:empty)::after {
margin-left: 0.25em;
}