I have created a minimal example that represents a simple tabbing system, with tabs on the left and content on the right.
In my example, the bottom position of the tabs influences the positioning of content on the right. This only happens when I clear positioning inside the content block.
This is the minimal example's HTML, including a self-explanatory description in the content block. So what goes wrong is that the line below the clearfix is wrongly positioned. In fact, it's positioned under the left tabs' bottom.
* {
box-sizing: border-box;
}
.clearfix {
clear: both;
}
.nav-tabs {
float: left;
display: block;
list-style: none;
padding-left: 0;
margin: 0 auto 0 0;
width: 20%;
}
.nav-tabs > li {
position: relative;
display: block;
float: none;
}
.nav-tabs > li > a {
position: relative;
display: block;
padding: 10px 15px;
border: 1px solid transparent;
}
.nav-tabs > li.active > a {
border-color: blue transparent blue blue;
}
.tab-content > .tab-pane {
display: none;
}
.tab-content > .tab-pane.active {
display: block;
}
.tab-body {
position: relative;
border: 1px solid red;
padding: 20px;
width: 80%;
margin-left: 20%;
}
<ul class="nav-tabs">
<li><a>foo</a></li>
<li class="active"><a>bar</a></li>
<li><a>baz</a></li>
<li><a>boo</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane">
<div class="tab-body">
<h1>Foo content</h1>
</div>
</div>
<div class="tab-pane active">
<div class="tab-body">
<div style="float:right">
floating right
</div>
<h1>Bar content</h1>
<div class="clearfix"></div>
<p>
This line is wrongly positioned to the bottom of the tabs left, why? And how can it can corrected?
</p>
</div>
</div>
</div>
I don't know exactly what goes wrong, and therefore how to fix it... To make editing / fixing / trying all a bit easier, here's the JSFiddle.
try this
HTML
<ul class="nav-tabs">
<li><a>foo</a></li>
<li class="active"><a>bar</a></li>
<li><a>baz</a></li>
<li><a>boo</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane">
<div class="tab-body">
<h1>Foo content</h1>
</div>
</div>
<div class="tab-pane active">
<div class="tab-body">
<div class="pull-right">
clearfix
</div>
<h1>Bar content</h1>
<p>
This line is wrongly positioned to the bottom of the tabs left, why? And how can it can corrected?
</p>
</div>
</div>
</div>
CSS
* {
box-sizing: border-box;
}
.clearfix {
clear: both;
}
.nav-tabs {
float: left;
display: block;
list-style: none;
padding-left: 0;
margin: 0 auto 0 0;
width: 20%;
}
.nav-tabs > li {
position: relative;
display: block;
float: none;
}
.nav-tabs > li > a {
position: relative;
display: block;
padding: 10px 15px;
border: 1px solid transparent;
}
.nav-tabs > li.active > a {
border-color: blue transparent blue blue;
}
.tab-content > .tab-pane {
display: none;
}
.tab-content > .tab-pane.active {
display: block;
}
.tab-body {
position: relative;
border: 1px solid red;
padding: 20px;
width: 80%;
margin-left: 20%;
}
.pull-right{
float:right;
}
demo
Related
This question already has answers here:
How do I vertically align text in a div?
(34 answers)
How can I horizontally center an element?
(133 answers)
Closed 5 years ago.
I've looked at several posts here on stackoverflow and can't find an answer that solves my issue. The issue appears to be the same as many other questions, with the simple difference that I am trying to center text that will change and I want it perfectly centered.
Below is a a basic example of what I have currently; the text on the bottom is centered between the text on the left and right, where as the text on the top is centered in the DIV. The text on the bottom should reflect the same.
I have tried a multitude of solutions, from display: inline block (which decreases my spacer width) to margin: 0 auto, all the way to converting the containing div to display: table-cell, vertical align middle.
Any help will be greatly appreciated!
body {
background: #333;
color: white;
}
.container {
width: 80%;
height: 150px;
position: relative;
left: 10%;
}
.container .slide, li { display: none; }
.active { display: block !important; }
.header { text-align: center; }
ul {
margin: 0;
padding-left: 0px;
}
li {
list-style: none;
font-variant: small-caps;
}
.spacer {
width: 100%;
height: 1px;
background: white;
box-shadow: 1px 1px 15px 1px rgba(0, 150, 255, 1);
margin-top: 5px;
margin-bottom: 5px;
}
.slide {
width: 100%;
height: 79%;
background-color: white;
}
.focus { text-align: center; }
.left { float: left; }
.right { float: right; }
<div class="container">
<div class="header">
<div class="spacer"></div>
<ul>
<li class="active">Summary</li>
</ul>
<div class="spacer"></div>
</div>
<div class="slide active">
</div>
<div class="slide">
</div>
<div class="focus">
<div class="spacer"></div>
<ul class="left">
<li class="active">ASDF</li>
</ul>
<ul class="right">
<li class="active">POV - ASR</li>
</ul>
<ul>
<li class="active">LKJHGFDSAPOIURTQAJK</li>
</ul>
<div class="spacer"></div>
</div>
</div>
EDIT
This post is not able to use the answers utilized in both related questions; though they are similar in nature, setting a specific width for my LI elements to achieve perfect center is not possible with pure CSS (SCSS maybe but not CSS3). The display: table answer did not work for me. Hence the reason I asked it as a new question; I tried 15 different suggested answers before posting my question.
I would put the 3 links in the footer in a single ul, use display: flex on the ul, and flex: 1 0 0 on the li's so they'll take up the same amount of space, then use text-align in each li to position the text.
body {
background: #333;
color: white;
}
.container {
width: 80%;
height: 150px;
position: relative;
left: 10%;
}
.container .slide,
li {
display: none;
}
.active {
display: block !important;
}
.header {
text-align: center;
}
ul {
margin: 0;
padding-left: 0px;
}
li {
list-style: none;
font-variant: small-caps;
}
.spacer {
width: 100%;
height: 1px;
background: white;
box-shadow: 1px 1px 15px 1px rgba(0, 150, 255, 1);
margin-top: 5px;
margin-bottom: 5px;
}
.slide {
width: 100%;
height: 79%;
background-color: white;
}
.focus {
text-align: center;
}
/* new stuff */
.bottom {
display: flex;
}
.bottom li {
flex: 1 0 0;
}
.bottom li:first-child {
text-align: left;
}
.bottom li:last-child {
text-align: right;
}
<div class="container">
<div class="header">
<div class="spacer"></div>
<ul>
<li class="active">Summary</li>
</ul>
<div class="spacer"></div>
</div>
<div class="slide active">
</div>
<div class="slide">
</div>
<div class="focus">
<div class="spacer"></div>
<ul class="bottom">
<li class="active left">ASDF</li>
<li class="active left">LKJHGFDSAPOIURTQAJK</li>
<li class="active left">POV - ASR</li>
</ul>
<div class="spacer"></div>
</div>
</div>
If you don't want your left and right items to effect the positioning of the center item, you can take them out of the flow by using position: absolute;.
With this in place, you can align them using left or right.
ul.left, ul.right {
position: absolute;
}
ul.left {
left: 0;
}
ul.right {
right: 0;
}
Working example:
body {
background: #333;
color: white;
}
.container {
width: 80%;
height: 150px;
position: relative;
left: 10%;
}
.container .slide, li { display: none; }
.active { display: block !important; }
.header { text-align: center; }
ul {
margin: 0;
padding-left: 0px;
}
li {
list-style: none;
font-variant: small-caps;
}
.spacer {
width: 100%;
height: 1px;
background: white;
box-shadow: 1px 1px 15px 1px rgba(0, 150, 255, 1);
margin-top: 5px;
margin-bottom: 5px;
}
.slide {
width: 100%;
height: 79%;
background-color: white;
}
.focus { text-align: center; }
ul.right {position: absolute; right: 0;}
ul.left {position: absolute; left: 0;}
<div class="container">
<div class="header">
<div class="spacer"></div>
<ul>
<li class="active">Summary</li>
</ul>
<div class="spacer"></div>
</div>
<div class="slide active">
</div>
<div class="slide">
</div>
<div class="focus">
<div class="spacer"></div>
<ul class="left">
<li class="active">ASDF</li>
</ul>
<ul class="right">
<li class="active">POV - ASR</li>
</ul>
<ul>
<li class="active">LKJHGFDSAPOIURTQAJK</li>
</ul>
<div class="spacer"></div>
</div>
</div>
I've removed your float properties because with this implementation, they are no longer necessary.
I need some assistance with my code I am looking to creating a footer with four equal columns, right now the fourth column in spaced too far to the right, any assistance you can provide is appreciated.
#footer {
background: #E5E0DD;
margin: auto;
min-width: 860px;
padding: 0;
}
#footer div {
margin: 0 auto;
overflow: hidden;
padding: 26px 0 0;
width: 960px;
}
#footer div div {
margin: auto;
padding: 0;
text-align: left;
width: 240px;
}
#footer div div h3 {
color: #000;
font-size: 12px;
font-weight: bold;
margin: 0 0 5px 0;
padding: 0;
text-transform: uppercase;
}
#footer div div ul,
#footer div div ul li {
margin: auto;
list-style: none;
padding: 0;
}
#footer div div ul li {
font-size: 12px;
line-height: 22px;
color: #06C;
}
#footer div p {
color: #c1c1c1;
font-size: 10px;
margin: left;
padding: 0 0 2px 0;
text-align: right;
text-shadow: 1px 1px 1px #fff;
}
<div id="footer">
<div>
<div>
<h3>Policies & Directives</h3>
<ul>
<li><span class="ms-rteThemeForeColor-5-4">
</span></li>
</ul>
</div>
<div>
<h3>In the know...</h3>
<ul>
<li><span class="ms-rteThemeForeColor-5-4">
<!-- FAQs- Select and move from comments based on appropriate
page-->
<a href="#"
</span></li>
</ul>
</div>
<div>
<!-- Centers -->
<h3>Centers</h3>
<ul>
<li><span class="ms-rteThemeForeColor-5-4">
FAQs<br/>
<!-- Glossary -->
</span></li>
</ul>
</div>
<div>
<!-- About -->
<h3>About</h3>
<ul>
<li><span class="ms-rteThemeForeColor-5-4">
Mission Statement<br>
Overview<br>
Organizational
</span></li>
</ul>
</div>
</div>
</div>
This was unnecessary and difficult for no reason. I need help with my code. I am looking for columns equally spaced.
I'm going to give you the basic building blocks and the following methods you can go about achieving this:
Float Method
Inline-Block Method
Flex Method
.footer-col {
min-height: 200px;
width: 25%;
background: #ddd;
border: 1px solid white;
box-sizing: border-box;
}
.float .footer-col {
float: left;
}
footer#footer {
margin-bottom: 20px;
clear: both;
overflow: auto;
}
.inline-block .footer-col {
display: inline-block;
max-width: 24.5%;
}
#footer.flex {
display: flex;
}
h3 {
border-top: 1px solid #ddd;
padding-top: 15px;
}
h3:first-of-type {
border: 0px;
padding: 0px;
}
<h3>Float Method</h3>
<footer id="footer" class="float">
<div class="footer-col"></div>
<div class="footer-col"></div>
<div class="footer-col"></div>
<div class="footer-col"></div>
</footer>
<h3>Inline-Block Method</h3>
<footer id="footer" class="inline-block">
<div class="footer-col"></div>
<div class="footer-col"></div>
<div class="footer-col"></div>
<div class="footer-col"></div>
</footer>
<h3>Flex Method</h3>
<footer id="footer" class="flex">
<div class="footer-col"></div>
<div class="footer-col"></div>
<div class="footer-col"></div>
<div class="footer-col"></div>
</footer>
The rest is up to you!
There are at least 4 ways to create an element with children that are evenly divided. Inline-block, tables, floats, and flex-box.
Inline-block has a funny space to account for between them... so, 25% will not work as you expect. Tables are for data(in my opinion). Floats are great, but they take themselves our of the document flow in a way you need to understand well. Flex-box is probably the most versitile, but you need to be aware of the browser prefixes or use autoprefixer. Here is an example of both floats and flex-box.
https://jsfiddle.net/sheriffderek/fbatsw02/
Markup
<footer class="site-footer floats">
<div class="inner-w">
<ul class="module-list">
<li class="module">one</li>
<li class="module">two</li>
<li class="module">three</li>
<li class="module">four</li>
</ul>
</div>
</footer>
<footer class="site-footer flex-box">
<div class="inner-w">
<ul class="module-list">
<li class="module">one</li>
<li class="module">two</li>
<li class="module">three</li>
<li class="module">four</li>
</ul>
</div>
</footer>
Styles
* {
box-sizing: border-box;
margin: 0;
}
.site-footer .inner-w {
width: 100%;
max-width: 960px;
margin: 0 auto;
}
.module-list {
list-style: none;
margin: 0;
padding: 0;
}
.module {
padding: 1rem;
}
/* with floats */
.site-footer.floats {
background: wheat;
overflow: hidden; /* fixes how the inner float collapses this element */
}
.site-footer.floats .module {
width: 25%;
float: left;
}
/* with flexbox */
.site-footer.flex-box {
background: lightgreen;
}
.site-footer.flex-box .module-list {
display: -webkit-box;
display: flex;
-webkit-box-orient: horizontal;
-webkit-box-direction: normal;
flex-direction: row;
}
.site-footer.flex-box .module {
flex-basis: 25%;
}
My page header has a misaligned <li> element. Here is a screenshot:
Basicly I want to say "center both elements vertically, one to the left and the other to the right".
I'm able to align a <li> element
horizontally with style="float:right"
vertically with style="vertical-align:middle".
...But not at the same time. Based on a similar question, I was expecting this to work:
style="float:right; vertical-align:middle"
It doesn't.
I also found some ways to align an entire list, but those were not applicable to aligning an individual element of a list.
Here is the relevant html-thymeleaf code:
<div th:fragment="header">
<nav>
<ul class="navcontainer">
<li class="navtitle"><h2>Personal Expense Tracker</h2></li>
<li class="navlogout" th:inline="text" style="float:right">[[(${user != null ? 'Logout ' + user : ''})]]</li>
</ul>
</nav>
</div>
Here is the relevant css code:
nav {
background-color: #333;
border: 1px solid #333;
color: #fff;
display: block;
margin: 0;
overflow: hidden;
}
nav ul{
margin: 0;
padding: 0;
list-style: none;
}
nav ul li {
margin: 0;
display: inline-block;
list-style-type: none;
transition: all 0.2s;
}
nav > ul > li > a {
color: #aaa;
display: block;
line-height: 2em;
padding: 0.5em 2em;
text-decoration: none;
}
nav > ul > li > a:hover {
background-color: #111;
}
With the code you added..
Using flexbox, you can do this:
nav {
background-color: #333;
border: 1px solid #333;
color: #fff;
display: block;
margin: 0;
overflow: hidden;
}
nav ul {
margin: 0;
padding: 0;
list-style: none;
display: flex;/* added */
align-items: center;/* added */
justify-content: space-between;/* added */
}
nav ul li {
margin: 0;
display: inline-block;
list-style-type: none;
transition: all 0.2s;
}
nav > ul > li > a {
color: #aaa;
display: block;
line-height: 2em;
padding: 0.5em 2em;
text-decoration: none;
}
nav > ul > li > a:hover {
background-color: #111;
}
<div th:fragment="header">
<nav>
<ul class="navcontainer">
<li class="navtitle"><h2>Personal Expense Tracker</h2></li>
<li class="navlogout" th:inline="text" >Log out</li>
</ul>
</nav>
</div>
the question is a little vague. If you could give me a visual of your problem / what you're looking for as a result I could probably help more.
Anyways here is the classic way to horizontally and vertically align an element to its parent.
Best of luck!
.container {
position: relative;
display: block;
margin: 0 auto;
width: 50%;
max-width: 1000px;
height: 100px;
background: grey;
}
.element {
position: absolute;
display: block;
width: 50%;
height: 50px;
background: red;
top: 50%;
margin-top: -25px;
left: 50%;
margin-left: -25%;
}
<ul class="container">
<li class="element"></li>
</ul>
You should give height or line-height to the element (or in some case parent element has no height) so vertical-align:middle will not work because there is no height.
First give height to the element which you want to set vertically middle if it does not work give height to the parent element.
Continuing the question through the title...
My html li has a padding, but when I remove it, the text is positioned as before yet, almost outside of its own container. And I'm just testing, though.
Some view (I'm not allowed to add images yet)... I just want that the text be inside its container.
The respective content is:
(note: the class no-s just removes the ugly selection)
/* Context menu */
.context-menu {
position: fixed;
padding: 4px 0 4px 0;
background-color: #fff;
border: 2px solid #628;
}
.context-menu list {
list-style: none
}
.context-menu li {
display: block;
width: 100%;
padding-left: 15px;
padding-right: 100px
}
.context-menu .base,
.context-menu .second {
display: inline-block
}
.context-menu .base {
float: left
}
.context-menu .second {
float: right
}
.context-menu li .active,
.context-menu li .inactive {
display: inline-block;
font: 600 14px"Segoe Ui", sans-serif
}
<div class="context-menu no-s" style="left: 60px; top: 60px;">
<list>
<!-- A context menu item -->
<li>
<!--Left content-->
<div class="base">
<span class="active">Copy</span>
</div>
<!--Right content-->
<div class="second">
<span class="active">CTRL + C</span>
</div>
</li>
</list>
</div>
you can delete the padding-left (even if necessary padding-right but now not necessary) from here;
.context-menu li {
display: block;
width: 100%;
padding-left: 15px;
padding-right: 100px
}
that's all
or you can use flexbox model for this section.
Are you able to change the padding of the .context-menu?
I changed it to this: padding: 4px 20px 4px 0; and it seems to work
/* Context menu */
.context-menu {
position: fixed;
padding: 4px 20px 4px 0; /* changed right padding */
background-color: #fff;
border: 2px solid #628;
}
.context-menu list {
list-style: none;
}
.context-menu li {
display: block;
width: 100%;
padding-left: 15px;
padding-right: 100px
}
.context-menu .base,
.context-menu .second {
display: inline-block
}
.context-menu .base {
float: left;
}
.context-menu .second {
float: right;
}
.context-menu li .active,
.context-menu li .inactive {
/* display: inline-block; */
font: 600 14px"Segoe Ui", sans-serif
}
<div class="context-menu no-s" style="left: 60px; top: 60px;">
<list>
<!-- A context menu item -->
<li>
<!--Left content-->
<div class="base">
<span class="active">Copy</span>
</div>
<!--Right content-->
<div class="second">
<span class="active">CTRL + C</span>
</div>
</li>
</list>
</div>
fiddle
The title appears overtop of the content above it.
Adding the Twitter's .clearfix to the container doesn't seem to solve the problem. I'm not sure how to push my titlebar down below the content above it.
Suggestions?
.clearfix reference (from Twitter Bootstrap) -- written in less:
.clearfix {
*zoom: 1;
&:before,
&:after {
display: table;
content: "";
// Fixes Opera/contenteditable bug:
// http://nicolasgallagher.com/micro-clearfix-hack/#comment-36952
line-height: 0;
}
&:after {
clear: both;
}
}
And use like:
.title-tabs {
.clearfix; /* HERE */
border-bottom: 1px solid #91b6eb;
position: relative;
h3 {
float: left;
margin: 0;
display: block;
position: absolute;
left: 0;
bottom: 0;
}
/* etc... */
Here is probably what you are looking for
http://jsfiddle.net/rUQ2z/2/
<div class="main-content">
<div class="container">
<div class="access-details">
<div class="pull-left">
<div>Logged in: <b>John Doe</b>
</div>
<div>Access Lvl: Noob</div>
</div>
<div class="pull-right">
<div>Sunday, January 13th, 2013</div>
</div>
</div>
<div class="title-tabs clearfix">
<h3>Client Details</h3>
<ul class="nav">
<li class="active">Details
</li>
<li>Program
</li>
<li>Special
</li>
<li>Docs
</li>
<li>Notes
</li>
</ul>
</div>
<p>Welcome to Client Details</p>
</div>
</div>
h3 {
font-size: 36px;
}
.clearfix {
*zoom: 1;
&:before, &:after {
display: table;
content:"";
// Fixes Opera/contenteditable bug: // http://nicolasgallagher.com/micro-clearfix-hack/#comment-36952 line-height: 0;
}
&:after {
clear: both;
}
}
.title-tabs {
.clearfix;
border-bottom: 1px solid #91b6eb;
position: relative;
h3 {
float: left;
margin: 0;
display: block;
left: 0;
bottom: 0;
}
ul {
float: right;
margin: 0;
display: block;
right: 0;
bottom: 0;
li {
float: left;
a {
display: block;
padding: 3px 5px;
background-color: #f9fafe;
.border-radius(5px, 0, 0, 5px);
border-color: #91b6eb;
border-width: 2px 2px 0 2px;
border-style: solid;
text-transform: uppercase;
font-weight: bold;
}
}
li + li {
margin-left: -2px;
}
}
}
There is no need for position: absolute on the same class that uses float. You either use floats or you use position relative/absolute.
You want to apply clearfix to the container that contains your float which will update the height of your container allowing the content below the clearfix to not overlap with your content above.
EDIT
If you are looking for an alternative answer dealing with dynamic content where you want the menu items to float right and and align bottom then tables is a way to do this.
Take a look at the following code. I made the container be the table, title-tabs be the title-row and I added ul-container so I can make the menu items be contained within a table cell.
http://jsfiddle.net/rUQ2z/11/
<div class="main-content">
<div class="container">
<div class="access-details">
<div class="pull-left">
<div>Logged in: <b>John Doe</b>
</div>
<div>Access Lvl: Noob</div>
</div>
<div class="pull-right">
<div>Sunday, January 13th, 2013</div>
</div>
</div>
<div class="title-tabs">
<h3>Client Details</h3>
<div class="ul-container">
<ul class="nav">
<li class="active">Details
</li>
<li>Program
</li>
<li>Special
</li>
<li>Docs
</li>
<li>Notes
</li>
</ul>
</div>
</div>
<p>Welcome to Client Details</p>
</div>
</div>
h3 {
font-size: 36px;
}
.clearfix {
*zoom: 1;
&:before, &:after {
display: table;
content:"";
// Fixes Opera/contenteditable bug: // http://nicolasgallagher.com/micro-clearfix-hack/#comment-36952 line-height: 0;
}
&:after {
clear: both;
}
}
.container {
display: table;
width: 100%;
}
.title-tabs {
.clearfix;
display: table-row;
border-bottom: 1px solid #91b6eb;
position: relative;
h3 {
margin: 0;
vertical-align: bottom;
display: table-cell;
left: 0;
bottom: 0;
}
.ul-container {
display: table-cell;
text-align: right;
vertical-align: bottom;
}
ul {
margin: 0;
display: inline-block;
right: 0;
bottom: 0;
li {
float: left;
a {
display: block;
padding: 3px 5px;
background-color: #f9fafe;
.border-radius(5px, 0, 0, 5px);
border-color: #91b6eb;
border-width: 2px 2px 0 2px;
border-style: solid;
text-transform: uppercase;
font-weight: bold;
}
}
li + li {
margin-left: -2px;
}
}
}
What happened is that your .title-tabs was set to relative, and h3 set to absolute and bottom, because you the .title-tabs do not have a height, so the h3 element will take the last bottom and goes out of the .title-tabs line. Remove bottom from h3 css.