How can I center-justify a list of links in CSS?
This is similar to this question: How to Center-Justify text in CSS?, except using links instead of text. The fiddle from that answer there (http://jsfiddle.net/L4pzm/) doesn't work when I use links instead of text.
This is how they did it in the above fiddle:
.center-justified {
margin: 0 auto;
text-align: justify;
width: 30em;
}
Here is the fiddle I created: http://jsfiddle.net/hsm4w0p5/
<div class="center-justified"><p>
First Second<br>
Third <a href="4">Fourth Fifth</p>
</div>
As you can see in the example above, the links aren't justified. I want to make it so that the word "Second" is aligned to the right to match with the word "Fifth".
I don't think this is possible using text-align: justify, but can use flexbox to do something similar:
Html:
<div class="center-justified">
<div class="row">
First Second
</div>
<div class="row">
Third <a href="4">Fourth Fifth
</div>
</div>
CSS:
.center-justified {
margin: 0 auto;
width: 30em;
}
.row {
display: flex;
justify-content: space-between;
}
a {
text-decoration: none;
}
http://jsfiddle.net/czcegf2d/1/
Be sure to check the browser compatibility of flexbox (which is quite good these days) and see that it fits your needs. http://caniuse.com/#feat=flexbox
If you don't need to be dynamic you could target each link with the :nth-child(n) CSS selector to float left and right inside the .center-justified container.
like:
.center-justified a:nth-child(2n) {float:right}
Related
I want to achieve this design:
Note that both text are perfectly top aligned but I'm almost sure this is impossible to achieve in CSS in a scalable way (I mean not hardcoding pixels with position relative/top for example).
Using flex looked like a good way to achieve this but since this is text, the top alignment is correct but based on the text 'bounding box' but the letters '77' don't take up 100% height of that box, causing it to not be perfectly aligned.
I understand why this happens since letter 'a' doesn't take up the same space as letter 'X' but I was just wondering if someone can find out a very nice tricky to achieve this design.
Here are my two attempts at this:
div#a {
background:#EEE;
line-height: 1;
}
span {
vertical-align: text-top;
}
#b {
display: flex;
align-items: flex-start;
}
<div id="a">
<span style=" font-size: 48px;">77</span>
<span style="font-size:14px;;">USD</span>
</div>
<div id="b">
<div style=" font-size: 48px; line-height: 48px;">77</div>
<div style=" font-size: 14px;">USD</div>
</div>
(please note there is a related issue but they didn't want 'perfect' alignment like this)
I think you can use this approach and tuning the line-height property.
div {
display: flex;
flex-flow: row nowrap;
align-items: flex-start;
margin-bottom: 10px;
}
div .container span {
line-height: 60%;
}
<div id="a">
<div class="container">
<span style=" font-size: 48px;">77</span>
</div>
<div class="container">
<span style="font-size:14px;">USD</span>
</div>
</div>
<div id="b">
<div class="container">
<span style=" font-size: 48px;">100</span>
</div>
<div class="container">
<span style="font-size:14px;">USD</span>
</div>
</div>
MDN documentation indicates that modifying the value of span { vertical align: text-top; } by replacing text-top with super (or *50% to shift the baseline) will help you accomplish your objective!
I learned this while preparing to suggest that you replace the aforementioned declaration with the HTML <sup> tag, which enables 'superscript' on specified inline text.
Check out documentation and decide what works best for your project~
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/sup
I have a p tag i would like vertically aligned next to an image. The problem I am having is that only the first line of text is vertically aligned. How do
i get the text in the p tag to wrap next to the image and maintain vertical alignment?
HTML
<body>
<div class="blog">
<div class="post">
<img src="https://dummyimage.com/600x400/a3b5c4/000000.jpg&text=example+of+donation+feed">
<p>“Once someone dreams a dream, it can't just drop out of existence. But if the dreamer can't remember it, what becomes of it? It lives on in Fantastica, deep under earth. There are forgotten dreams stored in many layers. The deeper one digs, the closer they are. All Fantastica rests on a foundation of forgotten dreams.”</p>
</div>
</div>
</body>
CSS
<style>
.blog img {
vertical-align: middle;
}
.blog p {
display: inline;
}
</style>
display: flex; on the parent and align-items: center to center the contents vertically.
.post {
display: flex;
align-items: center;
}
<div class="blog">
<div class="post">
<img src="https://dummyimage.com/600x400/a3b5c4/000000.jpg&text=example+of+donation+feed">
<p>“Once someone dreams a dream, it can't just drop out of existence. But if the dreamer can't remember it, what becomes of it? It lives on in Fantastica, deep under earth. There are forgotten dreams stored in many layers. The deeper one digs, the closer they are. All Fantastica rests on a foundation of forgotten dreams.”</p>
</div>
</div>
You can also use grid system though it may detour.
.parent {
display: grid;
grid-template-columns: repeat(2, 1fr);
}
img { grid-area: 1 / 1 / 2 / 2; }
p { grid-area: 1 / 2 / 2 / 3; }
in my case, it automatically aligned vertically.
For mor information. You can visit CSS Tricks.
CSS code shoulde be:
.blog img {
vertical-align: middle;
float:left;
width:50px;
height:50px;
margin-right: 15px;
}
.blog p {
display: inline;
}
If I understand the requirement, you want the whole paragraph adjacent to your image?
The simplest solution is to use CSS table properties. Note that this does not mean to use tables. Rather it simply displays elements as such.
Here is some sample CSS
div.blog {
display: table;
}
div.blog>img {
display: table-cell;
}
div.blog>p {
display: table-cell;
}
The display:table in the first property is not strictly necessarily, but the default would be display:table-row, which gives you less control over sizing.
I would like to create a page header with a icon. However sometimes the page titles becomes too big and wrap to another line.
for now my result is:
but I would like to get this result (photoshoped)
Important! the icon must be aligned to the first line, like above picture.
Here is my HTML code:
<div class="page-header">
<div class="header-icon"><img src="to_do.png"></div>
<h1 class="page-title" >Não está em aula <small>2016-02-14 14:04</small></h1>
</div>
Here is my CSS
.page-header .header-icon{
display: inline-block;
vertical-align: middle;
padding-right: 10px;
}
.page-header .page-title{
display: inline;
vertical-align: middle;
}
You can define a bigger height for the icon. For this you have to add float:left and a fixed defined height in the .header-icon class.
For example like this:
.page-header {
width: 400px;
}
.page-header .header-icon {
display: block;
vertical-align: middle;
padding-right: 10px;
float: left;
height: 100px;
}
.page-header .page-title {
display: inline;
vertical-align: middle;
}
<div class="page-header">
<div class="header-icon">
<img src="http://placehold.it/30x30">
</div>
<h1 class="page-title">Não está em aula <small>2016-02-14 14:04</small></h1>
</div>
Alternatively you can use padding-bottom for the .header-icon class.
The very simple answer (which some people will disapprove of) is to create a 2 column, 1 row table - the image goes in column one, the text in column two.
This way, any text overflow is catered for within column two - the separation between the image and the text is maintained.
I think this is a perfectly acceptable use of a simple presentational table when you have an image / text combination on one line (which happens often).
Whatever others say, this is the simplest way to do it.
when I add a border to my <h3> tag it wraps around the row instead of the actual element inside it. I have tried various changes of the HTML structure and CSS however I am unable to find a solution.
Here is my HTML:
<div class="container">
<div class="row">
<div class="brand col-md-5"><img src="media/img/logo.png" alt="Driven Car Sales" class="img-rounded logo-custom"></div>
<h3 class="phone-number col-md-7">01429 7654287</h3>
</div>
</div>
Here is my CSS:
.phone-number {
text-align: right;
margin-top: 2.8em;
diaplay: inline-block;
border: 1px solid orange;
As you can see the text aligns right as I need the number to display right of the page.
Any tips would be much appreciated.
almost good, change
diaplay: inline-block;
to
display: inline-block;
Wrap the h3 with a div with the class col-md-7.
The col-md-7 class is used for a column and in most cases, you don't really apply it directly to elements like a header tag.
I know this question has been asked many times, but I never saw a satisfactory answer. I mean, an answer that actually works.
So, here we go again. Take this jsFiddle: http://jsfiddle.net/KFMyn/3/
If you remove the align="center" from the HTML, what CSS do you need to use to make the result look the same as the original?
The answers I found usually amount to margin:0 auto and/or text-align:center but neither of those have the desired effect of making the result look the same as the original.
The text align center covers most of the text elements but a little extra is needed to centre align the table
div {width:400px; text-align:center;}
table {margin: 0 auto;}
table td {text-align: left;}
http://jsfiddle.net/NYuv8/4/
div {width:400px; text-align: center;}
table {display:inline-block;}
Should work as well in addition to Paul's answer.
http://jsfiddle.net/KFMyn/13/
div {width:400px; margin: 0 auto; text-align: center; }
div > * { margin: 0 auto; }
Works for me. But this may not work properly when you have multiple nested dom elements
margin: 0 auto;
text-align: center;
Above Code might not be working when you are using bootstrap grids.
Here is the quick solution for this using bootstrap 4 and IT WORKS IN EVERY BROWSERS
<div class="row">
<div class="col-sm-2">
<div class="customClass mx-auto">
<p class="text-center"> your text goes here </p>
</div>
</div>
</div
you can put any column like col-sm-2, 3, 4.......12
Replacement for Center tag in different situations
1. text-center
Works with p, a, button, h tags and more i.e all the tags containing data or text directly
2. Flex
It can used to align complete element inside another element, however it has more utilities check documentation for reference
Use can center elements using flex in following manners
display:flex;
justify-content:center;
align-items:center;
Another import property is flex-direction i.e
flex-direction:column
flex-direction:row
Use flex direction according to the type of alignment you want
3. mx-auto (bootstrap class)
You can try and style the table as well, like this:
div {
width:400px;
margin: 0 auto;
text-align: center;
}
div table {
margin: 0 auto;
}
Why not just leave it
<div align="center">
It still works just fine with all browsers as far as I know. I have plenty of old html and I got to this thread only because my NetBeans IDE is warning me this is obsolete. My guess is the browsers are automatically translating to the correct solution. Same thing with
<font size="6">bla bla</font>
still works just fine and probably automatically converted to
<span style="font-size:36px">bla bla</span>
div { width: 400px; text-align: center; }
table { display: inline-block; }
This should work. Check example here: http://jsfiddle.net/ye7ngd3q/2/
and if you want elements inside table cell left/right aligned then you can define individual classes and assign to element respectively as show below:
HTML
<div align="center">
A centered div
<p>A</p>
<table border="1">
<tr><td class="align-left">centered</td><tr>
<tr><td class="align-right">div</td><tr>
</table>
<ul><li>A centered div</li></ul>
</div>
CSS
div { width: 400px; text-align: center; }
table { display: inline-block; }
.align-left { text-align: left; }
.align-right { text-align: right; }