I am using a PHP include that features my header content. For my navigation section I am using a list which just features text. I am using a small bit of php on the list items which basically states whatever the current page is, have an image displayed over that list item within the navigation. Below is what I am 'trying' to achieve:
As you can see, I want that blue shaped image placed over the top of the current page list item.
But below is what I have achieved and you will notice that the image is displayed but not like it should. I have used absolute positioning on the .current class which displays the whole image, however I cannot use absolute position on the image because then it wont display properly on another list item. What can I do?
Below is the relevant CSS and HTML/PHP:
<div id="navbar">
<ul>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="current"';?> href="#">Home</a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'shows.php')) echo 'class="current"';?> href="pages/shows.php">Shows</a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'classes.php')) echo 'class="current"';?> href="pages/classes.php">Classes</a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'noticeboard.php')) echo 'class="current"';?> href="pages/noticeboard.php">Notice Board</a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'about.php')) echo 'class="current"';?> href="pages/about.php">Our Story</a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'contact.php')) echo 'class="current"';?> href="pages/contact.php">Contact</a></li>
</ul>
.current {
width:138px;
height:57px;
background-image: url('../images/current.png');
background-repeat: no-repeat;
}
the <li> element is restricting the size of your <a> tag.
set the size of the individual <li> items to match the size of your tag and you will see the entire thing.
Modified code:
<div id="navbar">
<ul>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'index.php')) echo 'class="current"';?> href="#">Home</a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'shows.php')) echo 'class="current"';?> href="pages/shows.php">Shows</a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'classes.php')) echo 'class="current"';?> href="pages/classes.php">Classes</a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'noticeboard.php')) echo 'class="current"';?> href="pages/noticeboard.php">Notice Board</a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'about.php')) echo 'class="current"';?> href="pages/about.php">Our Story</a></li>
<li><a <?php if (strpos($_SERVER['PHP_SELF'], 'contact.php')) echo 'class="current"';?> href="pages/contact.php">Contact</a></li>
</ul>
</div>
#navbar ul li {
width:138px;
height:57px;
}
.current {
width:138px;
height:57px;
background-image: url('../images/current.png');
background-repeat: no-repeat;
}
For obvious reasons, the background image of an element cannot overflow the element itself. Therefore, if you're putting the background image on your A tag, you need to make sure that the A tag is big enough to fit the background. You could try something like this:
A {
display: inline-block;
width: 138px;
height: 57px;
}
You'll have to adjust all your other CSS to position things properly.
Related
i am literally stuck with getting these ul/li elements in one row.
Only first li has class, others are in another ul but without class or id. i tried multiple searches and loops, but can't get it right.
Below results returns too many rows where elements don't fit together.
Can someone help me?
idealy the result from below example would be:
'part', 'desc', 'qty'
'106128-001','LBL, BLNK,THERMAL PRINT','1'
'106128-004','LBL, BLNK THERMAL,RECT 2.40x4','1'
HTML example
<div id='tab1' class="section tab" data-position="auto" style="max-height:none;">
<ul class="links">
<li id="innertab1">106128-001</li>
</ul>
<ul id="combom0" class="tab1 cols2 compare">
<li style="border: none;">
<div>
<ul>
<li><strong>Description: </strong>LBL, BLNK,THERMAL PRINT</li>
<li><strong>Qty: </strong>1</li>
</ul>
</div>
</li>
</ul>
<ul class="links">
<li id="innertab1">106128-004</li>
</ul>
<ul id="combom1" class="tab1 cols2 compare">
<li style="border: none;">
<div>
<ul>
<li><strong>Description: </strong>LBL, BLNK THERMAL,RECT 2.40x4</li>
<li><strong>Qty: </strong>1</li>
</ul>
</div>
</li>
</ul>
My poor attempt with trying to count rows:
$search = $html->find('div[id=tab1]',0);
$i=0;
$alles=array();
foreach ($search->find('* li') as $test){
if(!empty($test->innertext)){
if($i>3){$i=0;}
$i++;
if($i==1){
echo $alles_li['part'] = strip_tags($test->innertext);
}
if($i==2){
// don't need this, bad stripping
strip_tags($test->innertext);
}
if($i==3){
$alles_li['desc'] = strip_tags($test->innertext);
}
if($i==4){
$alles_li['qty'] = strip_tags($test->innertext);
}
}
// if less than 3 elements don't add
if(count($alles_li)==3){
$alles[]=$alles_li;
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
http://hotelshimlahill.com/demo/
this website I've made in bootstrap,
the header section including nav bar is coming from a separate file called 'header.php'
Now i want to add active class for each page on which user is visiting.
how can i do that? Kindly assist me.
Thankyou.
In your individual page such as about.php, services.php, you can do:
$currentPage = 'about'; // current page is about, do the same for other page
include('header.php');
Then in your header.php, you can check:
<ul class="nav navbar-nav">
<li class="<?php if($currentPage =='about'){echo 'active';}?>" >Home</li>
<li class="<?php if($currentPage =='services'){echo 'active';}?>" >about</li>
</ul>
Or you can try to use jQuery as well:
$(document).ready(function($){
var url = window.location.href;
$('.nav li a[href="'+url+'"]').addClass('active');
});
All menu links are inside ul > li. so, give id to all li. and on each page by using jquery addClass "active" to li.
in you bootstrap css you find this line i think it should be at line no 4841
.navbar-inverse .navbar-nav > li > a:hover, .navbar-inverse .navbar-nav > li > a:focus{
background-color: #5C0000;
color: #FFFFFF;
}
you change this line as
.navbar-inverse .navbar-nav > li > a:hover, .navbar-inverse .navbar-nav > li > a:focus, .navbar-inverse .navbar-nav > li > a.active{
background-color: #5C0000;
color: #FFFFFF;
}
now in your project file find the section where the navigation generated those line
<ul class="nav navbar-nav">
<li>Home</li>
<li>about</li>
<li>services</li>
<li>facilities</li>
<li>booking</li>
<li>testimonials</li>
<li>careers</li>
<li>contact Us</li>
</ul>
then change it as accrodinly
<ul class="nav navbar-nav">
<li><a href="index.php" <?php if($active=='Home') echo "class='active'"; ?>>Home</a></li>
<li><a href="about.php" <?php if($active=='about') echo "class='active'"; ?>>about</a></li>
<li><a href="services.php" <?php if($active=='services') echo "class='active'"; ?>>services</a></li>
<li><a href="facilities.php" <?php if($active=='facilities') echo "class='active'"; ?>>facilities</a></li>
<li><a href="reservations.php" <?php if($active=='booking') echo "class='active'"; ?>>booking</a></li>
<li><a href="testimonials.php" <?php if($active=='testimonials') echo "class='active'"; ?>>testimonials</a></li>
<li><a href="careers.php" <?php if($active=='careers') echo "class='active'"; ?>>careers</a></li>
<li><a href="contactUs.php" <?php if($active=='contact') echo "class='active'"; ?>>contact Us</a></li>
</ul>
then you have to a litle bit change in every files that you have in navigation link
such as
index.php
add this line at the top of the file that i mention on the navigation link
<?php $active ='Home'; ?>
and for about.php
<?php $active ='about'; ?>
I think it of course select of your active page
thnaks
I am making a CSS navigation bar, which is stored in a folder called 'includes'. Basically what I want is, when a link is clicked, for it to become active. Im linking to this folder with "include('includes/header.php');"
My CSS is:
nav ul li a {
font-size:22px;
color:#fff;
height:67px;
line-height:67px;
text-decoration:none;
min-width:101px;
text-align:center;
float:left;
background: grey;
border-right:1px solid black;}
nav ul li a.current, nav ul li a:hover {padding-bottom:5px;}
The content of header.php is:
<header>
<div class="container">
<nav>
<ul>
<li>Home</li>
<li>Services</li>
<li>Blog</li>
<li>Forums</li>
<li>Contact</li>
<li>Account</li>
</ul>
</nav>
How do I go about using this method, and having each link made active when clicked?
You could do something like this for the header
<ul>
<li><a <?php if (isThisTheCurrentPage) echo 'class="current"'?> ...>...</a></li>
<li><a <?php if (isThisTheCurrentPage) echo 'class="current"'?> ...>...</a></li>
<li><a <?php if (isThisTheCurrentPage) echo 'class="current"'?> ...>...</a></li>
...
</ul>
Once I had to do something similar, and I achieved this by setting some variables at the beginning of each php file. Then, the header.php must test these variables in the if statements.
I did something like this in the past very similar to what Romain said:
I named each page something like and included the nav file:
<?php $page = 'one';
require_once'./template/nav.php';?>
Here is the content of my nav file:
<ul class="nav navbar-nav">
<li <?php echo ($page == 'one') ? "class='active'" : ""; ?> >
Home</li>
<li <?php echo ($page == 'two') ? "class='active'" : ""; ?> >Page two</li>
<li <?php echo ($page == 'three') ? "class='active'" : ""; ?> >page three</li>
</ul>
Hope that helps
This is my first post so forgive as I am just new in the world of web development.
Normally, when I try to make a website, I create a file called header.html and footer.html so that I only change data once in all of the pages rather than having multiple same headers on many html files. And include them all in a php file together with the content and the php codes that comes per page.
Now my problem is because I only have 1 header, the css is designed in a way that whatever the current menu/tab is, it will be marked as "selected" so that its obvious to the user what page they are currently in.
My question is how do I solve this problem:
1.) To have the class="selected" depending on what the current page/url is.
<!--Menu Starts-->
<div class="menu">
<div id="smoothmenu" class="ddsmoothmenu">
<ul>
<li>Home</li>
<li>About </li>
<li>Services </li>
<li>Features</li>
<li>Support
<ul>
<li>Support 1</li>
<li>Support 2</li>
</ul>
</li>
</ul>
</div>
</div>
<!-- Menu Ends--!>
Thank You :)
If you're looking for a non-javascript / php approach...
First you need to determine which nav-link should be set as active and then add the selected class. The code would look something like this
HTML within php file
Call a php function inline within the hyperlink <a> markup passing in the links destination request uri
<ul>
<li><a href="index.php" <?=echoSelectedClassIfRequestMatches("index")?>>Home</a></li>
<li><a href="about.php" <?=echoSelectedClassIfRequestMatches("about")?>>About</a> </li>
<li><a href="services.php" <?=echoSelectedClassIfRequestMatches("services")?>>Services</a> </li>
<li><a href="features.php" <?=echoSelectedClassIfRequestMatches("features")?>>Features</a></li>
<li>Support
<ul>
<li><a href="support1.php" <?=echoSelectedClassIfRequestMatches("support1")?>>Support 1</a></li>
<li><a href="support2.php" <?=echoSelectedClassIfRequestMatches("support2")?>>Support 2</a></li>
</ul>
</li>
</ul>
PHP function
The php function simply needs to compare the passed in request uri and if it matches the current page being rendered output the selected class
<?php
function echoSelectedClassIfRequestMatches($requestUri)
{
$current_file_name = basename($_SERVER['REQUEST_URI'], ".php");
if ($current_file_name == $requestUri)
echo 'class="selected"';
}
?>
You could ID each link and use JavaScript/Jquery to add the selected class to the appropriate link.
<!--Menu Starts-->
<div class="menu">
<div id="smoothmenu" class="ddsmoothmenu">
<ul>
<li id="home-page">Home</li>
<li id="about-page">About </li>
<li id="services-page">Services </li>
<li id="features-page">Features</li>
<li id="support-page">Support
<ul>
<li id="support1-page">Support 1</li>
<li id="support2-page">Support 2</li>
</ul>
</li>
</ul>
</div>
</div>
<!-- Menu Ends--!>
On your content page use jQuery to do something like:
$(document).ready(function(){
$("#features-page").addClass("selected");
});
Another method you could use is:
Add class element based on the name of the page
Give each link a separate id then use jQuery on the individual pages.
<li>Home</li>
<li>About </li>
<li>Services </li>
<li>Features</li>
<li>Support
<ul>
<li>Support 1</li>
<li>Support 2</li>
</ul>
</li>
On the services page:
$(document).ready(function(){
$("#services").addClass("selected");
});
Or even better as robertc pointed out in the comments, there is no need to even bother with the id's just make the jquery this:
$(document).ready(function(){
$("[href='services.php']").addClass("selected");
});
One variant on Chris's approach is to output a particular class to identify the page, for example on the body element, and then use fixed classes on the menu items, and a CSS rule that targets them matching. For example, this page:
<DOCTYPE HTML>
<head>
<title>I'm the about page</title>
<style type="text/css">
.about .about,
.index .index,
.services .services,
.features .features {
font-weight: bold;
}
</style>
</head>
<body class="<?php echo basename(__FILE__, ".php"); ?>">
This is a menu:
<ul>
<li>Home</li>
<li>About </li>
<li>Services </li>
<li>Features</li>
</ul>
</body>
...is pretty light on dynamic code, but should achieve the objective; if you save it as "about.php", then the About link will be bold, but if you save it as "services.php", then the Services link will be bold, etc.
If your code structure suits it, you might be able to simply hardcode the page's body class in the page's template file, rather than using any dynamic code for it. This approach effectively gives you a way of moving the "logic" for the menu system out of the menu code, which will always remain the same for every page, and up to a higher level.
As an added bonus, you can now use pure CSS to target other things based on the page you're on. For example, you could turn all the h1 elements on the index.php page red just using more CSS:
.index h1 { color: red; }
You can do it from simple if and PHP page / basename() function..
<!--Menu Starts-->
<div class="menu">
<div id="smoothmenu" class="ddsmoothmenu">
<ul>
<li><a href="index.php" <?php if (basename($_SERVER['PHP_SELF']) == "index.php") { ?> class="selected" <?php } ?>>Home</a></li>
<li><a href="about.php" <?php if (basename($_SERVER['PHP_SELF']) == "about.php") { ?> class="selected" <?php } ?>>About</a> </li>
<li><a href="services.php" <?php if (basename($_SERVER['PHP_SELF']) == "services.php") { ?> class="selected" <?php } ?>>Services</a> </li>
<li><a href="features.php" <?php if (basename($_SERVER['PHP_SELF']) == "features.php") { ?> class="selected" <?php } ?>>Features</a></li>
</ul>
</div>
</div>
Sorry for my bad English, however may be it could help. You can use jQuery for this task. For this you need to match the page url to the anchor of menu and then add class selected to it. for example the jQuery code would be
jQuery('[href='+currentURL+']').addClass('selected');
I aim to have a similar Bar at the top as here: http://www.keltainenporssi.fi/
The colour of the current site is in red, while other sites are in black.
How would you reach my aim?
Perhaps by php?
The syntax in pseudo-code may be
if $site = A
use-styleA
if $site = B
use-styleB
else
use-FinalStyle
It is common to use a server-side script such as PHP to do such a thing. For example:
<?PHP
echo '<ul id="top-nav">';
foreach($page as $pageId => $text) {
echo '<li';
if($curPage == $pageId)
echo ' class="active"';
echo '>';
echo '<a href="pages/' . htmlspecialchars($pageId) . '">';
echo htmlspecialchars($text);
echo '</a>';
echo '</li>';
}
echo '</ul>';
?>
This would produce output like: (Formatted for readability)
<ul id="top-nav">
<li>Home</li>
<li class="active">Page one</li>
<li>Page two</li>
</ul>
Non-dynamic approach:
You can use a CSS rule for each site, like so:
/* site-one.css */
#top-nav li.site-one a {
color: red;
}
/* site-two.css */
#top-nav li.site-two a {
color: red;
}
/* site-three.css */
#top-nav li.site-three a {
color: red;
}
HTML:
<ul id="top-nav">
<li class="page-one">Page one</li>
<li class="page-two">Page two</li>
<li class="page-three">Page three</li>
</ul>
You can have a single css for the bar which all the domains will be fetching. Make sure you add !important rules on that css so it's rules are not overriden by anything else. Otherwise shield the bar in a "namespace" by adding an id on the list/div or whatever you'll use to implement it.
Yes, you could assign the anchor element's class based on the current site.