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>
Related
I want to achieve this
But using this css
border-left: 3px solid red;
border-radius: 3px;
It is producing this result
plz ignore spacing, i will add that
I would recommend using pseudo-classes in order to create your red bar. Try this:
span {
font-family: Arial;
font-size: 3em;
}
span::before {
content: "";
display: inline-block;
margin-right: 10px;
width: 10px;
height: 40px;
border-radius: 50px;
background-color: red;
}
<span>2.1 Cr</span>
Use border-top-left-radius and border-bottom-left-radius
.div{
margin-top:40px;
width:50px;
height:10px;
border-radius:50px;
background:red;
transform:rotateZ(90deg);
}
<div class="div"></div>
Edited:
try this
Fiddle file:
https://jsfiddle.net/swuzqpbt/
htmlelement {
position: relative;
height: 40px;
border: none;
margin: 20px auto;;
}
htmlelement ::before {
content: '';
display: inline-block;
background: red;
width: 5px;
height: 40px;
top: 0;
left: 0;
border-radius: 140px;
position: absolute;
border:none;
}
I'm trying to draw additional (mock) buttons onto my page with plain CSS, but my span element is not showing up. I've tried giving it a display: block; and I've also tried positioning it absolutely, but nothing seems to work. And out of those two ways, which is the preferred/most clear method?
header {
position: relative;
display: flex;
justify-content: space-around;
border-bottom: 6px solid black;
padding: 15px 0 10px 0;
}
img {
width: 43px;
height: 43px;
}
.red-button {
background: yellow;
width: 50px;
height: 50px;
}
header:before {
content: "";
position: absolute;
z-index: 1;
top: 74px;
width: 100%;
border-bottom: 6px solid maroon;
}
header:after {
content: "";
position: absolute;
top: 0;
width: 100%;
border-bottom: 6px solid $light-red;
}
<body>
<header>
<img src="./assets/pokeball.svg" alt="pokedex">
<span className="red-button"></span>
</header>
</body>
Just try to replace className by class.
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>
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;
}
How can I do something like the picture below?
I would like to have an extra thick line to all my h1's but am not quite sure of a best practice to do it.
HTML:
<h1>This is Our Work</h1>
CSS:
h1{
border-bottom: 1px solid #246cb4;
display: inline-block;
}
h1:after {
content: "";
display: block;
border: 1px solid black;
width: 50px;
margin-top: 0px;
position: absolute;
}
Codepen:
You don't need any pseudo elements in this case.
You can draw multiple background images with css3 linear-gradient() with precisely controlled size and positions:
h1 {
background-image: linear-gradient(to right, #246cb4, #246cb4),
linear-gradient(to right, #246cb4, #246cb4);
background-repeat: no-repeat;
background-size: 100% 1px, 50px 3px;
background-position: bottom 2px left, bottom 1px center;
}
h1{
background-image: linear-gradient(to right, #246cb4, #246cb4),
linear-gradient(to right, #246cb4, #246cb4);
background-size: 100% 1px, 50px 3px;
background-repeat: no-repeat;
background-position: bottom 2px left, bottom 1px center;
position: relative;
display: inline-block;
}
<h1>This is Our Work</h1>
You need to add position:relative to the h1 and set margin:0 auto to h1:after
h1 {
border-bottom: 1px solid #0D6CC4;
display: inline-block;
position:relative;
}
h1:after {
content: "";
display: block;
border: 2px solid #0D6CC4;
width: 50px;
margin-top: 0px;
position: absolute;
right: 0;
left:0 ;
bottom:-2px;
margin:0 auto;
}
<h1>This is Our Work</h1>
Try this just with a few tweaks in styling related to position.
h1{
border-bottom: 1px solid #246cb4;
display: inline-block;
position: relative;
}
h1:after {
content: "";
display: block;
border: 2px solid black;
width: 50px;
position: absolute;
left: 0;
right: 0;
margin: auto;
margin-top: -1.5px;
}
<h1>This is Our Work</h1>
used this css
h1:after {
content: "";
display: block;
border: 3px solid black;
width: 50px;
margin-top: 0px;
position: absolute;
right: 0;
left: 0;
margin: 0 auto;
bottom: -3px;
}
h1 {
border-bottom: 1px solid #246cb4;
display: inline-block;
position: relative;
}
You need to position element :after and :before
.main-title{
text-align: center;
}
.inner-title{
position: relative;
font-size: 24px;
padding: 0 0 15px;
margin: 0 0 15px;
text-transform: uppercase;
letter-spacing: 1px;
}
h1:before {
position: absolute;
content: '';
width: 150px;
left: 50%;
margin-left: -75px;
height: 1px;
background: blue;
bottom: 0;
}
h1:after {
position: absolute;
content: '';
width: 50px;
left: 50%;
margin-left: -25px;
height: 3px;
background: blue;
bottom: -1px;
}
<div class="main-title" >
<h1 class="inner-title">Sevices</h1>
</div>
Since you don't know how long every h1 tag will be, i suggest you to translate the element to the center of its parent, like in this example: http://codepen.io/anon/pen/jyMzqN
h1 {
border-bottom: 1px solid #246cb4;
display: inline-block;
position: relative;
}
h1:after {
content: "";
display: block;
position: absolute;
height: 3px;
background-color: black;
width: 50px;
left: 50%;
bottom: -2px;
transform: translateX(-50%);
}