Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9026443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:31:14+00:00 2026-06-16T06:31:14+00:00

Using only JavaScript (no jquery) – I want to make it so when a

  • 0

Using only JavaScript (no jquery) – I want to make it so when a list item has a class of ‘active’ that it be centered inside its container.

The final result will be a carousel where the ‘active’ li will be centered on the page and clicking on another li will make it active and then it will auto center (scroll). The problem I’m having is that I can’t seem to get it to center to start with. The width is 100% of the page so that the width is dynamic.

Any and all help would be great.

html:

<div id="container">
    <ul id="carousel">
      <li>0</li>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li class="active">4</li>
      <li>5</li>
      <li>6</li>
      <li>7</li>
      <li>8</li>
      <li>9</li>
    </ul>
  </div>

css:

html, body {margin:0;padding:0;height:100%;width:100%;}
ul{
  position:relative;
  width:100%;
  height:322px;
  margin:0 auto;
  padding:0;
  overflow-x:hidden;
  white-space: nowrap;
}
li{
  position:relative;
  display:inline-block;
  padding:0;
  margin-right:20px;
  list-style-type:none;
  width:200px;
  height:200px;
  top: 70px;
  background-color:#efefef;
  border:1px solid #d3d3d3;
  border-radius: 12px;
}
li:first-child {
  margin-left: 20px;
}
li.active {
  top: 20px;
  width:300px;
  height:300px;
}

javascript:

var r = document.getElementById('carousel').offsetWidth;
var l = document.getElementById('carousel').scrollWidth;
var m = r / 2;
document.getElementById('carousel').scrollLeft = 0;
document.getElementById('carousel').scrollLeft = m;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T06:31:15+00:00Added an answer on June 16, 2026 at 6:31 am

    You must subtract the half of the width of the active <li> and the space between the previous <li> from m

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a WordPress theme that only has about 10 lines of jQuery, yet
I have a javascript external file that has a link. Using only javascript, how
I have one page website only using HTML, CSS and JavaScript. I want to
I have built up a website without any server side scripting/language, using only HTML/JavaScript/jQuery.
Using only JavaScript and framework like dojo/jQuery... Is there a way to find the
I want to send a JSON object to facebook, but only using javascript/ajax i.e
I want to pull a value using ONLY javascript from another page. I tried
I wonder how I can write the jQuery is method using only javascript? You
Is it possible to overwrite CSS file permanently using JavaScript / jQuery? I want
I am trying to create a form using only JavaScript. I mean create a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.