I have a <ul> <li> and I require the use of display: inline-block;. This is required in order to "float" the li's whilst the last element is 100% wide of it's parent container and there could be any amount of li's (floating would mean the amount of li's is finite depending on the width of it's containing element). So the total width of the <ul> will be greater than the width of the viewport.
This is fine except I require the "floated" elements to multiline and I expect all elements which are not multi lined to be 100% height of the <ul>.
I can achieve what I want by setting the height of the <ul> in JS but this is something I really do not want to do.
Here is a JS fiddle.
http://jsfiddle.net/d5WBv/3/
Does anyone have a solution. I'm not sure if flexbox or display: table; can solve this but I cannot seem to get it to....
Thanks!
I guess, you mean that the li's all should have the same height?
If so, you could display them as table-cells:
ul {
display: table;
width: 100%;
}
li {
vertical-align: top;
display: table-cell;
padding: 10px;
margin: 0;
}
Also, check the updated fiddle.
I have a CSS solution for you, check out this Working Fiddle
DownSide: it requires to Double the ul li elements,
(one of them is for taking the real space in the document flow, (he don't render as we want but so will be hidden), and one of them is showed, on top of the other, with the display you want.
HTML:
<div class="Container">
<ul class="Hidden">
<li>This stays on one line</li>
<li>And this</li>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed auctor libero neque, nec tristique metus rutrum et. Integer semper libero quis magna placerat, a posuere sem congue.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed auctor libero neque, nec tristique metus rutrum et. Integer semper libero quis magna placerat, a posuere sem congue.</li>
</ul>
<ul class="Visible">
<li>This stays on one line</li>
<li>And this</li>
<li>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed auctor libero neque, nec tristique metus rutrum et. Integer semper libero quis magna placerat, a posuere sem congue.Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed auctor libero neque, nec tristique metus rutrum et. Integer semper libero quis magna placerat, a posuere sem congue.</li>
</ul>
</div>
CSS:
*
{
padding: 0;
margin: 0;
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.Container
{
position: relative;
}
.Hidden
{
visibility: hidden;
}
.Visible
{
position: absolute;
top: 0;
height: 100%;
}
ul
{
white-space: nowrap;
background-color: #cccccc;
font-size: 0;
}
li
{
display: inline-block;
padding: 10px;
border-right: 1px solid red;
background-color: #2c2c2c;
text-align: center;
color: #fefefe;
white-space: normal;
vertical-align: top;
font-size: 16px;
height: 100%;
}
li:last-child
{
width: 100%;
border-right: 1px solid #2b2b2b;
}
Related
What is the best or right way to vertically align small absolutely positioned ::before content so that its baseline lines up with the parent’s text?
In this snippet, I want the “XL” to have the same baseline as the “Lorem ipsum”. Just tweaking top: would be too fragile.
body {margin: 0 30px; position: relative}
p {background-color: lightblue}
p::before {content: "XL"; font-size: 75%; position: absolute; right: 100%; background-color: lightgray}
<body>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi in dictum purus. Etiam accumsan quam et turpis elementum, in tempor. </p>
</body>
Correct answer: use line-height and use em.
You've come across a classic example when you want to use the text baseline as transform-origin of your element. I recommend this reading to understand limitations.
Also, in your current solution you're relying on the width of the closest relatively positioned ancestor to move the :before. You don't really need that. You can safely leave it at it's current position (top left corner of parent) and simply move it 100% of its own width towards the left:
document.querySelector('input[type="range"]').addEventListener('input', function(){
document.body.style.fontSize = this.value + 'px'
})
body {
padding: 24px 0 0 1em;
font-size: 18px;
}
p {
margin-top: 0;
background-color: lightblue;
}
p::before {
content: "XL";
position: absolute;
transform: translateX(-100%);
background-color: lightgray;
font-size: 75%;
line-height: 1.75em;
}
input[type=range] {
width: 80vw;
left: 10vw;
top: 0;
position: absolute;
}
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi in dictum purus. Etiam accumsan quam et turpis elementum, in tempor. </p>
<input type="range" value="18" step=".01" min="10" max="84">
Initial answer (when I believed you simply want to align to bottom):
You need to give the parent position:relative; and child: bottom: 0:
body {padding: 0 30px; }
p {
background-color: lightblue;
position: relative
}
p::before {
content: "XL";
font-size: 75%;
position: absolute;
right: 100%;
background-color: lightgray;
bottom: 0;
}
<body>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi in dictum purus. Etiam accumsan quam et turpis elementum, in tempor. </p>
</body>
Out of scope: don't use margin on <body>. Use padding instead.
I would adjust the line-height
div {
margin: 0 30px;
position: relative
}
p {
background-color: lightblue
}
p::before {
content: "XL";
font-size: 75%;
position: absolute;
right: 100%;
background-color: lightgray;
line-height:1.75;
}
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi in dictum purus. Etiam accumsan quam et turpis elementum, in tempor. </p>
</div>
<div style="font-size:20px;">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi in dictum purus. Etiam accumsan quam et turpis elementum, in tempor. </p>
</div>
<div style="font-size:35px;">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi in dictum purus. Etiam accumsan quam et turpis elementum, in tempor. </p>
</div>
I want a CSS style for my html reports that should be nice to read: therefore body has a maximum width and is centered. I also want figures/img to be centered relatively to the page, when the image is wider than the container and when not it should also be centered.
I have tried some translateX stuff, and also tried the display: contents; option. I have failed using position: absolute;, because then the consecutive figures are vertically overlapping...
Example snippet below. How can I achieve the centering?
body {
background-color: #ebebeb;
max-width: 200px;
margin-left: auto;
margin-right: auto;
}
figure,
div.figure {
position: relative;
text-align: center;
border: 1px solid #44c;
padding: 2px;
left: 50%;
transform: translateX(-50%);
}
img {
background-color: #999;
padding: 2px;
}
<body>
<p>
Some text inside a paragraph that should not be too wide and centered in the page. bla bla bla blabla bla bla aaaaaaaaaaaaaaaaaa aaa aaaaa aaaaaaaaaaaaaaa aaa.
</p>
<figure>
<img width=500px src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e5/Seine_wide.jpg/640px-Seine_wide.jpg"></img>
</figure>
<p>
Some text below.
</p>
</body>
Like this?
note: you need to change max-width on .cont to make the center container grow bigger or smaller
body{
margin:0;
padding:0;
text-align:center;
vertical-align: top;
}
.cont{
text-align:center;
display: inline-block;
max-width:600px;
vertical-align: top;
}
figure{
display: inline-block;
margin:0;
vertical-align: top;
}
img{
max-width:100%;
vertical-align: top;
}
p{
box-sizing: border-box;
text-align:left;
padding:10px 5px;
vertical-align: top;
}
<div class="cont">
<figure>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e5/Seine_wide.jpg/640px-Seine_wide.jpg" alt="">
</figure>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin sed vehicula erat. Proin odio nisi, fermentum ut nisi eu, laoreet rutrum leo. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sit amet malesuada arcu. Cras ullamcorper enim justo, sed mattis velit ullamcorper a. Morbi ac egestas lectus. Ut sit amet maximus mauris, eget vehicula ligula. Phasellus sit amet odio gravida, tempus ipsum at, blandit odio. Morbi quis libero non metus ornare varius nec sed tellus. Aenean lacinia, dui vehicula ultrices scelerisque, ex odio viverra nunc, eget posuere tortor est faucibus dui. Aliquam vestibulum libero a auctor ultricies. Morbi sed eros odio. Phasellus molestie sit amet nisl at fermentum. Praesent est erat, tincidunt ut diam nec, molestie varius est. Duis purus libero, placerat sit amet nulla et, pharetra lobortis neque. Quisque quis risus ultricies, semper tellus sit amet, feugiat orci.
</p>
</div>
Alright, I found a concise CSS syntax for limiting the width of everything in <body> except <figure>, using the power of asterisks:
style all descendants of body but not body itself: body * {max-width: 200px;}
override max-width in figure only: figure {max-width: none !important;}
Make img inherit the max-width from the containing figure: img {max-width: inherit;}.
body {
background-color: #ebebeb;
}
body * {
max-width: 200px;
margin-left: auto;
margin-right: auto;
}
figure {
text-align: center;
border: 1px solid #44c;
padding: 2px;
max-width: none !important;
}
img {
background-color: #999;
padding: 2px;
max-width: inherit;
}
<body>
<p>
Some text inside a paragraph that should not be too wide and centered in the page. bla bla bla blabla bla bla aaaaaaaaaaaaaaaaaa aaa aaaaa aaaaaaaaaaaaaaa aaa.
</p>
<figure>
<img width=500px src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e5/Seine_wide.jpg/640px-Seine_wide.jpg"></img>
</figure>
<p>
Some text below.
</p>
</body>
I would like to change the position of the arrow that appears in details. I've tried float:left but if the line is too big, like the one in the example above, the arrow moves on the start of the line underneath when I resize the window. I would like it to stay on besides the very first letter on the first row.
How can I do that?
Example:
<details>
<summary>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut
gravida iaculis arcu, et hendrerit arcu. Morbi rhoncus ex quam, quis.
</summary>
Content goes here.
</details>
<style>
summary::-webkit-details-marker {
display: none
}
summary:after {
content: "+";
color: #fff;
float: left;
}
details[open] summary:after {
content: "-";
}
</style>
use :before instead of :after
summary::-webkit-details-marker {
display: none
}
summary:before {
content: "+";
color: red;
float: left;
margin-right: 10px;
}
details[open] summary:before {
content: "-";
}
<details>
<summary>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut
gravida iaculis arcu, et hendrerit arcu. Morbi rhoncus ex quam, quis.
</summary>
Content goes here.
</details>
You can position the marker using absolute positioning
details {
width: 20em;
margin: auto;
}
summary {
position: relative;
padding-left: 1em;
}
summary::-webkit-details-marker {
color: red;
position: absolute;
left: -.5em;
}
<details>
<summary>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut gravida iaculis arcu, et hendrerit arcu. Morbi rhoncus ex quam, quis.</summary>
Content goes here.
</details>
I want to be able to center most anything vertically on a screen:
For example
Forms
Buttons
Is this possible to do with some custom css class?
Right now I am using the code below. It works but is not very good as I choose at which percentage the content will start. So I need one of these for every page and if content becomes longer I have to modify the percentage.
.loginForm .scroll {
height: 100%;
}
.loginForm .loginForm-wrapper {
position: absolute;
top: 30%;
left: 0;
right: 0;
}
There are different ways to center content vertically. I'm often using the way to transform them. Let's say you got a container in which another element should be centered.
.container {
position: relative; /* required */
height: 150px;
width: 100px;
background: #efefef;
text-align: center;
border-radius: 4px;
}
.center-me {
display: block;
font-family: Consolas;
font-size: 12px;
padding: 5px 10px;
background: #000;
color: #fff;
border-radius: 4px;
position: absolute; /* required */
top: 50%; /* required */
left: 50%;
transform:
translateY(-50%) /* required */
translateX(-50%);
}
<div class="container">
<div class="center-me">
center
</div>
</div>
You could do something with a little bit of JS, though I think #Jan Unld's answer is sufficient.
calcMiddle = function () {
var middleObject = document.querySelector('#middle'),
middleHeight = middleObject.clientHeight / 2,
middleWindow = window.innerHeight / 2,
middleMargin = middleWindow - middleHeight;
middleObject.style.marginTop = middleMargin + "px";
};
calcMiddle();
body {
margin: 0;
}
#middle {
padding: 10px;
background: #abcabc;
}
<body onresize="calcMiddle();">
<div id="middle">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean a arcu sed neque volutpat lacinia mattis vitae nulla. Mauris in rutrum dui. Nullam nec dui rhoncus diam aliquam molestie. Nullam volutpat odio dignissim lorem ornare luctus. Aenean iaculis lectus venenatis aliquam porttitor. Etiam facilisis libero nec risus blandit aliquam. Donec sed ipsum nibh. Vestibulum sit amet accumsan ipsum. Maecenas viverra auctor orci, et scelerisque enim congue et.</p>
</div>
</body>
Example:
http://jsfiddle.net/link2twenty/y7h60kr7/
My problem is kind of similar to this, but I have img within lis of my unordered list.
Following is my markup. (think of it as UI for comments, as in Facebook or G+, where comment text and user's profile picture thumbnail is shown side-by-side)
<ul class="comments">
<li><img src="thumb.jpg" class="user-img" />Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
<li><img src="thumb.jpg" class="user-img" />Aliquam tincidunt mauris eu risus.</li>
<li><img src="thumb.jpg" class="user-img" />Morbi in sem quis dui placerat ornare. Pellentesque odio nisi, euismod in, pharetra a, ultricies in, diam. Sed arcu. Cras consequat. Morbi in sem quis dui placerat ornare. Pellentesque odio nisi, euismod in, pharetra a, ultricies in, diam. Sed arcu. Cras consequat.</li>
<li><img src="thumb.jpg" class="user-img" />Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
</ul>
And following is the CSS for the same.
.comments {
list-style-type: none;
clear: both;
}
.comments li {
padding: 3px;
font-size: 12px;
}
.comments li img {
margin-right: 10px;
}
And I'm getting result as following:
As it is clearly visible that when text doesn't fit the width, it goes below the image which I don't want. How can I keep it aligned instead of flowing below thumbnail image?
Note:
The images used for list items are not intended to be used as list-style-image, this is prototype design and images will be different for each li.
You can use:
.comments
{
list-style-image:url('thumb.jpg');
}
IF the thumbnail images are different for each li use this Demo
If the thumbnail images are same for all li. Do this -
.comments li {
background: url(thumb.jpg) no-repeat left top;
padding: 0 0 10px 50px;
font-size: 12px; min-height: 44px; /*thumb image height*/
}