I am trying to build a simple navigation where some parts of the navigation items are hidden depending on the available space:
.container {
display: flex;
border: 1px solid red;
width: 100%;
max-width: 600px
}
ul {
margin: 0 30px 0px 0px;
display: flex;
list-style: none;
padding: 0;
flex-grow: 1;
}
li {
margin: 0;
padding: 0px 5px;
border: 1px solid green;
margin-left: 20px;
}
li:first-child{
padding-left: 0px;
margin-left: 0px;
}
li a {
line-height: 18px;
height: 18px;
overflow: hidden;
display: block;
}
.important {
color: red;
}
<div class=container>
<ul>
<li>
<a href=#>
<span class=important>my</span>
<span>elem</span>
</a>
</li>
<li>
<a href=#>
<span>my</span>
<span class=important>elem</span>
</a>
</li>
<li>
<a href=#>
<span class=important>my</span>
<span>elem</span>
</a>
</li>
<li>
<a href=#>
<span>my</span>
<span class="important">2nd</span>
<span>elem</span>
</a>
</li>
<li>
<a href=#>
<span>longerfirst</span>
<span class="important">elem</span>
</a>
</li>
</ul>
</div>
I successfully made some words disappear with using a fixed height and overflow:hidden on the a tag.
Now I'm looking for a solution to keep the red words (.important) visible and hide the others. Is there a way to do this?
If the word on the invisible line is longer than the visible one, the visible has too much whitespace, is there also a solution to this?
With Javascript it would be fairly easy, but I'm looking for a CSS only solution.
JSFiddle: http://jsfiddle.net/86qjexz3/
Related
I want the entire li element to be clickable not just the phone, could it be possible?
Also, are the HTML tags being used correctly for a contact information?
ul {
display: flex;
text-align: center;
justify-content: center;
list-style: none;
padding-left: 0;
}
li {
width: 400px;
border: 1px solid red;
}
li a {
position: relative;
width: inherit;
border: 1px solid green;
}
<address>
<ul>
<li class="">
<a href="12345"><img src="./img/phone.svg" /><span>Phone</span>
<p>+1 (234) 567-89-00</p>
</a>
</li>
<li class="">
<a href="email#email"><img src="./img/email.svg" /><span>Email</span>
<p>email#email</p>
</a>
</li>
<li class="">
<a href="my streeet"><img src="./img/address.svg"/><span>Address</span>
<p>street</p>
</a>
</li>
</ul>
</address>
Thank you!!
You can either set your styles to
a {
display: block;
height: 100%
}
or
li {
display: flex;
}
You should put the end the a tag after the img
<address>
<ul>
<li class="">
<img src="./img/phone.svg" /><span>Phone</span>
<p>+1 (234) 567-89-00</p>
</li>
<li class="">
<img src="./img/email.svg" /><span>Email</span>
<p>email#email</p>
</li>
<li class="">
<img src="./img/address.svg"/><span>Address</span>
<p>street</p>
</li>
</ul>
</address>
Or you can delete the a tag.
The image works without it, but the img won't be clickable
A li is by default not clickable. You can solve this issue by either using javascript or by adding height: 100%; and display: block; to your a.
Just give a tag 100% width and height and it will occupy all the space of li and li will become clickable another way of doing this is that just remove li and and insert a tag directly.
ul {
display: flex;
text-align: center;
justify-content: center;
list-style: none;
padding-left: 0;
}
li {
width: 400px;
border: 1px solid red;
}
li a {
position: relative;
display: block;
width: 100%;
height: 100%;
border: 1px solid green;
}
<address>
<ul>
<li class="">
<a href="12345"><img src="./img/phone.svg" /><span>Phone</span>
<p>+1 (234) 567-89-00</p>
</a>
</li>
<li class="">
<a href="email#email"><img src="./img/email.svg" /><span>Email</span>
<p>email#email</p>
</a>
</li>
<li class="">
<a href="my streeet"><img src="./img/address.svg"/><span>Address</span>
<p>street</p>
</a>
</li>
</ul>
</address>
This was already answered here
Here is the answer for those who don't want to look there
<li onclick="location.href = '';">Make A List Item Clickable</li>
The list element supports a onclick event.
for your case just remove the <a> element and replace it with a text-node or a <p>
I have written a simple piece of code that creates a bottom navigation bar for a responsive version of a website am working on. The problem am facing I guess is the width of the bar conflicting on my home page but when I navigate to other pages the width of the navigation bar is okay.
below is my code:
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.mobile-nav {
display: none;
width: 100%;
height: 25%;
justify-content: center;
align-items: center;
position: fixed;
bottom: 0;
border-top: 1px solid rgb(230, 230, 230);
z-index: 99;
background: fixed;
background-color: aliceblue;
}
.mobile-nav-header {
width: 25%;
height: 20%;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
flex-wrap: wrap;
}
.menu__text {
color: black;
border: none;
text-decoration: none;
}
.nav-text {
list-style-type: none;
}
.nav__label {
font-weight: 500;
}
#media only screen and (max-width: 768px) {
.mobile-nav {
display: flex !important;
height: 9% !important;
width: 100% !important;
left: 0;
right: 0;
z-index: 10;
}
}
<div className="mobile-nav">
<div className="mobile-nav-header">
<ul className="nav-text">
<li>
<IoHomeSharp size={25} className="mobile-nav-header-icon" />
</li>
<li>
<span className="nav_label">Home</span>
</li>
</ul>
</div>
<div className="mobile-nav-header">
<ul className="nav-text">
<li>
<IoChatbubblesSharp size={25} className="mobile-nav-header-icon" />
</li>
<li>
<span className="nav__label">Option B</span>
</li>
</ul>
</div>
<div className="mobile-nav-header">
<ul className="nav-text">
<li>
<RiMotorbikeFill className="mobile-nav-header-icon" size={25} />
</li>
<li>
<span className="nav__label">Option C</span>
</li>
</ul>
</div>
<div className="mobile-nav-header">
<ul className="nav-text">
<li>
<GrRestaurant className="mobile-nav-header-icon" size={25} fill="#0000FF" />
</li>
<li>
<span className="nav__label">Option D</span>
</li>
</ul>
</div>
</div>
My problem is when at the first page the navigation bar stretches past the width I gave it but when I move to another page it takes up the width I gave it.
Someone, please help me out on this.
The reason this is happening is because the content of your homepage is exceeding the usual width , this has happened before to me with images that got outside my containers .
Inspect your page with devtools (if you are using chrome) and point your cursor as right as you can to find the element that exceeds your container.
How do I align an ol so that it can look like this:
Justified
By simply "text-align: justify;" to the ol it looks like this:
Not justified actually!
Please and thank you :)
There is a similar answer that I used to create the leading dots in pure CSS. This is a CSS trick that puts dots behind everything, then covers up the dots behind the text using background: white;. If the background is a solid color, then this could work.
ol {
list-style: none;
max-width: 20em;
overflow-x: hidden;
padding: 0;
}
ol li:before {
content: "...................................................";
float: left;
letter-spacing: 0.25em;
white-space: nowrap;
width: 0;
}
ol span:first-child {
background: white;
padding-right: 0.33em;
}
ol span + span {
background: white;
float: right;
padding-left: 0.33em;
}
<ol>
<li>
<span>United States</span>
<span>86%</span>
</li>
<li>
<span>Canada</span>
<span>5%</span>
</li>
<li>
<span>UK</span>
<span>4%</span>
</li>
<li>
<span>Australia</span>
<span>3%</span>
</li>
<li>
<span>Germany</span>
<span>2%</span>
</li>
</ol>
As Paulie_D mentioned, you might want to consider using a simple table like this:
<table>
<tbody>
<tr>
<td>[Flag]</td>
<td>[Name]</td>
<td class="text-right">[Percentage]</td>
</tr>
</tbody>
</table>
And you should make sure your table width is set to 100% of the container in your CSS file:
table {
width: 100%;
}
.text-right {
text-align: right;
}
Flexbox version:
ul {
list-style-type: none;
margin: 0 auto;
width: 80%;
padding: 0;
}
.list {
display: flex;
}
.first {
margin-right: 0.5em;
color: #2B91AF;
flex: 1;
display: flex;
}
.price {
text-align: right;
flex: 0 0 4em;
}
.first:after {
content: '';
border-bottom: dotted 2px tomato;
flex: 1;
}
<ul>
<li class="list">
<i class='first'>Co-Pay:</i>
<i class="price">$150.00</i>
</li>
<li class="list">
<i class='first'>Pay:</i>
<i class="price"> $5.00</i>
</li>
<li class="list">
<i class='first'>Co-Pay: item</i>
<i class="price"> $15.00</i>
</li>
<li class="list">
<i class='first'>Co-Pay: great deal</i>
<i class="price"> $1.00</i>
</li>
</ul>
For part of a site I'm making, I'm looking to have a grid of square objects, and have them pack together tightly so there's no spaces.
Here is what I have made:
But here's what I want it to look like:
So far I've only been doing this by padding and adding margins, and then by vertically aligning each list item. But I want it to go one step further than vertical alignment, I want each item to fit directly underneath the one above it.
I'm sure there's a very different, better approach than the one I've taken, which would be great too!
Here's what I have done:
HTML:
<header class="results">
<ul class="container">
<li>
<a id="name">Temp</a>
<a id="position">Temp</a>
</li>
<li>
<a id="name">Temp</a>
<a id="position">Temp</a>
<a id="position">Temp</a>
</li>
<li>
<a id="name">Temp</a>
<a id="position">Temp</a>
</li>
<li>
<a id="name">Temp</a>
<a id="position">Temp</a>
</li>
<li>
<a id="name">Temp</a>
<a id="position">Temp</a>
</li>
<li>
<a id="name">Temp</a>
<a id="position">Temp</a>
</li>
</ul>
</header>
CSS:
body {
margin: 0;
}
.page {
background: #fff;
}
header.results {
max-width: 100%;
}
header.results .container {
padding: 1em 0 2em;
margin: 0px auto 3px;
line-height: 1;
}
header.results .container li {
width: 30%;
display: inline-block;
padding: 2em 2em 0.75em;
margin: 0px auto 3px;
background: rgb(240,240,240);
vertical-align: top;
}
header.results .container li #name {
text-align: center;
display: block;
margin-top: 0.5em;
font-weight: 500;
}
header.results .container li #position {
text-align: center;
display: block;
margin-top: 0.5em;
font-weight: 250;
font-size: 85%;
}
If you're not supporting older browsers (IE 8 & 9), you could implement this with CSS columns, as shown here.
For full browser support, I'd go with the jQuery masonry plugin.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Hello Stack Overflow community! I am a complete beginner to programming and am attempting to learn my way around HTML and CSS at the moment. One of the projects I am working on is to re-create the Google homepage without looking at the source code. I am struggling right now with positioning of various elements (logo, search box, footer).
Can someone look at my code and tell me specifically why my positioning will not work on the three elements I mentioned?
Also, is my HTML semantically correct when it comes to "id" and "class"? Here is the code:
<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="main.css"/>
<title>Google</title>
<div class="container">
<nav id="nav">
<ul>
<li>+You</li>
<li>Search</li>
<li>Images</li>
<li>Maps</li>
<li>Play</li>
<li>YouTube</li>
<li>News</li>
<li>Gmail</li>
<li>Drive</li>
<li>Calendar</li>
<li>More</li>
</ul>
</nav>
<img src="https://www.google.com/images/srpr/logo4w.png" alt="Google" width=280 height=95/>
<div id="sign">SIGN IN</div>
<form>
<input type="text">
</form>
<div id="footer">
<ul>
<li>Advertising Programs<li>
<li>Business Solutions<li>
<li>Privacy & Terms<li>
<li>+Google<li>
<li>About Google<li>
</ul>
</div>
.container {
width: auto;
height: 600px;
position: relative;
}
#nav {
background-color: #333333;
height:30px;
}
li a {
text-decoration: none;
font-family: arial;
color: #ABABAB;
float: left;
padding: 5px;
font-size: 13px;
font-weight: 600;
margin-top: 2px;
margin-right: 13px;
}
img {
margin-left: 130px;
margin-top: 195px;
}
#sign {
border: 2px solid #D94A4A;
background-color: #D94A4A;
height: 16px;
width: 65px;
float: right;
margin-right: 40px;
margin-top: 20px;
border-radius: 3px;
font-size: 11px;
font-family: arial;
text-align: center;
color: white;
font-weight: 600;
padding-top: 7px;
}
input {
width: 550px;
height: 25px;
border: 1px solid #3492F7;
margin-top: 290px;
margin-left: 670px;
}
#footer {
position: relative;
bottom: 0;
font-family: verdana;
display: inline;
}
You forgot some basics : see Uzziel's answer for precisions, he pointed those accurately (comments too).
However, you should know that Stack Overflow is not a place to get all your code corrected for you, but I think it could be helpful for you to see where it was wrong.
I tried not to change the HTML structure too much, I did a few modifications mainly in the CSS.
There's a website, JSFiddle.net, where you can edit HTML and CSS (and JS) online and see the result ± instantly.
I put my code here, so you can tweak it as you wish afterwhile.
HTML
<div class="container">
<nav id="nav">
<ul>
<li>
<a href="#">
+You
</a>
</li>
<li>
<a href="#">
Search
</a>
</li>
<li>
<a href="#">
Images
</a>
</li>
<li>
<a href="#">
Maps
</a>
</li>
<li>
<a href="#">
Play
</a>
</li>
<li>
<a href="#">
YouTube
</a>
</li>
<li>
<a href="#">
News
</a>
</li>
<li>
<a href="#">
Gmail
</a>
</li>
<li>
<a href="#">
Drive
</a>
</li>
<li>
<a href="#">
Calendar
</a>
</li>
<li>
<a href="#">
More
</a>
</li>
</ul>
</nav>
<div id="sign">
SIGN IN
</div>
<img src="https://www.google.com/images/srpr/logo4w.png" alt="Google" />
<form>
<input type="text" />
</form>
<div id="footer">
<ul>
<li>
<a href="#">
Advertising Programs
</a>
</li>
<li>
<a href="#">
Business Solutions
</a>
</li>
<li>
<a href="#">
Privacy & Terms
</a>
</li>
<li>
<a href="#">
+Google
</a>
</li>
<li>
<a href="#">
About Google
</a>
</li>
</ul>
</div>
</div>
CSS :
.container {
width: 100%;
height: 1000px;
position: relative;
}
nav {
background - color: #333333;
height: 30px;
}
li {
list-style: none;
display: inline-block;
}
li a {
text-decoration: none;
font-family: arial;
color: # ABABAB;
float: left;
padding: 5px;
font - size: 13px;
font - weight: 600;
margin - top: 2px;
margin - right: 13px;
}
img {
width: 280px;
margin: 195px auto 0 auto;
display: block;
}
#
sign {
border: 2px solid# D94A4A;
background - color: #D94A4A;
height: 16px;
width: 65px;
float: right;
margin - right: 40px;
margin - top: 20px;
border - radius: 3px;
font - size: 11px;
font - family: arial;
text - align: center;
color: white;
font - weight: 600;
padding - top: 7px;
}
input {
width: 550px;
height: 25px;
border: 1px solid #3492F7;
margin: 20px auto;
display: block;
}
# footer {
position: absolute;
bottom: 0;
font - family: verdana;
}
You're missing some basic HTML structure - you don't close the HEAD tag; there is no BODY tag; you don't close the HTML tag.
No, the code you presented is not semantically correct when it comes to ID and CLASS because you're not closing all of your DIVs. The "container" DIV in particular isn't closed. Just remember when using ID and CLASS that an ID should only show up once in the document; CLASS can be re-used as often as you want.