HTML Container Element doesnt have height [duplicate] - html

This question already has answers here:
How come my float elements aren't within their parent
(2 answers)
Closed 8 years ago.
The Header part of my template doesn't get the height of its nested Elements.
<header id="header">
<div id="header-inner">
<div id="top-left">
Site Title
</div>
<nav id="top-right">
<div class="menu">
<ul>
<li class="current_page_item">Home</li>
<li class="page_item page-item-2">Home2</li>
</ul>
</div>
</nav>
</div>
</header>
#header {width:100%; float:left;}
#header-inner {width:600px; margin:0 auto;}
#top-left {float:left;}
#top-right {float:right;}
I also made a jsfiddle:
http://jsfiddle.net/hqpb3cyc/
The only solution I know is to give the #header and / or #header-inner float:left; or display:inline-block; But I think thats not the right way to do this!?
Hope someone can help me
best regards

You have to clear floats. For example:
<br style="clear: both" />
#header {
width: 100%;
float: left;
background: #D3D3D3;
}
#header-inner {
width: 600px;
margin: 0 auto;
}
#top-left {
float: left;
}
#top-right {
float: right;
}
<header id="header" role="banner">
<div id="header-inner">
<div id="top-left">
Site Title
</div>
<nav id="top-right">
<div class="menu">
<ul>
<li class="current_page_item">Home
</li>
<li class="page_item page-item-2">Home2
</li>
</ul>
</div>
</nav>
</div>
<!-- clear both floats -->
<br style="clear: both" />
</header>
Addinional you can use pseudo-element :after to clear floats:
#header {
width: 100%;
float: left;
background: #D3D3D3;
}
#header-inner {
width: 600px;
margin: 0 auto;
}
#top-left {
float: left;
}
#top-right {
float: right;
}
#header-inner:after {
content: "";
clear: both;
}
<header id="header" role="banner">
<div id="header-inner">
<div id="top-left">
Site Title
</div>
<nav id="top-right">
<div class="menu">
<ul>
<li class="current_page_item">Home
</li>
<li class="page_item page-item-2">Home2
</li>
</ul>
</div>
</nav>
</div>
</header>

You should clear the floated inner element. You don't even have to change your HTML. You can do that in CSS as well by using the after pseudo element:
#header {width:100%; float:left;}
#header-inner {width:600px; margin:0 auto; border: 1px solid red;}
#top-left {float:left;}
#top-right {float:right;}
#header-inner::after{
display: block;
content: "";
clear: both;
}
<header id="header" role="banner">
<div id="header-inner">
<div id="top-left">
Site Title
</div>
<nav id="top-right">
<div class="menu">
<ul>
<li class="current_page_item">Home</li>
<li class="page_item page-item-2">Home2</li>
</ul>
</div>
</nav>
</div>
</header>
Updated fiddle

Related

HR doesn't appear in proper place

Trying to separate my header and navtab with a simple horizontal line. Nothing fancy.
Yet, it appears above the header for some reason. I know it has something to do with floating of twitter button and company logo. Before I did this the line appeared as it should. I'm stuck here.
.main_header {
background: #d0d0d0
}
.company_logo {
float: left;
text-align: left;
padding: 10px 0 10px 50px;
}
.twitter {
float: right;
margin-top: 10px;
margin-right: 10px;
}
<header class="main_header">
<div class="company_logo">
<img src="images/logo.png" width="20%">
</div>
<div class="twitter">
</div>
</header>
<hr>
<nav class="navbar">
<div class="container">
<ul>
<li>Home</li>
<li>About</li>
<li>Portfo</li>
<li>Servi</li>
<li>Contact</li>
</ul>
</div>
</nav>
You're right. Since the children of .main_header are floated, their parent has no height.
One solution is to clear the floats.
Below, I'm using group class and one of the clearfix methods.
For further reference, see What methods of ‘clearfix’ can I use?
.main_header {
background: #d0d0d0
}
.company_logo {
float: left;
text-align: left;
padding: 10px 0 10px 50px;
}
.twitter {
float: right;
margin-top: 10px;
margin-right: 10px;
}
.group::after {
content: "";
display: block;
clear: both;
}
<header class="main_header group">
<div class="company_logo">
<img src="images/logo.png" width="20%">
</div>
<div class="twitter">
</div>
</header>
<hr>
<nav class="navbar">
<div class="container">
<ul>
<li>Home</li>
<li>About</li>
<li>Portfo</li>
<li>Servi</li>
<li>Contact</li>
</ul>
</div>
</nav>
Try :
hr{
clear: both;
}
Should clear floating elements on either side.

Positioning one element next to a non-neighboring element with only css?

<header>
<div>
<h1>Title</h1>
</div>
<div id="nav">
<nav>
<ul>
<li>foo</li>
</ul>
</nav>
</div>
</header>
<div id="main">
content!
</div>
I have the following page layout, where the title div renders across the top of the window, then div#nav renders below it, and div#main renders below it:
<body>
<header>
<div>
<h1>Title</h1>
</div>
<div id="nav">
<nav>
<ul>
<li>foo</li>
</ul>
</nav>
</div>
</header>
<div id="main">
content!
</div>
</body>
Is it possible, using only CSS and without changing the DOM, to position the div#nav (or its child nav element) to the right side of div#main?
Yes you can do it like this, https://jsfiddle.net/Lddyn573/1/
header > div{width: 100%; background-color: lightblue}
header > #nav{float: right; width: 50%; background-color: lightgreen}
#main{float: right; width: 50%; background-color: lightpink}
you can do it with position:absolute
check this jsfiddle
code :
header {
position:relative;
float:left;
width:100%;
background:blue;
}
#nav {
position: absolute;
right: 0;
background: yellow;
top: 100%;
width: 50%;
}
#main {
background: red;
float: left;
width: 50%;
}

How to move text list items to align with an image list item

Hi new to CSS here could anyone help me get the text to move down so it's aligned with my logo image? Also as an extension on that question does anyone know how I can make the text move across so the margins on either side match?
This is what it looks like now:
Here is the html:
<div id="header">
<div class="wrap">
<nav id="topnav"> <!-- This is the top nav bar-->
<ul id="topnavlist">
<li> <!-- Home logo-->
<div class="logo">
<img src="images/TechNow Logo 0.2.jpg" alt="TechNow Logo" height="70" width="auto">
</div>
</li>
<div class="navItems"><!-- Nav items-->
<ul>
<li>UK News</li>
<li>Smartphones</li>
<li>Reviews</li>
</ul>
</div>
</ul>
</nav>
</div>
</div>
And the CSS:
#header, #footer {
background-color:#115279;
float:left;
padding:15px 0;
min-width:100%;
}
.wrap {
position:relative;
margin:0 auto;
width:960px;
}
#topnavlist li {
font-family: verdana, sans-serif;
color:#D9330F;
list-style-type: none;
float: left;
display:inline;
padding: 10px;
}
.navItems {
height: 20%;
display: inline;
margin-top:30px;
padding-top: 50px;
padding: 50px;
}
Your HTML is invalid, you can't have div inside li. Replace div with li and wrap their children(lis) in a ul.
To vertically center the lis, give .navItems a height: 50px same as the height of your logo and give it a line-height: 50px(height)
#header,
#footer {
background-color: #115279;
float: left;
padding: 15px 0;
min-width: 100%;
}
.wrap {
position: relative;
margin: 0 auto;
width: 960px;
}
#topnavlist li {
font-family: verdana, sans-serif;
color: #D9330F;
list-style-type: none;
float: left;
display: inline;
padding: 10px;
}
.navItems {
display: inline;
height: 50px;
line-height: 50px;
}
<div id="header">
<div class="wrap">
<nav id="topnav">
<!-- This is the top nav bar-->
<ul id="topnavlist">
<li>
<!-- Home logo-->
<div class="logo">
<a href="index.html">
<img src="http://dummyimage.com/100x50/000/fff" alt="TechNow Logo" height="70" width="auto">
</a>
</div>
</li>
<li class="navItems">
<!-- Nav items-->
<ul>
<li>UK News
</li>
<li>Smartphones
</li>
<li>Reviews
</li>
</ul>
</li>
</ul>
</nav>
</div>
</div>
You can't have a div element as a child to a ul element. As for aligning all of your elements. By default, the img and a elements are considered inline elements in css. You can use the vertical-align property on inline elements. So your markup could be a bit more simple like
<nav id="topnav">
<img src="images/TechNow Logo 0.2.jpg" alt="TechNow Logo" height="70" width="auto">
UK News
Smartphones
Reviews
</nav>
and then the css
#topnav a {
vertical-align: middle;
}
Since I don't have the exact size of your image, this won't get you exactly what you're looking for, but it should get you on the right track. The main thing is to make sure you have valid HTML markup. This can cause many headaches and issues with your css if it's not.
As mentioned your current HTML is invalid. limust be children (and the only direct children) of a ul.
In fact there is no need to use divs at all.
#header,
#footer {
background-color: #115279;
float: left;
padding: 15px 0;
min-width: 100%;
}
.wrap {
position: relative;
margin: 0 auto;
width: 960px;
}
#topnavlist li {
font-family: verdana, sans-serif;
color: #D9330F;
list-style-type: none;
display: inline-block;
vertical-align: middle;
padding: 10px;
}
.NavItem {
color: white;
}
<div id="header">
<div class="wrap">
<nav id="topnav">
<!-- This is the top nav bar-->
<ul id="topnavlist">
<li>
<!-- Home logo-->
<a href="index.html">
<img src="http://placehold.it/70x70" alt="TechNow Logo" height="70" width="auto" />
</a>
</li>
<li>UK News
</li>
<li>Smartphones
</li>
<li>Reviews
</li>
</ul>
</nav>
</div>
</div>

horizontal centering elements in div

I am wondering how to center elements within a div but keep the text left aligned.
Here is a jfiddle of what I have so far. I want the text to be in the middle of the div but to maintain its left justification.
http://jsfiddle.net/MgcDU/5994/
HTML:
<div class="span4" id="middleFooter">
<div class="mcont" >
<div class="mrow"> <h5 class="tcell"><b>Useful Links</b></h5> </div>
<ul>
<div class="mrow"> <li class="tcell">Contact Us</li> </div>
<div class="mrow"> <li class="tcell">About Us</li> </div>
<div class="mrow"> <li class="tcell">Copyright Information</li> </div>
<div class="mrow"> <li class="tcell">Terms & Conditions</li> </div>
</ul>
</div>
</div>
CSS:
#middleFooter {
color: #ccc2a0;
}
.mcont {
background-color: blue;
}
.mrow li {
background-color: red;
margin-left: auto;
margin-right: auto;
}
.mrow h5 {
display: table-row;
background-color: yellow;
}
.tcell {
display: table-cell;
}
You can try something like this - http://jsfiddle.net/GxgrL/
html:
<div class="span4" id="middleFooter">
<div class="mcont" >
<div class="mrow"> <h5 class="tcell"><b>Useful Links</b></h5> </div>
<ul>
<li class="tcell">Contact Us</li>
<li class="tcell">About Us</li>
<li class="tcell">Copyright Information</li>
<li class="tcell">Terms & Conditions</li>
</ul>
</div>
</div>
css:
#middleFooter {
color: #ccc2a0;
}
.mcont {
background-color: blue;
}
li {
background-color: red;
display: block;
text-align: center;
}
li a {
background-color: green;
display: inline-block;
margin: 0 auto;
width: 170px;
text-align: left;
}
.mrow {
text-align: center;
}
.mrow h5 {
display: inline-block;
background-color: yellow;
text-align: left;
width: 170px;
}
You can get this effect by making the container div 50% width then floating it to the right. So in your fiddle add:
.mcont {
width: 50%;
float: right;
}
If you want to keep the blue background you'll need to wrap all the .mrow divs in a wrapper and apply the styles above so like:
<div class="mcont" >
<div class="wrapper">
<div class="mrow"> <h5 class="tcell"><b>Useful Links</b></h5> </div>
<ul>
<div class="mrow"> <li class="tcell">Contact Us</li> </div>
<div class="mrow"> <li class="tcell">About Us</li> </div>
<div class="mrow"> <li class="tcell">Copyright Information</li> </div>
<div class="mrow"> <li class="tcell">Terms & Conditions</li> </div>
</ul>
</div>
</div>
Then:
.wrapper {
width: 50%;
float: right;
}
Just assign your .mrow divs a width and give them margin:0px auto and they will be center aligned.
.mrow{
width:20%;
margin:0px auto;
}
Here's an example of your fiddle with that style added: http://jsfiddle.net/HcQcn/

How do i solve the issue of position of <ul>?

i created a style called "topbar" using css.
The code are as follows:
.topbar:hover ul{ display: inline;}
.topbar {
float: left;
margin-left: 20px;
margin-right: 20px;
font-family:"Georgia";
}
.topbar ul {
display: none;
top:30px;
position: absolute; border-style:solid;
border-width:1px; background-color:white;}
}
.clear {
clear: both;
}
then i went to create a ul "grid" and allows mouse hover enlarge of images
ul.grid, ul.grid > li {
margin: 0;
padding: 0;
}
ul.grid {
}
ul.grid > li {
float: left;
list-style-type: none;
text-align: center;
font-family:"Georgia", serif;
padding-top:50px;
padding-bottom:25px;
padding-right:25px;
padding-left:0px;
}
ul img:hover { width: 200px; height: 250px; }
my html code:
<body>
<div class="topbar">
<p>Title</p>
<ul>
<li>A-Z</li>
<li>Z-A</li>
</ul>
</div>
<div class="topbar">
<p>Genre</p>
<ul>
<li>Action</li>
<li>Comedy</li>
<li>Animation</li>
<li>Horror</li>
<li>Drama</li>
</ul>
</div>
<div class="clear"></div>
<br />
<div id="videos">
<ul class="grid">
<li>
<p class="image"><img src="http://3.bp.blogspot.com/-AGAaic1-yrM/TcWmj77lHzI/AAAAAAAAAkQ/K6zzSk1WgUY/s1600/thor-movie-poster-1.jpg" alt="Thor" width="175" height="200" /></p>
<p class="name">Thor</p>
<p class="genre">Action</p>
<p class="format">DVD</p>
<p class="year">2011</p>
</li>
<li>
<p class="image"><img src="http://www.galacool.com/wp-content/uploads/2010/02/hangover2.jpg" alt="Hangover" width="175" height="200" /></p>
<p class="name">Hangover</p>
<p class="genre">Comedy</p>
<p class="format">DVD</p>
<p class="year">2009</p>
</li>
</ul>
</div>
</body>
Why do i have a left unwanted space beside my "thor" movie image?
The left space must be the indentation of the List where you are putting them. Ideally, to remove this left-indentation, you should set both padding and margins to "0" for the "UL".
This small piece of code works perfectly in this context:
CSS
#navcontainer ul
{
margin: 0;
padding: 0;
list-style-type: none;
}
HTML
<div id="navcontainer">
<ul>
<li>Milk
<ul>
<li>Goat</li>
<li>Cow</li>
</ul>
</li>
<li>Eggs
<ul>
<li>Free-range</li>
<li>Other</li>
</ul>
</li>
</ul>
</div>