What is the best way to sketch a line stretching from the center of the page to the right edge?

Is it possible to create a line that extends from the center of the page to the right side using CSS, HTML, or JS?

This line should be able to adjust for various screen resolutions.

An example is shown in the accompanying image.

Answer №1

Learn how to style a horizontal rule using CSS.

hr {
 color: white;
 background: blue; 
 width: 75%; 
 height: 5px;
 margin-left:25%;
}

<body>
<hr />
<hr/>
</body>

To see the demonstration, follow this link:

http://jsfiddle.net/yvytty/jJRAt/

Answer №2

Perhaps it can be done like this?

Markup Language

<div class="line"></div>

Cascading Style Sheets

div.line {
    width: 75%;
    height: 1px;
    margin-left: 25%;
    background: blue;
}

See It In Action

Test it out first

Answer №3

html code:

<div id="lineID" class="line"></div>

css styling:

 .line{  
    background:red;    
    height: 1px;
margin-left:50%;
        }

javascript for added functionality:

//you have the option to include all CSS within this script as well

    var scr=screen.width/2
    var myLine = document.getElementById('lineID');
    myLine.style.cssText= "width:"+scr+"px";

View the live demo on JSFiddle!

Answer №4

In my opinion, the most effective way to create a line that scales correctly and is purely CSS from the center to the right is as follows:

HTML

<div class="lineblock"></div>

CSS

.lineblock{
    width: 50%; 
    height: 20px;
    float: right; 
    background: magenta; 
}

This technique ensures that the line will scale to fit all device screen sizes at 50% of the viewport width. It is also compatible with older browsers like IE 8.

Sources: HTML - Simple div CSS - Experimentation Browser Stats - Stat Counter's browser version usage for this month past.

  • Information accurate at the time of writing.

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

next.js retrieves information from Laravel

As part of my Laravel project, I have written a simple user registration feature with the following code: public function register() { $this->validate(request(), [ 'name' => 'required', 'email' => ...

Error found - PHP if condition inside HTML td element

Whenever I open my browser, I encounter this error message: Parse error: syntax error, unexpected 'if' (T_IF) I have been working on creating an HTML table to display prices for a Woocommerce product. Although I've come across similar issu ...

Retrieve the outcome of an AJAX request in JavaScript within a separate function

My uploadFunction allows me to upload files to my server using a Rest web service called with JQuery ajax. I also use this method for another function and need to determine the result of the web service call in order to add a row with the name of the uploa ...

Is there a way to showcase the present weather conditions using simpleweather.js in plain text format?

I have integrated the weather.js library into my HTML file by adding the necessary imports: <script src="js/current.js"></script> <script src="js/sugar.min.js"></script> <script src="js/weather.js"></script> <script ...

Faster method for submitting extensive forms via AJAX

Is there a way to make this code shorter, especially for the sections with ... 20 items? <input type='text' id='inputname' maxlength='50' placeholder='Name' required> <input type='text' id='i ...

Enhancing the dropdown menu with a fixed value

In another file, I have a dropdown menu generated using a while loop. I want to include one static value (Select contract) in the dropdown. Currently, the dropdown looks like this: 1234 4321 2323 3232 I would like it to look like this: Select contract ...

Utilizing Async / Await in the created lifecycle hook - Vue2

I recently installed the vue-element-loading package and added its component to my page.vue: <vue-element-loading :active="isActive" :is-full-screen="true"/> After adding a variable to my data: data () { return { isActive: false, } } I th ...

Using jQuery to target the element before

Is there a way to determine the width of elements located before an element when it is hovered over? I attempted to achieve this using the following code: $('ul li').hover(function() { $(this).prevAll().each(function() { var margin = $(this ...

Alert: Github Dependabot has flagged Babel as vulnerable to arbitrary code execution when compiling meticulously designed malicious code

My Github Repository's Security section is flagging this issue as Critical for both my Frontend and Backend code. I'm having trouble grasping the nature of this alert. Can someone explain what flaw it represents? After updating my dependencies, ...

Using useEffect to set the state and then accessing its properties

I have a React component where I am trying to access a property in my state that was set using the useEffect hook. However, I keep receiving an error that says Uncaught "TypeError: Cannot read property '0' of undefined". Here is the cod ...

Strategies for managing HTML constraint validation pop-up in Selenium

https://i.sstatic.net/wMmca.png Once you click on the "SIGN IN" button, a notification saying "Please enter your email." will pop up. I am trying to extract this message using Selenium. Is it doable at all? Below is the HTML snippet for the email input ...

What is the best location to house the fetching logic for mounting an app with intricate React v6 routing?

I have a protected route component that handles various tasks: Authorizing users with Auth0. Checking if the store is empty and making a request to the backend API for user data. Passing a function that returns an access token to the axios interceptor so ...

Showing MYSQL data in an html table

Hey there! I've been working on a website that features HTML5 video player to play videos. I have a "watch" page that retrieves data from the database based on the id parameter (watch.php?v=1). Now, I'm looking to display the most recent videos o ...

Data is successfully being stored in an array in AngularJS, however, it is not appearing in the user interface

Having an issue with displaying updated data on my UI. I am successfully pushing data into the database and an array using Angular 2-way binding. The data is being pushed as confirmed by the console, but it's not showing up on the user interface. Con ...

How to access an element inside a div using the eq() method

Within my #img_wrapper, I have multiple pictures each enclosed in a link: <div id="img_wrapper"> <a href="img1.jpg" style="display: none;"> <img src="img1.jpg" /> </a> <a href="img2.jpg" style="display: none;"> &l ...

A lengthy string of characters within a continuous paragraph without any interruptions

Is it possible to have a long string automatically move to the next line without breaking any characters in the string, rather than splitting the word between lines? The current display looks like this: My sample text ha s errors as it is display ...

Manipulating all components of a specific type in App.vue with VueJS: Is it possible?

Consider this template with FruitPrices as a component: <template> <div id="app"> <div> <span @click=SOME_FUNCTION> Change currency </span> <FruitPrices fruit="apple"></FruitPrice ...

Controlling the Output in Typescript without Restricting the Input

I am interested in passing a function as a parameter, allowing the function to have any number of parameters but restricting the return type to boolean. For example, a function Car(driver: Function) could be defined where driver can be ()=>boolean or ( ...

How can we convert props into state once they have been initialized using GetDerivedStateFromProps?

Presented here is a straightforward Component design: export default class Editor extends Component { constructor(props) { super(props); this.state = { field: "Some Initial Text" }; this.handleChangeField = this.handleChangeField.bind(this ...

Having trouble with an Ajax request in jQuery

Need some help with the code below - I can't seem to get the success or error event to fire. Even though the server returns a 200 OK response with the required data. Any assistance would be greatly appreciated! $('body').on('click&apos ...