I have an ul/li where each li contains 2 div. One of the div ("category") can content text that can wrap on multiple lines. I'm trying to get the other div ("abbr") of that li to have the same height. Since the parent li is stretching, I tried height: 100% on the abbr but it doesn't stretch it to.
Any idea?
<ul>
<li>
<div class="abbr">ABC</div>
<div class="category">Long or short</div>
</li>
<li>
<div class="abbr">ABC</div>
<div class="category">Very long or very shortshort</div>
</li>
</ul>
And the css I used:
ul {
list-style: none;
width: 200px;
}
div.abbr {
float: left;
padding: 16px;
border: 1px solid red;
width: 30px;
background: blue;
text-align: center;
}
div.category {
/*float: left;*/
padding: 16px;
border: 1px solid red;
margin-left:64px;
}
Here's a fiddle if you wanna play with it: http://jsfiddle.net/P6UzU/
You can use CSS tables as Danield say above, or you can simulate the effect you want:
http://jsfiddle.net/P6UzU/3/
I put the blue background and the red border in the li tag, and a white background in the "category" class. Also, change the whole border in "category" to a border-left.
You could use css tables and remove the float:left on the first div
FIDDLE
CSS
ul {
list-style: none;
width: 200px;
display:table; /* <--- */
}
li
{
display:table-row; /* <--- */
}
div.abbr {
padding: 16px;
border: 1px solid red;
width: 30px;
background: blue;
text-align: center;
display:table-cell; /* <--- */
}
div.category {
/*float: left;*/ /* <--- removed */
padding: 16px;
border: 1px solid red;
margin-left:64px;
display:table-cell; /* <--- */
}
As others have said,
li a { display: block; }
should achieve what you’re after. But you must also remove any padding from the <li> and set it on the <a> instead. For example:
li { padding: 0; }
li a { display: block; padding: 1em; }
guys final i get the answer plz chk this....
html code:--
<ul>
<li>
<div class="abbr">ABC</div>
<div class="category">Long or short</div>
</li>
<li>
<div class="abbr1">ABC</div>
<div class="category1">Very long or very shortshort & Very long or very shortshort</div>
</li>
</ul>
CSS:--
ul {
list-style: none;
width: 200px;
}
div.abbr {
float: left;
padding: 16px;
border: 1px solid red;
width: 30px;
background: blue;
text-align: center;
}
div.category {
/*float: left;*/
padding: 16px;
border: 1px solid red;
margin-left:64px;
}
div.abbr1 {
float: left;
padding: 16px;
border: 1px solid red;
width: 30px;
background: blue;
text-align: center;
}
div.category1 {
/*float: left;*/
padding: 16px;
border: 1px solid red;
margin-left:64px;
}
Script:--
$(document).ready(function() {
var rightHeight = $('.category1').height();
$('.abbr1').css({'height':rightHeight});
and to this thing work out you have to add jquery/1.8.2..
Demo:--Fiddle
Related
I am learning CSS while writing CSS I got some issue.
I need to remove this space from my displaying web page.
.grouping:before,
.grouping:after {
content: " ";
display: table;
}
.grouping:after {
clear: both;
}
nav {
background-color: white;
position: fixed;
top: 0px;
right: 0px;
left: 0px;
}
nav figure {
position: absolute;
}
img {
width: 100px;
}
.primary-nav {
float: right;
}
.primary-nav>li {
display: block;
float: left;
}
.primary-nav>li>a {
float: left;
padding: 25px 0;
width: 100px;
border-left: 1px solid;
}
nav li a {
font-family: Arial, Helvetica, sans-serif;
color: black;
text-transform: uppercase;
font-size: 15px;
text-align: center;
}
nav li:first-child a {
border-left: none;
}
nav li a:focus,
nav li a:hover {
background: red;
background-color: red;
}
body {
background-color: grey;
}
<nav class="grouping">
<figure>
<img src="images/logo.png" alt="LOGO">
<ul class="primary-nav">
<li>Home</li>
<li>Home2</li>
<li>Home3</li>
<li>Home4</li>
<li>Home5</li>
</ul>
</figure>
</nav>
Using this code resultant output must be like this:
Expected Result Image
But getting this result with spaces at corner indicated as an arrow in next image:
Generated Result Image
1) There is some margin on ul element rendered by the browser defaults in general, to fix this do:
ul {
margin: 0
}
2) Also figure element has default margins
figure {
margin: 0
}
Try to inspect the output of the browser to see exactly how the browser defaults renders that margin to see it better.
https://jsfiddle.net/t8netg8j/4/
I would use display: inline-block for the image and the li elements, erase some of the floats and reset some of the margins to 0 as shown below:
(note: I reduced the width of the elements from 100 to 80 px to fit them into the narrow snippet window, but of course you don't have to do that for a wider viewport)
(note #2: i added empty HTML comments at line ends and beginnings for all li elements to avoid the whitespace resulting from the use of inline-blocks which would cause the hover background not to fill the full space between two vertical borders)
html, body {
margin: 0;
}
nav figure {
position: absolute;
width: 100%;
margin: 0;
}
img {
width: 80px;
display: inline-block;
}
.primary-nav {
float: right;
margin: 0;
}
.primary-nav>li {
display: inline-block;
}
.primary-nav>li>a {
float: left;
padding: 25px 0;
width: 80px;
border-left: 1px solid;
}
nav li a {
font-family: Arial, Helvetica, sans-serif;
color: black;
text-transform: uppercase;
font-size: 15px;
text-align: center;
}
nav li:first-child a {
border-left: none;
}
nav li a:focus,
nav li a:hover {
background: red;
background-color: red;
}
body {
background-color: grey;
}
<nav class="grouping">
<figure>
<img src="images/logo.png" alt="LOGO">
<ul class="primary-nav">
<li>Home</li><!--
--><li>Home2</li><!--
--><li>Home3</li><!--
--><li>Home4</li><!--
--><li>Home5</li><!--
--></ul>
</figure>
</nav>
I am trying to create a horizontal top header with a main nav stacked on top of a sub nav. Both navs are created using <ul> with floated <li> items. The sub nav has extra horizontal space to the left whose cause I cannot discern (see the circled red area in the picture below):
Here's a fiddle: http://jsfiddle.net/W3qqh/
Questions
What's causing the space?
How can I eliminate the space?
Also feel free to suggest other alternatives for achieving the desired menu, if you believe my approach could be improved.
HTML
<div class="header">
<div class="home-logo">HOME</div>
<div class="main-nav">
<ul>
<li><a>Explore</a></li>
<li><a>Earn</a></li>
<li><a class="selected">Merchants</a></li>
</ul>
</div>
<div class="sub-nav">
<ul>
<li><a>Be Secure</a></li>
<li><a>Get Help</a></li>
<li><a>Contact Us</a></li>
</ul>
</div>
</div>
CSS
* { box-sizing: border-box; }
.header { height:99px; width: 100%; position:fixed;}
.header.glass {opacity: 0.8; filter: alpha(opacity=80);}
.home-logo { background: pink; height:99px; width: 239px; position: fixed; }
.main-nav { color: white; position: relative; left:239px; background: #25c0df; height: 66px; line-height:66px; padding: 2px; width: 100%; text-transform: uppercase; font-size: 14px;}
.main-nav ul { height: 0; padding: 0; margin: 0; list-style-type: none; }
.main-nav li { float:left; margin-left: 40px;}
.main-nav li a.selected { border-top: 2px solid white; }
.main-nav li a:hover { border-top: 2px solid white; }
.sub-nav { font-size: 12px; color: #4ebeb2; background: #26b; height: 33px; line-height: 33px; width: 100%; text-transform:uppercase; }
.sub-nav ul { height: 0; padding: 0; margin: 0; list-style-type: none; }
.sub-nav li { float:left; margin-left: 40px;}
In .sub-nav, add:
float:left;
position:relative;
left:239px;
In .main-nav, add:
float:left;
Solution: You had to add float:left; in both .main-nav and .sub-nav and you had to add position:relative; and left:239px; because of the logo on the left.
Your problem would have been solved with just having float:left; but you needed to added position and left. Otherwise, your text would be behind the logo.
JSFiddle Demo
UPDATE
In .main-nav, you have:
padding: 2px;
If you remove that and add position and left in .sub-nav, you wouldn't need float property.
JSFiddle Demo
Is this more what you were looking for: http://jsfiddle.net/W3qqh/2/?
First, set the outermost container to 100% width. Then float the three inner container divs and assign a width. Then apply a float to all <li> items (or use display: inline-block).
.header { width: 100%; }
.home-logo { width: 33.333%; float: left; background: pink; height:99px; }
.main-nav { width: 66.666%; float: left; color: white; background: #25c0df; height: 66px; line-height:66px; padding: 2px; text-transform: uppercase; font-size: 14px;}
.sub-nav { width: 66.666%; float: left; font-size: 12px; color: #4ebeb2; background: #26b; height: 33px; line-height: 33px; text-transform:uppercase; }
I making menu links which have varying lengths of link text. This makes some links wrap onto 2 lines and others not.
In the image below the red area is a link and so clickable, but the grey area is not. My code is the first example and I need to create the 2nd:
<ul>
<li>
Link 1
</li>
<li>
Link 2 which has very very very long text
</li>
<li>
Link 3
</li>
</ul>
ul {
padding: 0;
margin: 0;
display: table;
width: 400px;
border: 1px solid black;
}
li {
list-style-type: none;
display: table-cell;
background: grey;
width: 33%;
}
a {
background: red;
width: 100%;
display: inline-block;
height: 100%;
}
http://codepen.io/anon/pen/Gcaem/
This FIDDLE works.
CSS
ul {
padding: 0;
margin: 0;
display: table;
width: 400px;
border: 0px solid black;
}
li {
list-style-type: none;
display: table-cell;
background-color: transparent;
margin-left: 5px;
border: 5px solid white;
width: 33%;
height: 75px;
text-align: center;
border: 1px solid gray;
}
a {
display: table;
background-color: red;
color: white;
width: 100%;
height: 100%;
text-align: center;
text-decoration: none;
}
a span {
display: table-cell;
height: 100%;
width: 100%;
text-align: center;
vertical-align: middle;
}
This is the result in firefox:
I'm working on a new responsive design and I'm having an issue with my menu that I cannot figure out. Basically I want the background of the menu item to turn white when you hover over it and the text to turn blue. Right now, the text turns blue but the background will not turn white.
HTML Code:
<div class="header">
<div class="header_content">
<div class="logo">
<img src="images/new_logo.png" class="hdr_logo">
</div>
<div class="main_menu">
<ul>
<li>ABOUT US</li>
<li>OPERATIONS BRANCH</li>
<li>LOGISTICS BRANCH</li>
<li style="border-right:0;">COMMUNITY</li>
</ul>
</div>
</div>
</div>
CSS:
.header {
background: url('images/header2.png') repeat-x top left;
width: 100%;
height: 150px;
color: #FFF;
margin: 0;
padding: 0;
}
.header_content {
width: 960px;
margin: 0 auto;
height: 150px;
padding: 0;
}
.logo {
float:left;
width:120px;
}
.main_menu {
float:left;
height: 30px;
margin-top: 120px;
line-height: 30px;
padding: 0;
width: 830px;
}
.main_menu ul {
list-style-type: none;
padding: 0;
margin: 0;
text-align: center;
overflow: hidden;
height: 30px;
}
.main_menu li {
float: left;
border-right: 1px solid #FFF;
text-align: center;
padding: 0;
margin: 0;
padding-left: 2%;
padding-right: 2%;
overflow: hidden;
height: 30px;
cursor: pointer;
}
.main_menu li:hover {
background-image: none;
background-color: #FFF;
color: rgb(33,47,57);
}
EDIT: Problem Resolved
The floated li was not a block element so you could not change it's background color. As soon as I added display:block; to the li it resolved this issue.
Seems to work fine for me: http://jsfiddle.net/s3JT9/
.main_menu li:hover {
background-image: none;
background-color: #FFF;
color: red;
}
I changed the color to red to illustrate.
I am designing a top bar for a website and cannot figure out how to force the <a> elements below have the same height as the parent <li> elements. The code below shows the <li> elements with a green background and the <a> elements with a yellow background. I would like to have the yellow boxes span the whole height of the green boxes. Setting the height of the <a> elements to 100% doesn't do trick. What am I missing?
<!DOCTYPE html>
<head>
<style>
div#topbar {
width: 100%;
height: 30px;
top: 0;
border: 1px solid black;
}
#topbar ul {
margin: 0;
padding: 0;
list-style: none;
line-height: 30px;
}
#topbar ul li {
margin: 0;
padding: 0 10px;
background-color: green;
display: inline;
float: left;
}
#topbar a {
background-color: yellow;
}
</style>
</head>
<body>
<div id="topbar">
<ul>
<li>Item 1</li>
<li>Item 2</li>
</ul>
</div>
</body>
Its because tag is an inline element and it doesn't take height into account so you need make it block which can be made by various methods but most suitable in your case is 'float'.
div#topbar {
width: 100%;
height: 30px;
top: 0;
border: 1px solid black;
}
#topbar ul {
margin: 0;
padding: 0;
list-style: none;
line-height: 30px;
}
#topbar ul li {
margin: 0;
padding: 0 10px;
background-color: green;
display: inline;
float: left;
}
#topbar a {
background-color: yellow;
float:left; /*add this*/
}
http://jsfiddle.net/YMPe2/
Have you tried :
#topbar a {
background-color: yellow;
display: inline-block;
}
the a tag should be display:block, then it will fill the parent. And lose the padding on the li tag.