Styling of CSS class not applied when displayed following an <hr> element

I am facing an issue with styling links on my webpage. I have a list of links to different websites, each followed by a brief description. To ensure the link text is slightly larger than the description, I created a CSS class called a.link with a font-size of 16px. The list is contained within a standard <p> tag and separated by <hr> tags between each item. However, I noticed that only the first link in the list retains the new font size, while all subsequent links lose not only the styles defined in the link class but also those in the <p> tag. Removing the <hr> tags results in correct styling of all items, as does wrapping each link in its own <p> tag, although I prefer to avoid cluttering the list with extra tags if possible.

Below is the snippet of my stylesheet code:

#pane {
    float: right;
    width: 800px;
}

#pane p {
    margin-left: 15px;
    font-family: verdana;
    font-size: 12px;
}

#pane p a.link { font-size: 16px; }

And here is an example of the HTML generated:

<div id="pane">
<p>
    <a href="http://www.google.com" class="link">Google</a> - For finding stuff
    <hr>
    <a href="http://www.newegg.com" class="link">Newegg</a> - For buying stuff
    <hr>
</p>
</div>

Has anyone else experienced this issue and discovered a solution without adding <p> tags everywhere?

Answer №1

It is not possible to place an <hr> tag within a <p> tag as the browser will automatically close the <p> tag for you. This results in your CSS classes no longer being applied to the second link.

The browser corrects your code by transforming it into the following, thereby causing your CSS rules to become ineffective:

<div id="pane">
<p>
    <a href="http://www.google.com" class="link">Google</a> - For finding stuff
    </p><hr>
    <a href="http://www.newegg.com" class="link">Newegg</a> - For buying stuff

<p></p>
</div>

Answer №2

Is it really necessary to enclose this information within a paragraph? If not, you can eliminate that tag and reduce the clutter. Check out this link for more

<div id="pane">
    <p>
        <a href="http://www.google.com" class="link">Google</a> - A tool for search
    </p>
        <hr>
    <p>    
        <a href="http://www.newegg.com" class="link">Newegg</a> - Your shopping destination
    </p>    
        <hr>

    </div>

Answer №3

hr can't in p tag. you can switch p tag to div

<!doctype html>
<html>
<head>
<style>
#pane {
float: right;
width: 800px;
}

#pane div {
  margin-left: 15px;
  font-family: verdana;
  font-size: 12px;
}

#pane div a.large    { font-size: 26px; }
</style>
</head>
<body>
<div id="pane">
<div>
    <a href="http://www.google.com" class="large">Google</a> - To search for things
    <hr/>
    <a href="http://www.newegg.com" class="large">Newegg</a> - For purchasing items
    <hr/>
</div>
</div>
</body>
</html>

Answer №4

An unordered list (UL) enclosed within a NAV may be more appropriate for organizing a collection of links.

The issue arises from placing the HR tag inside a P tag, which does not pass validation according to the W3C validator. To illustrate this problem, I utilized the following sample code:

<!doctype html>
<html>
<head>
    <title>Test</title>
</head>
<body>
<div id="pane">
<p>
    <a href="http://www.google.com" class="link">Google</a> - For searching
    <hr>
    <a href="http://www.newegg.com" class="link">Newegg</a> - For shopping
    <hr>
</p>
</div>
</body>
</html>

In HTML, the end tag of a p element may be omitted if it is immediately followed by an ...HR...

Source: http://www.w3.org/TR/html-markup/p.html#p

As highlighted by @Bill, this behavior causes the parser to prematurely close the P tag, resulting in unexpected output.

The HR tag is intended to represent a thematic break according to W3C specifications, making its use within a paragraph semantically incorrect. Typically, a paragraph should encapsulate a coherent set of ideas on a single topic, aligning with standard conventions.

Answer №5

Great answer regarding the <p>, <hr> It is recommended to avoid using the <p>.

To style the a.link as display:block, allowing it to be displayed similarly to the surrounding <p> tag.

Don't forget to auto-close your <hr/>

Ensure that your CSS selector is accurate: #pane > a.link if you really need that level of specificity. It may be better to style a.link on its own, which allows for easier modification with another selector later without running into issues related to specificity.

Answer №6

It seems like there might be an issue with your HR tag. In XHTML, it's important to close your tags properly. So your HR tag should be written as <hr/>.

    <div id="content">
<p>
    <a href="http://www.amazon.com" class="link">Amazon</a> - Great for shopping
    <hr/>
    <a href="http://www.ebay.com" class="link">eBay</a> - Place for auctions
    <hr/>
</p>
</div>

Once you make that change, everything should work smoothly without any issues.

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

remove a row from a table within a Firestore database

I'm currently working on implementing a button to delete a specific row in my table from the Firebase database, but I'm uncertain about how to achieve this using a button function. My code is written in JavaScript and utilizes Firestore. Below i ...

Different results are being obtained when destructuring props in a component

Just diving into the world of React and trying to grasp destructuring. I've been doing some reading on it, but I'm currently stuck. When I try to destructure like this function MList({action}) { // const data = [action];}, I only get 'camera ...

Developing with Phonegap Build: A Guided Process

With all the conflicting information available, I am seeking clarity on this topic. Objective: To create and enhance a Phonegap app using Phonegap Build. 1) My preference is to utilize Phonegap Build without needing to install Android and iOS SDKs. 2) I ...

Is there a way to determine if a browser supports the offline event?

I attempted to implement the code from this Stack Overflow question: How can I check for support of the `focusin` event? However, in Chromium, the method hasEvent('offline') returns false even though it supports the `offline` event. Does anyone ...

The tooltip of the Material UI IconButton is displaying incorrectly

Within my ReactJS application, I am utilizing Material UI along with react-bootstrap-table components. One particular cell in my application utilizes the Material UI IconButton as shown below: <IconButton tooltip={t('tooltips:editRegulation&apos ...

Modify css with php instead of using FTP

As I work on constructing a website, I wonder how WordPress allows its admin users to edit CSS styles through the website instead of accessing the css file directly via FTP. How exactly does WordPress load the CSS file? My assumption is that it somehow re ...

The CSS :first-child selector appears to be malfunctioning with the majority of elements

Having some difficulty with the :first-child selector. It just won't cooperate in my code. The goal is to execute a simple transform on two elements using the :first-child, but it's failing in multiple instances. Let me explain: First scenario: ...

What is the best way to utilize CSS relative positioning for aligning two rows of information on a webpage?

What is the best way to use relative positioning to show two lines of information on an image without the order of information interfering with each other? I want the Files badge to appear at the bottom of the image, with the MusicBrainz and Discogs badge ...

Dynamic stacking of buttons in response to user navigation choices

Is it possible to create a navigation bar with 5 buttons using CSS and JS? Here is what I am looking to achieve: Display 5 nav/button options When a user clicks on a button, the other buttons should transition or hide, leaving only the selected button vi ...

Ensuring Your IMG is Perfectly Centered in DIV Across all Browsers

I have tried all the tips from the top Google results, but I am still struggling to center an image within a div. For example, the popular tricks mentioned in this link: or http://www.w3.org/Style/Examples/007/center.en.html do not seem to work in IE 8. ...

What could be causing the issue with the padding-top not being applied to my section?

I'm having trouble getting the desired 150px padding to appear on the top and bottom of a section in my project. Here's an example: Below is the code for the section: .wp-block-mkl-section-block .section-bg { position: absolute; top: ...

Is it feasible to capture a screenshot of a URL by using html2canvas?

Is it possible to take a screenshot of a specific URL using html2canvas? For example, if I have the following URLs: mydomain.com/home mydomain.com/home?id=2 mydomain.com/home/2 How can I capture and display the screenshot image on another page? window ...

Issue with scroll down button functionality not functioning as expected

Is there a way to create a simple scroll down button that smoothly takes you to a specific section on the page? I've tried numerous buttons, jQuery, and JavaScript methods, but for some reason, it's not working as expected. The link is set up co ...

Make sure the navigation bar is fixed to the top of the page only

Looking for a solution to stick the navigation menu to the top of the screen only when the screen is wider than 782px. Progress so far: https://jsfiddle.net/ah4ta7xc/1/ Encountering an issue where there is an unintended gap at the top when applying the s ...

Reposition the div element on mobile devices using media queries

Hello, I'm facing an issue on mobile with my website: dev site Today, I implemented a dropdown menu with flags to allow language switching. However, the dropdown in mobile is appearing under the hamburger nav bar. I was thinking of using media querie ...

What could be the reason for the CSS property of margin-right not being applied in the HTML code?

New Code: table { border-radius: 3px; margin-top: 5px; margin-left: 5px; margin-right: 5px; } <div class="table-responsive-md"> <table class="table table-striped table-bordered table-hover table-sm text-light"> <caption> ...

Require this form to be centered flawlessly

<form class="form-horizontal" role="form" id="contactf" action="http://titan.csit.rmit.edu.au/~e54061/wp/form-tester.php" method="post"> <div class="form-group"> <label class="control-label col-sm-2 lb" for="email">Email : </ ...

Unable to insert text into JEditorPane using text/html format

In my class, I am using a JEditorPane to display text that needs to support HTML. When I try to add text to the pane by setting the content type to HTML and then using setText(), nothing appears on the pane. The issue seems to be with setting the content ...

Passport and Node.js team up to create powerful user group functionalities

Seeking advice on this topic. I am interested in setting up a page with a login form as the main page. Upon logging in, users would be directed to their personalized dashboard. However, if someone logs in with admin privileges, they should be redirected t ...

Modifying SVG HTML5 pattern attributes for a particular instance or use case

Having a SVG Pattern that serves as the fill for multiple other SVGs presents a challenge. Any changes made to the pattern attributes will reflect in all SVGs using it. Is there a way to customize the pattern attributes for a specific instance/use only? ...