What is the preferred way to align a floated element
<div>
<button>goto</button>
<h1>TITLE</h1>
</div>
with the following css:
button {
float: right;
}
Why can't I use something like vertical-align: bottom ? I've seen solution which define a huge line-height but that seems to me like a hack. Is there a css3 solution ?
DEMO
There is a CSS3 option with the Flexible Box module, or flexbox for short. It is a candidate recommendation though, so it's still got browser support complexities.
The earliest support on Internet Explorer is IE10, but it supports an older version of the specification with a different syntax than the current version of the spec and requires the -ms- prefix. The same is true for IE10 Mobile, which supports the syntax from an interim version of the specification crafted in 2012. IE11 however supports the latest specification unprefixed. Android, too, only supports the old version of the spec with a -webkit- prefix prior to version 4.4. Both Safari and iOS Safari require the -webkit- prefix still today, but support the newer specification properties since version 6.1 on the desktop and 7.1 on iOS. Chrome and Firefox, being evergreen browsers, can safely use the current specification syntax unprefixed. And, if you care about BlackBerry, version 10 requires the -webkit- prefix using the current specification properties, while version 7 supports only the older specification.
Notwithstanding, if you have the luxury of designing for modern browsers only, flexbox is simple and intuitive. Below is how you could achieve what you want for those browsers that support the unprefixed version of the latest spec:
div {
background-color: lightgrey;
display: flex; /*creates the flexbox on the parent element*/
flex-direction: row; /*the content will be in rows versus columns*/
justify-content: space-between; /*distributes child elements evenly based on the space between them*/
}
h1 {
font-size: 40px;
}
button {
align-self: flex-end; /*aligns just this child element to the bottom of the flexbox parent*/
}
Assuming you want your button at the bottom, you can just absolutely position your button instead:
div {
...
position: relative;
}
...
button {
position: absolute;
bottom: 0;
right: 0;
}
JSFiddle demo.
Related
Google Chrome, somewhere between version 80 and 84 seems to have made breaking changes to its layout engine, causing the HTML below to be rendered differently than before. It is also different than Firefox (v78) or Edge (v18).
<head>
<style>
body { font-family: sans-serif; }
.inner { border: 1px solid black; }
.outer { display: flex; }
span { position: relative; top: 50%; }
</style>
</head>
<body>
<div class="outer">
<div class="inner">
<span>Text</span>
</div>
</div>
</body>
Google Chrome v84 renders this as:
Firefox, Edge, and Google Chrome <= v80 render it like this:
Although the latter rendering appears "uglier", it is actually what I expect the result to be (based on Firefox, Edge, and Chrome's previous behavior). That is, I expect the text to be shifted down by 50%, relative to its containing div, due to the top: 50% css property. But Chrome's rendering doesn't seem to change whether top is specified or not.
However, I want to point out that when viewing Chrome's and Firefox's DevTools, the values for all layout attributes appear to be the same, including top (which is shown as 9px in both). So although Chrome seems to be interpreting the layout the same way nominally, the rendering is different.
This change in rendering (and difference between browsers) has caused a number of problems in the layout of a website that uses top: 50% in combination with transform: translateY(-50%) in order to center a block vertically within another block-positioned element -- a "classic" recipe, as described by W3schools, LogRocket and others, e.g. here. One difference between the classic recipe and what I have above is that the span is positioned relative instead of absolute. This allows the height and width of the div.inner to be based on the preferred size of the span. This was all working very nicely before the recent Chrome update.
If anyone can explain the difference in rendering or discuss the recent layout changes in Chrome and/or a workaround to achieve a consistent layout in Firefox, Chrome, and Edge, I would really appreciate it.
This is a bug on Chrome 84 that is being fixed at the moment.
I am trying to use CSS hyphens to hyphenate long words, such as in
this example
As you can see the last letter drops on the new line. This happens in Chrome, not for example in Safari. As my layout is using blocks of certain width and I would like to fit the words into blocks, the solution is not to change the font-size or block width, but rather to solve the hyphenate issue.
p {
width: 105px;
border: 1px solid black;
text-align: center;
}
p.hyphenate {
-ms-hyphens: auto;
-webkit-hyphens: auto;
-moz-hyphens: auto;
hyphens: auto;
word-wrap: break-word;
}
<p class="hyphenate">ANONYMOUSLY</p>
<p class="hyphenate">EXEMPLARITY</p>
<p class="hyphenate">THAT EXEMPLARITY</p>
In the last paragraph "Y" drops on the new line without any hyphenation.
Here is the CodePen example. Works fine in Safari, does not work in Chrome:
https://codepen.io/jospo/pen/MWWjzLe
Thanks for any suggestions.
hyphens is only a Working Draft CSS property at present and currently is not fully implemented or supported by all browsers.
I suggest you review the support tables at CanIUse.com which has the following notes.
Chrome < 55 and Android 4.0 Browser support "-webkit-hyphens: none", but not the "auto" property. It is advisable to set the #lang attribute on the HTML element to enable hyphenation support and improve accessibility.
For Chrome: Only supported on Android & Mac platforms (and only the "auto" value) for now.
/*CSS for line*/
#line{
position: absolute;
top: 181px;
height: 1px;
width: 100%;
background-color: #E2E2E2;
}
HTML:
<div id="line"> </div>
I made a 100% width 1px line element using   to run through the bottom of my horizontal navigation menu - it was all fine until I tried it out in Safari and saw that it was off by 5 pixels, when I adjusted accordingly, it became off in Chrome and IE by 5 pixels - is there a way to mediate the problem to satisfy all three browsers?
You could determine your browser, and depending on your browser you can add a class to your <body>. Then you can define separate css rules for the different browsers. For instance, if your browser is safari and safari is the class, then:
body.safari #line {
/*...*/
}
and so on with the other browsers as well.
EDIT:
In php you can use get_browser() in a function to determine the browser.
In Javascript the value you are looking after is navigator.appName.
I have a div element with css border-radius. My Android 2.2.1 Device with default browser (webkit/533.1) dosn't support border-radius, so i try use modernizr to detect this feature. Problem is that modernizr return true thought border-radius dosn't work. What can I do now?
How can I detect it?
<style>
.border{
-moz-border-radius: 100%;
-webkit-border-radius: 100%;
-khtml-border-radius: 100%;
border-radius: 100%;
}
.div{
background: red;
width:100px;
height: 100px;
}
</style>
<div class="border div"></div>
<script>
$(document).ready(function(){
if (!Modernizr.borderradius) {
alert ("css border radius is not supported");
}
})
</script>
Demo on jsfiddle http://jsfiddle.net/7NvLM/
According to CanIUse, border-radius is supported by the Android 2.2 browser. 2.1 requires a prefix but is also supported. Therefore, Modernizr is reporting correctly.
However CanIUse also notes that Android 2.3 (and presumably earlier) does not support percentage values for the property. That'll be why it's not working for you.
I guess Modernizr is looking at basic feature support (which it has). You may need to write your own more specific test for this particular case.
For my input, which can either have classname="half" or "half.not-placeholder-value", Firebug shows both inputs to have a set, fixed width of 25em.
input.half, input.half.not-placeholder-value {
max-width: 25em;
}
Yet, according to Developer Tools, IE8 doesn't seem to use this particular max-width attribute. I say "doesn't seem to use" since Firefox's behavior differs with IE8 with respect to this attribute.
But the MDN docs say that IE7 supports max-width. Am I missing something?
This is not really an IE8 issue since IE8 does support the max-width attribute but only when given a defined width first.
input.half, input.half.not-placeholder-value {
width: 100%;
max-width: 25em;
}
Use IE8 css with width? First part targets IE, second part undoes it for IE9+.
input.half, input.half.not-placeholder-value {
max-width: 25em;
width:25em\9; /* IE 8 */
}
:root input.half, input.half.not-placeholder-value {
width:inherit\9; /* IE 9+ */
}