What is the best way to apply a background color to text seamlessly? (Using HTML5 and CSS3)

   <!-- 6장 연습문제 4 -->
<html lang = "ko">
    <head>
        <meta charset = "UTF-8">
        <style>
        img{margin-left: auto; margin-right: auto; display: block;}

        .hyper{
            text-decoration-line:none; 
        }

        .text{
            border : 1px solid black;
            text-align: center;
            color:black;
        }
        .text:hover{
            background-color:yellow;
            color: red;
            text-decoration-line:none;
        }
        </style>
    </head>

    <body>
        <img src = "images/book.png" >
        <a href = "http://naver.com" class = "hyper">
        <p class = "text">[책 자세히 보기]</p> </a>

    </body>
</html> 

I'm facing an issue where the background color is extending beyond the text when hovered over. I tried using a border to contain it, but it didn't work as expected. Can anyone provide a solution to this problem?

Answer №1

Include a new div and place your <p> tag inside it. Set the text-align attribute for the div.

<html lang="ko">
    <head>
        <meta charset="UTF-8">
        <style>
            img{margin-left: auto; margin-right: auto; display: block;

            }
            .hyper{
                text-decoration-line:none;
            }

            .text{
                display: inline-block;
                border : 1px solid black;
                color:black;

                vertical-align: middle;


            }
            .text:hover{

                background-color:yellow;
                color: red;
                text-decoration-line:none;
            }

            div {
                text-align: center;
            }

        </style>
    </head>

    <body>
    <img src="images/book.png">

    <div>
        <a href="http://naver.com" class="hyper">
            <p class="text">[Click here to learn more about the book]</p>
        </a>
    </div>

    </body>
    </html>

Answer №2

Verify the dimensions of the text class element.

The block element is set to occupy 100% of the width.

To address this, use display: inline-block.

By default, the text is a p element with a block attribute.

The width is currently at 100%.

Inspect the text element for display: inline-block,

Experiment with centering using text-align: center

Answer №3

The second response is effective because utilizing display:inline-block will impact the space of the associated div element.

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

Is there a way to conceal the scrollbar while still permitting scrolling?

I'm interested in creating a custom scrollbar, but in order to do so I need to hide the default scrollbar while still allowing scrolling functionality. I've attempted: overflow-y:hidden; but this method hides the scrollbar and prevents scrolli ...

Issues regarding the sizing of fonts on mobile devices

Struggling to create a responsive site that looks good on high dpi mobile devices? You're not alone. No matter how you adjust your h2 and p text sizes, they seem to be the same size when viewed in portrait mode on certain devices like the Galaxy S4. E ...

Tips for inserting information into HTML components in JSP

Hey everyone, I'm diving into JSP for the first time and I'm trying to wrap my head around how to display data from an ArrayList in my HTML. Can someone help me out? I'm thinking of doing something like this: <article> <p>< ...

Is there a way to display the output of jQuery in an input box rather than a span?

I need to display the result of this function in an input box instead of a span tag because I plan to utilize the result in the following form. function checkChange(){ $.ajax({ url: "ordercalculate.php", data: $('form').seria ...

Display a limited number of characters along with a button on the blog tag pages. Clicking on the button will reveal the complete text description

Hey, I am looking to replicate the way WooCommerce tags descriptions are displayed on my WordPress blog. I want it to show only a certain number of characters with a "show all" link, similar to how it appears on this site: I have found some code that work ...

Triangle-shaped image mapping and interactive hover effects

I am attempting to create a simple hover effect using two triangles. One triangle should start below the other, and upon hovering, it should appear above the first triangle. The problem I am encountering is that the surrounding boxes are obstructing the e ...

An effective approach to positioning HTML elements at specific X and Y coordinates

I have an innovative project idea! The concept is to enable users to create points by clicking on the display. They can then connect these points by clicking again. However, I am facing a challenge when it comes to creating HTML elements at the exact loc ...

Tips for concealing overflow x on a responsive website

Could use some help here. Whenever I switch to responsive mode, the overflow x feature gets enabled and I'm at a loss on how to disable it. I've attempted using @media to disable overflow but unfortunately it's not working as expected. ...

Getting URL parameters dynamically without reloading the page

Here's a particular issue I'm encountering: I've implemented the following function to fetch the GET Parameters from a URL. $(document).ready(function() { function getParam(variable) { var query = window.location.search.sub ...

How can I verify if an SVG file has a reliable source? (having troubles converting SVG to canvas)

While attempting to use an SVG generated by a chart plugin (https://wordpress.org/plugins/visualizer/), I am facing issues retrieving the source of the SVG-image being created. Interestingly, when using other SVGs with the same code, everything works perfe ...

The grid is evenly populated with icons

Is it possible to evenly distribute the icons by only using CSS? I'm aiming to achieve a result similar to this (unfortunately, I can't post images due to my lack of reputation). You can find an example solution that uses JavaScript here: _htt ...

An HTML form featuring two unique submit style buttons designed for triggering two separate PHP actions or files

I'm facing a challenge with my HTML file upload form that sends files to a database using PHP. The current setup allows users to upload a file, preview the data in an HTML table, and then submit it to the database. However, I need to merge the preview ...

endless cycle of scrolling for div tags

My goal is to incorporate a tweet scroller on I believe it uses the tweet-scroller from Unfortunately, this link seems broken as the demo is not functioning. I searched for an alternative solution and came across http://jsfiddle.net/doktormolle/4c5tt/ ...

Alter the URL path with the help of jQuery

I developed a website with content in two different languages, each stored in separate folders named en and bn. My goal is to toggle between these folders when clicking on an href element using jQuery. <a class="eng_link " style="" href="#"> English ...

Ways to conceal just a portion of the bootstrap sidebar?

I'm looking to create a sidebar using 'vue-bootstrap' that can be hidden when clicked, but I only want to hide 80% of the width of the sidebar. How can I achieve this effect of partially hiding the sidebar? <b-button v-b-toggle.sidebar-1 ...

Centered alignment with floating divs of abbreviated length

My HTML file has a structure similar to the following: <div style="float:right"> A line of text, floating to the right </div> <div style="text-align:center"> And here, several lines of<br /> text which should be centered. </div& ...

Vuejs dropdown selections are not working as expected due to a bug

My dropdown menu is being populated with options based on an API response that looks like the following: {"value":"1371","label":"apple"},{"value":"1371","label":"banana"},{&qu ...

The <hr> tag is not displaying properly within the <option> element

I'm facing an issue with a selection element in my Vue project <div class="resultContainer"> <section> <option class="resultBtn" v-for="exchange in finalExchanges"> {{exchange}} <hr></option> </section> ...

What is the method for reverting style properties back to their CSS defaults using Javascript?

The php-generated page contains multiple elements structured like this: <td class="defaultTDStyle" style="color:userDefinedCustomColor" id="myTDId"></td> There is a default style in place with additional styles ap ...

The jQuery bxSlider is failing to function properly in a responsive design

Anyone else experiencing issues with the jQuery bxSlider not functioning properly on a responsive layout? It seems to be working fine on larger screens. $(document).ready(function () { $('.slider1').bxSlider({ pagerCustom: '#bx-pager&apos ...