I'm trying to use the before selector to mimic some behavior of a list item when applied to elements. I can't use list items so I have to be able to get styles to work.
Here is a fiddle showing the issue: http://jsfiddle.net/7g6ncg7u/
.container {
width:300px;
}
.up:before{
content:'\25B2';
color:green;
padding-right:10px;
margin:1px;
}
.down:before{
content:'\25BC';
color:red;
padding-right:10px;
margin:1px;
}
What I want to have happen is the 2nd line of the 2nd span to align with the text above it, not the start of the line like its presently doing.
You can use float like this:
.down {
clear:both;
}
.down:before{
content:'\25BC';
float:left; /**ADD THIS**/
color:red;
padding-right:10px;
margin:1px;
}
Check the Snippet Below
.container {
width: 300px;
}
.down {
clear: both;
}
.up:before {
content: '\25B2';
color: green;
padding-right: 10px;
margin: 1px;
}
.down:before {
content: '\25BC';
float: left;
color: red;
padding-right: 10px;
margin: 1px;
}
<div class="container">
<span class="down" style="display: block;">regular bullet point text</span>
<span class="down" style="display: block;">regular bullet point text but this one is longer and will wrap</span>
</div>
Try wrapping the content of the bullet in a tag, such as a p tag. This will separate the bullet from the text and allow you to handle them separately. From there you can change the span to display: table; and the p to display:table-cell;.
Example fiddle: http://jsfiddle.net/7g6ncg7u/5/
Here's what I used, and it seems to work pretty well.
HTML:
<div class="container">
<div class="down" style="display: block;">
regular bullet point text
</div>
<div class="down" style="display: block;">
regular bullet point text but this one is longer and will wrap
</div>
</div>
CSS:
.container{
width:300px;
}
.down
.up:before{
content:'\25B2';
color:green;
padding-right:10px;
margin:1px;
}
.down:before{
content:'\25BC';
color:red;
padding-right:10px;
margin:1px;
}
div {
padding-left: 1.5em;
text-indent:-2.0em;
}
Related
I'm trying to achive a layout like this:
So the subtitle is always at least in the second line, even if it could fit in the first line next to the title. If the title is long and break into two or more lines, the subtitle should follow the title without breaking a new line.
This is what i did so far, but this solution is not perfect:
div.data {
background:white;
position:relative;
min-height: 2em;
float:left;
max-width:150px;
margin: 50px;
}
.title {
position:relative;
overflow:hidden;
background: white;
z-index: 10;
}
.title span {
position:relative;
}
.title span span {
position:absolute;
right:0px;
opacity:1;
top: 18px;
display: block;
transform: translateX(100%);
padding-left: 5px;
color: red;
}
span.subtitle {
position:absolute;
bottom:0;
left:0;
z-index: 5;
display:block;
color: red;
}
<div class="data">
<div class="title"><span>title <span>subtitle</span></span></div>
<span class="subtitle">subtitle</span>
</div>
<div class="data">
<div class="title"><span>reaallllyyyy loooooong title <span>subtitle</span></span></div>
<span class="subtitle">subtitle</span>
</div>
Sorry about the question's title, thats the best i could come up with.
I have simplied your HTML, I believe that you don't really want your current one.
Adding a pseudo element floated right seems to do what you want: force the subtitle to a new line only if it is on the first one:
The pseudoelement has a color for demo purposes, remove it in production
.data {
display: inline-block;
background-color: antiquewhite;
max-width: 150px;
margin: 10px;
}
.title {
color: blue;
}
.subtitle {
color: gray;
}
.title:before {
content: " ";
float: right;
width: 150px;
height: 1px;
background-color: green;
}
<div class="data">
<span class="title">title</span>
<span class="subtitle">subtitle</span>
</div>
<div class="data">
<span class="title">reaallllyyyy loooooong title</span>
<span class="subtitle">subtitle</span>
</div>
As mentioned in the comments, CSS can't detect when text has wrapped. Here is a jQuery solution. Someone may come up with a better way of doing this, but it is the best approach I could think of in a short space of time.
What I am doing it getting the width of the .data element, then cloning it and applying CSS to the clone to prevent the text from wrapping. I then compare the width of the existing and cloned element to determine whether the text has wrapped.
$(document).ready(function() {
// iterate through .data elements
$('.data').each(function() {
// store width of currently iterated element
var startTextWidth = $(this).width();
// clone currently iterated element
var clone = $(this).clone();
// apply CSS to cloned element to prevent text wrapping.
clone.css({
position: "absolute",
left: "-10000px",
maxWidth: "none"
}).appendTo("body");
// get the width of the cloned element with the newly applied CSS
textWidth = clone.width();
// if cloned elements width is larger than the existing elements width then the text has wrapped
if(textWidth > startTextWidth) {
// apply display:inline to the .title element
$(this).find('.title').css('display', 'inline');
}
// remove cloned element
clone.remove();
});
});
div.data {
background:white;
position:relative;
min-height: 2em;
float:left;
max-width:150px;
margin: 50px;
}
.title {
position:relative;
overflow:hidden;
background: white;
z-index: 10;
}
.subtitle {
color:grey;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="data">
<div class="title">title</div>
<span class="subtitle">subtitle</span>
</div>
<div class="data">
<div class="title">reaallllyyyy loooooong title</div>
<span class="subtitle">subtitle</span>
</div>
I have a blank HTML page and I want to align 2 elements...Vertically and Horizontally. These elements are a <img> tag, a <p> tag for text, and 2 <div> tags for containing those elements...
When I resize my window I don't want these elements to be cut-off by my browser. After countless hours of trying to figure this out, and searching Stack and various other websites...I came close, but I could never get it 100% like I want it...
There's this white-space at the bottom and the ride side of the bordered second div near the text, and the culprit appears to be the <p>. When I get rid of the tag the white-space goes away. However, I want the text under the image so I need it...
The white-space is making me question whether the content is placed in the center or not. How can I get rid of it?
HTML
<body>
<div id="container">
<div id="content">
<p>
<img src="http://www.iconsdb.com/icons/preview/blue/square-xxl.png" alt="Under Construction">
<br> UNDER CONSTRUCTION!
</p>
</div>
</div>
</body>
CSS
body
{
margin:0;
background-color: seagreen;
}
#container
{
position:relative;
height:100%;
width:100%;
min-width:400px;
}
#content
{
position:absolute;
top:50%;
left:50%;
transform:translate(-50%,-50%);
outline:3px solid red;
}
#content p
{
margin:0;
text-align:center;
font-family:Courier;
font-size:48px;
white-space:nowrap;
color:springgreen;
}
I changed you HTML to enclose your text in a span tag and removed the br:
<body>
<div id="container">
<div id="content">
<p>
<img src="http://www.iconsdb.com/icons/preview/blue/square-xxl.png" alt="Under Construction">
<span>UNDER CONSTRUCTION!</span>
</p>
</div>
</div>
</body>
Then I added this to your CSS. It styles the enclosing span as a block, so you don't need to <br> tag in your HTML. It also uses line-height to adjust spacing above and below the line of text.
#content span {
display: block;
margin: 0;
line-height: .8;
}
And removed the position attribute from here:
#container
{
/*position:relative;*/ /* Removed */
height:100%;
width:100%;
min-width:400px;
}
Here is a sample fiddle
UPDATE
It appears the reason why you are seeing white-space still on Firefox is that you are using outline instead of border on your CSS for #content.
I don't know exactly why Firefox is rendering the outline differently. But if you change your CSS for #content to the following, you'll get the same result on Chrome, Firefox, Edge and IE (11).:
#content
{
position:absolute;
top:50%;
left:50%;
transform:translate(-50%,-50%);
/*outline:3px solid red;*/
border: 3px solid red;
}
Here is the updated fiddle
I have gone through your code . i have made some changes in above given code . I hope this gone be helpful to you.
CSS
body
{
margin:0;
background-color: seagreen;
}
img{
display: block;
margin: auto;
width: 50%;
}
/* add this css to remove the white space under text */
p
{
margin-bottom: -9px !important;
}
#container
{
position:relative;
height:100%;
width:100%;
min-width:400px;
}
#content
{
position:absolute;
top:50%;
left:50%;
transform:translate(-50%,-50%);
outline:3px solid red;
margin-top: 200px;
padding-top: 10px;
}
#content p
{
margin:0;
text-align:center;
font-family:Courier;
font-size:48px;
white-space:nowrap;
color:springgreen;
}
<div id="container">
<div id="content">
<img src="http://spectrumapartments.com.au/wp-content/themes/spectrumapartments/img/building/red-squares.png" alt="Under Construction">
<br>
<p>UNDER CONSTRUCTION!</p>
</div>
</div>
I GAVE IT ANOTHER TRY, HOPEFULLY THIS WILL SOLVE IT FOR YOU. YOU SOUND VERY DESPERATE.
*{
border: 0;
margin: 0;
margin: 0;
}
.container {
font-size: 0;
}
.container span {
font-size: 35px;
background: #ff8ea1;
padding: 0;
margin: 0;
}
.container span.no-space {
display: inline-block;
vertical-align: top;
height: .75em;
line-height: .75em;
}
<div class="container">
<span>Under Construction</span>
<div style="height: 20px;"></div>
<span class="no-space">Under Construction</span>
</div>
TRY THIS ONE!
I am not able to display div text before image, this is what i want
But not able to get it right.
html
<div id="Header" class="header-first">
<h2 class="sometext">Fruit</h2>
<div id="number-of-fruits" style="display: inline; float: right;">0</div> <span>
<img id="someImage" src="http://www.journeys.travel/images/familytrips/iconCollapseArrow.gif" class="someimage">
</span>
</div>
css
.header-first {
background-color: red;
width: 100%;
height:50px;
}
.sometext {
display: inline-block;
margin: 2px 4px;
text-align: left!important;
color: blue;
}
.someimage {
float: right;
vertical-align: middle;
margin-top: 3px;
}
http://fiddle.jshell.net/WyPWs/1/
Here is an updated fiddle: http://fiddle.jshell.net/WyPWs/4/
Try wrapping your right hand side content in a single div, and then treat your items separately.
<div class="right-section">
<span class="number">0</span>
<img src="http://www.journeys.travel/images/familytrips/iconCollapseArrow.gif" class="my-image">
</div>
Use positioning. In this case I used relative positioning.
#number-of-fruits {
position:relative;
right:30px;
}
http://jsfiddle.net/WyPWs/10/
I'm trying to split my footer so that there is left aligned and right aligned text. I have the following but the two elements are displaying one after the other:
#footer {
clear: both;
background-color: #330066;
padding: .5em;
font-size: 0.8em;
color: #fff;
}
#footer p .left {
text-align:left;
float:left;
}
#footer p .right {
float:right;
text-align:right;
}
<div id="footer">
<p class="left">
Copyright © 2009
</p>
<p class="right">
Designed by xxxxxx
</p>
</div>
Should be really simple I'm sure but I just can't get it working - can anyone offer any advise please?
Thanks
Helen
You're using footer p .right and not footer p.right (note the space character). This means the .right and .left classes don't apply to the paragraphs, but to descendant elements inside the paragraph. Or it could also mean a typo, causing your CSS to fail :)
Please copy your HTML here, so we can help you better.
Edit: I see you've now posted your HTML. My assumption turns out to be correct. Get rid of the spaces between p and .left/.right. Also, if you're floating the paragraphs anyway, you can omit the text-align properties.
#footer p.left {
float: left;
}
#footer p.right {
float: right;
}
Edit: In response to your comment: it should work. Here's a little test case:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test case for the CSS footer problem</title>
<style>
#footer { width: 300px; outline: 1px solid red; }
#footer p.left { float: left; }
#footer p.right { float: right; }
</style>
</head>
<body>
<p>See http://stackoverflow.com/a/867599/96656 for details.
<div id="footer">
<p class="left">Copyright © 2009</p>
<p class="right">Designed by xxxxxx</p>
</div>
</body>
</html>
have you tried setting a width for the left and right, eg 50% each
No need to remove the clear:both on the #footer as suggested before.
As said Mathias Bynens you must write "p.left" instead of "p .left"
You will need a clear both after the two paragraphs and end up with something like :
#footer {
clear: both;
background-color: #330066;
padding: .5em;
font-size: 0.8em;
color: #fff;
}
#footer p.left {
text-align:left;
float:left;
}
#footer p.right {
float:right;
text-align:right;
}
<div id="footer">
<p class="left">
Copyright © 2009
</p>
<p class="right">
Designed by xxxxxx
</p>
<div style="clear:both"></div>
</div>
As paragraphs are block level elements, if you wish them to be displayed side by side you should remove the floats and set them to be inline:
footer p.left {text-align:left; display:inline; }
footer p.right {text-align:right; display:inline; }
Also I assume that should be either #footer or .footer beforehand?
Problem is that on your #footer you have a clear: both; which kills all floats.
It would be better if you have:
#footer {
background-color: #330066;
padding: .5em;
font-size: 0.8em;
color: #fff;
width: 100%;
overflow: hidden;
}
p.left { float: left; }
p.right { float: right; }
The width: 100%; and the overflow: hidden; will fix your problem, as it clears the floats after they are made.
With the code above you will be able just to have:
<div id="footer">
<p class="left">Copyright © 2009</p>
<p class="right">Designed by ****</p>
</div>
I have a header bar that spans horizontally across my web page, which is comprised of one div tag and three nested div tags.
HTML:
<div id="top-bar">
<div id="leftTop">
LEFT
</div>
<div id="rightTop">
RIGHT
</div>
<div id="centerTop">
CENTER
</div>
</div>
CSS:
#top-bar
{
margin: 0;
padding: 1px 4px;
font-size: x-small;
background-color: #005555;
font-family: Arial;
}
#top-bar .separator
{
padding: 0 7px;
border-right: 0px solid #fff;
border-left: 0px solid #fff;
}
#leftTop
{
display: inline;
float: left;
}
#rightTop
{
display: inline;
float: right;
}
#centerTop
{
color: #ffffff;
text-align: center;
}
And it works just great, except for the fact that the div tags are out of order in the HTML code, which I don't like. If I order the div tags by placing them Left, Center, and Right, in the HTML, then the Right div just disappears from the webpage! I'm guessing that it has something to do with the float and text-align attributes having a conflict.
Anyone have any ideas on what is going on here, or is there an easier way to do this in CSS?
Try float: left; on #centerTop or display: inline on all three without any floats.
This works fine, but it depends on what you need. If you dont know the height of the content and you want it to expand dynamicly, then this is not enough:
#leftTop
{
float: left;
}
#rightTop
{
float: right;
}
#centerTop
{
float:left;
text-align: center;
}
I just tested the code from the original post in Firefox 3.0.10, Opera 9.64, IE8 and Google Chrome 2.0.181.1
All browsers showed all 3 divs, not a single div fell off the screen... Are you perhaps using IE6?
I am running your HTML and CSS of FF 3.0.10.
When you re-arrange the CENTERTOP div to be between the LEFTOP and RIGHTTOP divs, the RIGHTTOP div doesn't fall 'off the page' but the "RIGHT" text just falls off onto the next line.
My solution is proposed below (you'll notice I have some additions and some best-practice techniques).
HTML CODE:
<html>
<head>
<link rel="stylesheet" href="global.css">
</head>
<body>
<div id="top-bar">
<div id="leftTop">
LEFT
</div>
<div id="centerTop">
CENTER
</div>
<div id="rightTop">
RIGHT
</div>
</div>
<div class="clearer">
</div>
<div id="randomContent">
RANDOM CONTENT
</div>
</body>
CSS CODE:
#top-bar {
margin: 0;
font-family: Arial;
}
#leftTop {
float: left;
width: 20%;
border: 1px solid red;
}
#centerTop {
float: left;
width: 20%;
border: 1px solid blue;
}
#rightTop {
border: 1px solid green;
}
.clearer {
clear: both;
}
#randomContent {
background-color: yellow;
}
So you'll notice in the HTML that the divs are arranged in order from LEFT to CENTRE to RIGHT. In this CSS, this has been reflected by floating the LEFTTOP and CENTRETOP divs left. You will also notice that I have specified a width property on the LEFTTOP and the CENTERTOP divs, to enable you to space out your divs as wide as you want. (You'll be able to visually see your width modifications as I've added in a border on the divs). No width percentage property has been applied on the RIGHTTOP div as it will consume the remaining 60% of the width (after the LEFTTOP and CENTRETOP have consumed the 40%).
I have also added a CLEARER div. Think of the CLEARER div is a horizontal line break. Essentially it acts as a line of demarcations to separate the floated divs from the content below.
You can then add whatever content you want in the RANDOMCONTENT div.
Hope this helps :)
I don't know that it disappears, but it would drop down a line. Lot's of websites put it out of order for that reason (I know I do).
Another alternative:
#top-bar
{
margin: 0;
padding: 1px 4px;
font-size: x-small;
background-color: #005555;
font-family: Arial;
}
#top-bar .separator
{
padding: 0 7px;
border-right: 0px solid #fff;
border-left: 0px solid #fff;
}
#top-bar>div
{
float: left;
width: 33%;
}
#rightTop
{
text-align: right;
}
#centerTop
{
color: #ffffff;
text-align: center;
width: 34%;
}
And then put <br style="clear:both"/> right before you close your top-bar div.
<div id="top-bar">
<div id="leftTop">
LEFT
</div>
<div id="centerTop">
CENTER
</div>
<div id="rightTop">
RIGHT
</div>
<br style="clear:both"/>
</div>
Not sure if you want the width's defined like this, however.
Another solution:
Set the leftTop, centerTop, and rightTop to display:table-cell,
Set the top-bar to display:table-row,
Set a container to display:table
Set the width of the container and row (#table-bar) to 100%;
Set the width of the columns to the desired ratios (e.g., 25% for left and right, 50% for center)
caveat: table, table-row, and table-cell css display values do not work in IE 5.5 or 6 (and maybe Opera 8); but they do work nicely in all contemporary browsers. IE conditionals can be used to split code for IE > 5 and IE < 7.
TEST:
<html>
<head>
<title>3 Column Header Test</title>
<style type="text/css">
body#abod {
background-color:#F5ECBD;
color:#000;
}
#hdrrow {
margin:0;
padding:0;
width:100%;
border:1px solid #0C5E8D;
display:table;
}
#top-bar {
margin:0;
padding:1px 4px;
width:100%;
font-size:100%;
background-color:orange;/*#005555;*/
font-family: Arial;
border:1px solid #000;
display:table-row;
}
#leftTop {
margin:0;
padding:0 16px;
width:24%;
text-align:left;
color:#000;
background-color:#F0DD80;
border:1px dashed #f00;
display:table-cell;
}
#centerTop {
margin:0;
padding:0 16px;
width:40%;
margin:0 auto;
text-align:center;
color:#000;
background-color:#F5ECBD;
border:1px dashed #f00;
display:table-cell;
}
#rightTop {
margin:0;
padding:0 16px;
width:24%;
text-align:right;
color:#000;
background-color:/*#F0DD80;*/transparent;
/*shows the orange row color*/
border:1px dashed #f00;
display:table-cell;
}
#footer {
padding:25px;
color:#000;
background-color:#F5ECBD;
}
</style>
</head>
<body id="abod">
<div id="hdrrow">
<div id="top-bar">
<div id="leftTop">
LEFT
</div>
<div id="centerTop">
CENTER
</div>
<div id="rightTop">
RIGHT
</div>
</div>
</div>
<h4 id="footer">Footer Lorem ipsum dolor sit amet</h4>
</body>
</html>
Use relative positioning to swap the positions of the divs after they have been floated:
The HTML
<div id="top-bar">
<div id="leftTop">
LEFT
</div>
<div id="centerTop">
CENTER
</div>
<div id="rightTop">
RIGHT
</div>
</div>
The CSS
#leftTop {
width:33%;
float:left;
}
#centerTop {
width:33%;
float:right;
position:relative;
right:33%;
}
#rightTop {
width:33%;
float:right;
position:relative;
left:33%;
}
I use the same process in my Perfect Liquid Layouts to change the column source ordering.