Align the active li vertically within the ul

Looking to create a layout similar to musixmatch where the active verse is displayed at the center of the ul.

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

I have experience doing this with JavaScript, but I'm curious if it's achievable using only CSS.

<ul id="lyrics">
    <li>You know, everybody's been tellin' me what they think about me for the last few months</li>
    <li class="active">It's too loud</li>
    <li>Maybe it's time I tell 'em what I think about them</li>
</ul>

CSS:

@import url('https://fonts.googleapis.com/css?family=Roboto');
body {
    background: #252525;
    font-family: 'Roboto', sans-serif;
    color: white;
}   

#lyrics {
    background-color: rgba(0, 0, 0, 0.45);
    position: fixed;
    width: 70%;
    list-style: none;
    top: 50%;
    transform: translate(-50%, -50%);
    left: 50%;
    margin: 0 auto;
    text-align: center;
    font-weight: 500;
    font-size: 2em;
    height: 50vh;
    color: rgb(240, 240, 240);
    overflow: hidden;
}

#lyrics .active {
    color: cornsilk;
    font-weight: 600;
    font-size: 1.2em;
}

Answer №1

What do you think about this CSS styling for lyrics?

#lyrics {
background-color: rgba(0, 0, 0, 0.45);
position: fixed;
width: 70%;
list-style: none;
top: 50%;
transform: translate(-50%, -50%);
left: 50%;
margin: 0 auto;
text-align: center;
font-weight: 500;
font-size: 2em;
height: 50vh;
color: rgb(240, 240, 240);
overflow: hidden;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;

}

Answer №2

When adjusting the text positioning to match the song, JavaScript is essential for handling time-related changes. However, if you're solely concerned with repositioning the text itself:

  • Enclose the next line in an active div tag
  • Modify the value of the top property to a negative pixel value or percentage

For instance, if you wish to highlight the following line of the song, utilize this code snippet:

body {
    background: #252525;
    font-family: 'Roboto', sans-serif;
    color: white;
}   

#box {
    background-color: rgba(0, 0, 0, 0.45);
    position: fixed;
    width: 70%;
    top: 50%;
    transform: translate(-50%, -50%);
    left: 50%;
    margin: 0 auto;
    text-align: center;
    font-weight: 500;
    font-size: 2em;
    height: 50vh;
    color: rgb(240, 240, 240);
    overflow: hidden;
}
ul {
    position: relative;
    top: -15%;
    list-style: none;
}

#lyrics .active {
    color: cornsilk;
    font-weight: 600;
    font-size: 1.2em;
}
<div id = "box">
<ul id="lyrics">
    <li>You know, everybody's been tellin' me what they think about me for the last few months</li>
    <li>It's too loud</li>
    <li class="active">Maybe it's time I tell 'em what I think about them</li>
</ul>
</div>

Several modifications have been made to the code. The ul has been enclosed within a div with the ID box. All CSS properties from the ID lyrics have been transferred to box, and the ul now has a relative position (relative to the enclosing box) with a negative value for the top attribute to move the text upwards and reveal the next line.

Note: A value like -120% was chosen based on preview results. For a full-page display, consider using values such as -20% or -15%.

https://i.sstatic.net/NwMtT.png I hope this guidance aligns with your needs and proves beneficial.

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

Reducing size and Assembling JavaScript and CSS

I find myself in a bit of a dilemma when it comes to using just one JS file and one CSS file for a website. Let me elaborate: As someone who mainly works with CMS platforms like Joomla, I often run into issues during page speed tests where experts recomme ...

Dividing an unordered list into two parts using HTML and CSS

Hey there, I've been struggling with this issue for two weeks now and I finally decided to ask for help. My goal is to create a new line in an unordered list inside a div. Here's what I want: Home Contact Us Education FAQ Stores Services Howev ...

Using CSS in Rails based on a certain condition

My project involves creating an app with a specific view. Within this view, I must check a condition and if it is met, I need to color a table row accordingly. While the simplest solution would be to modify the header within the view, I prefer to keep al ...

Ensure that every line of code within the button element contains all the required attributes

Struggling to get the underline on every section of my button. Attempted utilizing the -webkit-box-decoration-break property, but unfortunately it was unsuccessful. https://i.sstatic.net/jsGeQ.png It is necessary for both the initial line (New Delhi,) an ...

Is it possible for CSS to bypass an HTML element?

Summary: Can CSS be instructed to ignore an HTML element without affecting its children? The ignored element should not impact the styling of its children, treating them as if they were direct descendants of the parent. In Depth Analysis: Imagine a caref ...

Is it possible to resume CSS animation when the page is first loaded?

I'm looking for a way to ensure that my CSS animations on the elements in my header.php file continue smoothly when navigating to different pages. For example, if I have a 200-second looped animation and navigate to the "about us" page 80 seconds int ...

Is there a way to track the amount a user scrolls once they have reached the bottom of a page using Javascript?

It's a popular UI pattern on mobile devices to have a draggable element containing a scrollable element. Once the user reaches the end of the scrollable content, further scrolling should transition into dragging the outer element. For example, in this ...

The mobile version of Bootstrap v3.3.6's drop down submenu items fails to expand properly

I have been working on a website using Angular 1, Bootstrap, and CSS3. The Bootstrap navbar functions perfectly in desktop mode, but the submenu is not working in the mobile version. I am able to expand the main menu item, but the three submenus are not vi ...

Methods for verifying the device on which a web application is currently operating

After developing a web application using node.js, express, angular, and bootstrap, I noticed that the layout and forms were not displaying correctly on all devices. Specifically, when running the app on a different device, such as an iPad or laptop, the fo ...

The table borders in IE 11 do not display properly when using Html5 and CSS

In a previous version, the table had visible borders. However, when I added the property text-align:center; to my container, the table did not align to the center. I then tried adding display:inline-block; to the table, which successfully centered it. It l ...

Troubleshooting webpack issue: CSS background image fails to load

After reading numerous posts regarding CSS background images not loading, I believe my situation is unique. Here is how I've configured my webpack setup (relevant parts only): { test: /\.(png|svg|jpg|gif)$/, use: [ { ...

Safari-exclusive: Google Maps API dynamically altering page aesthetics post-loading

Recently, I encountered a peculiar problem. Upon loading a page, the text displayed with full opacity. However, upon the Google Maps API loading after 2 seconds, the entire page's styling suddenly changed. It was as if the text on the page became less ...

Improving Django/VueJS/PostgreSQL by incorporating leading and trailing whitespace tabs

Currently, I am managing a text-field in Django through Django-admin which requires maintaining white-space integrity. To achieve this, I am encapsulating it within <pre> </pre> tags for rendering with the assistance of vueJS and vue-material. ...

Guide to declaring variables using jQuery

Currently tackling a school project, I stumbled upon some online example solutions and managed to decipher most of the code. However, there is one line that has me puzzled. var $newTweet = $('<div class=tweet></div>'); My assumption ...

Creating circular borders in CSS: A step-by-step guide

Is it possible to create a circular border in CSS? Currently, the shape is square but I would like it to be circular. color: white; left: 52px; position: absolute; top: 65px; padding: 3px; background-color: rgb(0, 195, 255); ...

Navigation shadow in Bootstrap not showing up on nav.shadow

I've created a navigation bar component using vue.js and am styling it with bootstrap by including the framework in the main HTML file. Although I've added the desired styling to the navbar component, I'm facing an issue where adding the sh ...

The media query fails to start in the browser's mobile display

Instead of just adjusting the browser window size, I utilized Chrome's developer tools to make adjustments. I double-checked that the appropriate meta tags were included in the code: <meta name="viewport" content="width=device-width, initial-scal ...

Is there a way to locate ComputedStyle elements using Nokogiri?

I am currently using Ruby along with Nokogiri to parse through HTML documents. I am looking to select all nodes that match a CSS class with the style attribute display: none, but the CSS class is unknown in advance. For example: <html> <body&g ...

Adjust the starting color of a mat-input in Angular 9

I am encountering an issue with the styling of a standard matInput field within a mat-form-field, all contained within a [formGroup] div. The dark background of the parent element is causing visibility problems with the default dark text color of the input ...

Why are XAxis categories appearing as undefined in tooltip when clicked?

[When clicking on the x-axis, the values go from 0 to 1. I need the x-axis categories values to display on click event. When hovering over the x-axis value, it shows properly, but clicking on the x-axis does not show the correct values] Check out this fid ...