CSS Absolute position on the right covers other elements - html

I have an div element that has position: absolute;. I want it to always be on the right edge and not cover other elements.
Here is my HTML:
.color2 {
background-color: #ff0078 !important;
color: white !important;
}
.colorW {
background-color: white;
color: black;
border: 1px #d4d4d4 dotted;
}
.condition-input {
display: inline-block;
padding: 3px 7px;
line-height: 1;
text-align: center;
white-space: nowrap;
vertical-align: middle;
border-radius: 10px;
font-size: 0.9em !important;
width: 180px;
height: 19px;
background-color: #fff200;
color: black;
}
.condition-button-group {
position: absolute;
right: 12px;
}
<div>
<span class="badge color2">Height</span>
<span class="badge colorW">==</span>
<input type="text" class="form-control condition-input" />
<div class="d-inline condition-button-group">Text</div>
</div>
But on the page I see the following. I don't want the "Text" to cover the left on the input, there should be an indentation. How to fix it?

If you use absolute, your parent div needs to be positioned relative so that the absolutely positioned element is absolute within the parent and not the DOM as a whole (or other ancestors set to relative).
<div class="wrapper">
<span class="badge color2">Height</span>
<span class="badge colorW">==</span>
<input type="text" class="form-control condition-input"/>
<div class="d-inline condition-button-group">Text</div>
</div>
Style:
.wrapper{
position:relative;
}
.condition-button-group {
right:0;
top:0;
}
Check out this link: Position absolute but relative to parent
You may need to play with the style a bit to position it exactly where you want, but this is the route to take to do what you are trying to do.

You can add padding-right: 70px; to the parent div which is equal to the .condition-button-group class.
Updated code should looks like as below.
<div class="parent">
<span class="badge color2">Height</span>
<span class="badge colorW">==</span>
<input type="text" class="form-control condition-input"/>
<div class="d-inline condition-button-group">Text</div>
</div>
.parent{
position:relative;
padding-right:70px;
}
Hope this helps.
if you create any plunker that should be great.

Related

How do you add margin to a button with a css background image?

I have a button with a search icon background image but I am not able to add a margin-bottom to align the text area with the button. Why does adding a margin-bottom to the button not do anything? Or what is the correct way to align the textarea with the button?
.input-area {
width: 100%;
height: 46px;
}
.parse-text-button {
background: url(https://cdn1.iconfinder.com/data/icons/hawcons/32/698627-icon-111-search-32.png) no-repeat;
width: 24px;
height: 24px;
border: none;
margin-bottom: 10px;
}
<div class="input-area">
<textarea class="input" placeholder="Enter text here"></textarea>
<button class="parse-text-button" type="submit"></button>
</div>
If you want to be able to manually position the second element, I would recommend using position: relative in conjunction with a negative top. This allows full control over exactly where the image sits:
.input-area {
width: 100%;
height: 46px;
}
.parse-text-button {
background: url(https://cdn1.iconfinder.com/data/icons/hawcons/32/698627-icon-111-search-32.png) no-repeat;
width: 24px;
height: 24px;
border: none;
position: relative;
top: -11px;
}
<div class="input-area">
<textarea class="input" placeholder="Enter text here"></textarea>
<button class="parse-text-button" type="submit"></button>
</div>
Alternatively, you can align the two elements at the top by giving the second element vertical-align: top. This aligns the top of the image to the top of the textarea, though can cause problems if the elements are of differing heights (as in your example).
However, considering your image is a little offset from its bounds, you may opt to use this approach to save one line of code:
.input-area {
width: 100%;
height: 46px;
}
.parse-text-button {
background: url(https://cdn1.iconfinder.com/data/icons/hawcons/32/698627-icon-111-search-32.png) no-repeat;
width: 24px;
height: 24px;
border: none;
vertical-align: top;
}
<div class="input-area">
<textarea class="input" placeholder="Enter text here"></textarea>
<button class="parse-text-button" type="submit"></button>
</div>
Hope this helps! :)
Use vertical align
.parse-text-button {
background:
url(https://cdn1.iconfinder.com/data/icons/hawcons/32/698627-icon-111-search-32.png) no-repeat;
width: 24px;
height: 24px;
border: none;
margin-bottom: 10px;
vertical-align:top;
}
You can just make both <textarea> and the <button> inline-block so you can use vertical-align: middle which will make both elements vertically centered;
textarea {
display: inline-block;
vertical-align: middle;
}
.parse-text-button {
display: inline-block;
verical-align: middle;
}
also make sure to remove all unecessary margin top or bottom in both elements.
hope that helps
There actually is a margin at the bottom, but its extending below the icon and textarea - you can see it if you check it in the element inspector.
I'm guessing that what you really want to know is how to stop the button from being aligned to the bottom.
You can simply use vertical-align to align it, e.g.
.input-area {
width: 100%;
height: 46px;
}
.parse-text-button {
background: url(https://cdn1.iconfinder.com/data/icons/hawcons/32/698627-icon-111-search-32.png) no-repeat;
width: 24px;
height: 24px;
border: none;
vertical-align: top; /* align to the top */
margin-top: 4px; /* add a little extra space to the top for 'padding' */
}
<div class="input-area">
<textarea class="input" placeholder="Enter text here"></textarea>
<button class="parse-text-button" type="submit"></button>
</div>
Here the solution
.stylish-input-group .input-group-addon{
background: white !important;
}
.stylish-input-group .form-control{
border-right:0;
box-shadow:0 0 0;
border-color:#ccc;
}
.stylish-input-group button{
border:0;
background:transparent;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div class="container">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<div id="imaginary_container">
<div class="input-group stylish-input-group">
<input type="text" class="form-control" placeholder="Search" >
<span class="input-group-addon">
<button type="submit">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
</div>
</div>
</div>
</div>

Having issues using span and colors in HTML, CSS

I am trying to create a green dot on a chat window, just like facebook does.
My section of the html that refers to my problem looks like this:
<div class="col-md-8">
<span class="green"> </span> Chat (32)
</div>
My CSS code looks like this:
.green {
color: green;
}
Every other element in my CSS works fine, so it's not a reference issue. Only this one. the green dot is not being generated.
Any ideas on what the issue might be?
Setting the color to green implies that there is some text/character in that span to be colored (unicode character • should do).
.green {
color: green;
}
<div class="col-md-8">
<span class="green">•</span> Chat (32)
</div>
Alternatively (if cannot change the html) you might want to use background-color and make the element round
.green {
display:inline-block;
width:0.5em;
height:0.5em;
border-radius:50%;
background-color: green;
}
<div class="col-md-8">
<span class="green"> </span> Chat (32)
</div>
This is what you're looking for:
.green {
display: inline-block;
border-radius: 50%;
background-color: green;
width: 10px;
height: 10px;
}
<div class="col-md-8">
<div class="green"></div> Chat (32)
</div>
Then you can change that green element to another color when Offline.
You were missing a couple of things like a width/height, background-color, and border-radius.
Also, consider naming your css classes by what they represent, not how they end up looking.
.status {
width: 16px;
height: 16px;
display: inline-block;
border-radius: 50%;
}
.online {
background-color: green;
}
<div class="col-md-8">
<span class="status online"> </span> Chat (32)
</div>
Since a span is an inline element, it's sized by its content.
Give the span display inline-block so it responds to having a size set, a padding to give it a size, a border-radius to make it round and use background, not color, to give it a color.
.green {
display: inline-block;
padding: 6px;
background: green;
border-radius: 6px
}
<div class="col-md-8">
<span class="green"> </span> Chat (32)
</div>
CSS:
.greenDot{
background: #42b72a;
border-radius: 69%;
height: 6px;
margin: 0 3px 1px 0;
vertical-align: middle;
display: inline-block;
width: 6px;
}
HTML
<span class="greenDot"></span>Chat
.greenDot{
background: #42b72a;
border-radius: 69%;
height: 6px;
margin: 0 3px 1px 0;
vertical-align: middle;
display: inline-block;
width: 6px;
}
<span class="greenDot"></span>Chat

Is there a CSS method to style only the first row of wrapping elements?

Say you have a responsive-width container full of inline-block elements. Some of these elements hit the edge of the container and drop to new lines. This is great! ...Unless for some reason you want to apply style rules to only the FIRST line of the elements.
http://jsfiddle.net/nshdnazw/1/
HTML
<div class="container">
<div class="thinger"></div>
<div class="thinger"></div>
<div class="thinger"></div>
<div class="thinger"></div>
<div class="thinger"></div>
<div class="thinger"></div>
<div class="thinger"></div>
<div class="thinger"></div>
<div class="thinger"></div>
<div class="thinger"></div>
<div class="thinger"></div>
<div class="thinger"></div>
<div class="thinger"></div>
<div class="thinger"></div>
<div class="thinger"></div>
</div>
CSS
.container {
width: 50%;
background-color: #ababab;
}
.thinger {
display: inline-block;
background-color: #666666;
width: 100px;
height: 100px;
margin-top: 10px;
margin-right: 10px;
}
In the fiddle above, say for instance I want the top line of items to be slightly darker, or I wanted to remove the top margin. I could apply a negative margin to the container, but then the container moves. It's a hack instead of a fix. I can't think of a CSS solution for changing the color, though.
Text has a solution, using the ::first-line pseudoselector. Is there a similar method for inline blocks?
Here's a solution using your existing HTML.
It moves the default background color to a ::before pseudo element, and the background for the first row to an ::after pseudo element.
See code comments for explanation.
.container {
width: 50%;
background-color: #ababab;
position: relative;
z-index: -2; /* behind pseudo elements */
}
.thinger {
width: 100px;
height: 100px;
margin-top: 10px;
margin-right: 10px;
display: inline-block;
vertical-align: top;
}
.thinger::before, .thinger::after {
content: '';
display: block;
position: absolute; /* positioned relative to .container */
z-index: -1; /* to prevent covering any text */
width: 100px; /* same width as .thinger */
height: 100px; /* same height as .thinger */
}
.thinger::before {
background: #666; /* default background */
}
.thinger::after {
top: 10px; /* top row only (.thinger's margin-top is 10px.) */
background: brown; /* background of top row only */
}
Fiddle
Even CSS has ::first-line pseudo-element you cannot style elements contained in first line. That's because contained elements are children of the container but not that first-line pseudo-element.
I don't think there isn't any way CSS could check for elements wich have a specific position, so my best CSS solution, that could fit for your problem, is to just change the background color in the section of the first line. For example take an empty div-element, scale it up so it covers the whole first row/line (width is responsive too of course) and fix its position above the first row.
Now you can simply work with the z-index and background color to make a fixed or absolute, colored underlayer for the first line.
Something like this (z-index sadly don't seem to work):
.container {
width: 50%;
background-color: #ababab;
}
.thinger {
background-color: #666666;
width: 100px;
height: 100px;
margin-top: 10px;
margin-right: 10px;
display: inline-block;
z-index: 2;
}
div.background {
position: absolute;
top: 15px;
left: 8px;
width: 48%;
height: 105px;
background-color: #98bf21;
z-index: 1;
}
<div class="background"></div>
<div class="container">
<span class="thinger">
</span>
<span class="thinger">
</span>
<span class="thinger">
</span>
<span class="thinger">
</span>
<span class="thinger">
</span>
<span class="thinger">
</span>
<span class="thinger">
</span>
<span class="thinger">
</span>
<span class="thinger">
</span>
<span class="thinger">
</span>
<span class="thinger">
</span>
<span class="thinger">
</span>
<span class="thinger">
</span>
<span class="thinger">
</span>
<span class="thinger">
</span>
</div>
PS:
You know that if you put text in the boxes they move around?

How can I center these varied number of elements within a div?

I have a <div> with a number of sub-elements (which happen to be custom-sized buttons). It can have between 1 and 3 buttons.
Example of HTML with 2 buttons:
<div id="head">
<div id="control-buttons-container">
<button class="control-button">some button text</button>
<button class="control-button">proceed with this button</button>
</div>
</div>
When there are 3 buttons, they fill the entire <div>, so it is not an issue. However, when there are 1 or 2 buttons, they need to be centered but I can't seem to accomplish this without introducing ridiculous conditional margins or something.
How can I modify this CSS so that <div> elements with 1 or 2 of these buttons show the buttons centered within the div?
Please refer to the Fiddle: https://jsfiddle.net/bf33wc6w/1/
Edit: With only 2 buttons, I don't want them to be spread out. I want them to be in the center with only ~2px between them similar to their spacing for when there are 3 buttons.
You could set inline block on the items, with container set to text align center.
.control-buttons-container {
text-align: center;
font-size: 0; /*fix inline block gap*/
}
.control-button {
display: inline-block;
vertical-align: top;
font-size: 12px; /*reset font size*/
}
JSFIDDLE DEMO
.control-buttons-container {
text-align: center;
font-size: 0; /*fix inline block gap*/
}
.control-button {
background-color: #0E80B4;
color: white;
outline: none;
height: 73px;
width: 128px;
margin: 3px 1.5px;
display: inline-block;
vertical-align: top;
font-size: 12px; /*reset font size*/
}
.control-button:hover {
background-color: #3FA9DB;
}
#head, #body, #foot {
border: 1px solid red;
position: absolute;
width: 396px;
height: 80px;
left: 0;
}
#head {
top: 0;
}
#body {
bottom: 50%;
-ms-transform: translateY(50%);
-webkit-transform: translateY(50%);
transform: translateY(50%);
}
#foot {
bottom: 0;
}
<div id="head">
<div class="control-buttons-container">
<button class="control-button">some button text</button>
<button class="control-button">proceed with this button</button>
<button class="control-button">Seth Rollins, WWE Champion</button>
</div>
</div>
<div id="body">
<div class="control-buttons-container">
<button class="control-button">proceed with this button</button>
<button class="control-button">Seth Rollins, WWE Champion</button>
</div>
</div>
<div id="foot">
<div class="control-buttons-container">
<button class="control-button">Seth Rollins, WWE Champion</button>
</div>
</div>
Updates:
Fixed same id being used multiple times on a single page, which is in valid HTML - changed it to class.
Improved the position of middle block, make it to always stay in the middle more accurately - by using CSS transform.
Merged some duplicated CSS rules.
Like this:https://jsfiddle.net/bf33wc6w/7/
All I did was change your float to none and the margin to auto for the left and right margin?
.control-button {
background-color: #0E80B4;
color: white;
outline: none;
border: none;
height: 73px;
width: 128px;
margin: 3px auto;
}
Add these style rules:
#head, #body, #foot { text-align: center; }
#control-buttons-container { display: inline-block; }
As an aside, you shouldn't use the same id (control-buttons-container) multiple times in one document. You should use a classname instead.
Updated fiddle: https://jsfiddle.net/mr8e7kyt/
Try something like this:
<div id="control-buttons-container">
<div class="col-1">
<button class="control-button">some button text</button>
</div>
<div class="col-2">
<button class="control-button">proceed with this button</button>
</div>
<div class="col-3">
<button class="control-button">Seth Rollins, WWE Champion</button>
</div>
</div>
<div id="control-buttons-container">
<div class="col-1">
</div>
<div class="col-2">
<button class="control-button">proceed with this button</button>
</div>
<div class="col-3">
</div>
</div>
.control-button {
background-color: #0E80B4;
color: white;
outline: none;
border: none;
float: left;
height: 73px;
width: 100%;
}
.control-button:hover {
background-color: #3FA9DB;
}
#control-buttons-container {
max-width: 400px;
padding: 1px;
border: 1px solid red;
}
.col-1, .col-2, .col-3 {
width: 32.6%;
display: inline-block;
margin: auto
}
Isn't flawless, but it was made in a couple of minutes and gets the job done: JSFiddle
For the containers without 3 items you should remove the float: left; for the buttons inside it. Leave it for the one with 3 items. Then you can just set text-align: center; on the container.
You can add a class like no-float on the containers you want to control whether its children should be floated or not.
https://jsfiddle.net/bf33wc6w/10/
This answer will probably help you out. Wrap your buttons in a container, give it a fixed width, and change margin to auto. Be sure to remove float: left.

How to align a span and a div on the same line

I have the following HTML:
<div class="mega_parent">
<div class="parent">
<div class="holder">
<span class="holder_under">Left heading</span>
<div class="holder_options">
<span class="holder_options_1">Option 1</span>
<span class="holder_options_2">Option 2</span>
<span class="holder_options_3">Option 3</span>
</div>
</div>
</div>
</div>
and the following CSS:
.holder {
background-color: blue;
padding: 10px;
}
.holder_under {
padding-left: 10px;
font-size: 16px;
color: #999;
}
.parent {
float: left;
margin-right: 20px;
width: 600px;
}
.mega_parent {
background-color: blue;
margin: 130px auto;
min-height: 320px;
height: 100% auto;
overflow: auto;
width: 940px;
padding: 0px 10px;
}
Question:
How do I make the div with the class holder_options align in the same line as the span with the class .holder_under?
Here's what it looks like currently in jsFiddle.
Div's are by default block level elements. Please read up more about block level elements here.
"Block level elements - Their most significant characteristic is that they typically are
formatted with a line break before and after the element (thereby
creating a stand-alone block of content)."
Set it to display:inline-block;
.holder_options {
display:inline-block;
}
Working jsFiddle here.
by default div's are display:block which is set to take 100% of the width. set it to display:inline or display:inline-block to take only what it needs and allow others to fit on the same line
u need inline-block
This is where the magic value inline-block for the display property comes into play. Basically, it’s a way to make elements inline, but preserving their block capabilities such as setting width and height, top and bottom margins and paddings etc
css
.holder {
background-color: blue;
padding: 10px;
}
.holder_under {
padding-left: 10px;
font-size: 16px;
color: #999;
}
.holder_options {
display:inline-block;
}
html
<div class="holder">
<span class="holder_under">Left heading</span>
<div class="holder_options">
<span class="holder_options_1">Option 1</span> </div>
.holder_options
{
float:right;
}
Here's the JS Bin: http://jsbin.com/idilim/1/
Yes the structure you have laid out is not done well however, just float .holder_options to the left:
.holder_options {
float: left;
}
As Morpheus stated in a comment, style="display: inline;" should do it.
<div class="holder">
<span class="holder_under">Left heading</span>
<div class="holder_options" style="display: inline;">
<span class="holder_options_1">Option 1</span>
</div>
</div>