CSS style formatting - css

My footer and its content do not re-position proportional to each other when I reduce the height of the footer.
CSS code:
#footer {
clear: right;
background: #d1dceb;
text-align: right;
padding: 20px;
width: 70%;
float: right;
height: 5px;
}
#footer p.left {
float: left;
text-align: left;
margin-left: 5px;
}
#footer p.right {
float: right;
text-align: right;
margin-right: 5px;
}
And this is what I am getting:
Anything I should do to resolve this?

Remove height and add overflow: hidden:
#footer
{
clear: right;
background: #d1dceb;
text-align: right;
padding: 20px;
width: 70%;
overflow: hidden;
}

<div style="clear:both;"></div>
just before footer div ends and also try removing height from footer

Well, you're not adding any context and any HTML markup. But this is important:
#footer {
clear: right; /* why are you doing this? **/
background: #d1dceb;
text-align: right;
padding: 20px;
width: 70%;
float: right; /* why are you doing this? **/
height: 5px;
}
take a look at those commented lines, which quite probably you don't need at all.
Now, into your issue, you can use two options:
1) clear floats by using the "clearfix" method: simply add an empty div that clears the floats of preceding elements, like this:
<div class="clearfix"></div>
and then in CSS:
.clearfix{clear:both; float:none;}
Obviously you can use this as many times as you want since you're using re-usable classes.
The option 2 is as follows:
#footer p.right:after {content:'';clear:both; float:none; }
What we do here is to add some "empty" content, yet we assign it a "clear:both" property to clear everything, more or less as if we have added that div in option 1
Of course option 1 is way better, but well, there you go

You're using float to position elements, which means that the height of the floated elements are set to 0, just like if you're using positioning: absolute. DON'T use floats! Use flex.
#footer {
background: #d1dceb;
padding: 20px;
width: 70%;
display: flex;
margin: 0 auto; /* center element */
}
#footer p {
flex: 1 1 auto; /* fill up the entire available space */
}
#footer p.right {
text-align: right;
}
<div id="footer">
<p>Left footer element</p>
<p class="right">Right footer element</p>
</div>

Related

How can you define the width of a container in % relatively to an element that is NOT its direct parent?

I have a header as follow: logo + nav containing 4 links
I would like to arrange all this element next to each other at the top (that is working), but also to make them at equal distance of each other. The second part does not work, I don't manage to define the size of the a elements in % ...
I am using float:left to position all this element on top next to each other. I am using the css property width to make them occupy 20% each of the total top of the page.
<body>
<header>
<img src="mylogo.png" style="width:42px;height:42px">
<nav>
Welcome
About
Art Work
Events
</nav>
</header>
<h1>Title of the page</h1>
</body>
* {
box-sizing: border-box;
padding: 0px;
margin: 0px;
}
header {
width: 100%;
}
.logo {
float: left;
width: 20%;
}
nav {
float: left;
}
nav a {
width: 20%;
}
There is some space between the logo and the links, but the links does not arrange along the top at equal distance, they stay stuck to each other... I suppose it's because their width is relative to nav, which is not 100% as there is the logo. But I don't know how to define the size of these a elements relatively to the header that I fixed to be 100% of my page?
Here's my solution. I made some changes in your css and instead of float I used flex-box technique to align them. I made the header black to detect the header easily. You can change it in the css. Hope this solution will help you.
* {
box-sizing: border-box;
padding: 0px;
margin: 0px;
}
header {
height: 10vw;
line-height: 10vw;
width: 100%;
background-color: #000;
}
.logo img {
vertical-align: middle;
}
a {
display: inline-block;
height: inherit;
width: 20%;
text-align: center;
line-height: inherit;
vertical-align: bottom;
transition: all .33s ease-in-out;
}
a:hover {
background-color: white;
}
<body>
<header>
<img src="mylogo.png" style="width:42px;height:42px"><!-- -->Welcome<!-- -->About<!-- -->Art Work<!-- -->Events
</header>
<h1>Title of the page</h1>
</body>
I found out that the following CSS worked:
header {
width: 100%;
display: inline-block;
}
.logo {
float: left;
width: 20%;
}
header nav {
width: 80%;
float: left;
}
header nav a {
width: 20%;
float: left;
text-align: center;
}

why the `img right` collapse?

JSFiddle.
<div class="content clearfix">
<div class="left">img Left</div>
<div class="center">Text Center</div>
<div class="right">img Right</div>
</div>
.content {
width: 100%;
height: 80px;
border:1px solid #333;
}
.center {
width: 400px;
margin:0 auto;
text-align: center;
}
.left {
float: left;
}
.right {
float: right;
}
I used float to layout the page. the left and the center element perform well. But the img Right was broken. I can't find the reason and what's more I used line-hegiht, the collapse is worse. Thanks for your advantage.
Your .center element is not floated and is centered. If you inspect it, you see its horizontal margins going on left and right (margin : 0 auto;).
For the .left element, it's ok, it comes before the .center element and it is out of the float, so the .center element just ignore it.
But the .right element comes after, and it has to consider the previous HTML element (here : .center). That's why .right element is going under .center element.
So, some solutions :
you can use absolute positioning for the .right element : position: absolute; top: 0; right: 0; (there : http://jsfiddle.net/pg7v4js3/2/ )
other solutions depend on what is the real layout you try to achieve
This is a new fiddle: Fiddle
.clearfix:after{
}
.content{
width: 100%;
height: 80px;
border:1px solid #333;
}
.center{width: 80%; float: left; text-align: center;}
.left{float: left; width:10%;}
.right{float: right; width:10%;}

How to put some divs in a row?

I'm trying to put two divs without a linebreak between them.
this is the html:
<div id="header">
<div id="logo"></div>
<div id="left">
<div id="slideshow"></div>
</div>
</div>
and this is the CSS:
#header {
background-color: #13768a;
width: 962px;
height: 207px;
margin-right: auto;
margin-left: auto;
clear: both;
}
#logo {
background-image:url('logo.png');
height: 207px;
width: 250px;
margin-right: 0px;
padding: 0px;
}
#left {
width:712px;
height: 207px;
}
#slideshow {
background-color: #137387;
width: 686px;
height: 144px;
margin-right: auto;
margin-left: auto;
}
the problem is that I want it to look like this:
How I want it to look like
But it looks like this:
How it looks like
This is controlled by the display style property. Normally, div elements use display: block. You can use display: inline or display: inline-block instead if you want them on the same horizontal line.
Example using inline-block (live copy | source):
CSS:
.ib {
display: inline-block;
border: 1px solid #ccc;
}
HTML:
<div class="ib">Div #1</div>
<div class="ib">Div #2</div>
Introduce a float CSS property. Change CSS as below, for #logo and #left.
#logo {
background-image:url('logo.png');
height: 207px;
width: 250px;
margin-right: 0px;
padding: 0px;
float:right;
}
#left {
width:712px;
height: 207px;
float:left;
}
From the MDN Documentation,
The float CSS property specifies that an element should be taken from
the normal flow and placed along the left or right side of its
container, where text and inline elements will wrap around it.
Div elements normally use display:block which forces a line break before and after the element.If you want to remove the line breaks , you can use display:inline which will display elements horizontally.Make the div's display property to display:inline or display:inline-block you want to appear horizontally .
Try this way:
#logo {
background-image:url('logo.png');
height: 207px;
width: 250px;
margin-right: 0px;
padding: 0px;
float:right;}
#left {
position:relative;
width:712px;
height: 207px;
}
#slideshow {
position:absolute;
top:20px;
left:20px;
background-color: #137387;
width: 686px;
height: 144px;
margin-right: auto;
margin-left: auto;
}​
Basically I put a float:right; on the logo to position it right, then added position:relative to the #left div and position:absolute to the #slideshow div. This way you can adjust the top and left attributes to position the slideshow anywhere you want it.
display:inline is the css style that you need to use.

Need some help with getting my DIV centered

I have the following code:
<div id="ftr_btm">
<div id="ftr_ctr">
<div class="hdr_lnk">
<ul>
<li><a>Test1</a></li>
<li><a>Test2</a></li>
<li><a>Test3</a></li>
</ul>
</div>
</div>
</div>
and the following CSS:
#ftr_ctr {display: block; text-align: center; font-size:0.8em; position: absolute; height: 24px;margin: auto;}
.hdr_lnk ul li {
display: inline;
float: left;
padding: 0;
position: relative;
}
What I am trying to do is have the text (address links) appear horizontally centered with margins to each side of the UL's. It's not working and the text and UL's all goes to the left as in this:
fiddle
Is there anyone who could tell me how I can get the UL's to appear in the middle of the page.
thanks
I'm not quite sure of the context that you're putting that code in, but this should achieve the effect you want:
#ftr_ctr {
width: 100%;
text-align: center;
font-size:0.8em;
position: absolute;
height: 24px;
}
.hdr_lnk ul{
margin: auto;
}
.hdr_lnk li {
display: inline;
padding: 0;
}
The main things you needed were the width: 100%; rule in the #ftr_ctr element and the margin: auto; rule in the .hdr_lnk ul rule. (The width doesn't have to be 100%, but it needed to be set to stop the element from shrink-wrapping its contents). margin: auto; will centre contents equally vertically or horizontally or margin: 0 auto; will centre contents horizontally.

Login seems to be overflown from div

This is the header section which contains logo on the left side and login link on the right within the same div. I've been trying to get login text to not been overflown from the #header_section.
What's wrong with my code? By the way, I just start learning CSS.
div#container
{
background: url(../images/bg_inner.png) repeat;
margin-left: auto;
margin-right: auto;
width: 950px;
text-align: left;
}
div#header_section
{
width: 930px;
height: 65px;
position: relative;
margin: 5px;
}
#logo
{
background: url(../images/logo.png) left no-repeat;
margin: 5px;
border: none;
height: 55px;
width: 200px;
}
.login
{
font-size: 22px;
color:#4A4A4A;
width: 60px;
float: right;
}
<div id="header_section">
Login
</div>
Wild-guess: change
div#header_section
{
overflow: hidden;
...rest of stuff
}
Maybe you need a clearing div within your header section if the text is overflowing when it shouldn't. I'm only guessing this as you float you're login element (I'm assuming it's a div) to the right. It's difficult to tell without a detailed screenshot.
div#headerSection {
...
overflow: auto;
width: 100%
}
When you float an element you want to put it before the element that isn't floated. And as others have mentioned, you probably want a "clearing div" afterward, even if it works, just for good measure.
Basically, try this:
<div id="header_section">
Login
<div class="clear"></div>
</div>
Then define the following for the "clearing div":
.clear {
clear: both;
}

Resources