CSS - items not staying inline within div - html

I'm trying to get 2 items to display inline-block within a div but nothing I've tried is working.
The html I've used is
<div class="quotation">
<ul>
<li><img src="images/quotes.png" alt="" class="quotemarks"></li>
<li><p class="words">All honour to the Enderbies, therefore, whose house, I think, exists to the present day; though doubtless the original Samuel must long ago have slipped his cable for the great South Sea of the other world.</p></li>
</ul>
</div>
Whilst my CSS at the moment is as follows:
.quotation {
position: absolute;
margin: 20% 5% 10% 5%;
width: 88.2%;
max-height: 100px;
padding: 0.5%;
background-color: red;
color: #bdc3c7;
}
.quotation ul li {
position: relative;
display: inline-block;
text-decoration: none;
font-family: 'PT Sans', sans-serif;
font-weight: 100;
}
.quotemarks {
max-width: 20%;
}
.words {
width: 60%;
}
I cannot understand why .quotemarks and .words won't a) stay within .quotation and b) won't render inline.

You have quite some things wrong in your code and understanding of how css layout works.
You tell your list items to be display: inline-block. This tells them to be just as wide as their content.
You tell the content of your list items - the img and the paragraph - to have their width based on % - which refers to % of the width of the parent element - which happens to be the list item.
So basically the list item asks its content "How wide am I needed to be?" - while the content asks the parent list item "How wide are you? I'll be xy % of that."
It's easy to see that each element needs an answer before it can give one, creating an infinite loop of unanswered questions.
Apart from that, as soon as there is any whitespace (even a linebreak only) between two or more inline-block elements whose summed up width is 100% will make (at least) the last element wrap to a new line.
How to solve the inline-block whitespace issue: Either make your list-items float: left; (which has its own pitfalls!) or set font-size: 0; on the parent element (in this case the ul) , and re-set it on children as needed.
Also, put the width-controlling classes on the list items.
.quotation {
position: absolute;
margin: 20% 5% 10% 5%;
width: 88.2%;
max-height: 100px;
padding: 0.5%;
background-color: red;
color: #bdc3c7;
}
.quotation ul {
/*set this to avoid linebreak due to whitespace */
font-size: 0;
}
.quotation ul li {
display: inline-block;
text-decoration: none;
font-family: 'PT Sans', sans-serif;
/* re-set font-size here to what you need */
font-size: 14px;
font-weight: 100;
vertical-align: text-top;
}
.quotemarks {
max-width: 20%;
}
.words {
width: 60%;
}
.quotemarks img {
max-width: 100%;
}
<div class="quotation">
<ul>
<li class="quotemarks">
<img src="http://placekitten.com/g/200/300" alt="" />
</li>
<li class="words">
<p>All honour to the Enderbies, therefore, whose house, I think, exists to the present day; though doubtless the original Samuel must long ago have slipped his cable for the great South Sea of the other world.</p>
</li>
</ul>
</div>

Move your classes .quotemark and words to parent elements
<div class="quotation">
<ul>
<li class="quotemarks"><img src="images/quotes.png" alt=""></li>
<li class="words"><p>All honour to the Enderbies, therefore, whose house, I think, exists to the present day; though doubtless the original Samuel must long ago have slipped his cable for the great South Sea of the other world.</p></li>
</ul>
</div>
Make sure that you added necessary vertical-align rule (top, middle or bottom... ) to your list items.
Check out demo
I hope this will help.

Thanks guys, your solutions worked and, whilst my CSS is still ugly as sin, everything fits in the box and I was able to change the size of the quotemarks too.
<div class="quotation">
<ul>
<li class="quotemarks"><img src="images/quotes.png" alt=""></li>
<li class="words"><p>All honour to the Enderbies, therefore, whose house, I think, exists to the present day</p></li>
</div>
And the CSS
.quotation {
position: absolute;
margin: 20% 5% 200px 5%;
width: 88.2%;
max-height: 100px;
padding: 0.5%;
background-color: #f5f5f5;
color: #bdc3c7;
}
.quotation ul li {
position: relative;
display: inline-block;
text-decoration: none;
font-family: 'PT Sans', sans-serif;
font-weight: 100;
vertical-align: middle;
}
.quotemarks {
max-width: 20%;
margin: 0 10px 0 0;
}
.quotemarks img {
height: 40px;
}
.words {
width: 80%;
line-height: 15px;
font-size: 20px;
}

Related

Make div exact height as text

I create a div containing some text with some simple css:
.textBox {
display: inline-block;
font-size: 50px;
}
The container is simply:
<div class="textBox"> Test </div>
Nevertheless, the text isn't perfectly vertically centered within the div. There are 13px above and 11px below. Hence I would like the height of the div to be exactly as the height of the text.
So that's pretty hard. As a lot of people suggested you should probably consider line-height. But the problem with that is that the default styling rules won't know what letters you're gonna use. For example are there normal captials like M or maybe you'd use something like Â, then it needs to be bigger right? Or Q and it needs to be lower.
It really is too bad that there isn't any shrink-to-text property in this case. Maybe line-height with some trial and error will be enough for you.
.textBox {
display: inline-block;
background-color: darkcyan;
font-size: 50px;
line-height: 0.68em;
}
<div class="textBox">Test</div>
Not sure what your intened result should be. But if you want to highlight text for example, even line-height isn't precise. In that case I fall back at a helper tag that I'll style to fill the background behind my text. It's a bit more code though and as shown in the example for each font you need to find the correct settings again.
/* Setup textBox properties */
.textBox {
display: inline-block;
position: relative;
}
.textBox{ font-size: 50px; }
.textBox:hover{ font-size: 200px; }
/* Setup default properties for the marking*/
mark{
background-color: darkcyan;
position: absolute;
z-index: -999;
}
/* Specific setting first font-family */
.serif{
font-family: serif;
}
.serif mark {
height: 0.68em;
top: 0.25em;
left: 0.03em;
width: calc(100% - 0.03em);
}
/* Specific setting second font-family */
.sans-serif{
font-family: sans-serif;
}
.sans-serif mark {
height: 0.73em;
top: 0.2em;
left: 0.08em;
width: calc(100% - 0.08em);
}
/* Specific setting last font-family */
.verdana{
font-family: Verdana, sans-serif;
}
.verdana mark {
height: 0.94em;
top: 0.28em;
right: 0.09em;
width: calc(100% - 0.09em);
}
<div class="textBox serif">
<mark></mark>
Test
</div>
<br>
<br>
<div class="textBox sans-serif">
<mark></mark>
Meet
</div>
<br>
<br>
<div class="textBox verdana">
<mark></mark>
Testing
</div>
You can use:
height: fit-content;
like this:
.textBox {
display: inline-block;
font-size: 50px;
height: fit-content;
}
Use the property ,height : fit-content ; to the div

How do I make my hamburger menu appear directly under my hamburger icon?

I want to have to click on a hamburger menu icon and then have the list display beneath my icon. I set up my hamburger menu icon with this style
.menu-btn div {
position: absolute;
left: 100%;
top: 64%;
padding-right: 8px;
margin-top: -0.50em;
line-height: 1.2;
font-size: 18px;
font-weight: 200;
vertical-align: middle;
z-index: 99;
}
.menu-btn span {
display: block;
width: 20px;
height: 2px;
margin: 4px 0;
background: #989da1;
z-index: 99;
}
The menu of options taht should appear after you click on the hamburger menu is
<div class="responsive-menu">
<ul id="menu">
<li>Vote</li>
<li>Search</li>
<li>About</li>
<li>Log In</li>
</ul>
</div>
but I'm unclear how to set up the style of the hamburger menu so taht it appears directly under the hamburger menu when you click on it. Right now, its appearing centered at the top of the screen -- https://jsfiddle.net/wtp1k57b/1/ . How do I set up such a style?
PS - I'm looking for a solution that doesn't rely on hard-coding numeric (e.g. top: 27px) pixel values. Certainly its good to get things to work in my little Fiddle, but in my broader application I can't guarantee how big or small that hamburger menu will be.
I would like to show a completely different approach without using display: flex.
HTML
Your approach uses too many wrappers in my opinion. You can definitely reduce the amount of divs. Moreover, you should always try to use semantic tags over general tags like div or ul. Consider looking at this article.
Hence, as #scooterlord already mentioned, you should use a button for the hamburger icon. Moreover, I recommend to use a nav instead of a list.
CSS
First of all, you should bundle the attributes for the same selector at the same place for the purpose of improved clarity. You should not have three sections where you apply the universal selector, but combine it into one. Moreover, do not set the box-sizing to a specific value, but rather set it to inherit, so you can always override this value for a specific element without having to do it for all of its children. Furthermore, I do not understand what you want to achieve with margin: 0 auto on all elements and body. It does not make any sense for me.
Since you do not want to use absolute positioning, I would strongly advise you to avoid using pixels as a measuring unit. They behave badly if some people change their default font-size because of poor eyesight or other reasons. Instead, consider to apply relative units like rem, em or %. By setting the root element's font-size to 62.5% you are still able to calculate as if you were using pixels (1rem = 10px).
As I already mentioned, I avoided to use display: flex for such a trivial thing. I do not understand why it should be used at this point. Therefore, I also had to change the positioning of the menu button. The navigation could be easily positioned using percentages for top and left.
As a side note: You should really try to only post the relevant CSS code - the first step for me was to remove all the irrelevant parts of it.
Final Solution
This is my final solution without Flexbox, without fixed sizes and without absolute positioning using px:
$('.menu-btn').click(function() {
$('nav').toggleClass('nav-open');
});
* {
margin: 0;
padding: 0;
box-sizing: inherit;
}
html {
font-size: 62.5%;
}
body {
font: 1.6rem/1.4 Benton Sans, -apple-system, BlinkMacSystemFont, Roboto, Helvetica Neue, Helvetica, Tahoma, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
box-sizing: border-box;
}
header {
width: 100%;
background-color: orange;
text-align: center;
padding: 1rem;
position: relative;
}
nav {
display: none;
width: 30rem;
padding: 5rem;
background-color: #ededed;
position: absolute;
right: 5%;
top: 100%;
}
.nav-open {
display: block;
}
nav a {
display: block;
width: 100%;
text-align: center;
padding: 1.4rem 1.6rem;
text-decoration: none;
cursor: pointer;
font-size: 2.2rem;
color: #000;
}
nav a:hover {
background-color: #111;
color: #fff;
}
.menu-btn {
position: absolute;
right: 5%;
top: 50%;
margin-top: -1.1rem;
display: inline-block;
cursor: pointer;
border: none;
outline: none;
background-color: transparent;
}
#media only screen and (min-width: 500px) {
.menu-btn, nav {
display: none !important;
}
}
.menu-btn span {
display: block;
width: 2rem;
height: 0.2rem;
margin: 0.4rem 0;
background: #989da1;
z-index: 99;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header>
<h2>Page Title</h2>
<button class="menu-btn">
<span></span>
<span></span>
<span></span>
</button>
<nav>
Vote
Search
About
Log In
</nav>
</header>
Or see this fiddle.
Use the css properties: top and right to set the position of the element under your icon.
#menu
{
position: absolute;
top: 48px;
right: 2px;
background: #ededed;
list-style-type: none;
}
Use this CSS for your menu - no margin, and the position defined by the top and right settings:
#menu {
position: absolute;
width: 300px;
margin: 0;
padding: 50px;
background: #ededed;
list-style-type: none;
-webkit-font-smoothing: antialiased;
top: 50px;
right: 0;
}
https://jsfiddle.net/meuexde6/
I left out the transition for the testing, but you should basically animate the right parameter from -100px to 0 to achieve what you seemed to have in mind.
ADDITION AFTER COMMENT:
To define the position of the menu in relation to the button, you have to apply position: relative to their common parent element, .mobile-nav. The position values of an element with position: absolute always relate to the first ancestor which has position: relative.
I changed the values in my updated fiddle accordingly to these:
#menu {
position: absolute;
width: 300px;
margin: 0;
padding: 50px;
background: #ededed;
list-style-type: none;
-webkit-font-smoothing: antialiased;
top: 40px;
right: -32px;
}
Updated fiddle: https://jsfiddle.net/meuexde6/1/
If you really want the menu to stick directly to the button (hard to say - it has no borders), just adjust the top and right values as needed.
HTML5 Semantic Elements.
details > summary {
padding: 2px 6px;
width:12px;
border: none;
list-style: none;
}
details > summary::-webkit-details-marker {
display: none;
}
ul{
list-style: none;
margin-left:0;
padding-left:0;
}
<details>
<summary>☰</summary>
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
</details>
So, here goes. I know you are asking for a solution to a specific problem, I solved it alright, but I couldn't help noticing that you are struggling with your code. You must simplify the way you think and your code will become leaner. The purpose of this forum is to help others become better, right? :)
HTML
It is good practice to keep the menu toggle button OUTSIDE of the menu - will solve a lot of issues - check below.
It is not semantically right to use anything else rather than a button for the toggle function, so, why not use a button here? I also removed unnecessary clutter from your code, like some divs and the id - the id could be traded with the class, your call. I also removed .mobile-nav because it is not needed at all.
<button class="menu-btn">
<span></span>
<span></span>
<span></span>
</button>
<div class="responsive-menu">
<ul id="menu">
<li>Vote</li>
<li>Search</li>
<li>About</li>
<li>Log In</li>
</ul>
</div>
CSS
I absolutely positioned the menu-btn on the top right corner, and gave it a width equal to the #pageTitle height (which I set at 50px - a gold standard) to keep it rectangular; it should be a rule of thumb that the toggle buttons are rectangular and always the same height as the top navigation bar - in this case the before-mentioned id. The same I did for the .responsive-menu. I absolutely positioned it as shown below. The changes allowed me to remove a lot of css styling - now obsolete - like for example the absolute positioning of the ul menu inside the .responsive-menu.
.menu-btn {
position:absolute;
display:block;
right:0;
top:0;
width:50px;
height:50px;
background:yellow;
border:none;
padding:16px;
}
.responsive-menu {
position: absolute;
top: 50px;
right: 0;
display: none;
}
Javascript
By years of practice I realized that the most efficient way to toggle a menu instead of adding and removing classes is to add a class on the body tag; this can help heaps if you want to restyle anything else on the page depending on wether your menu is opened or not.
$('.menu-btn').on('click', function() {
$('body').toggleClass('responsive-menu-open');
});
Here is a working jsfiddle: https://jsfiddle.net/scooterlord/4atafhge/
I could have done a lot of other things in order to simplify the code even further - remove unnecessary ids and classes since most elements are considered unique and could be targeted using descendant classes, eg .responsive-menu ul, etc. After a lot of practice, you'll manage to think simpler and produce code with a smaller footprint.
Edit: Concerning the fact that you don't like the absolute pixels for alignment here is a trick.
Giving a fixed height to the parent container, equal to the toggle button's -in this case '#pageTitle' and setting its position to relative allows you to use top:100% to properly place the responsive menu exactly below the button (which is essentially the same height):
#pageTitle {
display: flex;
height: 50px;
position:relative;
}
.responsive-menu {
position: absolute;
top: 100%;
right: 0;
display: none;
}
Here is an updated fiddle: https://jsfiddle.net/scooterlord/4atafhge/1/
Edit: Natalia, I gave it some thought and here is what I came up with. I created an absolutely positioned .menu-wrapper, inside of which I placed the button and the responsive menu with float:right and no positioning - aka they are positioned statically. No more pixel values! YAY!
.menu-wrapper {
position:absolute;
top:0;
right:0;
}
.menu-btn {
float:right;
...
}
.responsive-menu {
float:right;
clear:both; // to clear the .menu-btn and sit exactly below it
...
}
Here is a working fiddle: https://jsfiddle.net/scooterlord/4atafhge/2/

Parent text wrapping beside child instead of behind it?

I will be the first to admit that I am new to this so please forgive me if this question has been answered before. Perhaps I am not wording my question correctly to find the answer I am looking for.
I have a child container with quick reference links on the left and the text it is to reference should be in the parent on the right. How do I get the text in the parent to wrap in the green viewable area and not across the whole parent container (behind the child)?
Text Wrapping Problem
<div class="ministry1"><div class="ministry2">
<ol id="sidebar1">
<li>The Scripture</li>
<li>God
<ul id="side1">
<li>God the Father</li>
<li><a href="#002">God the Son</li>
<li><a href="#003">God the Holy Spirit</li>
</ul>
</li>
<li>Man</li>
<li>Salvation</li>
<li>God's Purpose of Grace</li><!--'-->
<li>The Church</li>
<li>Baptism and the Lord's Supper</li><!--'-->
<li>The Lord's Day</li><!--'-->
<li>The Kingdom</li>
<li>Last Things</li>
<li>Evangelism and Missions</li>
<li>Education</li>
<li>Stewardship</li>
<li>Cooperation</li>
<li>The Christian and the Social Order</li>
<li>Peace and War</li>
<li>Religious Liberty</li>
<li>The Family</li>
</ol></div>
<h1>Baptist Faith and Message</h1>
<p id="box">
<h3 id="01"><u>I. The Scriptures</u></h3>
(with LOTS more text below...)
And they are styled as follows...
div.ministry1 {
display: block;
margin: auto;
background-color: #99ff99;
border: 2px solid #660066;
width: 1070px;
height: 446px;
overflow: auto;
font-family: 'Ubuntu', sans-serif;
opacity: 0.90;
}
div.ministry2 {
display: block;
position: fixed;
float: left;
background-color: lightblue;
border-right: 2px solid #660066;
width: 225px;
height: 446px;
overflow: auto;
font-family: 'Ubuntu', sans-serif;
opacity: 0.90;
}
#box {
float: right;
padding: 8px 10px 10px 10px;
opacity: 1.0;
}
If you want everything to be fixed width, the easiest possible solution will be making sidebar1 fixed and wrapping content on the right with div container with satisfying padding-left (or margin-left, it should make no difference here).
Using your containers (although I swapped box to be div, I need this element to be display: block, and it's default display for div).
div.ministry2 {
position: fixed;
}
.box {
padding-left: 250px;
}
You can check how this solution looks here: http://codepen.io/anon/pen/oLyEmV

How to prevent a span from breaking into the next line - responsive webdesign?

Following simple list, where in every h4, there is a span at the end.
<ul class="items">
<li>
<h4>Prevent LineBreakOfPlus <span class="goto">o</span>
</h4>
</li>
<li>
<h4>Digital Signage <span class="goto">o</span></h4>
… 
</ul>
Screenshot of the page's source:
The CSS for the span looks like this …
.items .goto {
font-family: 'QuaySans-Icons';
font-size: 1.6em;
position: relative;
float: right;
}
The final thing looks like this:
The problem I have with this is that when decreasing the width of the browser window (I'm working on a responsive webdesign) the span-icon is breaking into the next line.
Do you have any creative solution or idea on how to prevent this from happening?
Kind regards and thank you in advance,
Matt
If you want the icon to keep inline with the last word in your text line, you can simply do:
<ul class="items">
<li>
<h4>Prevent LineBreakOfPlus<span class="goto">o</span></h4>
</li>
<li>
<h4>Digital Signage<span class="goto">o</span></h4>
</li>
</ul>
and the CSS might be:
.items {
list-style: none;
margin: 0;
padding: 0;
}
.items li {
border-bottom: 1px solid gray;
margin: 0;
padding: 0;
}
.items h4 {
margin: 0;
}
.items .goto {
background-color: gray;
font-size: 1.6em;
margin-left: 10px; /* optional */
}
If there is no white space between your work and the span, the motif will simply follow the word if the li element is forced to flow into a second line.
You can use margin-left to create visual spacing or insert a &nbsp entity before the span, quite a few ways to do. The details depend a bit on what effect you want.
Fiddle: http://jsfiddle.net/audetwebdesign/VsBet/ (two examples of how to do it)
Keeping Icon Right Justified
Here is one approach to pinning the icon to the right of the h4 element:
.ex2.items h4 {
position: relative;
line-height: 1.5;
outline: 1px dotted blue;
padding-right: 2.00em;
}
.ex2.items .goto {
background-color: wheat;
line-height: 1.00;
font-size: 1.6em;
position: absolute;
right: 0;
bottom: 0.0em;
height: 1.00em;
width: 1.00em;
outline: 1px dotted red;
}
Use absolute positioning of the span to keep it to the right and bottom of h4. If h4 forms to line, the icon will follow the second line. You may need to adjust the positioning depending on the icon size. If you allow the icon to grow in size, you may get other issue in extreme cases. I might fix the icon to a px height or width (or a max value). Finally, set some padding-right in h4 to prevent the icon from overlapping the text as the window gets smaller.
Note I explicitly specified line-height values to accentuate the issue around not knowing the height of the icon. You may need to adjust these to vertically position the icon.
Decrease your font-size when you have less space. I guess you have the problem in media with max-width:480px. I found decreasing the font-size a good alternative to keep the design consistent in responsive sites
I've mocked it up on the demo, however it is a bit raw.
.items {
padding:0;
margin:0;
/*width:180px;*/
}
.items li {
border: 1px solid red;
list-style-type: none;
position: relative;
}
.items h4 {
margin:0; padding:0; font-size:16px; padding-right:10px;
}
.items .goto {
margin-top: -10px;
position: absolute;
right: 0;
top: 50%;
}
DEMO
Check the following link and decrease the width of browser.
RESULT

What causes this weird display ? I suspect it might be CSS or a bug in Chrome OS X?

This is what the weird view looks like, in Chrome on OS X on first load:
That blue bar running right across the screen is actually the navigation bar that should be at the top of the screen. See below.
This is what the original view looks like, and this is what it reverts to - once I scroll down (so that portion of the screen disappears) and scroll back up:
Edit1: I don't even know what code to post, if any because it is kinda random. If you ask some questions though, maybe something might jump out and I will know what code to either post or look at.
Thanks.
Edit2: Here is the code for the div#navigation:
<div id="navigation">
<div id="quota-info">
Plan: Chameleon<br />
# of Projects: 2 / 20<br />
# of Clients: 2 / 15<br />
Storage: 10.8 MB / 10.0GB <br />
</div>
<div id="user-info">
<span class="username">Hi Test</span><br />
Name: Test User<br />
Email: test#abc.com<br />
Last Logged In: about 2 hours ago<br />
</div>
<ul>
<li><img src="logo.png" /></li>
<li id="dashboard">Dashboard</li>
<li id="settings">Settings</li>
<li id="logout">Logout</li>
</ul>
</div>
Here is the CSS:
#navigation {
font-family: Verdana, Arial, sans-serif;
font-size: 13px;
background: #2d343f;
background-image: url('../images/nav-textured-bg.png');
background-repeat: repeat;
padding: 5px 5px;
margin: 0px auto;
position: relative;
height: 75px;
}
#navigation a {
text-decoration: none;
padding: 10px 15px;
display: inline;
height: 35px;
line-height: 35px;
color: #c6c8cb;
}
#navigation ul {
width: 100%;
margin: 0 auto;
text-align: center;
}
#navigation li {
width: 100%;
display: inline;
list-style-type: none;
}
#navigation li img {
position: relative;
top: 15px;
}
Edit 3:
Here is another screenshot of how it looks when I scroll up. The top navigational bar is still there. This blue thing is not even the menu, it's like a screenshot of it. When I hover over the menu links, they don't work.
You have to create a reduction. Start with a copy of the actual page and then remove stuff not related to the problem, one by one, until it disappears. Then you'll see what's causing it, whether it's a browser bug and what you can do to fix it.
PS: If it's a browser bug, don't neglect to report it. It's a web developer's responsibility.
I would start by cleaning up some of your CSS.
#navigation {
font-family: Verdana, Arial, sans-serif;
font-size: 13px;
background: #2d343f;
background-image: url('../images/nav-textured-bg.png');
background-repeat: repeat;
padding: 5px 5px;
margin: 0px auto; <--- see below
position: relative;
height: 75px;
}
this should be margin:0 since it's a block element. Block elements take up the entire line, so trying to center it makes no sense.
#navigation a {
text-decoration: none;
padding: 10px 15px;
display: inline; <--- conflict (see below)
height: 35px; <--- conflict (see below)
line-height: 35px;
color: #c6c8cb;
}
Inline elements can't have a width or height applied to them, but some browsers when you try to will automatically convert any inline element to inline-block for you. If that is what you want, you should specify it, otherwise drop the height.
#navigation ul {
width: 100%;
margin: 0 auto; <-- see below
text-align: center;
}
Centering an item with 100% width does not make sense here
#navigation li {
width: 100%; <--- conflict
display: inline; <--- conflict
list-style-type: none;
}
#navigation li img {
position: relative;
top: 15px;
}