I'm trying to create horizontal navigation bar, that fills out the screen, or parent container and will have equal horizontal padding for elements, that will equally reduce on smaller window width. Using display:table-cell I have got to the point that it nicely reduces but is not equal. Longer titles have more padding. I'm open to any working solutions, ideally css but if that is not possible, than anything that will work without glitches. What i have so far can be seen here
ul {
list-style:none;
padding:0;
width:100%;
display:table;
border-top:1px solid;
border-bottom:1px solid;
border-left:1px solid
}
li {
border-right: 1px solid;
margin:0;
display:table-cell;
position:relative;
text-align:center
}
<ul>
<li>some title</li>
<li>some longer title</li>
<li>some title</li>
</ul>
http://jsfiddle.net/xZ6q3/16/
Solved it (and learned a lot). The basic idea is to run through all list items and calculate the average padding. Then run through them again and set their width to their text width + average padding.
Heres the updated jsfiddle: http://jsfiddle.net/xZ6q3/21/
// calculate the average padding
var totalPadding = 0;
var count = 0;
$('li').each(function(){
var t = $(this);
var outer = t.outerWidth(true);
var inner = t.textWidth();
var padding = outer - inner;
totalPadding += outer - inner;
count++;
});
totalPadding /= count;
// set each width to text width + average padding
$('li').each(function(){
var t = $(this);
var inner = t.textWidth();
var newWidth = inner + totalPadding;
t.css('width', newWidth);
});
To make it work, I had to use Phil's textWidth() and listen to window resize events. See the jsFiddle.
Try this
ul {
border-bottom: 1px solid;
border-left: 1px solid;
border-top: 1px solid;
display: inline-block;
list-style: none outside none;
padding: 0;
}
li {
border-right: 1px solid;
display: inline-block;
margin: 0;
padding: 0 17px;
position: relative;
text-align: center;
}
First, I would go for float:left property and then change values for different screen resolutions.
ul li {
float:left;
padding-left: 15px;
padding-right: 15px;
}
#media only screen
and (min-device-width : 320px)
and (max-device-width : 480px) {
ul li {
padding-left: 5px;
padding-right: 5px;
}
}
Related
I am trying to add border to the text in the menu. What I want is to have the borders width as long as the text. I have achieved this for single line menu items.
As you see in the screen the menu item "test av lister" has correct border width but the next item has more width. I want the width to stop where the first line is ending.
Here is my css to achieve this so far
.mp-pusher .title__content {
border-bottom: 3px solid #fff;
width: fit-content;
}
If you think about all of those elements being a box, you can see why the border is moving across the entire element.
* {
border: 1px solid red;
}
Here's an example of what is happening:
.menu li {
display: inline-block;
margin-top: 10px;
border: 1px solid blue;
border-bottom: 3px solid red;
}
https://jsfiddle.net/sheriffderek/bmLj7fo0/
also: I'm not sure that 'fit-content' and those width properties are ready to be used yet. https://developer.mozilla.org/en-US/docs/Web/CSS/width . You can see a little 'mad scientist' potion next to the options that are experimental.
As a user... I'd find that visual treatment weird. Maybe you can dream up a new visual style for the smallest screens. : )
I achieved it by using this css
background-image: linear-gradient(to right, #F16A70, #F16A70);
background-position: bottom left;
background-repeat: no-repeat;
background-size: 50% 2px;
transition: background-size .5s ease;
and this jquery
$.fn.textWidth = function () {
var html_org = $(this).html();
var html_calc = '<span>' + html_org + '</span>';
$(this).html(html_calc);
var width = $(this).find('span:first').width();
$(this).html(html_org);
return width;
};
// added JS trick to make the border of the menu items just so that they are the size of the item
$(".title__content.pb-1").each(function () {
var tW = $(this).textWidth();
$(this).css('background-size', tW + 'px 3px');
});
I am trying to make the in my website always fit in the browser size. Right now, if I go to my website with a smaller screen, everything is messing together. Here is one and css example in my codes
html
<div class="contactus">
<h2>Contact Us</h2>
<h3>PhoneNumber</h3>
<h3>812-123-4567</h3>
<h3>Email</h3>
<h3>qwerty#asd.com</h3>
</div>
CSS
div.contactus {
position:absolute;
right:20px;
top:150px;
border: 2px solid;
border-color:#333;
width:100px;
font:20px;
In order to make it responsive you need to keep the code of the CSS in relative units because whey you make it with fixed units the code will not adapt to the screen so you will have to make your code something like this:
div.contactus {
position:absolute;
right:5%;
top:25%;
border: 2px solid;
border-color:#333;
width:100%;
font:20px;}
If the percentages are not ok just keep playing with them until you like it.
Responsive media queries would be extremely helpful, but the biggest problem I'm seeing is that your container (.contactus) is not wide enough to accommodate the text you're trying to place. Try changing the contactus width to 400px. However, on a smaller screen, you should have a media query that updates font size and container size based on the viewport width.
First of all welcome to our community, first read this topic to help you answers better questions and get more precise response: https://stackoverflow.com/help/how-to-ask
About your question, you have to explain better what you mean with "messing together".
I made a simple fiddle to help you with your responsive issues : https://jsfiddle.net/nLxa1745/
HTML
<div class="main">
<img class="user-pic" src="http://placehold.it/250x150">
<div class="contact-box">
<h4 class="user-name">Contact Us</h4>
<ul class="info-contact">
<li>
phonenumber
</li>
<li>
812-123-4567
</li>
<li>
email
</li>
<li>
qwerty#blabla.com
</li>
</ul>
</div>
</div>
CSS
body {
font-family: sans-serif;
color: #3f3f3f;
}
li{
margin: 5px 0px 0px 20px;
}
.main {
border: 2px solid gray;
width: 250px;
margin: auto;
box-shadow: 2px 2px 5px 1px gray;
}
.user-pic {
border-bottom: 2px solid gray;
}
.user-name {
margin-left: 15px;
margin-bottom: 0;
color: #B77425;
}
.info-contact {
list-style-type: none;
padding: 0;
}
#media only screen
and (min-width : 600px) {
.main {
width: 100%;
height: 150px;
}
.user-pic {
border-right: 2px solid gray;
float: left;
}
.contact-box {
float: left;
}
}
P.S. I'd recommend a reading about headings also https://webdesign.tutsplus.com/articles/the-truth-about-multiple-h1-tags-in-the-html5-era--webdesign-16824
I have a navbar, which is pretty standard, and I want to make it a bit thinner.
So I tried this:
http://www.bootply.com/9VC5N55nJD
But the buttons remain too big. Click the drop down, and you'll see the issue. Is there a way to make the navbar thinner in one place? Or do you need to add css for the navbar and the buttons and what ever else may crop up?
Also, if I say it must be 30px in height - on a mobile, that might be too narrow, so do I need a media query for the different sizes?
Here is a working fork of your code.
Bootstrap by default uses a padding-top: 15px and padding-bottom: 15px for .navbar-nav > li > a. The following CSS will take care of this:
.navbar-nav > li > a {
padding-top:10px;
padding-bottom:10px;
}
After reducing the screen size (and for mobile devices as you've mentioned) running a media query that resets them and kind of makes the navbar a bit larger will do the trick. The following is a hacky way to do so:
#media (max-width: 768px) {
// enter css for mobile devices here
.topnav {
font-size: 14px;
}
.navbar {
min-height:50px;
max-height: 50px;
}
.navbar-nav > li > a {
padding-top:15px;
padding-bottom:15px;
}
}
Use the below css and let me know.
.navbar-nav>li>a {
padding-top: 10px;
padding-bottom: 10px;
}
.navbar-brand {
float: left;
height: 40px;
line-height: 20px;
padding: 10px 15px;
}
.navbar-toggle {
background-color: transparent;
background-image: none;
border: 1px solid transparent;
border-radius: 4px;
float: right;
margin-bottom: 0;
margin-right: 15px;
margin-top: 6px;
padding: 6px 5px;
position: relative;
}
You need to fix the CSS for the dropdown. Here's the CSS to add to your stylesheet:
ul.navbar-nav, ul.navbar-nav li {
max-height: 40px;
min-height: 40px;
}
Changing bootstraps formatting like the navbar has been difficult. They have so many styles that are set, that it's tough to find which one you need to change. Just add the above style to the css sheet and you should be gravy.
Edit
Also, why not use height: 40px if you're just going to set the min-height and max-height as the same value?
I am trying to build a mouse-over-menu that appears when hovering over an element. This works great, however, for the bottom-rows the hover disappears in the scroll-bars.
HTML
<div style="width:1024px; height:50px; overflow:scroll; border: 1px solid red;">
<div class="dropdown">
<span style="margin-left:5px" class="checkboxlink ignore"></span>
<ul>
<li><span class="checkboxlink todo"></span>Te doen</li>
<li><span class="checkboxlink done"></span>Behandeld</li>
<li><span class="checkboxlink ignore"></span>Negeren</li>
<li><span class="checkboxlink park"></span>Parkeren</li>
</ul>
</div>
.... more divs
CSS
.dropdown{
position: relative;
display: inline-block;
font-size: 16px;
width: 40px;
border: 0px solid white;
vertical-align: top;
}
.dropdown:hover
{
border: 0px solid #ccc;
}
ul{
position: absolute;
list-style: none;
text-align: left;
z-index: 1;
margin:0;
padding:0;
display: none;
background-color: white;
border: 1px solid #ccc;
}
.dropdown:hover ul {
display: block;
}
See also here:
https://jsfiddle.net/w030L59t/
I tried positioning absolute, but the element still stays in the scrollable area.
The problem is any position: relative attribute for the dropdown parents. If you remove them, you can easily add position: absolute to your dropdown, to display it above the scrollable box:
JSFIDDLE DEMO
The problem removing position: relative is that the position of each dropdown is calculated once on page load. While the dropdowns work great without scrolling, you will notice in the demo, that each dropdown does not refresh its position.
This could be solved using a few lines of javascript, calculating the .offset.top of each parent after scrolling and updating the dropdowns position using top: <offset.top of its parent>. I have added the classes .list_item and ul.dropdown_list, although the ID #wrapper.
$(document).ready(function() {
// fire function everytime the wrapper is scrolled
$('#wrapper').scroll(function(){
// set element to relate to
var list_items = $('div.list_item');
// get each position
list_items.each(function() {
// store offset().top inside var
var list_item_position = $(this).offset().top;
// select previous dropdown_list item
$(this).prev().find('ul.dropdown_list').css({
// apply offset top
top: list_item_position + "px"
});
});
// write to console to track changes
console.log('positions updated');
}); // .scroll
}); // document.ready
JSFIDDLE DEMO WITH jQUERY
I have a jsfiddle here - https://jsfiddle.net/w4k70at5/
It's a realy simple problem but I'm a bit stumped.
I have a border between each link to separate them.
I don't want a border on the last link in the line.
I can remove the last border but can I remove the border after 'Link Six' and then when the page is resized remove the border on the last link on that line.
*{
margin: 0;
padding: 0;
}
nav{
margin: 50px;
max-width: 600px;
margin: 50px auto 0 auto;
}
ul{
list-style: none;
text-align: center;
}
li{
display: inline;
margin: 0 0 10px 0;
}
a{
display: inline-block;
border-right: 1px solid red;
font-size: 1.5em;
padding: 5px;
text-decoration: none;
}
li:last-of-type a{
border-right: none;
}
You can perform that using Jquery :
1st possibility:
Add a JQuery function which will add a class to your ul if it's the last one on the line:
$(function() {
var lastElement = false;
$("ul > li").each(function() {
if (lastElement && lastElement.offset().top != $(this).offset().top) {
lastElement.addClass("noborder");
}
lastElement = $(this);
}).last().addClass("noborder");
});
And a CSS class to remove the border:
.noborder a {
border: 0;
}
If you want, you can remove your CSS rule with the previous solution:
li:last-of-type a {
border-right: none;
}
JSFiddle 1: https://jsfiddle.net/ghorg12110/w4k70at5/4/
2nd possibility:
Same JQuery function but will not target the last ul > li a (because you already target it with your CSS):
$(function() {
var lastElement = false;
$("ul > li").each(function() {
if (lastElement && lastElement.offset().top != $(this).offset().top) {
lastElement.addClass("noborder");
}
lastElement = $(this);
});
});
And a CSS class to remove the border:
.noborder a {
border: 0;
}
JsFiddle 2: http://jsfiddle.net/ghorg12110/x93456gr/
FULL CSS SOLUTION
https://jsfiddle.net/w4k70at5/
I added a class to the <li> that cause a problem, and this one is set up like following :
When the screen size is under 590, I tell the class to show a right red border
When screen size is above, border is transparent
I used media queries CSS function to perform this. This allows to set different class behiaviour for different variables (like the screen size in this case).