Creating a line that extends from the left side of the viewport to the full length of an element within a bootstrap column

I'm currently working on creating a title that has a unique design:

https://i.sstatic.net/VhEBQ.png

The design requires a horizontal line to start from the left side of the viewport and end at the same point where the title ends. I am aiming to keep everything contained within a Bootstrap column. Here is the initial markup that I have:

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f0929f9f848384829180b0c5dec1dec1">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
    <div class="row">
        <div class="col-12">
            <div class="title-with-line">
                <h2>A great title</h2>
            </div>
        </div>
    </div>
</div>

I prefer to keep the HTML markup as it is without making major changes. Additional elements can be added inside the column without any issue. It is important to me that nothing is placed outside of the column div.

Answer №1

To create a title with a line underneath, you can use the following CSS:

.title-with-line {
  width: fit-content;
  border-top: 2px solid black;
  padding-left: calc(2em * 2);
  position: absolute;
  left: 0;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d1b3bebea5a2a5a3b0a191e4ffe0ffe0">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
    <div class="row">
        <div class="col-12">
            <div class="title-with-line">
                <h2>Our Services</h2>
            </div>
        </div>
    </div>
</div>

Answer №2

After much trial and error, I have finally discovered a solution that is responsive on any screen size. I am grateful for the helpful responses that directed me to this solution :

.title-with-line {
    position: relative;
    margin-top: 25px; /* This margin only to offset the line form the top of the body for demonstration */
}

.title-with-line h2 {
    position: relative;
    float: left;
}

.title-with-line h2:before {
    content: '';
    display: block;
    position: absolute;

    top: 0;
    right: 0;

    width: 100vw;
    height: 1px;

    background-color: black;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a2c0cdcdd6d1d6d0c3d2e2978c938c93">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container">
    <div class="row">
        <div class="col-12">

            <div class="title-with-line">
                <h2>An amazing heading</h2>
            </div>
        </div>
    </div>
</div>

Answer №3

//To create a horizontal line, you can use the <hr> tag.

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Here is an example</h1>
<hr>
<h2>Another example</h2>
</body>
</html>

Answer №4

This snippet of code will get the job done

.title-with-line h2 {
  position:relative;
  padding-top:20px;
  display:inline-block;
}
.title-with-line h2:before {
  content:"";
  position:absolute;
  height:1px;
  background:black;
  right:0;
  top:10px;
  width: 50vw;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
    <div class="row">
        <div class="col-12">
            <div class="title-with-line">
                <h2>A Great Title</h2>
            </div>
        </div>
    </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is a way to increase the boldness of an HTML anchor tag when hovering without using CSS, but by modifying the inline style?

Is it possible to make the following anchor tags appear bolder on hover using inline styles, without utilizing any external or embedded CSS file? <a href="https://en.wikipedia.org/wiki/Internet_of_Things" target="_blank">Internet of things<br& ...

Clicking on the icon reveals the current date

I need some help with the input field that displays the calendar date. Currently, I can only click on the input field to show the calendar date. What I actually want is for users to be able to click on a calendar icon to display the calendar date, which sh ...

Expanding beyond the dimensions of the webpage, HTML CSS Height set to 100% creates a unique

I'm struggling with the CSS on a page I'm working on. I have a header and main content each enclosed in div tags, with the header set to a height of 250px and the main content set to a height of 100%. I've also set the html and body heights ...

When floating a heading 4 (h4) and two divs to the left, and a single div to the right,

When floating a h4 and 2 divs to the left, and another div to the right, how can I place a block inside the remaining space that perfectly fills it? The background color of this space should not spread outside. [h4][div][div][remaining space][div] Thank ...

Getting the URL of a CSS background image in NodeJS and Express: A step-by-step guide

I've recently learned that using getComputedStyle in a Node environment is not possible due to it being a browser-specific behavior. Despite this, I am still interested in retrieving all background image URLs within Node and downloading them as shown ...

Every time I attempt to input text into the text area, the page automatically refreshes, preventing me from completing any entries

Currently, I am working on developing a webpage that can dynamically update text inputted by users in a database. However, I have encountered an issue where the page refreshes automatically whenever I click on the textarea. If I type quickly enough, some o ...

Angular binding for selecting all data

Upon checking a checkbox for a single item, the bound data is retrieved and added to an array. However, this does not happen when using selectAll. Code snippet in Angular for obtaining the object of a checked item: $scope.selectedOrganisations = []; $sco ...

Is Jade used for making subsequent lines children of an included partial?

Recently, I've encountered an issue with a basic jade layout. Here is an example: include test.jade #bar hi In the test.jade file: #foo hello No matter what I try, the #bar element always ends up as a child of #foo. <div id="foo">hello &l ...

do not display entries with expired duration

update1: Unfortunately, even after testing in the sandbox, the result is still returning empty :( https://codesandbox.io/s/happy-https-u8lu2 After filtering my starsValues based on height and weight, I am able to get some results. However, I also n ...

Utilizing a Web Interface for Remote Monitoring of Windows Servers

I am in need of creating a webpage that will indicate whether a server is currently operational or not. These servers are all Windows based, with some running on 2008 and others on 2003. They are spread across different networks within various client locat ...

Guide on showing long blocks of text in MySQL without formatting restrictions

I've come across a problem that I can't seem to solve anywhere. After storing data from a topic or comment in the database, the message is stored in a longtext format. However, when the longtext shows fine in the table (with enters and multiple ...

As the screen size shrinks, two components merge together

One issue I'm facing with my site is the component called NavPanel. This consists of two components - a back button (BackToButton) and a search field (SearchTextField). Everything looks fine on a standard screen size, but when the screen size decrease ...

Captivating captions for captivating visuals. Pictures don't conform to a linear arrangement

As someone who is new to website creation, I am currently working on adding a CSS effect where a popup-text appears when an image is hovered over. I want to create a row of images for the popups by using an unordered list. However, I'm facing a proble ...

Adjust the div container to wrap underneath the image if the webpage width decreases

Hello there, I am currently in the process of creating my own portfolio. I have a brief introduction that I would like to display next to an image of myself, with the image positioned on the right side. I have a div called "container" which contains anoth ...

Prevent automatic submission of forms when selecting attributes in HTML forms using AngularJS

I need to create a form where users can select their branch of study. Here is the form I have designed- <form method="post" [formGroup]="formData" (click)="dataSubmit()" > <div class="form-group"> <label for="branch">Selec ...

Angular2 poses a strange problem with ngClass

It seems like Angular is expecting me to use single quotes for the class names even if there are no hyphens in my CSS class name. I've tried everything but Angular keeps insisting on using hyphens for this specific CSS class... it's strange, or m ...

Creating a dynamic full-width background image that adjusts its height according to the content: a step-by-step guide

Currently, I'm attempting to apply a background image to a webpage that spans 100% width in order to adjust to the viewport size. The method I am using involves placing an image directly after the body tag with the subsequent styling: img#background ...

Embed fashion and graph elements into a CSV document (generated from <script>)

Encountering an issue with my code. I am looking to export an HTML table to a CSV file (specifically Libre Office Calc, regardless of csv, xls or xlsx format, as long as it runs on a Linux Server). Found a script online that works well after some modificat ...

Incorporating a CSS Stylesheet into an ASP.NET Custom Control

Currently, I am in the process of developing a unique custom control that consists of a combination of tables and buttons. Alongside this, there is an external CSS stylesheet that defines the styles for these specific elements. This control falls under th ...

The Bootstrap container is extending beyond the boundaries of the page view

Since I'm not a CSS enthusiast, I opted to incorporate Bootstrap into my small project. However, I've encountered an issue where one of the containers is extending beyond the page view, and I can't seem to figure out why. <!-- STYLE ...