In the example below I'm trying to add some inline input range elements that align with the line but also align with the right edge of the outer div. In the actual site the html is code that's been run through prettify so it's nothing but <span> elements inside a <pre> element so whitespace and in particular, linefeeds, is relevant.
In any case I insert these <input type="range"> elements wrapped by 2 divs and I'm trying to get them to align to the content in the same line but on the right of the outer container.
I haven't actually found a solution. Originally I had a normal font size and if I added some extranous content inside the .holders things would appear to align but in reality once I increased the font size it showed they were not actually aligning.
Is there some way to fix this?
I get that normally you might do something like this with table (separate column for the sliders) or a flexbox but that would require parsing all the code to find the linebreaks and then generating a bunch of new html.
.outer {
position: relative;
background: pink;
padding: 1em;
width: 200px;
}
.holder {
display: inline-block;
}
.holder>div {
position: absolute;
display: inline-block;
right: 0;
}
.holder input {
position: absolute;
display: inline-block;
right: 1em;
}
<h1>test</h1>
<div class="outer">
<div>
<span>foo: </span>
<div class="holder">
<div>
<input type="range">
</div>
</div>
</div>
<div>
<span>bar: </span>
<div class="holder">
<div>
<input type="range">
</div>
</div>
</div>
</div>
Note: the code above shows the issue but the real code is whitespace sensative pre and spans which are hard to read. Here it is.
body {
font-size: 35pt;
}
.outer {
position: relative;
background: pink;
padding: 0.2em;
width: 300px;
}
.holder {
display: inline-block;
}
.holder>div {
position: absolute;
display: inline-block;
right: 0;
}
.holder input {
position: absolute;
display: inline-block;
right: 1em;
}
<pre class="outer"><span>foo: </span><div class="holder"> <div><input type="range"></div></div></div>
<span>bar: </span><div class="holder"><div><input type="range"></div></div>
</pre>
As I mentioned above the <pre> and <span> are generated, after which I insert the range inputs.
Yet another wrikle is that on the actual site the <pre> section gets scrollbars if a line is too long (just like S.O. code area) but in that case I still want the input areas at the right of the visible area (if you haven't scrolled).
body {
font-size: 35pt;
}
.outer {
position: relative;
background: pink;
padding: 0.2em;
width: 300px;
overflow: auto;
}
.holder {
display: inline-block;
}
.holder>div {
position: absolute;
display: inline-block;
right: 0;
}
.holder input {
position: absolute;
display: inline-block;
right: 1em;
}
<pre class="outer"><span>longlongline: </span><div class="holder"> <div><input type="range"></div></div></div>
<span>bar: </span><div class="holder"><div><input type="range"></div></div>
</pre>
A screenshot of the final result will probably help.
Normally the sliders are inline (no CSS apart from display: inline-block) but if the window is not wide enough the sliders would get pushed off. So, instead I make them transparent and set them to use absolute positioning so they don't get pushed off. This way you can still both read the code and interact with the sliders.
They appear to align ATM but if you go to the page and make sure your window is less than 450px wide and set the prettyprint.pre font size to say 15pt then you'll see the alignment is just luck and that they aren't actully being "aligned" it's just certain sizes of various things happen to make them appear aligned.
I fully understand they might not actually be a solution except to parse the code and separate each line into its own container but if possible I'd like to use the correct CSS (assuming it exists) to make them align.
.outer {
background: pink;
padding: 1em;
width: 200px;
}
.holder {
display: inline-block;
vertical-align: middle;
}
.holder>div {
display: inline-block;
}
.holder input {
display: inline-block;
}
you can try to add "vertical-align:middle"
.outer {
position: relative;
background: pink;
padding: 1em;
width: 200px;
}
.outer>div{
position: relative;
display: inline-block;
}
span{
vertical-align:middle;
}
.holder {
vertical-align:middle;
display: inline-block;
}
.holder>div {
position: relative;
display: inline-block;
right: 0;
}
.holder input {
position: relative;
display: inline-block;
}
<h1>test</h1>
<div class="outer">
<div>
<span>
foo:
</span>
<div class="holder">
<div>
<input type="range" />
</div>
</div>
</div>
<div>
<span>
bar:
</span>
<div class="holder">
<div>
<input type="range" />
</div>
</div>
</div>
</div>
Related
I am trying to append a tooltip to a list/div block of elements - but these elements are set at 100% width. I'd like to have it so the tool tip is only appearing a few px away from the last word in the list/sentence.
Doing it html + css only, you need to wrap each line in a div, like
.gray-container {
display: flex;
flex-direction: column;
width: 100%;
background: red;
}
.line {
display: inline-block;
position: relative;
}
.tooltip {
display: inline-block;
position: absolute;
background: black;
width: 30px;
height: 20px;
margin-left: 10px;
}
<div class="gray-container">
<div class="line"><input type="radio">None<div class="tooltip"></div></div>
<div class="line"><input type="radio">Two</div>
<div class="line"><input type="radio">Three</div>
<div class="line"><input type="radio">Four</div>
</div>
Now for demo purposes and because i don't have any idea of the methods you're using to create the tooltips since you didn't post your code i'm using a div with class="tooltip", but this solves your question of the div being 100%.
I am attempting to tile a webpage with div elements of various sizes. However, I am running into an issue with once x number of div elements have filled the width of the screen the following div is placed below the previous 'row', rather than being floated to fit into space between elements in the previous 'row'. The code below better demonstrates what I mean; I'd like the 'game' div to be floated to fit into the space above where it is currently positioned.
h1 {
color: white;
}
.center {
display: flex;
justify-content: center;
align-items: center;
text-align: center;
}
.container {
display: inline-block;
}
.default {
margin: 1em;
float: left;
}
/* For hover text */
.hover_img {
width: 100%;
height: 100%;
position: relative;
float: left;
}
.hover_img h4 {
color: white;
}
.hover_img:hover img {
opacity: .2;
}
.hover_img:hover .center_text {
display: block;
}
.center_text {
position: absolute;
top: 50%;
left: 0;
display: none;
font-weight: bold;
text-align: center;
}
img {
margin: 0;
}
.rectangle-tile-horizontal {
height: 15em;
width: 35em;
}
.red {
background-color: rgba(255, 63, 63, 0.8);
}
#game, #game img {
width: 30em;
height: 30em;
}
#app, #app img {
width: 40em;
height: 35em;
}
<div class="container">
<div class="rectangle-tile-horizontal red center default">
<h1><b>Projects</b></h1>
</div>
<div class="rectangle-tile-horizontal hover_img default" id="app">
<img src="http://cohenwoodworking.com/wp-content/uploads/2016/09/image-placeholder-500x500.jpg">
<div class="center_text"><h4>Web App</h4></div>
</div>
<div class="hover_img default" id="game">
<img src="http://cohenwoodworking.com/wp-content/uploads/2016/09/image-placeholder-500x500.jpg">
<div class="center_text"><h4>Breakout</h4> </div>
</div>
I'm afraid what you want to do is actually re-order your divs to create a space-filling layout. To the best of my knowledge, using only CSS for this is difficult, if not outright impossible.
I suggest you take a look at this SO post, or perhaps even the Bulma framework is what you want.
If, however, you move away from re-ordering the containers automagically and instead look towards a solution that elastically adapts the width of each container to fill the available space while maintaining its "order" (first, second, third), I am sure CSS will be a viable solution. If you require assistance, please use the search or ask anew.
Create a style for your div class or id like
.className
{display:inline;}
and use it in your each div
Hope this will help you
An example of this
http://jsfiddle.net/JDERf/
EDIT: The problem is solved, so thanks to everyone who helped!
Original post:
So I am trying to put three divs next to each other (until thus far this part has been successful) with the third and last div to like go to attach to the bottom of the divs, which I have no clue how to do this.
How can I put the third div to attach to the bottom of the middle div and stay within the container?
To show you, I made a quick example. Something like this:
The black colour in the image is the 'body'.
The grey is a container div I put the three other divs in.
Each other box represents a div with what I want them to do and how approx. I want them to be positioned of one another.
I hope this can be done only using html and css. I would appreciate any help.
So far I have this as html for the divs:
#nav,
#textarea,
#contactallpages {
vertical-align: top;
display: inline-block;
*display: inline;
}
#containerpage {
position: relative;
margin: auto;
padding-top: 5%;
padding-bottom: 5%;
background-color: black;
height: 100%;
width: 70%;
}
#centercontainer {
background-color: lightblue;
width: 75%;
margin: 0 auto;
padding: 2%;
}
#nav {
float: left;
background: #aaaaaa;
height: 50%;
width: 15%;
padding: 1%;
}
#textarea {
display: inline-block;
background: #cccccc;
height: 70%;
width: 64%;
padding: 1%;
}
#contactallpages {
background: #bbbbbb;
position: absolute;
width: 15%;
padding: 1%;
bottom: 0;
}
<div id="containerpage">
<div id="centercontainer">
<div id="nav">
<ul>1
</ul>
<ul>2
</ul>
<ul>3
</ul>
</div>
<div id="textarea">
<header>
<h1>Welcome</h1>
</header>
<p>
Text text more text.
</p>
<p>
And more text.
</p>
</div>
<div id="contactallpages">
Random small textbox
<br>More small text.
</div>
</div>
</div>
The way you should lay this out is one container div and 3 children div's set to display: inline-block;
Using display: inline-block; will position all the div's next to each other and allows you to use the vertical-align property.
Now all you would need to do is set the proper vertical-alignment for each of the child div's. You can also set the height to the container div (#myPage) and that is the height that vertical-align will use to determine the positioning.
https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align
#myPage div {
display: inline-block;
width: 100px;
}
#centerFold {
height: 200px;
vertical-align: middle;
background-color: yellow;
}
#navBar, #contact{
height: 100px;
}
#navBar {
vertical-align: top;
background-color: red;
}
#contact {
vertical-align: bottom;
background-color: blue;
}
<div id="myPage">
<div id="navBar">
</div>
<div id="centerFold">
</div>
<div id="contact">
</div>
</div>
Try out flexbox if you do not have too much to worry about backward compatibility. My time at the moment doesn't allow to elaborate, but the essential part would be
#centercontainer {display: flex}
#contactallpages {align-self: flex-end}
Be aware though that some prefixing will be necessary for older browsers and this is only the standards-compliant solution. It does everything you want and you can forget about floating. Adding a
#textarea {flex-grow: 1}
would even allow the center to grow not only in height but in width also.
This has always driven me crazy and never found the right answer.
I want to achieve the following:
http://juicybyte.com/stack-overflow.jpg
Meaning, I want to have an image on a div on the left, and text that nicely vertical-aligns itself depending on how much content there is. Height of the text div can be fixed.
However, everything is no go.
<div id="widgetWhite">
<div id="widgetWhiteIcon">
<img src="/images/iconWhiteIconTn.png" alt="White Icon" />
</div>
<div id="widgetWhiteContent">
<p>I would love it if this worked.</p>
Download PDF
</div>
</div>
The CSS:
#widgetWhiteIcon {
width: 82px;
margin: 0 10px 0 20px;
display: inline-block;
vertical-align: middle;
}
#widgetWhiteContent {
width: 108px;
font: normal normal 11px/14px Arial, sans-serif;
height: 110px;
display: inline-block;
vertical-align: middle;
}
#widgetWhiteContent a {
color: #f37032;
}
Don't really care about IE6.0, but IE7.0 is required unfortunately.
Thanks for any help!
Here, I put together a solution for you based on the site I linked. I didn't bother mapping your existing css into it, but I think you will get the idea.
http://jsfiddle.net/M3h6v/5/
<div class="ie7vert1">
<img src="http://placehold.it/120x150" alt="White Icon" />
<div class="ie7vert2">
<div class="ie7vert3">
<p>I would love it if this worked.</p>
Download PDF
<br style="clear: both;" />
</div>
</div>
</div>
.ie7vert1 {
display: table;
#position: relative;
overflow: hidden;
border: 1px dashed gray;
float: left;
width: 100%;
}
.ie7vert2 {
#position: absolute;
#top: 50%;
display: table-cell;
vertical-align: middle;
}
.ie7vert3 {
#position: relative;
#top: -50%;
border: 1px dashed red;
}
The vertical-align property has two prerequisites for use:
The elements you are trying to vertically-align must be siblings.
The elements you are trying to vertically-align must not be block-level elements.
That being said, this is actually quite easy to solve:
<div id="widgetWhite">
<div id="widgetWhiteIcon">
<img src="http://placehold.it/100x100" alt="White Icon" />
</div><div id="widgetWhiteContent">
<p>I would love it if this worked.</p>
Download PDF
</div>
</div>
Note that the closing div for #widgetWhiteIcon and the opening div for #widgetWhiteContent are touching: </div><div id="widgetWhiteContent">. This allows for you to control the spacing between these two elements, since normally any space between inline elements in your markup is shown in the presentation.
Edit: You could equivalently set font-size: 0 on #widgetWhite without worrying about whitespace. font-size is inherited in the children elements, so you would need to explicitly set that after, like so: #widgetWhite { font-size: 0; } #widgetWhite * { font-size: 12px; }
CSS:
p { margin: 0; }
#widgetWhite > div {
vertical-align: middle;
display: inline-block; }
#widgetWhiteContent { margin: 0 0 0 4px; }
#widgetWhiteContent a {
margin: 1em 0 0;
display: block; }
Preview: http://jsfiddle.net/Wexcode/DcWB8/
You have to set a fixed height for the wrapper div (div#widgetWhiteContent) first in order for vertical-align to work. To keep everything in div#widgetWhiteContent vertically aligned with div#widgetWhiteIcon, both div's should be at the same height.
So a good solution would be to set a height for the outer div, and then set the height of both child div's to 100%.
Your CSS goes like this
<style>
#widgetWhite {
height: 110px;
}
#widgetWhiteIcon {
width: 82px;
margin: 0 10px 0 20px;
display: inline-block;
height: 100%;
}
#widgetWhiteContent {
clear: left;
width: 108px;
font: normal normal 11px/14px Arial, sans-serif;
height: 100%;
display: inline-block;
vertical-align: middle;
}
#widgetWhiteContent a {
color: #f37032;
}
</style>
I have fixed height divs that contain text in them. I would like the text to be vertically aligned in the middle of the div, but the problem lies in the fact that some of the text is single-line, and some splits itself over onto two lines. For IE8, Chrome and Firefox, using display: table-cell and vertical-align: middle provides the solution I need:
JS Fiddle is here. Take the asterisk off the width: 300px to see the formatting when the text is on one line.
However, IE7 does not support the display: table-cell property. The only solutions I have found to this apply only to single lines, and not to text that may be 1 or 2 lines. How can I have it display in IE7 as it does in more modern browsers, without the use of any scripts?
How about an IE7 CSS call putting position:relative on the div, and absolute on the h6, and keep the code for vertical-align for modern browsers.
http://jsfiddle.net/yap59cn3/
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="ie7.css">
<![endif]-->
ie7.css
div
{
/* Use inheritance, and override only the declarations needed. */
position:relative;
}
h6
{
height:auto; /* override inherited css */
position:absolute;
top:45%;
}
The goal is to make IE7 "presentable" -- no matter what you do, it will never look as pretty as a modern browser. To me, it's not worth the headache (not even a little).
Personally I've started to (ab)use padding to get vertical aligns. It's especially handy if you use fixed height, since you can offset the height with the value of the padding to get a perfect full-height element.
Note: This solution only works if you know what text will come in the <h6> in advance. If you dynamically add it, I'd suggest wordcounting to try to figure out if it's gonna wrap or not.
Solution:
HTML
<div>
<h6 class="OneLineVertCentered">Here is some text. Look at this lovely text. Isn't it nice?</h6>
</div>
<div style="margin-top: 1em;"> <!-- Margin only for displaying the boxes properly -->
<h6 class="TwoLineVertCentered">Here is some text. Look at this <br />
lovely two-line text. Isn't it nice?</h6>
</div>
CSS
div {
background-color: yellow;
height: 30px;
width: 200px;
width: 300px;
}
h6.OneLineVertCentered,
h6.TwoLineVertCentered {
font-size: 12px;
line-height: 1em;
}
h6.OneLineVertCentered {
padding-top: 10px;
}
h6.TwoLineVertCentered {
padding-top: 3px;
}
Fiddle:
http://jsfiddle.net/Snorbuckle/CnmKN/
Snippet (same as fiddle):
div {
background-color: yellow;
height: 30px;
width: 200px;
width: 300px;
}
h6.OneLineVertCentered,
h6.TwoLineVertCentered {
font-size: 12px;
line-height: 1em;
}
h6.OneLineVertCentered {
padding-top: 10px;
}
h6.TwoLineVertCentered {
padding-top: 3px;
}
<div>
<h6 class="OneLineVertCentered">Here is some text.
Look at this lovely text. Isn't it nice?</h6>
</div>
<div style="margin-top: 1em;">
<h6 class="TwoLineVertCentered">Here is some text. Look at this <br />
lovely two-line text. Isn't it nice?</h6>
</div>
You can use a helper span element to vertical align your text like the following example:
html
<div class="container">
<span class="aligner"></span>
<h3>Text to be aligned center in the beloved ie7</h3>
</div>
css
div.container {
background-color: #000;
color: #fff;
height: 300px;
width: 250px;
position:relative;
margin:12px auto;
text-align:center;
}
.aligner {
display: inline-block;
height: 100%;
content: ' ';
margin-right: -0.25em;
vertical-align: middle;
}
h3 {
display: inline-block;
vertical-align: middle;
}
Fiddle: http://jsfiddle.net/groumisg/dbx4rr0f/
Normally, we would use a pseudo element for this, but ie7 (what a surprise!) does not support :after, :before...etc. Also, note that ie7 does not support display: inline-block for elements that are not inline by default, like div. To use display: inline-block for a div you would have to use the following hack:
div {
display: inline-block;
*display: inline;
zoom: 1;
}
as suggested here Inline block doesn't work in internet explorer 7, 6
You should be able to accomplish this with line-height and vertical-align: middle;.
div {
background-color: yellow;
height: 30px;
line-height: 30px;
width: 200px;
*width: 300px;
}
h6 {
font-size: 12px;
line-height: 1em;
height: 30px;
vertical-align: middle;
}
check this out
http://jsfiddle.net/CnmKN/59/
CSS Code
div {
background-color: yellow;
height: 30px;
width: 200px;
*width: 300px;
display:table;
}
h6 {
font-size: 12px;
line-height: 1em;
display: table-cell;
vertical-align: middle;
height:90px;
}
I know two other methods to vertically center elements than with table-cell:
1) With line-height:
.element {
height: 60px;
line-height: 60px
}
This will only work if the text is in a single line.
2) position absolute/margin auto
.parentElement {
position: relative;
}
.element {
position: absolute;
top: 0;
bottom: 0;
margin: auto 0;
}
You maybe will have to use height (auto or a value) and display inline/inline-block. Just try.
Key point is not to use pixels for alignment, use only %-s.
Works even on IE5 :)
here is Demo
.wrapper{
position: relative;
width: 100%;
height: 200px; /* change this value to see alignment*/
background-color: red;
margin: 0 auto;
}
.cell{
position: absolute;
display:block;
background-color: blue;
left:50%;
top:50%; /*this puches element half down*/
margin-left:-100px; /* this is the half size of cell width:200px;*/
margin-top: -.5em; /*this is the half size of font size*/
width: 200px;
color: #fff;
text-align:center;
}
<div class='wrapper'>
<div class='cell'>vertically aligned text</div>
</div>
div {
background-color: yellow;
height: 400px;
width: 200px;
display: table-cell;
vertical-align: middle;
width: 300px;
}
h6 {
font-size: 12px;
line-height: 1em;
height: 30px;
}