I set a background color for a div, but for some reason the color box is not underneath the text, but like 100px toward the top. This is my HTML code;
<div class="youtube_gallery_caption">The Fast Show - Unlucky Alf</div>
and This is CSS
.youtube_gallery_caption {
background:red;
color:black !important;
width:140px;
height:10px;
padding:10px;
position:relative;
z-index:99;
display:none;
}
Thank you.
Remove height and add line-height:30px...change the value to whatever height you want. Also, add overflow: hidden;. You might also want to add padding to just the left and right, versus all around, so you're not adding to the line-height. Instead this will remove any padding on the top and bottom padding:0 10px;
Try replacing your CSS with this:
.youtube_gallery_caption {
background:red;
color:black !important;
width:140px;
line-height:30px;
padding:0 10px;
position:relative;
z-index:99;
display:none;
overflow:hidden;
}
Your box has a height of 10px, and you font-size isn't specified so is likely larger.
Match up the font-size and line-height with the actual height of the div.
Eg: (using the 10px value):
http://jsfiddle.net/D2g9H/
.youtube_gallery_caption {
background:red;
color: black !important;
width:140px;
height: 10px;
font-size: 10px;
line-height: 10px;
padding:10px;
position:relative;
z-index:99;
}
Related
For example if I have a h1 element with the value "hello" with background-color: pink;, if the width of this element is shrunk to 10px it looks like this by default: https://i.stack.imgur.com/r4s4S.png .
I am wanting the pink part of the element (the background color) to be on the other side of the h1 element (closer to where "o" is, not "H").
What I have tried:
I have tried float: right on the h1 element but that moves the element off screen when the width is changed
and
I have tried giving h1's width a negative value (unsure why this didn't work).
My current HTML code:
<h1 style="background-color: pink; width: 10px;">hello</h1>
You can use ::after selector of h1 element for this.
h1{
display:inline-block;
position:relative;
}
h1::after{
position:absolute;
content:"";
top:0;
right:0;
width:10px;
height:100%;
background:pink;
z-index:-1;
}
<h1>hello</h1>
You can use a box-shadow for this:
h1 {
display: inline-block;
box-shadow: -10px 0 inset pink;
}
<h1>hello</h1>
Is there any way to define the height of a div without it being fixed to that defined size? For example setting a div to 200px it will be locked to that, and so when it scales it wont change.
I know that you can use % but when I try to do this my image disappears so im forced to using units like px or vw, and im not sure how to create a fluid design without being locked into a specific set size.
Here is my code. I'm wanting .face background image to appear but by using a % it no matter how big I make it, it wont show the image. If I add a px or vw number to its container .topwrap it then creates a large gap to the div container .lowerwrap. So basically I'm trying to find a way to get the image to display itself in a correctly sized div that is able to scale to browser size.
/*----------------------------Top ----------------------------*/
.topwrap{
position:relative;
top:50px;
height:auto;
}
.face{
position:relative;
float:right;
background:url(../images/face2.png) no-repeat;
background-size:100%;
top:10%;
width:50%;
height:200%;
display: flex;
right:16%;
display: block;
min-width:160px;
}
.txtwrap{
position:absolute;
margin-top:1.3%;
font-size:1.3vw;
float:right;
right:39%;
text-align: center;
}
.sptxt{
color:#171717;
font-weight:bold;
}
.sptxt2{
color:#171717;
font-weight:400;
}
/*----------------------------Lower----------------------------*/
.lowerwrap{
position:relative;
float:left;
width:100%;
height:300px;
top:50px;
padding-top:10px;
display: flex;
box-shadow: 0px 10px #333 inset;
}
.contentwrap{
position:relative;
width:100%;
background:url(../images/content.png) no-repeat;
background-size:100%;
display: block;
z-index:1;
}
/*----------------------------Footer----------------------------*/
.footerwrap{
position:relative;
float:left;
width:100%;
background:#09C;
top:50px;
text-align:center;
}
.foottxt{
text-align: center;
font-family: 'Roboto', sans-serif;
font-size:0.5vw;
color:#FFFFFF;
font-weight:200;
z-index:300;
}
Since we cannot see the HTML that you are applying your styles to, it is difficult to provide a solution. Have you tried using the following on the .face class?
min-height: 200px;
If you want to allow a <div class="face"> element to resize fluidly based on that percentage width, but maintain a given aspect ratio, try replacing your .face rule set with this:
.face {
position:relative;
float:right;
background:url(../images/face2.png) no-repeat;
background-size:100%;
top:10%;
width:50%;
right:16%;
display: block;
min-width:160px;
}
.face:before {
display: block;
content: '';
padding-top: 100%;
}
The above code assumes face2.png's height and width are the same (a square image, or 1:1 aspect ratio). To make div.face fit your actual image, divide your image's height by its width in pixels, multiply that by 100%, and use that for .face:before's padding-top value. For example, if face2.png is 180px tall by 120px wide, use padding-top: 150%;.
See: http://www.goldenapplewebdesign.com/responsive-aspect-ratios-with-pure-css/
I have a div containing only an image with a height of 400px. The div has no padding but it's height is 406px causing an ugly grey 6px horizontal stripe below its img.
The reason for the grey background is that comparable divs may contain a caption beneath their img.
What causes the extra 6px and how can I get rid of it?
P.s. I'm aware the HTML markup is not semantic/HTML5 but I'd rather not change it.
The basic markup is
<body>
<div>
<div class='img w960'>
<img src='timg-960-480.png' alt=''>
</div>
</div>
</body>
The CSS for this example is
body>div{
font-size:20px;
width:26em;
margin:5em auto;
text-align:justify;
}
div.img{
border:0px solid #fff;
border-radius:.5em;
background:#ddd;
margin:1em 0;
width:1px;
overflow:hidden;
display:table;
}
div.w960{
position:relative;
left:-7em;
}
div.w960 img{
width:40em;
}
div.img h3{
margin:0;
padding:1em;
font-size:20px;
font-style:italic;
}
Set line-height: 0 on your div.img. This will affect the image caption h3 but you can correct that with an extra CSS line. The image is set inline and sits on the text baseline.
body>div{
font-size:20px;
width:26em;
margin:5em auto;
text-align:justify;
}
div.img{
border:0px solid #fff;
border-radius:.5em;
background:#ddd;
margin:1em 0;
width:1px;
overflow:hidden;
display:table;
line-height: 0;
}
div.w960{
position:relative;
left:-7em;
}
div.w960 img{
width:40em;
}
div.img h3{
margin:0;
padding:1em;
font-size:20px;
font-style:italic;
}
I always solve this problem by setting the image's display property to block (display: block;).
I have a p element with a defined Background Color that should adapt it's size automatically to the text inside it. I tried Setting the width to Auto, but the p element still spans 100% as visible by the defined Background Color.
how do I get the width to adjust accordingly without Setting the width in % or px everytime?
Code:
#job-xyz.titel {
background-color: #dceff5;
font-size:17px;
color:black;
text-align: left;
margin-top:0px;
padding-left:10px;
margin-left:45px;
width:auto;
}
<div class="header2">
<p class="titel"><strong>Linux Softwaree</strong></p>
</div>
To set width equal to content, use display:inline-block
#job-xyz.titel {
background-color: #dceff5;
font-size:17px;
color:black;
text-align: left;
margin-top:0px;
padding-left:10px;
margin-left:45px;
width:auto;
display:inline-block /*added*/
}
Also, you have assigned the css wrong way around
#job-xyz.titel should be .header2 > p.titel
Your code demo and working demo
final working demo with proper css
<p> is a block-element and has to be set to inline via the "Display" property in CSS.
corrected code by "NoobEditor"
#job-xyz.titel {
background-color: #dceff5;
font-size:17px;
color:black;
text-align: left;
margin-top:0px;
padding-left:10px;
margin-left:45px;
width:auto;
display:inline-block /*added*/
}
I have this code.
Wish div align blue with red div without affecting the rest of the page.
Notice that the div#leftcontent not go to the bottom of the page and I'm not understanding why.
Maybe the solution is to put the div#leftcontent aligned to the bottom of the page. But how?
Thanks.
you have to change the red div:
bottom: 0px; /*delete*/
position:relative;
maring-top: 86px; /*to align to the blue*/
check this out: http://jsfiddle.net/85unG/48/
and for the height, the reason the its not touching the bottom is:
div#wrap {
height: 768px; /should be 739 px;/
}
CSS absolute positioning to the rescue!
First ditch the fixed height and the overflow:auto for div#wrap. Replace overflow:auto with overflow:hidden:
div#wrap
{
width:1024px;
/*height:768px; /* Forget about it! */
margin:5px auto;
border:2px solid #ccc;
/*overflow:auto;/* Forget about it! */ overflow:hidden;
position:relative;
}
... now note that div#wrap has a relative position:
div#wrap
{
width:1024px;
margin:5px auto;
border:2px solid #ccc;
overflow:hidden;
position:relative; /* AWESOME */
}
This means div#wrap is non-statically positioned, so we can position things absolutely within it... like div#footer and div#social-networks:
div#wrap div#leftcontent div#footer {
position:absolute;
bottom:0px;
}
div#wrap div#nav div#social-networks {
position:absolute;
bottom:0px;
}
This will position the bottom edge of div#footer and div#social-networks 0px away from the bottom edge of its non-statically positioned ancestor - namely, div#wrap.
Bad news though: positioning things absolutely can screw up the natural flow of things, so you have to manually reserve some space for div#footer and div#social-networks. Do this with padding:
div#wrap
{
width:1024px;
margin:5px auto;
border:2px solid #ccc;
overflow:hidden;
position:relative;
padding-bottom:50px; /* this is new... you can choose a better number than 50px */
}