Tips for beginners on determining the best placement for CSS properties on a webpage

As a newcomer to CSS, I have encountered some issues when working with properties in different sections of code. For example:

P1: Please review the implementation of #menu and #menu a. The code can be found here.

P2: Also, take a look at #menu a specifically. You can find the corresponding code here.

I have noticed that applying properties such as float: right, position: relative, among others, seems to yield similar results whether placed within #menu{ } or #menu a{ }...

Now I'm struggling to determine the appropriate placement for these properties. Can someone provide me with some basic guidelines on when and where to apply them correctly? :)

Answer №1

When looking at your example above, it is important to remember to specifically target the anchor tag if you want it to function as a button.

If you choose to apply those styles to the div instead, don't forget to include display: block; for the anchor element to ensure it works properly.

It's best practice to always apply CSS directly to the element you are aiming to style.

Answer №2

Check out this simplified version of your code, designed to help beginners get comfortable with CSS through best practices...

body { 
    font-family: 'myriad pro', helvetica, sans-serif;
    color: #444;
}
a {
    color: #444;
    text-decoration: none;
}
#header {
    float: left;
    width: 96%;/*if you use box-sizing you can put 100% */
    padding: 2%;
}
#logo {
    float: left;
}
#logo h1 {
    margin: 0;
}
#logo h1 span {
    font-weight: normal;
}
#menu {
    float: right;
    margin: 0;
    list-style: none;
}
#menu a {
    border-radius: 25px;
    display: block;
    border: solid 2px #444;
    padding: 5px 20px;
    line-height: 24px;
}
#menu a:hover {
   background: #444;
   color: #fff;
}
<div id="header">
    <div id="logo">
        <h1><span>d</span>tech</h1>
    </div>
    <ul id="menu">
        <li><a href="#"><span>Get in touch</span></a></li>
    </ul>
</div>

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

How can I center a <a> vertically within a list that has varying heights?

Help Needed: Alignment Issue with Anchor Tags in List I am facing a challenge with vertically aligning the anchor tags inside my list. The number of list items varies based on the pages of my website, so I have used display flex on the list and flex: 1 on ...

Issue with a child element that is malfunctioning because of the parent element

There seems to be an issue with the child tag link not working because of the parent tag link. Is there a solution for this problem? Please provide suggestions. Here is the code snippet: <div class="d-flex flex-wrap mx-auto mb-4"> < ...

Unseen components and columns with Bootstrap 4

I am attempting to hide a checkbox input in the middle of some column elements using Bootstrap 4. <div class="container"> <div class="row"> <a class="col-2"> 1 of 2 </a> <input type="checkbox" class="invisibl ...

Tips for maintaining an open side menu

I'm having trouble keeping the side menu open when a user clicks on one of the child menu items. Ideally, I want section 1 to display section 2 content when hovered over, and keep both sections open even when section 2 is clicked. The only time they s ...

Distinguishing $(document).width() and $("html").width() in jQuery on Internet Explorer 11

There seems to be an issue with IE11... APPROXIMATED (I presume...) console.log($(document).width()); // 808px; EXACT VALUE console.log($("html")[0].getBoundingClientRect().width); // 807.30993px; ROUNDED DOWN (or to the nearest whole number) console ...

Adding external stylesheets or libraries in Express is a simple process that can greatly

Currently, I am in the process of developing a todo application using the express framework on a node server. The starting point for my application is an index.ejs file located in the same folder as my jquery.min.js file. However, when I attempt to include ...

What is the proper way to request permission for allowing others to access the mailto function?

I want to create a feature where users can open email on click using JavaScript. However, I have encountered an issue with using the mailto function in Chrome, as it requires changing the handlers settings to make it work. My query is whether it is possib ...

Internet Explorer automatically moves the cursor to the beginning of a textarea when it gains

When trying to insert "- " into an empty textarea, I am facing issues with Internet Explorer. While Firefox and Chrome work perfectly fine by inserting the text as expected, IE causes it to jump to the beginning of the textarea after insertion. Therefore, ...

Special Bootstrap's hamburger menu

My website has a vertical navigation bar with a breakpoint set for medium-sized screens to display a hamburger menu. When the hamburger menu appears, I want to hide the text following the icons to save space and only show the icons. Clicking on the hamburg ...

Instructions for adding a unique custom external CSS link, such as Bootstrap CSS, to a specific REACT component

Is it possible to incorporate custom CSS from the Bootstrap website into a React component? Since this CSS file cannot be imported directly, what steps should I take to include it? <link href="https://getbootstrap.com/docs/4.5/dist/css/bootstrap. ...

Switching the default browser for npm live-server

When I use the npm live-server package to preview my website as it changes, it keeps opening in Edge even though Chrome is set as my default browser on my system. I attempted to use the command suggested on the npm website: live-server --browser=chrome H ...

Integrate Vue.js with Laravel

Hey there fellow developers, I'm a university student who is new to using frameworks. Recently, I completed a project for my internship using the Laravel framework for the back end. Now, I want to tackle the front end using VueJS. Can anyone guide me ...

Attempting to align three sections horizontally

I'm having trouble with my current HTML code and I can't seem to figure out what the issue is. As a beginner in HTML, I am struggling to understand. My goal is to have three columns: one on the left taking up 20% of the space, one in the center t ...

Waiting for response in AngularJS Controller and setting callback

I have developed an AngularJS application with controllers.js and factories.js that I am excited about. However, I am facing a challenge when trying to manipulate the values within the controller (retrieved from the factories). The issue is that these val ...

Develop a real-time streaming service using AngularJS

I am looking to develop a website that pulls information from multiple APIs and presents it in a user-friendly format. Since I wanted to experiment with AngularJS, I opted to use it for this particular project. However, I am struggling with figuring out h ...

Traversing snapshots

My current setup involves using fancyBox, where images are wrapped in <a> tags to allow for enlargement upon clicking. However, I am facing an issue where users can navigate not only within the same panel but also between panels. Ideally, I want to l ...

Ways to optimize the spacing between the menu bar and widgets

I have successfully incorporated multiple images side by side on my blog Following is the code used for this layout: <div class="images"> <figure> <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSA7XLfGrT1rMS-Ifoguz-X2igsT ...

Implementing dynamic database content into a Wow Slider on a jQuery Mobile platform

I am currently using static data in a slider with wow slider jquery mobile. However, I would like to retrieve data from a database and display it in the slider. Can someone provide suggestions on how to do this? Thank you. <script type="text/javascri ...

Center text within a span element

I'm struggling to set up arrow navigation with both next and previous buttons that need to function in Internet Explorer 7 and newer versions. For the next button, I want the background image to appear to the right of the text. As for the previous bu ...

Components from Bower are currently not available

Trying to automate the injection of bower components into my HTML using grunt-wiredep. It seems simple enough, but I'm having trouble setting the correct path to the bower directory when running localhost. Currently, I'm receiving this error: ht ...