How to Create a Centered Heading with Horizontal Lines That Disappear When Crossing Text?

Patricia Arquette
Release: 2024-10-30 21:19:30
Original
460 people have browsed it

How to Create a Centered Heading with Horizontal Lines That Disappear When Crossing Text?

How to Create a Heading with Horizontal Lines on Either Side

This scenario involves the task of creating a centered heading with horizontal lines vertically centered on each side, while maintaining a transparent background due to the presence of a background image. Despite attempts to center the title and create a line using pseudo-class, the challenge remains in making the line disappear when it crosses the text of the title.

One potential solution is to utilize a background gradient with transparency where the words reside. However, this approach becomes impractical when dealing with varying title lengths, making the placement of gradient stops impossible.

The code provided initially is as follows:

<code class="css">h1 {  
    text-align: center;  
    position: relative;  
    font-size: 30px;  
    z-index: 1;  
}  

h1:after {  
    content: '';  
    background-color: red;  
    height: 1px;  
    display: block;  
    position: absolute;  
    top: 18px;  
    left: 0;  
    width: 100%;  
}  </code>
Copy after login

Referencing the link provided in the answer, a modified version of the code is as follows:

<code class="css">h1 {
    position: relative;
    font-size: 30px;
    z-index: 1;
    overflow: hidden;
    text-align: center;
}
h1:before, h1:after {
    position: absolute;
    top: 51%;
    overflow: hidden;
    width: 50%;
    height: 1px;
    content: '\a0';
    background-color: red;
}
h1:before {
    margin-left: -50%;
    text-align: right;
}
.color {
    background-color: #ccc;
}</code>
Copy after login

This modified code resolves the issue and ensures that the horizontal lines disappear whenever they cross the text of the title.

The above is the detailed content of How to Create a Centered Heading with Horizontal Lines That Disappear When Crossing Text?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!