Need help in CSS positioning in my code - html

I have been struggling with some CSS Style. The problem is that i am not able to position the images properly. Due to some reason the image is not displaying in proper expected flow. And I also want to centralize the whole content. It is not properly centralize when you resize the the browser. You can easily notice all this issue once you copy past my code. Here is my code. Thanks
HTML
body {
font-family: 'Open sans',sans-serif;
}
#content p {
padding: 5px;
font-size: 0.8em;
}
#wrap {
max-width: 900px;
padding: 3%;
margin: 0 auto;
}
ul {
list-style-type: none;
padding: 0;
}
#contact a {
padding-left: 35px;
background-repeat: no-repeat;
background-size: 20px;
}
#content a {
width: 100%;
display: inline-block;
height: 100%;
}
#content li img {
width: 100%;
}
a {
text-decoration: none;
}
#content li {
float: left;
width: 25%;
margin: 3%;
box-sizing: border-box;
background-color: bisque;
}
footer {
clear: both;
text-align: center;
}
footer img {
width: 25px;
}
h1 {
font-weight: normal;
font-family: 'Change one', sans-serif;
color: #fff;
font-size: 2.4em;
padding: 0px;
margin: 0px;
display: inline-block;
}
h2 {
font-size: .9em;
font-weight: normal;
color: #fff;
margin: 10px 0;
}
#content ul {
padding: 0;
margin: 0;
}
<div id="wrap">
<div id="content">
<ul>
<li><a href="img/numbers-01.jpg"><img src="img/numbers-01.jpg">
<p>Experimentation with color and texture</p></a>
</li>
<li><a href="img/numbers-02.jpg"><img src="img/numbers-02.jpg">
<p>Experimentation with color and texture Experimentation with color and texture</p></a>
</li>
<li><a href="img/numbers-06.jpg"><img src="img/numbers-06.jpg">
<p>Experimentation with color and texture</p></a>
</li>
<li><a href="img/numbers-09.jpg"><img src="img/numbers-09.jpg">
<p>Experimentation with color and textureExperimentation with color and texture</p></a>
</li>
<li><a href="img/numbers-12.jpg"><img src="img/numbers-12.jpg">
<p>Experimentation with color and texture</p></a>
</li>
</ul>
</div>
<footer>
<img src="img/twitter-wrap.png">
<img src="img/facebook-wrap.png">
<p>© 2014 Chimed.</p>
</footer>
</div>

The issue is that some of your text items in the <p> elements wrap to 3 lines, and some only wrap to 2 lines. This makes them taller than the others. When the next <li> wraps to the next line, it ends up being positioned to the right of the taller item.
Represented visually:
To fix this, you could try to make all your items the same height. That way they would wrap cleanly around each other.

Two "issues" are in your code, as I can see so far.
You're trying to center your content and doing it right with the #wrap. But the list elements inside the list have a width of 25% each, plus 3% margin to the sides, so 31% in total. So the closest you can get to 100% width of the surrounding element is 93%, leaving a gap on the right side, because of your float: left for the list elements. This should fix the problem:
ul {
width: 93%;
margin: 0 auto;
list-style-type: none;
padding: 0;
}
#content li {
display: inline-block;
vertical-align: top;
width: 25%;
margin: 3%;
box-sizing: border-box;
background-color: bisque;
}
I added a width and centering margin to the ul and you're li elements are now inline-block and aligned at their top line.

Related

Section not showing background color

I have tried this very same method on a previous website in which it worked. I am not sure why isn't it working now.
body {
max-width: 100%;
}
#header_encapsulator {
max-width: 1000px;
margin: auto;
background-color: orange;
}
header {
position: fixed;
max-width: 60em;
width: 100%;
}
.logo {
float: left;
font-family: 'Tangerine', cursive;
font-size: 3rem;
padding: 0 0 0 1rem;
text-decoration: none;
font-weight: bold;
color: black;
}
nav {
float: right;
padding: 1.5rem 1rem 0 0;
}
nav a {
text-decoration: none;
}
nav a:first-child {
padding-right: .8rem;
}
h1 {
font-size: 2rem;
text-align: center;
padding: 12rem 0 2rem 0;
font-family: 'Roboto Slab', serif;
}
<div id="header_encapsulator">
<header>
<a class="logo" href="/">Logo_name</a>
<nav>
Menu_1
Menu_2
</nav>
</header>
</div>
<h1>Heading</h1>
I am not sure why margin:auto inside #header_encapsulator doesn't center <header> if I don't specify a max-width. The website will break on a monitor with >1000px width. It doesn't take 10000px value not does it take % values too.
Secondly, the header_encapsulator should show a orange background image edge to edge of the display. It doesn't show. I know that empty divs/sections doesn't display it's properties, but I have done it before, the exact same way.
Edit:
I have created a simpler html and css file with the same code. You can download it here and test it locally. I don't get the orange background 100% of the monitor width.
Don't know if there's any reason to have the #header-encapsulator not being fixed itself instead of the header inside (which causes the encapsulator to be "empty", thus, not having a height), but simply moving the position: fixed property to #header-encapsulatorand adding a width: 100% should fix the issue
Edit: Removed 10000px max-width property from #header_encapsulator and width:100% from header.
body {
max-width: 100%;
}
#header_encapsulator {
width: 100%;
background-color: orange;
/*Put it in here*/
position: fixed;
}
header {
/*Removed it from here*/
max-width: 60em;
margin:auto
}
.logo {
float: left;
font-family: 'Tangerine', cursive;
font-size: 3rem;
padding: 0 0 0 1rem;
text-decoration: none;
font-weight: bold;
color: black;
}
nav {
float: right;
padding: 1.5rem 1rem 0 0;
}
nav a {
text-decoration: none;
}
nav a:first-child {
padding-right: .8rem;
}
h1 {
font-size: 2rem;
text-align: center;
padding: 12rem 0 2rem 0;
font-family: 'Roboto Slab', serif;
}
<div id="header_encapsulator">
<header>
<a class="logo" href="/">Logo_name</a>
<nav>
Menu_1
Menu_2
</nav>
</header>
</div>
<h1>Heading</h1>
You are specifying max-width: 100%.
This is the maximum width that will be taken up by the div and in order to reach this width, the div element will require enough content to fill it to 100%.
Use width instead of max-width. This will force any div to be the specified width, regardless of content.
Give a height to your #header_encapsulator like so:
#header_encapsulator {
width: 1000px;
background-color: orange;
height: 100px; // or anything you want
}
plus if you want margin: auto header you should do something like:
#header_encapsulator header {
margin: auto;
//other css if you want
}

current page width inconsistent

I have a "current" class which highlights the current page. And it works as far as highlighting the current page. With the padding I've added the highlighted area covers the a-element and go towards the top of the page which is what I want it to do. But each highlighted area is the width of the a-element and since each a-element has a different number of characters within, the widths are inconsistent.
To correct this I gave the a-element a display: block. This correction did give a consistent width, but now the highlighted area goes down towards the page as opposed to towards the top of the page as I wanted it.
How can I get a consistent width of highlighted area to go up towards the top of the page?
nav {
margin: 100px auto;
}
.top-nav {
width: 785px;
color: dimgrey;
text-align: center;
margin: 0 auto;
}
.top-nav li {
width: 150px;
float: left;
margin: 0 3px;
}
.top-nav a {
color: dimgrey;
width: 150px;
}
.current {
padding: 150px 0px 5px 0px;
background-color: #fab938;
color: white;
}
span.fa-bath {
font-size: 50px;
margin-top: -45px;
margin-bottom: 10px;
border-radius: 100%;
padding: 20px;
background-color: #fab938;
}
<nav>
<ul class="top-nav">
<li>Quem Somos</li>
<li>O Que Fazemos?</li>
<li><span class="fa fa-bath"></span></li>
<li>Donations</li>
<li>Contact</li>
</ul>
</nav>
I would change the way you build you nav.
Instead of using a float use a inline-block on the LI.
Next to that you can use vertical-align: middle; to align the items from the middle or use top/bottom.
also your nav container is not fitting the size of the 5 Li you are using.
https://jsfiddle.net/wyrscn48/1/
nav {
margin: 100px auto; }
.top-nav {
width: 935px;
color: dimgrey;
text-align: center;
margin: 0 auto;
}
.top-nav li {
width: 150px;
margin: 0 3px;
display: inline-block;
vertical-align: top;
}
.top-nav a {
color: dimgrey;
width: 150px; }
.current {
background-color: #fab938;
color: white; }
span.fa-bath {
font-size: 50px;
margin-bottom: 10px;
border-radius: 100%;
padding: 20px;
background-color: #fab938;
}

Why does CSS Flexbox not properly size the right-most container?

After having had troubles trying to display three containers in a row with the middle one being centered on the page and the side ones being of a fixed width I came across the CSS Flexbox model, mentioned in a Stackoverflow question.
Using display: flex instead of float: left or displaying the containers as inline-box whilst messing with margin seems to be working quite well, with way fewer lines of code.
However, I ran into an issue with flexbox that I can't seem to solve on my own:
I want a container #menubar to hold three containers in a row: #logo, nav and #search.
<div id="menubar">
<div id="logo"></div>
<nav>
<ul>
<li>Articles</li>
<li>Images</li>
<li>About</li>
<li>Disclaimer</li>
</ul>
</nav>
<div id="search"></div>
</div>
The #logo-container as well as the #search-container are of a fixed size (width: 80px, height: 80px). One should be placed at the very left side of the #menubar-container and one should be placed at the very right.
The nav-container should be centered within the middle of the #menubar-container. Basically the positioning is working and I get the desired layout:
[#logo left] [nav centered] [#search right]
However, for some reason the #logo-container is being displayed at the specified dimension of 80px width * 80px height while the #search-container is being displayed at 79px width * 80px height, even through the CSS looks like:
header div#menubar div#logo {
width: 80px;
height: 80px;
background-color: orange;
}
header div#menubar div#search {
width: 80px;
height: 80px;
background-color: orange;
}
To confirm I made a screenshot and zoomed in with Photoshop, selecting the container to view its dimensions.
I can't figure out why the #search-container is missing one pixel in width.
Here is a JSFiddle with the HTML and CSS I am using.
Am I using flexbox correctly?
How do I fix it so both side-containers are 80x80 pixel in dimensions?
Am I using flexbox correctly?
Yes and no
Instead of width you should, ideally, be using the flexshorthand property combining, flex-grow, flex-shrink and flex-basis.
header div#menubar div#logo {
flex: 0 0 80px;
height: 80px;
background-color: orange;
}
Alternatively, you can ensure that the element doesn't shrink by using width AND the flex-shrink value of 0
* {
margin: 0;
padding: 0
}
body {
background-color: #dfe3e5;
}
header div#top {
height: 22px;
/*background-image: url('../img/colorbar.png');
background-position: top left;
background-repeat: repeat-x;*/
background-color: gray;
}
header div#menubar {
background-color: #1c2227;
display: flex;
justify-content: space-between;
}
header div#menubar div#logo {
flex: 0 0 80px;
height: 80px;
background-color: orange;
}
header div#menubar nav {
display: table;
text-align: center;
background-color: darkred;
}
header div#menubar nav ul li {
display: inline-block;
margin-left: -4px;
list-style-type: none;
line-height: 80px;
text-align: center;
}
header div#menubar nav ul li a {
outline: 0;
display: block;
text-transform: uppercase;
padding: 0px 20px;
font-size: 1.1em;
font-family: 'Raleway', "Trebuchet MS", Arial, Helvetica, sans-serif;
color: #eee;
text-decoration: none;
}
header div#menubar nav ul li a:hover {
color: #000;
background-color: orange;
}
header div#menubar div#search {
flex: 0 0 80%;
height: 80px;
background-color: orange;
}
<header>
<div id="menubar">
<div id="logo"></div>
<nav>
<ul>
<li>Articles
</li>
<li>Images
</li>
<li>About
</li>
<li>Disclaimer
</li>
</ul>
</nav>
<div id="search"></div>
</div>
</header>
Then you get the right result

CSS Padding is overflowing parent

I'm trying to make a horizontal navbar, and I've got it sitting at the top of the page with each of the three list items filling up a third of the space, all good on that part.
The issue is when I try to make the anchor tag (which is within the list item) fill up all of the list item's space. I set it to display: block;, so it takes up the full width, however I can't get it to fill up vertical space how I want it to. I can use height: 100% and it will fill up the space, however if I do it that way the anchor tag's text rests at the top of the list item's space, however I want it to be vertically centred. Top and bottom padding came to mind for this, so I tried setting padding: 100%;. When this happened, I could no longer see the text of the anchor tag. By dragging the space with my mouse it looks like the tag is taking up more space than is in the list item.
This is an image of the nav bar with height: 100%;:
Note: the red lines surrounding each section are the borders which I set visible to view boundaries
The relevant HTML code is as follows:
<ul class="main_nav">
<li>Home</li>
<li>Test page</li>
<li>Test page 2</li>
</ul>
The relevant CSS code is as follows:
body * {
font-family: Roboto sans-serif;
box-sizing: border-box;
border: solid 2px red;
}
header ul.main_nav {
background-color: gray;
color: #EBEBEB;
margin: 0px;
padding: 0px;
width: 100%;
height: 50px;
list-style-type: none;
list-style-position: inside;
overflow: hidden;
}
header ul.main_nav li {
text-align: center;
float: left;
width: calc(100% / 3);
height: 100%;
}
header ul.main_nav li a {
display: block;
height: 100%;
text-decoration: none;
}
EDIT: I would rather not use fixed sizes to solve this, as I'd largely prefer being able to edit a single value (50px for the ul) to adjust the whole navbar
you can do this using flexbox,
*,
*:before,
*::after {
box-sizing: border-box;
}
body * {
font-family: Roboto sans-serif;
border: solid 2px red;
}
ul.main_nav {
background-color: gray;
color: #EBEBEB;
margin: 0;
padding: 0;
width: 100%;
height: 50px;
list-style-type: none;
list-style-position: inside;
display: flex
}
ul.main_nav li {
flex: 1;
display: flex;
height:100%;
}
ul.main_nav li a {
flex: 1;
display: flex;
justify-content: center;
align-items: center;
background: yellow
}
<ul class="main_nav">
<li>Home
</li>
<li>Test page
</li>
<li>Test page 2
</li>
</ul>
if you are going to have one line only in your menu, yo can use line-height, see below
*,
*:before,
*::after {
box-sizing: border-box;
}
body * {
font-family: Roboto sans-serif;
border: solid 2px red;
}
ul.main_nav {
background-color: gray;
color: #EBEBEB;
margin: 0;
padding: 0;
width: 100%;
height: 50px;
list-style-type: none;
list-style-position: inside;
overflow: hidden;
font-size: 0;
}
ul.main_nav li {
text-align: center;
width: calc(100% / 3);
height: 100%;
display: inline-block;
font-size: 16px
}
ul.main_nav li a {
display: block;
height: inherit;
background: yellow;
text-decoration: none;
line-height: 32px /*this should be the same value has the parent height*/
}
<ul class="main_nav">
<li>Home
</li>
<li>Test page
</li>
<li>Test page 2
</li>
</ul>

Vertical and horizontal alignment of <li> within a fluid navbar in a table cell

Objective
What I am trying to achieve is have my <nav>s links be centered (vertically and horizontally), my <nav>s links have a defined liquid width that can contain the text, and my <nav> element be liquid and expand to fit the needs of the <li>s.
Attempts
I vertically centered my <li>s within my fixed navbar, but now the text is overflowing from the cells and I want the cells to expand to fit the text within.
HTML
<nav id="page-nav">
<ul>
<li>
About Us</li>
<li>
Connect
</li>
<li>
Blog
</li>
</ul>
</nav>
CSS
#page-nav {
height: 100%;
margin-right: 5%;
float: right;
}
#page-nav ul {
height: 100%;
width: 100%;
margin: 0%;
padding: 0%;
display: table;
text-transform: uppercase;
font-size: 125%;
color: #4a4a4a;
white-space: nowrap;
}
#page-nav li {
height: 100%;
padding: 0% 2%;
display: table-cell;
vertical-align: middle;
}
#page-nav a {
transition: 0.2s ease;
}
#page-nav a:hover {
color: #fff;
}
try changing your #pagenav CSS:
#page-nav {
text-align: center !important;
height: 100%;
float: none;
}
If i am understanding what you want that should center it all on itself in a row.
If I understand you correctly, your main concern is to center vertically and horizontally. I added some color for demo purposes on this CodePen which produced this
To center your text vertically just set the line-height of your <li> equal to the height of your <ul> and make the containing <nav> to have a height: auto;. But do not use a table for this.
Then center your text horizontally give your <li> a rule of text-align: center; to center it.
Here is an example, modify according to your own needs
#page-nav {
/* you can set the height of container to auto */
height: auto;
width: 20%;
margin-right: 5%;
float: right;
}
#page-nav ul {
/* modify to your height */
height: 2em;
width: 100%;
margin: 0%;
padding: 0%;
text-transform: uppercase;
font-size: 125%;
color: #4a4a4a;
white-space: nowrap;
}
#page-nav li {
height: 100%;
padding: 0% 2%;
/* use inline-block instead */
display: inline-block;
/* set line height equal to its parents height */
line-height: 2em;
/* Center your text inside the li */
text-align: center;
vertical-align: middle;
}