Horizontal Line Styling on Each Side of Titles - html

I am working on a Wordpress blog, and I am trying to figure out how to add horizontal lines on each side of some of my titles like the ones in this link:
http://falive.jegtheme.com/?slider=highlightslider&homelayout=normal&homesidebar=true&layout=full&header=1&sticky=true
In the blog above, titles in the sidebar, and the 'share this article' title has the desired effect that I am looking for, but can't seem to figure out how to get it. I know the basics of HTML and CSS, so this could be something that I am simply overlooking or just haven't learned yet.
Also, is there a way to take this type of styling to the next level by adding more unique types of lines (like long curly lines) through CSS?
Thanks in advance!

use :before or :after
Example 1:
h2{
padding: 0 20px;
text-align: center;
}
h2:before,
h2:after{
content: '';
width: 150px;
height: 1px;
margin: 0 10px;
background: #ccc;
display: inline-block;
vertical-align: middle;
}
<h2>title</h2>
<h2>title title title</h2>
Example 2
div{
text-align: center;
}
h2 {
padding: 0 20px;
position: relative;
text-align: center;
display: inline-block;
}
h2:before,
h2:after {
content:'';
width: 100%;
position: absolute; top: 50%;
height: 1px;
background: #ccc;
transform: translateY(-50%);
}
h2:before{
right: 100%;
}
h2:after{
left: 100%;
}
<div>
<h2>title</h2>
<br>
<h2>title title title</h2>
</div>

Using your browser's developer tools, inspect the span elements containing those titles. You'll see :before and :after CSS3 selectors in which some positional/border styling is used.
Can you use other kinds of lines? Sure -- CSS3 would allow you to use a wide variety of things, but the list is probably too long to list here on SO.

Related

why the img cannot be centered vertically when i use vertical-align? [duplicate]

This question already has answers here:
Vertically-aligned inline-block element not perfectly centered within container
(2 answers)
Closed 1 year ago.
the img cannot be centered vertically when i use vertical-align, I really dont understand why.
h1 {
position: relative;
line-height: 50px;
background: blanchedalmond;
}
h1::before {
content: '';
position: absolute;
top: 50%;
width: 100%;
border-top: 1px solid green;
}
img {
width: 30px;
height: 30px;
vertical-align: middle;
}
<h1>
<img src="https://img01.yzcdn.cn/vant/cat.jpeg" />
vertical-align middle
</h1>
First, you don't have vertical align middle applied. You have posted it as text in your HTML but as such, it also only works as text but not as a code command.
Then, vertical-align: center; only works in combination with tables or table-cells. So you would need to apply: display: table-cell;. However, the more modern solution would be flexbox. Use: h1 { display: flex; align-items: center; } and the image will be vertically centered within the <h1> tag.
Then you have 2 issues:
The critical issue: <h1><div></div></h1> is an invalid HTML markup that will neither pass the W3C nor the WHATWG markup check. A <div> cannot be a child within a header tag.
The minor issue is with the <img /> tag. Since HTML5 the image tag is an empty tag. Means it does not have a closing tag nor does it have a slash at the end. It's simply written <img>. Only a few frameworks/libraries still use the slash.
h1 {
position: relative;
line-height: 50px;
background: blanchedalmond;
display: flex;
align-items: center;
}
.line {
position: absolute;
top: 50%;
width: 100%;
border-top: 1px solid green;
}
img {
width: 30px;
height: 30px;
}
<h1>
<div class="line"></div>
<img src="https://img01.yzcdn.cn/vant/cat.jpeg" />
vertical-align middle
</h1>

Structuring div elements under text (HTML/CSS)

I have designs which have a blue bar underneath text as a theme, such as the images below
I have currently been structuring my html like this for these bars:
<div class="Header">
<h4>My Website</h4>
<div class="YBlock2"></div>
</div>
Where the CSS class is defined as
.YBlock2{
background-color:blue;
width: 50px;
height: 10px;
}
However, I have been wondering if this is the best way to do this. It looks great on my computer, but I do not know if it is the best practice to use when aiming for the site to be used on a variety of different screen sizes and devices, or the best way in general.
You can use pseudo-elements as :after.
h4:after {
content: '';
display: block;
margin-top: 3px;
width: 50px;
height: 10px;
background-color: blue;
}
<h4>My website</h4>
Probably a pseudo element is best, but for fun you could also do this.
h1 {
max-width: 50px;
border-bottom: 3px solid blue;
overflow-x: visible;
white-space: nowrap;
}
<h1>Bananas are pretty tasty</h1>
<p>So are pears</p>

How to vertically align text in the middle of a div that has a percentage height? [duplicate]

This question already has answers here:
How do I vertically align text in a div?
(34 answers)
Closed 8 years ago.
Here is what I have right now. In other divs using vertical-align:middleand setting the line-height to the same value as the height property it should work!The only thing is that in those divs I used pixel dimension and not percentages. Can anybody tell me why this wont work with percentages? also setting the text-sizeto 50% should also make text half the size of the div but it is really really small still? What is going on here?
#chooseStateAlabama {
width: 20%;
height: 25%;
top: 0;
left: 0;
position: absolute;
background: url(../_images/_unitedStates/_states/chooseStateAlabama.png);
background-size: 100% 200%;
float: left;
color: #FFFFFF;
font-family: Arial;
font-style: normal;
font-weight: bold;
font-size: 50%;
line-height: 25%;
text-align: center;
vertical-align: middle;
}
You can use display:inline-block , height:100% and vertical-align:middle to a single element or pseudo element aside the text (before or after): DEMO
#chooseStateAlabama:before {/* this can be an extra tag within HTML structure if pseudo used for other purpose */
content:'';
display:inline-block;
height:100%;
vertical-align:middle;
}
If you happen to have more content or more than 1 line, then use an element to wrap it as well and apply to it display and vertical-align. DEMO2 to see behavior
If you can alter the markup you can use quite a few ways to get the result you want.
http://jsfiddle.net/vvpn6cge/
Because you have text ( that could be one or many lines long I guess) then you could get the result using CSS table cells (see the fiddle).
.outer-container {
display: table;
width: 100%;
}
.txt-vertical-align {
display: table-cell;
text-align: center;
vertical-align: middle;
}
As a further alternative, you might use flexbox
display: flex;
justify-content:center;
align-content:center;
flex-direction:column;
(stolen from this stackoverflow question)
http://jsfiddle.net/L85h8vvj/7/
HTML
<body>
<div class='container4'>
<div>Example :)</div>
</div>
</body>
CSS
body{
margin:0px;
}
div.container4 {
height: 100%;
width:100%;
position: absolute;
}
div.container4 div {
margin: 0;
background: yellow;
position: absolute;
top: 50%;
left: 50%;
margin-right: 50-%;
transform: translate(-50%, -50%)
}

Vertically center text by its underline and overline's center

I want to vertically center a text by its underline and overline. The text element is inside a div that is absolutely positioned and has an unknown (variable) height. The text's font size and horizontal position also have to be variable.
In other words: I want to position the text so that the center between underline and overline is exactly at the center of the containing div.
Additionally, I want to display a rectangle (using a div) in front of the text:
<div class="container">
<div class="rectangle"></div>
<div class="text">Text</div>
</div>
Here's an example: http://codepen.io/zabbarob/pen/CHxLe
* { margin: 0; padding: 0; border: 0; }
.container {
position: absolute; top: 5px; height: 25px;
zoom: 800%; /* debugging */
vertical-align: middle;
}
.rectangle {
display: inline-block;
width: 50px; height: 100%;
background-color: green;
}
.text {
display: inline-block;
text-decoration: underline overline;
font-size: 20px;
position: absolute; left: 50px;
background: lightgreen;
/* center vertically by underline and overline */
top: 0; bottom: 0;
line-height: 25px;
}
Hmm, rather than using an actual overline/underline (which could be a bit of a headache to customize), have you considered mimicking it using border top/bottom instead? So you could modify your CSS definition for text as follows:
.text {
display: inline-block;
/*text-decoration: underline overline;*/
font-size: 20px;
position: absolute; left: 50px;
background: lightgreen;
/* center vertically by underline and overline */
top: 0; bottom: 0;
line-height: 23px; /* Height of the element beside it, minus 2px for the borders */
border-top:1px solid #000;
border-bottom:1px solid #000;
}
Here's a modified CodePen to demonstrate. Depending on your requirements, this may not be optimal (for example, border scales with zoom, whereas underline does not), but it does at least give you a different way of approaching the problem.
Hope this helps! Let me know if you have any questions.
Have you tried using the methods outlined here: http://css-tricks.com/centering-in-the-unknown/ ?
Always works for me.
Hope this helps

Trailing Line Decoration Headers in CSS

I am trying to create header tags with a bit of fancy decoration.
Eventually, I want to arrive at this:
I am having trouble adding the trailing line decoration after the text though.
My original thoughts were to have a container, then in that container would be the h1 and a span tag that would contain the line. But I can't quite seem to get the line to be centered with the text sitting over it.
I tried:
.container {
width: 100%;
}
h1 {
display: inline;
position: relative;
z-index: 2;
}
span {
display: inline-block;
height: 50%;
width: 100%;
border-bottom: 1px solid red;
}
and the HTML
<div class="container">
<h1>Test</h1>
<span></span>
</div>
But had no luck. Anyone know any tricks that might help me accomplish this? The main thing is that the length and height of the text is variable, so I am trying to get the line to take up the remainder of the box and sit right in the middle.
I also tried display: table-cell with no luck...
You need something like this
html - <h2>Test</h2>
css
h2{
position:relative;
overflow:hidden;
}
h2:after {
content:"";
top:48%;
width:100%;
margin-left:10px;
height:5px;
position:absolute;
background:orange;
}
How about using css pseudo elements like :after?
HTML
<div class="foo">INSIGHT</div>
CSS
.foo {
position: relative;
color: orange;
overflow: hidden;
}
.foo:after {
content: "";
position: absolute;
width: 90%;
top: 50%;
margin-left: 10%;
border-top: 3px solid orange;
}
DEMO