Background-color only on the text field - html

this is my code:
HTML:
<div id="hello">
Hello
</div>
CSS:
#hello {
font-size:30px;
background-color:red;
}
As you can see in the fiddle (https://jsfiddle.net/en27xg3b/), the background color is on all the div. I want it to be only where the "Hello" is.
I'm pretty sure it's a simple code, but I forget how to do it.

Use inline-block or inline: https://jsfiddle.net/en27xg3b/1/
#hello {
font-size:30px;
background-color:red;
display: inline-block;
}
<div id="hello">Hello</div>
Or wrap the text in a span: https://jsfiddle.net/en27xg3b/2/
#hello span {
font-size:30px;
background-color:red;
}
<div id="hello"><span>Hello</span></div>

Wrap your text inside a span. Like this
#hello span{
font-size:30px;
background-color:red;
}
<div id="hello">
<span>Hello</span>
</div>

<div> tag is display block so it fill background full.
In this case, You only set display : inline; for <div> tag
Like this https://jsfiddle.net/en27xg3b/4/

I would take a look at http://www.w3schools.com/cssref/tryit.asp?filename=trycss_text_background
but what you can do is have a style
<style>
span.highlight {
background-color:red;
}
</style>
now in your html you can do
<div id="hello">
<span class="highlight">
Hello
</span>
More Text
</div>

Related

Span jumps to new line even though it has inline-block?

So i've read multiple times that putting display:inline-block; in your span will fix it. But i just can't get it to work for me. Probably i just missed something, but i would like help on this.
<html>
<head>
<style>
#font-face {
font-family:myRobotoRegular;
src:url(fonts/Roboto-Regular.ttf);
}
#font-face {
font-family:myRobotoBold;
src:url(fonts/Roboto-Bold.ttf);
}
#font-face {
font-family:myRobotoLight;
src:url(fonts/Roboto-Light.ttf);
}
body {
background-color:black;
color:white;
}
h1 {
font-family:myRobotoBold;
text-align:center;
}
.right {
float:right;
width:49%;
}
.individual {
height:100%;
margin:0 auto;
overflow-y:scroll;
padding-right:10px;
text-align:left;
width:440px;
}
.bannerGreen {
background-color:#0D731D;
padding:10px;
}
.bannerTitle {
font-family:myRobotoBold;
}
.bannerRarity {
font-family:myRobotoLight;
}
.description {
background-color:black;
font-family:myRobotoRegular;
padding:20px 10px 10px 10px;
}
.quotes {
color:#C0B9A7;
}
.orangeStat {
color:#F26A1C;
display:inline-block;
font-family:myRobotoBold;
font-size:20px;
}
.yellowStat {
color:#FFD30B;
display:inline-block;
font-family:myRobotoBold;
font-size:20px;
}
.imgDiv {
float:right;
margin-top:-10px;
}
.img {
height:58px;
}
</style>
</head>
<body>
<div class="mainDiv">
<div class="right">
<div class="individual">
<h1>
CATEGORY
</h1>
<div class="bannerGreen">
<div class="imgDiv">
<img class="img" src="">
</div>
<span class="bannerTitle">
TITLE
</span>
<br>
<span class="bannerRarity">
SUBTITLE
</span>
</div>
<div class="description">
DESCRIPTION
<span class="yellowStat">
STATISTICS
</span>
DESCRIPTION
<span class="orangeStat">
A STATISTIC
</span>.
</div>
<br>
</div>
</div>
</div>
</body>
</html>
The "A" in "A STATISTIC" should well be able to be on the first line, but instead it follows the entire span to the second line.
(Bonus points if you recognize this. ;) )
EDIT: Inline instead of inline-block surely fixes the problem, but then the period after the .orangeStat span jumps away from the span. And i'd like it to not jump away but i do not want it to be included in the span either. Is there a way to get that?
Well, if you want the A and STATISTIC to be treated individually, then you should put them in individual (inline-)block elements:
<span class="orangeStat">
A
</span>
<span class="orangeStat">
STATISTIC
</span>
OR (edit):
Use inline instead of inline-block and to remove the space between the STATISTIC and the period, try something like this:
<span class="orangeStat">
A STATISTIC<!--
--></span>.
Increase the width of the outer container
.individual {
width: 490px;
}
The words are coming in second line because there is no enought space in the container.
Or
Decrease the font-size of yellowStat and orangeStat

How to add text next to a shape?

I am trying to add text next to the circle.
HTML
<div class="row">
<div class="col-sm-2">
<span><div class="circle"></div></span><p>available</p>
</div>
</div>
CSS
.circle {
width:10px;
height:10px;
border-radius:50px;
font-size:20px;
color:#fff;
line-height:100px;
background: green;
}
Right now, the circle is on top <p> tag.
I am trying to add two circle: available and a red one saying not available. Also, if you know a better way to do this, please let me know.
Thanks,
The div takes up 100% of the width by default, which is why your p tag wraps to the next line. You can set e.g. display:inline-block on the div to change this behaviour.
See this fiddle for your two circles.
Just add the following code :
.col-sm-2 > span, .col-sm-2 > p {
display: inline-block;
}
What this does is it causes the two elements span and paragraph <p> tags to become inline-block and hence align on the same line.
See this below :
.circle {
width:10px;
height:10px;
border-radius:50px;
font-size:20px;
color:#fff;
line-height:100px;
background: green;
}
.col-sm-2 > span, .col-sm-2 > p {
display: inline-block;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
<div class="col-sm-2"> <span><div class="circle"></div></span>
<p>available</p>
</div>
</div>
You could use a icon instead. I like the font awesome icon set http://fortawesome.github.io/Font-Awesome/icon/circle/:
You just add the fontawesome Css files and in your html add:
<i class="fa fa-circle">available</i>

Is it possible to display a div on hover of a <p> tag

I've styled a <p> tag which behaves as a cart button.
Now I want to make a pop up box which will appear on hover of the <p> tag containing cart details in it.
HTML
<div id="product_list">
<p class="poverlay" style="display:none;">Cart Details</p>
test elements test elements test elements test elements test elements
</div>
CSS
<style>
#product_list{
display:block;
z-index:1;
width:100px;
height:100px;
border:1px solid #ccc;
padding:10px;
}
#product_list:hover > p.poverlay {
display:block !important;
top:0px;
left:0px;
position:absolute;
padding:10px;
color:#f00;
font-weight:bold;
z-index:2;
}
</style>
You can use something like this in your CSS:
Your pop up starts hidden:
#popupId{
display: none;
}
You make it appear when you hover your p element
p:hover #popupId{
display: block;
}
And you fill it with the information you want to show.
yeh I did it !!!! in the way I want.
Thanks all Josh ,Ankit, thriqon , punitha

How can I make more than one square using html and css?

I currently have a <div> square but don't know how to make another square with a different style. When ever I use <div> to make another square in css, the style would be the same as the first square.
CSS:
div{
height:100px;
width:95px;
background-color:#B80000;
border-radius:4px;
text-align:center;
margin-left:132px;
}
html:
<div>
<span>M</span>i'm lovin' it<l>™</l></div>
try like this
CSS:
#squareA{
height:100px;
width:95px;
background-color:#B80000;
border-radius:4px;
text-align:center;
margin-left:132px;
}
#squareB{
height:100px;
width:95px;
background-color:#B8FFFF;
border-radius:4px;
text-align:center;
margin-left:132px;
}
html:
<div id="squareA">
<span>M</span>i'm lovin' it<l>™</l></div>
<div id="squareB">
<span>M</span>i'm lovin' it<l>™</l></div>
Explanation:
you were styling all the divs in your css. the same style will apply to all the divs that you have in your markup. if you need to apply separate styles to separate elements, for e.g. two divs, one way is to give them both different ids and apply styles to particular ids.
P.S : there are a loads of other ways too. try to read more on CSS styling.
Use a different id for each one.
Then for your css
Div#first {
}
div#second {
}
Use classes instead of ids or the literal div selector in your CSS. Create a class that represents your square and two classes that represent your colors.
HTML:
<div class="square a">
<a href="http://www.mcdonalds.com/us/en/home.html">
<span>M</span>i'm lovin' it<l>™</l>
</a>
</div>
<div class="square b">
<a href="#">
<span>B</span>bee<l>™</l>
</a>
</div>
CSS:
.square {
border-radius:4px;
height:100px;
width:95px;
border-radius:4px;
text-align:center;
margin-left:132px;
}
.a {
background-color:#B80000;
}
.b {
background-color:#00ff00;
}
http://jsfiddle.net/mSA6E/
You can use the html "id" attribute. See this jsfiddle:
div {
width: 100px;
height: 100px;
}
#red {
background-color: red;
}
#green {
background-color: green;
}
<div id="red"></div>
<div id="green"></div>
You can put as much css classes as you like to an html tag. For example:
.square { display:block; width:100px; height:100px; }
.red { background:#f00; }
.green { background:#0f0; }
.blue { background:#00f; }
Then
<div class="square red">Red square</div>
<div class="square green">Green square</div>
<div class="square blue">Blue square</div>
This approach is also more verbose than having multiple repetitions of the same instructions.

How to properly add divs inside another div so the original design doesn't break?

I have a this basic HTML:
<div id="views-container">
<div id="html-container"></div>
<div id="original-page-container"></div>
<div id="result-page-container"></div>
</div>
This is the css for those divs:
#views-container > div{
height:90%;
width:33%;
display:inline-block;
}
#html-container{
background-color: pink;
}
#original-page-container{
background-color: yellow;
}
#result-page-container{
background-color: gray;
}
This is the final output:
But then I want to add two divs inside
html-container
, like this:
<div id="views-container">
<div id="html-container">
<div id="html-content-area">
<textarea id="OutputHTML"></textarea>
</div>
<div id="html-info-area">
bla bla bla
</div>
</div>
</div>
when I do this, this is how the page is rendered.
I want the textarea inside html-content-area
to use all width and heigth from html-container.
Later, i will need to hide html-content-area in order
to show something in html-info-area.
How could I add those divs inside html-content-area
so they don't break the original design?
I pasted the code in fiddle:
http://fiddle.jshell.net/eyCbB/
Thanks in advance!
Hope you were looking for this
I have made some changes in your fiddle.
#views-container > div{
height:90%;
width:32%;
display:inline-block;
vertical-align:top;
}
html,body {
width:100%;
height:100%;
}
#views-container {
height:100%;
}
Have a look http://fiddle.jshell.net/eyCbB/1/