Incorporating hyperlinks within the navigation buttons

I'm working on a design where I have six buttons that need to be displayed on top of a background image. How can I ensure that the text inside the buttons stays contained within them? Here is the current code structure I am using, which involves utilizing a list method for simplicity:

ul
{
    padding:0px;
    margin:0px;
    width:510px;
    float:left;
    list-style:none;
    background-image: url(http://www.returngis.net/wp-content/uploads/pics/demos/grass.jpg);
    z-index:150;
}

ul li
{
    width:50px;
    height:100px;
    padding:20px;
    float: left;
    display:inline-block;
    z-index:100;
}
<ul>
    <li><img src="http://via.placeholder.com/350x150" width="50" height="100" />LINK1</li>
    <li><img src="http://via.placeholder.com/350x150" width="50" height="100" />LINK2</li>
    <li><img src="http://via.placeholder.com/350x150" width="50" height="100" />LINK3</li>
    <li><img src="http://via.placeholder.com/350x150" width="50" height="100" />LINK4</li>
    <li><img src="http://via.placeholder.com/350x150" width="50" height="100" />LINK5</li>
</ul>

Answer №1

To create clickable text links, wrap them in anchor tags and adjust their positioning by using negative margins. Here is an example:

ul
{
    padding:0px;
    margin:0px;
    width:510px;
    float:left;
    list-style:none;
    background-image: url(http://www.returngis.net/wp-content/uploads/pics/demos/grass.jpg);
    z-index:150;
}

ul li
{
    width:50px;
    height:100px;
    padding:20px;
    float: left;
    display:inline-block;
    z-index:100;
}

ul li a {
    display: block;
    margin-top: -64px;
}
<ul>
    <li><img src="http://via.placeholder.com/350x150" width="50" height="100" /><a>LINK1</a></li>
    <li><img src="http://via.placeholder.com/350x150" width="50" height="100" /><a>LINK2</a></li>
    <li><img src="http://via.placeholder.com/350x150" width="50" height="100" /><a>LINK3</a></li>
    <li><img src="http://via.placeholder.com/350x150" width="50" height="100" /><a>LINK4</a></li>
    <li><img src="http://via.placeholder.com/350x150" width="50" height="100" /><a>LINK5</a></li>
</ul>

Answer №2

Web Development

<ul>
    <li>Link One</li>
    <li>Link Two</li>
    <li>Link Three</li>
    <li>Link Four</li>
</ul>

Styling with CSS

ul
{
    padding:0px;
    margin:0px;
    width:510px;
    float:left;
    list-style:none;
    background-image: url(http://www.example.com/images/bg.jpg);
    z-index:150;
}

ul li
{
   background-image: url(http://via.placeholder.com/350x150);
   background-size: 100% 100%;
    width:50px;
    height:100px;
    padding:20px;
    margin: 10px;
    float: left;
    display:inline-block;
    z-index:100;
}

Check out the code on JSFIDDLE: https://jsfiddle.net/d3Lw4hjq/

Answer №3

You have the option to absolutely position the image within the list item and modify the padding on the list item to margin. Then, utilize display: inline-flex; justify-content: center; align-items: center; to precisely position the text above the image.

ul
{
    padding:0px;
    margin:0px;
    width:510px;
    float:left;
    list-style:none;
    background-image: url(http://www.returngis.net/wp-content/uploads/pics/demos/grass.jpg);
    z-index:150;
}

ul li
{
    width:50px;
    height:100px;
    margin:20px;
    z-index:100;
    position: relative;
    float: left;
    display: inline-flex;
    justify-content: center;
    align-items: center;
}

ul li img {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0; left: 0;
  z-index: -1;
}
<ul>
    <li><img src="http://via.placeholder.com/350x150"/>LINK1</li>
    <li><img src="http://via.placeholder.com/350x150"/>LINK2</li>
    <li><img src="http://via.placeholder.com/350x150"/>LINK3</li>
    <li><img src="http://via.placeholder.com/350x150"/>LINK4</li>
    <li><img src="http://via.placeholder.com/350x150"/>LINK5</li>
</ul>

Answer №4

To display the images, you have two options. First, you can set the images as the background of the li's:

<ul>
    <li>LINK1</li>
    <li>LINK2</li>
    <li>LINK3</li>
    <li>LINK4</li>
    <li>LINK5</li>
</ul>

For the CSS styling:

li { background-image: url('http://via.placeholder.com/350x150'); }

Second option is to absolutely position the images within the list items:

ul li
{
    position: relative;
}

ul li img {
  position: absolute;
  z-index: -1;
}

View example here: https://jsfiddle.net/v78501mn/

Note that if your li's have "display: inline-block", using "float: left" is not necessary.

Answer №5

Wow, I had no idea you could change the img-tag's behavior with extra css code! That's awesome! I'm still learning, but I can see how helpful this is now. Thanks a lot!

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

Troubleshoot: Problem with iPhone Orientation on Bootstrap Framework

While working on a responsive website design, I encountered an issue that needs addressing. Initially, switching from portrait mode to landscape didn't trigger the expected responsive behavior; instead, it simply zoomed into the site. To prevent this ...

Mobile Safari on iOS devices is known for its capability to overlay HTML5 <video> on top of everything

Although a similar question was asked 6 years ago without any answers, I am currently facing the same issue. Problem: The <video> tag in my Angular/Ionic application overlaps my image and two buttons in iOS Mobile Safari - no matter what I try! It ...

Create a distinct CSS animation 'origin' declaration for every <g> element

I am using CSS animation to apply a 'wobble' effect to each letter within a word. The letters are represented by SVG groups <g>. However, in the provided example, the wobbling effect becomes more pronounced with each subsequent letter, wher ...

Unable to transmit a variety of text or variables via email through PHP

Can someone assist me in figuring out how to send more content in an email? I have been attempting to include additional information in the email, but haven't been successful. I am using ajax to call and send the email, however, only basic emails seem ...

The text sliding feature gradually moves further away from the left side with each iteration

I am in the process of transferring an existing slider from one website to another. Instead of creating a text slider from scratch, I decided to modify the code I found for the new site. However, the slider is not functioning correctly on the new site. Th ...

Is there a way to alter the color of options within a select tag when hovering the mouse over them

After searching through multiple websites, I couldn't find a solution on how to change the option background color on hover from blue to green. The screenshot provided below illustrates what I am requesting for in a clearer manner. I attempted to use ...

Creating a List with Sublists that are displayed when hovering over the parent List is a key element of effective design

Hovering over 'View Rows' should open up both New Records and Old Records <div> <li>Add Rows</li> <li>DeleteRows</li> <li>View Rows <ul> <li>View New Records</li ...

Tips for creating a dropdown that autocompletes similar to the one found in Google Chrome

Looking to create a unique custom drop-down autocomplete suggestion list using HTML and CSS, similar to Chrome's design (shown below). In my attempts to achieve this, I utilized an absolutely positioned table beneath the input element. I applied vert ...

Using requestAnimationFrame to animate several independent objects

I'm currently working on animating two different objects: a square and a circle. Each object has its own button to initiate the animation, which involves moving the object from left to right. However, I've run into an issue where clicking on one ...

Click X button to toggle and close the dropdown menu

After successfully creating a responsive menu that toggles with a hamburger icon at 1205px resolution, I encountered an issue. Once the hamburger icon is clicked and the dropdown menu opens, there seems to be no way to close it. I am looking to add an X bu ...

Overflow issues with flexbox design on mobile devices

Hello, I'm currently working on creating a sliding panel of images. While I have successfully implemented it, I am encountering some browser compatibility issues. The sliding panel functions perfectly on Chrome desktop, however, when viewed on mobil ...

The importance of CSS style prioritization

I can't seem to figure out CSS declaration priority. I have an external CSS file with a rule and some inline CSS declarations that are supposed to override it. According to my understanding, inline styles should take precedence over external CSS rules ...

The edges of the cube are not joined together

Looking for a 3D transform cube that can freely flip left and right, with the button fitting into it perfectly. I've found the CSS code to achieve this, but when I set the width of the ".cube" below 200px, the borders of the cube don't connect. I ...

Tips for positioning text on the left and right sides of a div in HTML styling

I am struggling with positioning two pieces of text within a div. Despite having some styling already in place, the text is currently displaying one after the other on the left-hand side. I want to position one piece of text to the left and another to the ...

My HTML and CSS design is not displaying correctly on mobile devices

Currently, I am in the process of utilizing a free website template. However, I am facing an issue with adjusting the background image. Is there anyone who can provide some guidance on resolving this particular problem? I would be happy to share the code ...

Utilizing checkboxes to toggle the visibility of a div element with varying headings

By toggling a checkbox, I aim to show or hide the same div with a different heading. $('#cbxShowHide').click(function() { this.checked ? $('#block').show(1000) : $('#block').hide(1000); }); #block { display: none; bac ...

"The 'BillInvoice' object must be assigned a primary key value before it is ready for this relationship" - Error Message

There is an issue in my Django project related to the creation of a BillInvoice and its associated BillLineItem instances. The error message I'm receiving states: "'BillInvoice' instance needs to have a primary key value before this re ...

Stylish dropdown menu design inspired by W3CSS website

I recently implemented a CSS dropdown menu example that I found on a website, but it's not working with my code. I double-checked for typos but couldn't find any. Even when I tried to directly copy the code and replace the content with my own, i ...

The CSS Scroll-Snapping feature seems to be overlooking one specific element that it should be snapping to

Currently, this website is designed to work exclusively for mobile devices. When viewed in Chrome's mobile view using the inspector, everything appears to be functioning correctly. However, when accessed through Safari or on an actual mobile phone, th ...

Elegant rounded bordered sidebar design to highlight selected navigation items using HTML and CSS

I am looking to replicate the sidebar design displayed in the image below by using HTML and CSS. While I have successfully rounded the borders on the left side of a selected link, I am stuck when it comes to rounding the borders on the right side. ...