On my markup I expect the footer with 25 pixels of bottom margin. I did this:
div.footer {
margin-bottom: 25px;
}
Without success — there is no bottom margin. If I do this:
div.footer {
margin-bottom: 25px;
float: left;
}
It works!
I know! I can solve the problem with floating, but it is a good practice? There is no other way?
Thanks in advance.
== UPDATE! / A little piece of my code ==
CSS:
div.rlside-margin {
margin: 0 25px;
}
div.tpside-margin {
margin: 25px 0;
}
div.allside-margin {
margin: 25px;
}
div.max-width {
width: 60em;
margin: 0 auto;
}
div.header {
background-color: #efefef;
width: 100%;
height: 90px;
}
div.post-header {
background-image: url("/Images/PostHeader_Background.jpg");
width: 960px;
height: 50px;
}
div.content {
position: relative;
}
div.footer {
margin-bottom: 25px;
}
HTML:
<div class="header">
<div class="max-width">
<ul class="navigation float-right">
<li>Home</li>
<li>Sobre</li>
<li>Fale conosco</li>
</ul>
</div>
</div>
<div class="max-width">
<div class="post-header">
<ul class="options float-left">
<li><span>Ofertas</span></li>
<li><span>Lista de compras (0)</span></li>
</ul>
<div class="search-bar float-right">
<form>
<input class="float-left" type="text" placeholder="Por qual produto você procura? Digite aqui" />
<button class="magnifier"></button>
</form>
</div>
</div>
</div>
<div class="content">
#RenderBody()
</div>
<div class="max-width">
<div class="footer">
<div class="tbside-margin">
<hr />
<ul class="bottom-navigation">
<li>Home</li>
<li>Sobre</li>
<li>Fale conosco</li>
</ul>
</div>
</div>
</div>
This is occurring because you've taken div out of the "normal flow." You've likely floated other elements, effectively taking them out of the main flow. Now that you want to position the div with margins it's not relative to the elements that have been taken out of the normal flow. That's why adding the float works, it places it into a new flow.
"A floated box is positioned within the normal flow, then taken out of
the flow and shifted to the left or right as far as possible. Content
may flow along the side of a float. [...] When a box is taken out of
normal flow, all content that is still within normal flow will ignore
it completely and not make space for it."
http://coding.smashingmagazine.com/2007/05/01/css-float-theory-things-you-should-know/
That's gonna depend a lot on your layout. Showing some html or URL would help. But, as Christian said, setting up the position would help. You might also try with position:relative; or also, instead of margin, try padding-bottom:25px;
I've solved!
At my footer's navigation, I have the follow markup:
<ul class="bottom-navigation">
<li>Home</li>
<li>Sobre</li>
<li>Fale conosco</li>
</ul>
And my CSS:
div.footer ul.bottom-navigation li {
float: left;
}
The question is: I can't float my menu items to the left. Why? I don't know, but I did this to resolve:
div.footer ul.bottom-navigation li {
display: inline;
}
It works — but I don't know why. The explanation will be very welcome.
Thanks!
You need to clear the float on the floated item's parent element.
Some left floated text
Some more left floated text
ul li span {
float: left;
}
.clearfix {
*zoom: 1;
}
.clearfix:before, .clearfix:after {
content: " ";
display: table;
}
.clearfix:after {
clear: both;
}
Related
For my class I have to have 3 divs floated left in a row with the outer two half the size of the middle one. It's driving me crazy that the rows aren't centered on the page. Is there a way to center them without getting rid of the float?
I tried creating a container div with text-align just as a shot in the dark but that didn't work. All other research I've seen is to change the float to display but I have to use float so I can't do that.
div.container {
width: 100%;
text-align: center;
border: none;
background-color: pink;
height: 100%;
}
div.cover {
width: 20%;
}
div.author {
width: 50%;
font-family: calibri;
}
div.links {
width: 20%;
}
<div class="cover">
<p class="inner">
<img src="Images/Divergent.jpg"><br>
</p>
</div>
<div class="author">
<p class="inner" style="margin-top: 10px;">
<b>Divergent<br>Veronica Roth</b><br>
</p>
</div>
<div class="links">
<ul>
<li>
<p class="link" onclick="parent.open('https://www.britannica.com/biography/Veronica-Roth')">
https://www.britannica.com/biography/Veronica-Roth
</p>
</li>
</ul>
</div>
Your instinct to wrap the 3 "columns" in a container div is correct. This allows you to use what is commonly referred to as the "clearfix" trick. Items that are "floated" are ignored by the normal box flow of the page which is why the container seems to collapse and ignore your floating contents.
Frustrating indeed!
This is the "clearfix":
div.container:after {
content: ''; /* no content in this pseudo element */
display: table; /* be 100% wide */
clear: both; /* clear the previous floats */
}
The :after pseudo selector on the container is the same thing as putting an empty div as the last item in the container. By clearing the floats, the container will wrap around the floating items.
This is a hack... but it works! The entire web development community has used this to "fix" the difficulties inherent with using floats for years to create layouts before the advent of real layout systems like Flexbox and CSS Grid.
After the container clears the floating items inside, just set the widths so that they add up to 100% and you are good.
div.container {
width: 100%;
text-align: center;
border: none;
background-color: pink;
height: 100%;
}
div.container:after {
content: '';
display: table;
clear: both;
}
div.cover {
width: 25%;
float: left;
}
div.author {
width: 50%;
font-family: calibri;
float: left;
}
div.links {
width: 25%;
float: left;
}
<div class="container">
<div class="cover">
<p class="inner">
<img src="Images/Divergent.jpg"><br>
</p>
</div>
<div class="author">
<p class="inner" style="margin-top: 10px;">
<b>Divergent<br>Veronica Roth</b><br>
</p>
</div>
<div class="links">
<ul>
<li>
<a href="https://www.britannica.com/biography/Veronica-Roth">
Veronica-Roth
</a>
</li>
</ul>
</div>
</div>
div.container position:relative;
div.cover float:left;
div.autor put inside tag align="center"
div.links float:right;
or
div.autor margin:5%;
or
display:inline-block;
or you can use text-align:center; in css on the parent div and then display:inline-block; on each div inside the parent div
When I useclass="top-menu" and class="pull-right" these two together I get the result is the image is pull-right but the background color does not change to black, What is wrong with my code?
If I delete the class="pull-right" the background color becomes black
.top-menu {
background-color: black;
}
.pull-right {
float: right;
}
<header>
<div class="header-top">
<div class="container-fluid top-menu">
<img src="..." alt="Awsome_Ticket_Logo" class="pull-right">
</div>
</div>
</header>
When you float something, it is no longer part of its parent's bounding box. Since there is nothing else in your top-menu parent, the bounding box will be considered empty and it will have a height of 0.
A common solution to this is to add a clearfix to the parent. This will make it include whatever space was taken up by its floated children:
.top-menu {
background-color: black;
}
.pull-right{
float: right;
}
.top-menu:after {
content: "";
clear: right;
display: table;
}
<header>
<div class="header-top">
<div class="container-fluid top-menu">
<img src="https://via.placeholder.com/150" alt="Awsome_Ticket_Logo" class="pull-right">
</div>
</div>
</header>
That's because when things float the container becomes empty, you can use a clearfix: https://jsfiddle.net/wwWaldi/nrysp61w/18/
.clearfix::after {
content: "";
clear: both;
display: table;
}
I see a lot of developers has this problem with their code. Normally, This issue comes with bad styling of your page structure. Also, the first solution that many developers come with is to use clear: both; styling in pseudo. But, I think if you learn how to style like a good developer you will never need to use clear. Just start to write your page in a normal and standard way.
When you give a float to the child of a parent which doesn't have a float property, The parent will lose its height (if its the only child of that parent). The best way to avoid this happening its to divide the sections of your page and give them a good floating. This way you'll never need to use clear styling.
header,
.header-top,
.top-menu {
width: 100%;
float: left;
}
.top-menu {
padding: 5px;
background-color: red;
}
img{
width: 100px;
height: 100px;
display: block;
float: left;
background: blue;
}
<header>
<div class="header-top">
<div class="container-fluid top-menu">
<img src="" alt="Awsome_Ticket_Logo" class="pull-right">
</div>
</div>
</header>
I have a menu in the top of my website with this css:
.menu {
width: 100%;
display: block;
float: left;
}
inside of it, I have few divs:
.menu .menu-item {
position: relative;
width: 260px;
float: left;
height: 430px;
}
This is all good, but when I try to add a small div underneath the menu, with this HTML structure:
<div class="menu">
<div class="menu-item">
</div>
</div>
<div class="menu-bar">
</div>
and this css:
.menu-save {
position: relative;
display: block;
margin: 0 auto;
width: 100%
height: 20px;
}
With this CSS my expected output is that the menu-bar div goes underneath the whole menu, but what I'm currently getting is that menu-bar sits inside of menu, at the top of it. What CSS am I missing?
I think
use clear: both CSS property to avoid the floating problem
<div class="menu">
<div class="menu-item">
</div>
</div>
<div class="clear"></div>
<div class="menu-bar">
In Css add this one
.clear{
clear:both;
}
I'm having some problems with my css, my 2 div's are getting under eachother. I want them both next to eachother.
I'm having a body with a user menu and content div's inside.
HTML:
<div id="body">
<div id="user-menu">
#if (Request.IsAuthenticated)
{
<ul id="account-menu">
<li>#Html.ActionLink("My profile", "MyProfile", "Profile")</li>
<li>#Html.ActionLink("Links", "Links", "Profile")</li>
<li>#Html.ActionLink("History", "History", "Profile")</li>
<li>#Html.ActionLink("Credits", "Credits", "Profile")</li>
<li>#Html.ActionLink("Settings", "Manage", "Account")</li>
</ul>
}
</div>
<div id="content">
#RenderBody()
</div>
</div>
<div id="footer">
<p>© #DateTime.Now.Year - Immo QR by eNetricity.com</p>
</div>
</div>
CSS:
#body
{
margin: 0 auto;
max-width: 960px;
}
#user-menu
{
float: left;
width: 15%;
}
#content
{
float: right;
width: 85%;
}
Image:
Greets
Mathias,
you have closed a div unnecessarily, please remove that.
also give float left to both div.
Two things come to mind which should help fix the problem:
Since the right-floating <div> comes after the left-floating <div> it may be moving to the next line prior to floating. Try floating both elements left.
Sometimes browsers round up to the nearest pixel when calculating sizes. Does your code work better with a 10/90 split? 20/80? If your full container is 960px, try calculating the column sizes manually in pixels.
Try setting border: 0 on both the floated elements because unless you're resetting your CSS via a reset or normalise, they will inherit a nominal border:
#body
{
margin: 0 auto;
max-width: 960px;
}
#user-menu
{
float: left;
width: 15%;
}
#content
{
float: right;
width: 85%;
}
#content, #user-menu{
border: 0;
}
#footer{
clear: both;
}
Heres a codepen:1:
HTH
When inspecting my elements, i noticed my height on the first li is very low.
I'm trying to add a background to the whole li after selecting the "city (2)".
The background fills only City(2) an half of New York. I tried without floating, but then i can't get them lined up.
I've added 2 photos. One where i have floating, and one without.
<ul class="specs">
<li>
<div class="trigger">› City (2)</div>
<div class="cityByLocation">
<ul>
<li class="cityInfo">
<div class="cityName" style="float: left;">
› New York
</div>
<div class="cityDistance" style="float: left;">
430 miles
</div>
<div class="btn-take"></div>
</li>
<li class="cityInfo">
<div class="cityName" style="float: left;">
› Chicago
</div>
<div class="cityDistance" style="float: left;">
430 miles
</div>
<div class="btn-take"></div>
</li>
</ul>
</div>
</li>
<ul>
css:
.cityName{
float: left;
width: 45%;
}
.cityDistance {
float: left;
width: 20%;
}
.specs {
padding: 0;
}
.specs li {
padding: 0;
padding-top: 10px;
list-style: none;
}
.specs ul {
list-style: none;
padding: 0;
}
You need to clear your floats as Eric said. The simplest way to achieve that is by applying overflow: hidden to the container, so:
.cityByLocation {
overflow: hidden;
}
http://jsfiddle.net/a3GLG/
I think this will solve your problem.
The problem happens when a floated element is within a container box, that element does not automatically force the container’s height adjust to the floated element. When an element is floated, its parent no longer contains it because the float is removed from the flow.
You can use 2 methods to fix it:
{clear:both} or .clearfix
.clearfix:after {
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
.clearfix {
display: inline-block;
}
html[xmlns] .clearfix {
display: block;
}
* html .clearfix {
height: 1%;
}
source
You can also fix it by setting overflow: hidden to li element. It will clear and auto calculate height.
-- update --
thanks #mickmackusa
https://jsfiddle.net/e94pzbbn/1/