I am having trouble setting margins for a sticky position in a WordPress website

Can someone help me reposition the left-sidebar with a sticky top to be below the main header when scrolling down?

Adding margin-top to sticky-top under col-sm-1 is not working.

<style>
    .col-sm-1 .sticky-top { margin-top: 50px}
</style>

The length of 50px doesn't matter. The important thing is to move the div below the header.

The page's code:

<?php get_header(); ?>
<div class="container-fluid">
    <div class="row">
        <div class="col-sm-10" >50%
            <main role="main">
                <section>
                    <?php if (have_posts()): while (have_posts()) : the_post(); ?>
                        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                            <?php if ( has_post_thumbnail()) : ?>
                                <?php the_post_thumbnail(); ?>
                            <?php endif; ?>
                            <h1>
                            </h1>
                            <div class="row">
                                <div class="col-sm-1 " >50%
                                    <?php global $post; ?>
                                    <style>
                                        .logo-img2{
                                        border: 1px solid #d0d0ff;padding:20px; margin-top: 0px; margin-bottom: 0px;
                                        }
                                    </style>
                                    <style>
.col-sm-1 .sticky-top { margin-top: 50px}
                                    </style>
                                    <div class="sticky-top ">
                                        <a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo get_permalink($post->ID);?>">
                                        <div>
                                            <img  src="<?php echo get_template_directory_uri(); ?>/img/icons/facebook.png" alt="Logo" class="logo-img2"">
                                        </div>
                                    </a>
                                    <a href="https://twitter.com/share?url=<?php echo get_permalink($post->ID);?>">
                                        <div>
                                            <img  src="<?php echo get_template_directory_uri(); ?>/img/icons/twitter.png" alt="Logo" class="logo-img2"">
                                        </div>
                                    </a>
                                    <?php $pinterestimage = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); ?>
                                    <a href="http://pinterest.com/pin/create/button/?url=<?php echo urlencode(get_permalink($post->ID)); ?>&media=<?php echo $pinterestimage[0]; ?>&description=<?php the_title(); ?>">
                                        <div>
                                            <img  src="<?php echo get_template_directory_uri(); ?>/img/icons/pinterest.png" alt="Logo" class="logo-img2" >
                                        </div>
                                    </a>
                                        <div class=" logo-img2" style="padding: 0px; text-align: center">
                                            <ul class="list-inline">
                                                <li class="list-inline-item">
                                                    <a href="#" data-toggle="tooltip" title="Default tooltip">Tooltip</a>
                                                </li>
                                            </ul>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-sm-11" >
                            <span class="date"><?php the_time('F j, Y'); ?> <?php the_time('g:i a'); ?></span>
                            <span class="author"><?php _e( 'Published by', 'html5blank' ); ?> <?php the_author_posts_link(); ?></span>
                            <span class="comments"><?php if (comments_open( get_the_ID() ) ) comments_popup_link( __( 'Leave your thoughts', 'html5blank' ), __( '1 Comment', 'html5blank' ), __( '% Comments', 'html5blank' )); ?></span>
                            <?php the_content(); ?>
                            <?php the_tags( __( 'Tags: ', 'html5blank' ), ', ', '<br>'); ?>
                            <p><?php _e( 'Categorised in: ', 'html5blank' ); the_category(', '); ?></p>
                            <p><?php _e( 'This post was written by ', 'html5blank' ); the_author(); ?></p>
                            <?php edit_post_link(); ?>
                            <?php comments_template(); ?>
                                </div>
                        </article>
                    <?php endwhile; ?>
                    <?php else: ?>
                        <article>
                            <h1><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h1>
                        </article>
                    <?php endif; ?>
                </section>
            </main>
        </div>
        <div class="col-md-2" style="background-color:orange;height: 100%; width: 100%;">50%
            <?php get_sidebar(); ?>
        </div>
    </div>
</div>
<?php get_footer(); ?>

Some lines that were not deleted are still visible as <!--. Apologies for that.

You can view the content at this link:

Answer №1

Welcome to Stack Overflow!

If you have set the position to sticky, remember to specify a top value, like in your case where you should use top: 60px instead of using margin

.col-sm-1 .sticky-top {
    /* margin-top: 50px; */
    top: 60px;
}

The distinction between static or relative positioning and absolute or fixed positioning lies in how they affect the document flow. Elements with static & relative positions retain their natural space in the flow of the document, while those with absolute & fixed positions do not — their space is removed and they behave more like floating elements.

Find more information on this topic here

Avoid using default framework classes for targeting specific classes/making them selectors

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

Error message indicating that the jQuery script has not been properly defined in a Wordpress environment

Seeking assistance with a JavaScript issue on my WordPress site. I have added an external JS file but it's not functioning properly and throwing errors in the code below. Any help to resolve this would be greatly appreciated. var clock; jquery(docu ...

How to animate a left border shifting to the center using JavaScript

As I'm modifying my current div, I need to add a vertical line in the center of it. I've come across various solutions where a left border is added and then shifted using the left property by 50% (which effectively places it in the middle). Here ...

Storing historical data of file uploads on user accounts using PHP

I need a solution to keep track of the files uploaded by users on my website using PHP sessions. My goal is to allow users to see a list of their uploaded files once they log in to the designated page for file uploads. Can this be achieved using only PHP ...

What's the best way to get rid of the one-pixel gap between these elements on high-resolution displays like

http://jsfiddle.net/36ykrp9x/5/ HTML <div class="container"> <button>O</button><button>O</button> </div> CSS .container { width: 100%; background-color: rgb(120, 200, 200); text-align: center; } butt ...

Leveraging JQuery animation to manipulate the spinning of HTML jackpot

I have been using the following code to simulate a spinning wheel for users. It is functional, but I am facing an issue where the rotation stops abruptly at a specific circle before applying the style for the winning circle. My query is: Is there any way ...

Using jQuery to retrieve the tag name of the selected element

What is a simple method for retrieving a tag name? For instance, if I input $('a') into a function, how can I extract the tag name 'a'? ...

Testing tools for ensuring the functionality and design of the mobile version of a

Is there a way to preview how my website will appear on mobile before it goes live? Most tools I've come across only work for hosted sites. ...

Exploring the World of WordPress Search

I have a searchform.php file and a search.php file. Inside the search.php file, I have the following code: <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div class="row"> <div class= ...

Organize icons within a container that spans the entire height

I am looking to organize multiple icons within a container that spans the entire height of the website, rather than just the viewport. Currently, I am only able to achieve the height of the viewport using the following code: .full-container { posit ...

Attempting to modify the chosen indicator symbol in a multiselect dropdown using bootstrap-select

The current icon that I'm considering changing Perhaps to something similar in the same library but different Currently working on a multi-select dropdown using bootstrap-select and struggling with the checkmark design, although it is important to i ...

In the WordPress REST-API, only the "get" method is permitted

I recently installed the REST-API plugin on my WordPress site and have successfully been able to retrieve posts using the following endpoint: mywp.org/wp-json/wp/v2/posts However, I have encountered a restriction where only "get" requests are allowed. If ...

Obtain the rotational value in 3D CSS using JavaScript by extracting it from the matrix3d()

My element has been 3D transformed in the following way: .foo { transform: rotateX(-30deg) rotateY(35deg); } Now, I am looking to retrieve these values using JavaScript. Extracting the 3D matrix is simple: var matrix = $('.foo').css('tr ...

The div element is experiencing a resizing issue

I am currently working on the following HTML markup: <div class="card" style="height:100%"> <div class="controlPanelContainer"> <div class="card" style="background-color:yellow;height:200px"> </div> <div class= ...

What's the best way to preserve the aspect ratio of my background image while utilizing background-size: cover;?

I have a background image that I'm working with. Is there a way to preserve the aspect ratio of the image even when using background-size: cover;? Currently, the image fits perfectly on the screen but appears slightly distorted. I've experimented ...

Side panel with collapsible Bootstrap functionality

The accordion panel is structured as follows <p> <a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample"> Link with href </a> </p> <di ...

Adjust the placement of image when changing the size of the window

Is there a way to prevent an image with a fixed position from covering up page content when the browser window is resized? <div > <img id="img1" class="wrapperImage"></img> </div> <style type="text/css"> .wrapperImag ...

It's impossible to center text in Bootstrap 5

Creating a website for my school project has been challenging, especially when it comes to centering text. I have been struggling to position the text in the middle of the background picture despite trying various methods. Here is the HTML code: pastebin ...

Could you please guide me on resolving the Blazor/Bootstrap 5 CSS problem related to styling "striped" tables?

Looking for a solution to fix a CSS issue involving Bootstrap 5 "table-striped" and displaying Blazor Razor components. In my Blazor/Bootstrap 5 page, I have a table displaying content fetched from a service at runtime. Initially, the Bootstrap CSS is vis ...

Swapping values between HTML tables and arrays with the power of JavaScript

I have a unique table structure that I need help with: https://i.sstatic.net/fr7oJ.png My current table has 2 rows and multiple columns, but I want to change it to have 2 columns and multiple rows like this: https://i.sstatic.net/uhkp9.png To create th ...

Implement the Bootstrap datetimepicker functionality for the specified div element

The date picker successfully functions when I assign the id to the input tag as shown below: <div class="col-sm-4" id="datepicker"> <input type="text" class="form-control" id="inputDate" placeholder="Date"> <span class="glyphicon gl ...