How to adjust a floated element's position? - html

I have a ul wrapped in a .box. I have set the bottom border for the .box. The ul items also have bottom border, I want the li border to place over the .box border so that the .box border is no more visible. I'm trying to do that by setting margin-bottom: -1px but doesn't work.
Please see attached image:
Here's what I'm trying:
HTML:
<div class="box">
<ul>
<li>Hello</li>
<li>World</li>
</ul>
</div>
CSS:
.box{
border-bottom: 1px solid blue;
overflow: hidden;
}
ul{
list-style: none;
margin: 0;
padding: 0;
}
ul li{
float: left;
background: green;
border: 1px solid red;
}
Demo:
http://jsfiddle.net/b9H2j/

Problem
You basically want the li to overflow its .box parent, but .box is set with overflow:hidden;.
Solution
A possible solution can be to first set the li with position:relative; and then make it overflow with bottom:-1px;. Then remove overflow:hidden; from the .box container and find an alternative way to overcome the clear bug.
For example:
.box {
display:table; /* overcome the clear bug */
width:100%;
border-bottom: 1px solid blue;
}
ul {
list-style: none;
margin: 0;
padding: 0;
}
ul li {
position:relative;
bottom:-1px;
float: left;
background: green;
border: 1px solid red;
}
See jsFiddle demo

Have you considered changing your HTML structure? It seems somewhat to me "unnatural" to do what you're trying to achieve with your current HTMl. Anyhow, here's another solution. It requires you to set a height for your list items though.
You could do something like this:
HTML
<div class="box">
<ul>
<li class="left">Hello</li>
<li class="left">World</li>
<li class="add-bottom-border">something</li>
</ul>
</div>
CSS
.box{
display:table; /* overcome the clear bug */
width:100%;
}
ul{
list-style: none;
margin: 0;
padding: 0;
}
ul li{
height:20px;
background: green;
border: 1px solid red;
}
.add-bottom-border {
overflow:auto;
border-bottom: 1px solid blue;
}
.left {
float:left;
}
JSfiddle
There's en explanation of the reasoning behind overflow:auto; here.

Related

Display table-cell width percentages not obeyed

Why do the cells in the below example stretch fully collectively when it should only cover half the table width?
I've looked through numerous stackoverflow posts with similar issues, but none of their answers worked for me. And this is reflected in the below codepen example.
The reason why I'm trying to utilise table/table-cell for this is, because I will have elements, which will have variable heights. I can't have it zig-zag up and down using a float, and I don't want to set min/max-heights for the elements.
http://codepen.io/anon/pen/LWMROW
ul {
border: 1px solid black;
display: table;
padding: 0;
table-layout: fixed;
width: 100%;
}
ul li {
border: 1px solid black;
display: table-cell;
width: 25%;
}
<ul>
<li>This is a cell</li>
<li>This is a cell</li>
</ul>
If you would like to mimick a tr/td, I believe a displaying the ul tag as flex can do the job. Also the width of the li tags can be set using css flex property
ul {
border: 1px solid black;
display: table;
padding: 0;
table-layout: fixed;
width: 100%;
}
ul li {
border: 1px solid black;
flex:0 0 25%;
margin:0;
border:solid red;
}
ul{
position:relative;
display:flex;
list-style-type:none;
}
li{
}
<ul>
<li>This is a cell</li>
<li>This is a cell<br><br>addsd</li>
</ul>

adjusting li border-bottom "length"

This is css for an unordered-list with the id "leftmenu"
#leftmenu ul li{
list-style:none;
padding:15px 0 8px 0;
border-bottom:1px dashed white;
float:left;
clear:both;
The problem is the border only goes as far as the text go. see:http://imgur.com/dhx2OKk
I want it to be like that border under "Links"
The problem is that your list-items should be displayed as regular block items. These would always scale to the full width of any container. For a <li> element that is actually the default behavior.
By setting float: left; to the <li> items, alter this behavior. The following code would achieve what you are after (also check the JS fiddle)
* {
margin: 0;
padding: 0;
}
h4 {
border-bottom: 1px solid black;
}
.menu {
width: 200px;
}
.menu>ul {
list-style-type: none;
}
.menu>ul>li {
margin-top:10px;
border-bottom: 1px dotted black;
}
http://jsfiddle.net/fhckxene/
edit: for fun play round with the jsfiddle, for example by adding float: left; or display: inline-block; to the <li> style.

Div is 0px tall

My <div> with id="page" contains a full page. My <div> with id="main" is 25px (the 25px is only coming from it having padding) tall and should be a full page tall still. Mywithid="container"` is inside main and should be a full page tall without the footer but it is 0px tall which is causing its inner elements to spill out of it.
Mywithid="container"` is not containing its inner HTML elements. They are spilling out of the container. How do I make them not spill out? Here is the HTML, CSS, and and image of what it looks like showing me hovering over "main" and it taking 25px. Cheers.
HTML:
<div id="page"
<div id="main">
<div id="container">
<div id="left-hand-side"></div>
<div id="right-hand-side">
<img src="resources/Logo.png"/>
<?wp_nav_menu(array('theme_location'=>'primary', 'menu_class'=>'nav-menu'));?>
</div>
</div>
CSS:
/*
Theme Name: 2011-child-theme-commons
Description: Child theme for the Commons website
Author: admin
Template: twentyeleven
(optional values you can add: Theme URI, Author URI, Version)
*/
#import url("../twentyeleven/style.css");
#branding {
display:none;
}
.nav-menu .menu-item {
font-size:2em;
margin-bottom: 70px;
margin-right:30px;
list-style-type: none;
text-decoration:none;
float: right;
}
.nav-menu .menu-item a {
color:#333;
}
.first_menu_item a:hover {
color:#FEBA2E;
list-style-type: none;
text-decoration:none;
}
.second_menu_item a:hover {
color:#FF46C8;
list-style-type: none;
text-decoration:none;
}
.third_menu_item a:hover {
color:#2B6AFF;
list-style-type: none;
text-decoration:none;
}
#comments {
display:none;
}
body {
background: #FFF;
}
#container {
margin-top:-24px;
border-top: 1px solid #ddd;
border-right: 1px solid #ddd;
border-left: 1px solid #ddd;
position:relative;
background: url("../../../resources/plan_edited.png") no-repeat;
background-size:contain;
height: 100%;
width: 100%
}
#colophon {
border-bottom: 1px solid #ddd;
border-right: 1px solid #ddd;
border-left: 1px solid #ddd;
}
#right-hand-side img {
margin:auto;
z-index:2;
width:100%;
}
#right-hand-side h1 {
float:right;
margin-right:30px;
margin-bottom:50px;
font-size:2em;
cursor:pointer;
}
#logo {
cursor:pointer;
}
#right-hand-side {
float:right;
width:36%;
position:absolute;
height:inherit;
z-index:2;
margin-left:64%;
}
#left-hand-side {
z-index:3;
height:inherit;
width:64%;
float:left;
position:absolute;
}
#left-hand-side article {
background-color:#FFF;
margin-left:10%;
margin-top:10%;
margin-right:10%;
border:#666 1px solid;
position:absolute;
padding-top:2em;
}
#left-hand-side .entry-content {
padding: 0.5em;
width: 85%;
}
#left-hand-side .entry-header {
width: 85%;
}
It isn't containing it because on the #right-hand-side you have a margin on your image of 90px so that will force the image out of the box.
http://jsfiddle.net/Cb5h8/
What you are experiencing is that elements to not automatically resize to contain their floated child elements. The term used for the remedy is "Clearing floats". There are multiple ways to do that, the easiest one is probably just to add overflow:auto; to the container. It will force the container to contain its floated children.
Detailed information can be seen here: http://www.quirksmode.org/css/clearing.html
#container {
margin-top:-24px;
border-top: 1px solid #ddd;
border-right: 1px solid #ddd;
border-left: 1px solid #ddd;
position:relative;
background: url("../../../resources/plan_edited.png") no-repeat;
background-size:contain;
height: 100%;
width: 100%;
overflow: auto;
}
EDIT:
The #left-hand-side and #right-hand-side styles are positioned absolutely, which prevents the #container from properly containing them. You need to remove the position:absolute; styles. If you really need the position:absolute; styles, then the only way for you to accomplish this is by giving the container a specific height, which is enough for it to contain it's children.

Styling navigation bar: alternative to display:table/table-cell

For a website project, we have a simple navigation bar where each link is outlined in gray, and hovering outlines it in black.
I got it working and my code is here: http://jsfiddle.net/BpjK8/ -- although I admit it's a little dirty and 'hacky' in a way. (You can see when hovering link A, the border of link B beside it is visible, making it somewhat distracting.)
My question is: can this be done in a better way without using display: table and display: table-cell? It would be nice if the answer involved display: inline or float: left but I can't get the border effect working properly.
Demo Fiddle
Of course! Add:
body{
text-align:center; /* centre the content */
}
Then set your ul to display:inline-block to allow it to be subject to the centering, and give your li:
float:left;
display:block;
So they display one after another correctly.
You may not want to centre your entire document however, so instead you may want to wrap the ul in a div with text-align:center; set.
Try this css:
ul {
margin: 50px auto;
list-style:none;
}
li {
display:block;
float:left;
border: 1px solid #CECECE;
border-right: 1px solid #fff;
padding: 2px;
}
li:last-child {
display:block;
float:left;
border: 1px solid #CECECE;
border-right: 1px solid #CECECE;
padding: 2px;
}
li:hover {
border: 2px solid #000;
position: relative;
z-index: 9999;
padding: 1px;
}
a {
position: relative;
text-decoration: none;
line-height: 40px;
padding: 20px;
color: #000;
}

need to align list items to center of div

I want to align a list of links to be centered within a div.
I tried margin: 0 auto, but no luck.
Note: the navigation-wrapper div needs to stay in place, this is just the snippet of html that is of concern.
Check http://jsfiddle.net/bkmorse/aaCY7/ to see the links that I need to align properly in the div.
html
<div id="navigation-wrapper">
<ul id="top-navigation">
<li>Home</li>
<li>Volunteer</li>
<li>Support</li>
<li>Educate</li>
<li>Contact</li>
<li>Gift Shop</li>
<li>Directions</li>
</ul>
</div>
css
#navigation-wrapper {
width:465px;
border:1px solid red;
}
#top-navigation {
margin: 0 auto;
border:1px solid blue;
padding:5px 0;
}
#top-navigation li {
display:inline;
list-style-type:none;
margin:2;
}​
Change your CSS to this. That works out.
#navigation-wrapper {
width:465px;
border:1px solid red;
margin:0 auto;
text-align:center;
}
#top-navigation {
border:1px solid blue;
padding:5px 0;
}
#top-navigation li {
display:inline;
list-style-type:none;
margin:2;
}
Margin:auto work with define width.Write like this:
#navigation-wrapper {
width:465px;
margin: 0 auto;
border:1px solid red;
text-align:center;
}
#top-navigation {
display:inline-block;
border:1px solid blue;
padding:5px 0;
}
check this http://jsfiddle.net/aaCY7/1/
Since your top-navigation ul doesnt have a set width it is actually centered. However the li's within it are not. If you want to see those items centered then
#top-navigation {
margin: 0 auto;
border:1px solid blue;
padding:5px 0;
text-align: center;
}
If you want that list centered in the div but still left align the things inside use this
#top-navigation {
margin: 0 auto;
border:1px solid blue;
padding:5px 0;
width: 300px;
}
or do both if some combination of the two is needed.
i just need to know the display inline for the li items that woked for me. In case of center align we can add tex-align: center in the main div. In the above case
#top-navigation {
margin: 0 auto;
border:1px solid blue;
padding:5px 0;
text-align: center;
}
This should worked