I'm trying to dispaly some element in single line inside header of my blog, but i have problem with logo in it.
here is my code:
CSS:
.list {
margin:0;
padding: 0;
list-style-type: none;
display: table;
table-layout: fixed;
width:100%;
}
img {
padding: 0;
}
.list>a {
display: table-cell;
border-left:1px #47c9af solid;
text-align: center;
color:#47c9af;
height:30px;
text-decoration: none;
}
.container {
background-color: white;
color:#47c9af;
height:100%;
font-family: WYekan !important;
}
HTML:
<div class="container">
<ul class="list">
</li>
<li></li>
<a href='#'><li><h1 >Header Of My Blog </h1></li></a>
<a href='#'><li><p>SubHeader</p></li></a>
<a href='#'><li><h3></h3></li></a>
</ul>
</div>
The result is something like this:
As you can see logo is not in order with other elements, what should i do?
Thanks.
Fixing the Alignment
To fix the alignment you simply need to introduce the vertical-align property to align everything to the top:
.list {
...
vertical-align: top;
}
Making your HTML Valid
A problem with your markup is that ul elements must only contain li children. Your current ul element has a children which have li elements inside them - this is invalid. Wrap your a elements within the li elements instead:
<ul>
<li>
<a></a>
<li>
</ul>
Making your HTML Semantic
You need to ask yourself some questions about your current markup:
What is this a list of? Why are you using the ul element instead of the header element?
Why are you using a p element as a "SubHeader"?
First, let us rearrange your HTML, so the code is valid. A list (ul) has list-items (li). The list items may contain anchors, paragraphs, headings, etc. Not the other way around.
Then we'll need to change the CSS a bit, so the anchors get the right color.
But the most important thing is the vertical alignment of the table-cells. By adding vertical-align to the list items, they'll be in the right alignment.
.container {
background-color: white;
color:#47c9af;
height:100%;
font-family: WYekan !important;
}
.list {
margin:0;
padding: 0;
list-style-type: none;
display: table;
table-layout: fixed;
width:100%;
}
img {
padding: 0;
}
.list>li {
display: table-cell;
border-left:1px #47c9af solid;
text-align: center;
height:30px;
vertical-align: middle;
}
.list li a {
color:#47c9af;
text-decoration: none;
}
<div class="container">
<ul class="list">
<li><img src="http://placehold.it/150x75" /></li>
<li></li>
<li><h1>Header of my blog</h1></li>
<li>Subheader</li>
<li><h3></h3></li>
</ul>
</div>
You can define your .list>a and img tag vertical-align:top;
.list>a, img{
vertical-align:top;
}
.list > a {
vertical-align: bottom;
}
as you have wrap everything inside the anchor tag, we should target the a tag
here's a Jsfiddle
Related
I'm just starting to develop in HTML and CSS, and despite reading about the box model I am still having trouble with some of the basics of positioning.
I want to create a header navigation bar with three elements - one to the left of the page, one to the right, and one in the center. I want these elements to be inline with each other.
At the moment, they are represented in HTML like so
<body>
<div class="header">
<ul class="child">
<li id="lodestone">The Lodestone</li>
<li id="mogstation">The Mog Station</li>
<li id="user">User Account</li>
</ul>
</div>
I have then attempted to align them using the 'text-align' property in CSS.
.header {
background-color: #ffd9e7;
border: black;
display: block;
box-sizing: border-box;
}
.header ul {
display: inline-block;
}
.header > ul > li {
display: inline-block;
}
#lodestone {
text-align: left;
}
#user {
text-align: right;
}
#mogstation {
text-align: center;
}
However, instead of the expected result it produces this.
The three items are aligned, next to each other, on the left.
Can anyone recommend what css property I should be using to solve this problem? My research has shown there are ways of using float, but other people recommend against it, and when I try I get issues with the text overflowing off the page.
If you give the ul and lis a width and (100 ul /30 for li s for example) then they should display correctly
.header {
background-color: #ffd9e7;
border: black;
display: block;
box-sizing: border-box;
}
.header ul {
display: inline-block;
width:100%;
}
.header > ul > li {
display: inline-block;
position:relative;
vertical-align:top;
width:30%;
}
#lodestone {
text-align: left;
}
#user {
text-align: right;
}
#mogstation {
text-align: center;
}
<div class="header">
<ul class="child">
<li id="lodestone">The Lodestone</li>
<li id="mogstation">The Mog Station</li>
<li id="user">User Account</li>
</ul>
</div>
I added vertical-align:top; but it's excess to requirements, you could take that out..
Fiddle
Hope this helps
Take a look at CSS Flexbox for a different approach to layout your elements
header{
display: flex;
justify-content: space-around;
}
<header>
<div>A</div>
<div>B</div>
<div>C</div>
</header>
Why not make the li elements a third of the width?
First make the ul 100% width, you'll also need to ensure there's no padding on the right of the ul as it tends to be automatically added by browsers:
.header ul {
display: inline-block;
padding: 0;
width: 100%;
}
Then have each li 33%
.header > ul > li {
display: inline-block;
width: 33%;
}
Style the rest as required
I'm trying to make a bar on the top of the page, but am unsure of how to make all links fall on the same line.
This is what I have:
#tb {
list-style-type: none;
position:relative;
margin: auto;
padding: 0;
overflow: hidden;
text-align: center;
display: block;
width: 60px;
}
<div class id="tb">
Home
News
Contact
About
</div>
I've tried removing many combinations in the CSS, but so far nothing. If the answer is incredibly obvious, please excuse my ignorance, as I am new to CSS.
Thanks,
-Tysuna
You've set a width on #tb which is causing the as inside it to wrap to new lines. The a tags will automatically be placed on the same line as they are classed as inline elements.
Inline-level elements are those elements of the source document that do not form new blocks of content; the content is distributed in lines (e.g., emphasized pieces of text within a paragraph, inline images, etc.).
(http://www.w3.org/TR/CSS21/visuren.html#inline-boxes)
Removing the width will allow the div to fill the entire width of its container (as it is a block element).
A block-level element occupies the entire space of its parent element (container), thereby creating a "block."
(https://developer.mozilla.org/en/docs/Web/HTML/Block-level_elements)
As #tb is now as wide as the body the a tags will be on the same line, only wrapping if the window size is decreased enough.
#tb {
text-align: center;
}
<div class id="tb">
Home
News
Contact
About
</div>
Your CSS in this particular example can also be simplified as many of the properties specified are already defaults, not applicable or would have no effect.
Update: specifiy for a-tag separatly, isntead of inheritance
#tb {
list-style-type: none;
position:relative;
margin: auto;
padding: 0;
overflow: hidden;
text-align: center;
display: block; /*property will be inherited by 'a'-tag elements too*/
width: 60px;
}
#tb a{
display: inline-block; /* specifiy for a-tag separatly*/
}
<div class id="tb">
Home
News
Contact
About
</div>
You can use some flexbox magic:
#tb {
display: flex;
justify-content: center;
}
<div class id="tb">
Home
News
Contact
About
</div>
Check the below code.
You can use explicitly display:inline-block or display:inline to make any element to stay in the same line.But Here if you remove the width thats enough.
Instead of div structure better use ul which will make your job easier.Bydefault it will align in a line.
#tb {
list-style-type: none;
position:relative;
margin: auto;
padding: 0;
overflow: hidden;
text-align: center;
display: block;
}
<div class id="tb">
Home
News
Contact
About
</div>
remove width: 60px - #tb
use ul li and display: inline-block for li
#tb {
list-style-type: none;
position:relative;
margin: auto;
padding: 0;
text-align: center;
display: block;
}
#tb > li{
display: inline-block;
vertical-align: top;
margin: 0 5px;
}
<ul id="tb">
<li>Home</li>
<li>News</li>
<li>Contact</li>
<li>About</li>
</ul>
I did take a look at loot of similar questions here, but no one helped me solve my problem. I have a problem with vertically align the img on the left side in the li cell (this is working), but i can't align the text next to img. The line-height from ul li div is messing my things.
Here is a Jsfiddle.
What i wan't to achive is this:
Vertically and horizontally align img in 1/3 of the li cell on the left side.
Vertically and horizontally align text in 2/3 of the li cell, text align should be left. Text can be multiline and with bolded heading in first line.
You can also edit html code, if it is necessary.
HTML
<div class="product_banner_right">
<div class="product_banner_right title">
<h3>LOOK DOWN</h3>
</div>
<ul>
<li>
<img src="http://dummyimage.com/26x23/000/fff.png" alt="" />
<p><span>HEADING1</span>first line text
<br>second line text</p>
</li>
<li>
<img src="http://dummyimage.com/48x9/000/fff.png" alt="" />
<p><span>HEADING2</span>first line text</p>
</li>
<li>
<img src="http://dummyimage.com/40x24/000/fff.png" alt="" />
<p><span>HEADING3</span>first line</p>
</li>
<li>
<img src="http://dummyimage.com/46x17/000/fff.png" alt="" />
<p><span>HEADING4</span>first line text
<br>second line text
<br>third line text</p>
</li>
</ul>
</div>
CSS
.product_banner_right {
font-size: 1.2em;
position: relative;
width: 250px;
}
.product_banner_right .title {
height: 40px;
background: #1b3a6f;
}
.product_banner_right .title h3 {
text-align: center;
line-height: 40px;
}
.product_banner_right ul {
margin: 0;
padding: 0;
}
.product_banner_right ul li {
display: block;
height: 70px;
border: 1px solid black;
}
.product_banner_right ul li img {
vertical-align: middle;
padding: 0 10px;
max-width: 50px;
}
.product_banner_right ul li p {
vertical-align: middle;
display: inline-block;
}
.product_banner_right ul li p span {
font-weight: bold;
}
change the following styles to :
.product_banner_right {
font-size: 100%;
position: relative;
width: 250px;
}
.product_banner_right ul li img {
vertical-align: middle;
padding: 0 4%;
width: 11%;
max-width: 50px;
}
.product_banner_right ul li p {
vertical-align: middle;
display: inline-block;
width: 80%;
}
the result:
I have rewrote your html to accomodate the changes.
I have applied two options:
variable height list items.
fixed height list items with overflow.
Fixed height list items
CLICK FOR DEMO
This option is fully browser compatible but would require manually adjustment of the top margin for each list item.
Alternatively this option could still be used with the box flex model described below.
Fix height of list item and add scroll on overflow:
height:70px;
overflow:auto;
Variable height list items
CLICK FOR DEMO
This option relies on css3 flex box model:
display:flex;
display:-webkit-flex;
align-items:center;
-webkit-align-items:center;
justify-content:center;
-webkit-justify-content:center;
Please note flex box requires browser support. It is now highly compatible with modern browsers however old versions of the useless outdated browser ie will not support it.
Users of these browsers will still have a nice viewing experience however the images will be aligned at the top of each list box and not the center.
i don't know if this is the best approach but it looks allready a bit better than yours.
i just changed the following:
.product_banner_right ul li img {
vertical-align: middle;
padding: 0 10px;
/* CHANGED*/
width: 33%;
}
.product_banner_right ul li p {
vertical-align: middle;
display: inline-block;
/* CHANGED*/
width: 66%;
position:absolute;
}
but you still have to get the text to fit into the table.
hope it helped, cheers!
You can make the texts in separate classes and then arrange the as you wish using margin
I'm trying to make css-based highscores.
What I basically want, is a triangle shape with list-items.
like so:
1
2 3 4
5 6 7 8 9
I tried several methods to center my elements, but I just can't get it to work.
When I position the elements absolutely, they are layered on top of each other.
Margin-left: 10% and margin-right:10% aren't working either, it's just changing margins
between the list item elements.
I've included a jsfiddle, so you can take a look at it:
http://jsfiddle.net/us454/
hopefully someone can help me out!
Here is a possible solution. Either way. Do not style the UL for that reason. Better use the list items:
http://jsfiddle.net/y3p8f/
ul.triangle
{
margin: auto;
padding: 0;
display: block;
text-align: center;
}
ul.triangle li
{
border-radius:50px;
background-color:black;
display: inline-block;
width: 20px;
height: 20px;
}
HTML:
<ul class="triangle">
<li></li>
</ul>
<ul class="triangle">
<li></li>
<li></li>
</ul>
....
Update:
here is a slightly more clean version: http://jsfiddle.net/y3p8f/2/
This appends all items into one UL
ul.triangle,
ul.triangle li
{
margin: auto;
padding: 0;
}
ul.triangle > li
{
display: block;
text-align: center;
}
ul.triangle > li > ul li
{
border-radius:50px;
background-color:black;
display: inline-block;
width: 20px;
height: 20px;
}
You can use div and text align to center the content
See this example
http://jsfiddle.net/4gUrZ/
div { text-align:center; }
If you go to http://www.leapfm.com and scroll down to the footer you'll see that the links are on the right bottom hand side.
How can I put em' in the center?
Please use Firebug or inspect element for easy access to the code.
So I'll start top down from the div class="footer", you'll need to change or add these css properties:
.footer {
display: block;
text-align: center;
width: auto;
}
The li for the links inside footer:
.footer li {
display: inline-block;
}
Then the a tags inside the above li tags:
.footer li a {
display: inline-block;
float: none;
}
Enclose the li-elements properly in a ul, and set margin: 0 auto on it.
Instead of using li-elements as separators, you could use li + li:before { content: "|"; }
Remove the CSS float:right for the class declaration.
.footer a {
float: right; //Remove this
display: block;
margin-right: 5px;
color: black;
}
Try to enclose the <li> tag with <ul>
and after that add this css :
.footer {
display: block;
text-align: center;
}
.footer ul {
margin: 0 auto;
}
.footer li {
float: left;
display: inline-block;
}
Remove float:right; from .footer a and enclose you are li tags with ul
<div class="footer">
<ul>
<li>Submit |</li>
<li class="divider"></li>
<li>FAQ | </li>
<li class="divider"></li>
<li>Contact Us </li>
</ul>
</div>
First of all you should enclose each of these LI's in a UL or OL.
Contrary to what the others have pointed out, you dont need to give the UL/OL a margin of 0 auto because it will already take up the entire space it has. Or give a display:block to the footer and text align. So when you set a text-align:center to the li; they will magically go in the center of the element. No need to center the parent element(the ul).
Also, if you want to do things the right way, get rid of the br tag and use margin or padding to space your way around your layout.
So; all you need to add to your code is this:
ul{
text-align:center;
}
ul li{
display:inline-block;
}
Sorry to put this in an answer. I would have rather be a comment to the top answer. I dont have enough rep for that. Not glory hunting!
Hope this helps